How to decrease sensitivity when detecting objects in image.

asked 2019-07-12 17:57:28 -0500

updated 2019-07-13 04:42:27 -0500

supra56 gravatar image

I am trying to measure the size of an object in the image, but the code I am working with is only looking for the details in the picture and not the object I want. Here is my code:

edged = cv2.Canny(gray, 50, 20) #50 and 20 are tshld one and two
edged = cv2.dilate(edged, None, iterations=1)
edged = cv2.erode(edged, None, iterations=1)

cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
cnts = imutils.grab_contours(cnts)

(cnts, _) = contours.sort_contours(cnts)
pixelsPerMetric = None

for c in cnts:
    # if the contour is not sufficiently large, ignore it
    if cv2.contourArea(c) < 60: 

    orig = image.copy()
    box = cv2.minAreaRect(c)
    box = if imutils.is_cv2() else cv2.boxPoints(box)
    box = np.array(box, dtype="int")

    box = perspective.order_points(box)
   cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 3)

Note: the image below is the result of my code. I want to be able to detect the green target and measure it's dimensions. However, the box is only enclosing small portions of the image and not the entire target. If I should post entire code, let me know!


image description

edit retag flag offensive close merge delete


These pictures are hardly helpful. I cannot see no box...

Witek gravatar imageWitek ( 2019-07-13 16:36:39 -0500 )edit

yes, that is the issue. I need the code to create a box around the target and measure its dimensions but it isn't finding the target :/

berlin01 gravatar imageberlin01 ( 2019-07-14 16:46:48 -0500 )edit

OK, I think I understand now. Instead of finding edges, do image thresholding and continue with your code. It should work.

Witek gravatar imageWitek ( 2019-07-15 03:33:52 -0500 )edit

It worked! Thank you so much :D

berlin01 gravatar imageberlin01 ( 2019-07-15 17:07:48 -0500 )edit

You're welcome.

Witek gravatar imageWitek ( 2019-07-16 04:41:29 -0500 )edit