Get the coordinates of the outline (x, y) of a form in an image using python

I need to get a matrix with the coordinates (x, y) of the contour of the next image with python.

enter image description here

I try this with an opencv canny detector and find the outlines, but I have many outlines and I donโ€™t know how to get the one that I want.

import numpy as np from matplotlib import pyplot as plt import cv2 #from skimage import measure, feature, io #from skimage import img_as_ubyte x1 = 330 xf = 690 y1 = 0 yf = 400 img = cv2.imread('test.tif') img = img[y1:yf, x1:xf] edge = cv2.Canny(img, 100, 200) image, contours, hierarchy = cv2.findContours(edge, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) 

enter image description here

I need an array with the coordinates (x, y) of the path. I think this is at the output of the cv2.findContours() loops, but I do not find the loop that I want ...

I also tried using the matplotlib.pyplot.contour function:

 import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread('test.tif', 0) # read image img = cv2.threshold(img, 0, 255, cv2.THRESH_OTSU)[1] # threshold image img = cv2.medianBlur(img, 15) # remove noise # skeletonize size = np.size(img) # get number of pixels skel = np.zeros(img.shape, np.uint8) # create an array of zeros with the same shape as the image and 256 gray levels element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) # create a structurant element (cross) done = False while(not done): eroded = cv2.erode(img, element) temp = cv2.dilate(eroded, element) temp = cv2.subtract(img, temp) skel = cv2.bitwise_or(skel, temp) img = eroded.copy() zeros = size - cv2.countNonZero(img) if zeros == size: done = True cs = plt.contour(skel, 1) p = cs.collections[0].get_paths()[0] v = p.vertices x = v[:, 0] y = v[:, 1] 

enter image description here

But I only have closed paths, not an open path going from left to right of the image.

Thanks so much for your answers.

+5
source share
1 answer

You almost found the answer to your question. First of all, there is a difference between edge detection detection and . Essentially, detecting an edge leads to what you call (incorrectly) an โ€œopen pathโ€ (that is, an edge) and a path, resulting in what you call a โ€œclosed pathโ€ (i.e., Path).

Definition Canny edge is a popular border detection algorithm. Since you want to detect an edge as an array with coordinates (x, y) going from left to right of the image, detecting the Canny edge is a good idea.

The edge response, which is not in the desired format.

 import numpy as np import matplotlib.pyplot as plt import cv2 img = cv2.imread('test.tif') edge = cv2.Canny(img, 100, 200) ans = [] for y in range(0, edge.shape[0]): for x in range(0, edge.shape[1]): if edge[y, x] != 0: ans = ans + [[x, y]] ans = np.array(ans) print(ans.shape) print(ans[0:10, :]) 

The ans array (a shape equal to (n, 2) ) stores the (x, y) -coordinates of the pixels n that make up the detected edge. This is the result you are looking for.

Here is the image in which I painted these n pixels in white:

enter image description here

Hope this helps you.

+2
source

Source: https://habr.com/ru/post/1235461/


All Articles