简体   繁体   中英

How to find distance between two contours

I'm working on a project to find defective parts on a metal ring.I successfully find defective parts on the surface but I cannot detect the protrusions on the inner surface of the metal ring.

I thought to determine the error using the distance between the inner and outer surface, but I don't know how I can calculate the distance between the two contour.

sucsess, frame = capture.read()

kernel = np.ones((1,1),np.uint8)
blur = cv2.bilateralFilter(frame,6,140,160)
threshold = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,21,16)
closing = cv2.morphologyEx(threshold,cv2.MORPH_CLOSE,kernel)
erosion = cv2.erode(closing,kernel,iterations =0)
contours, hierarchy = cv2.findContours(erosion,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
            area = cv2.contourArea(cnt)
            if area > 72000 and area < 80000:
                cv2.drawContours(frame,cnt,-1,(0,0,0),3)
                for cnt2 in contours:
                    area2 = cv2.contourArea(cnt2)
                    if area2 > 30 and area2 < 200:
                        cv2.drawContours(frame,cnt2,-1,(0,0,0),3)
cv2.imshow("frame",frame)
cv2.imshow("Erosion",erosion)
cv2.waitKey(0)

This is my code. First image is the object I am looking, second image is the output of the erosion.

enter image description here

enter image description here

My main problem is I am not able to detect any protrusion inside inner radius.

Any suggestion and help are welcome.

沿中心光线测量

I thought to determine the error using the distance between the inner and outer surface, but I don't know how I can calculate the distance between the two contour.

One method is to take both contours and calculate the centroid, giving you a nominal centre point. Then from this point, cast rays out through 360 degrees, and compute the points of intersection with the inner and outer contours. (Closest point by Euclidean distance for example.) Then you have two corresponding radii on both the inner and outer surface, so you can subtract inner from outer to get the ring thickness. Compute this all the way around, using an angle increment proportional to the accuracy you need. The standard deviation of the thickness all the way around is a measure of protrusions (lower numbers are better!).

My main problem is I am not able to detect any protrusion inside inner radius.

If you are only interested in the inner radius, another way is to take the extracted contour from the inner surface, and again compute the centroid to find a nominal reference point. Take the average distance from this centre to each point on the contour, and that gives you the ideal fitted circle. Compute the distance from this ideal circle to each closest point on the actual contour and that gives you a measure of the perturbations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM