I need to findout extreme points of a image(cloth). I have tried with https://www.geeksforgeeks.org/find-co-ordinates-of-contours-using-opencv-python/ but i need to findout like this,
But the current issue is for some images it is detecting currectly and for some images it is not. Can anyone help on this?
the successed image is,
Original image of the failed one is,
Am I doing any wrong methods here? If yes, please suggest me the right one!
Advertisement
Answer
Use the following method.
- change contour approximation variable according to your desired number of points. I have choose
0.0035
.
import cv2 image = cv2.imread("image.png") gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) _, threshold_image = cv2.threshold(gray_image, 250, 255, cv2.THRESH_BINARY_INV) cv2.imshow("threshold_image", threshold_image) contours, hierarchy = cv2.findContours(threshold_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) selected_contour = max(contours, key=lambda x: cv2.contourArea(x)) approx = cv2.approxPolyDP(selected_contour, 0.0035 * cv2.arcLength(selected_contour, True), True) cv2.drawContours(image, [approx], 0, (0, 0, 255), 5) for point in approx: x, y = point[0] string = str(x) + " " + str(y) cv2.circle(image, (x, y), 2, (255, 0, 0), 2) cv2.putText(image, string, (x, y), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 255, 0)) cv2.imshow("cordinate image", image) cv2.waitKey(0) cv2.destroyAllWindows()