how to fit lines to edges and find the center point (opencv)

681 Views Asked by At

I have an image to which I apply a bilateral filter, followed by adaptive thresholding to get the image below.

original image (this is a screenshot off the depth image of the object) enter image description here

thresholded imageenter image description here

I would like to fit lines to the vertical parts/lines and find the center poiint, output like image below:

enter image description here

I cant seem to understand the output of the cv2.adaptiveThreshold(). How are the purple pixels (i.e my edges) represented? and how can a line be fitted? MWE:

import cv2

image = cv2.imread("depth_frame0009.jpg")
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

bilateral_filter = cv2.bilateralFilter(gray_image, 15, 50, 50)
plt.figure()
plt.imshow(bilateral_filter)
plt.title("bilateral filter")
#plt.imsave("2dimage_gaussianFilter.png",blurred)
plt.imsave("depthmap_image_bilateralFilter.png",bilateral_filter)


th3 = cv2.adaptiveThreshold(bilateral_filter,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
plt.figure()
plt.imshow(th3)

========

edit:

Canny edges enter image description here contours enter image description here

1

There are 1 best solutions below

4
On

They are represented as an image, a matrix of uint8.

The reason it is purple and yellow is because matplotlib is applying a colormap to it.

I generally prefer to use some specific parameters when plotting image processing output images, eg

plt.imshow(th3, cmap='gray', interpolation='nearest')

If you are specifically interested in finding and fitting lines you may want to use a different representation, such as Hough lines. Once you have the lines in the image you can take the best fit lines and find your center point between them.