Home > Software design >  FindContours not detecting light colored points
FindContours not detecting light colored points

Time:09-13

I am trying to detect points from a scatter plot and get the location of those points. For that I am trying to use OpenCV findContours function, but for some reason, its not able to detect and draw contours for light colored points on the graph

Here's my code-

img = cv2.imread('808.png')
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# finding contours/shapes
_, threshold = cv2.threshold(imgGray, 127, 255, cv2.THRESH_BINARY)
  
# using a findContours() function
contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

i = 0
  
# list for storing names of shapes
for contour in contours:
    if i == 0:
        i = 1
        continue

    approx = cv2.approxPolyDP(
        contour, 0.01 * cv2.arcLength(contour, True), True)

    cv2.drawContours(img, [contour], 0, (0, 0, 0), 5)
  
    # finding center point of shape
    M = cv2.moments(contour)
    if M['m00'] != 0.0:
        x = int(M['m10']/M['m00'])
        y = int(M['m01']/M['m00'])
  
cv2.imshow('shapes', img)
  
cv2.waitKey(0)
cv2.destroyAllWindows()

And here are the results - 1st is the input and 2nd is the result input_image

output_image

As seen clearly, the function detects and draws correct contours around dark colored points but the light colored points are not getting detected properly. Any help on this or in general the method to do the entire (detecting points and then getting the values from scatter plots) will be much appreciated. Thanks!

CodePudding user response:

It's your threshold level. It's too low. Always inspect your intermediate values.

after BGR2GRAY:

gray

after thresholding at 127:

threshold 127

Pick a higher threshold, say 192:

threshold 192

Now you've got them all.

  • Related