0

I'm trying to follow the deformation of a plastic part with image processing via Python and OpenCV. I put red markers on the part, took pictures with a smartphone and put together the code to find contours on the image, filter out the ones that are the right size. The code worked perfectly on images that were taken at night using the flash on the smartphone but when I took them during the day with no additional lighting, I just cannot exclude the markers when segmenting the image.

I cannot understand why it does not work as the markers clearly stand out on the HSV version of the image. Am I missing something about this

Code and images are bellow.

The code:

a1 = 0
a2 = 0
a3 = 0
b1 = 187
b2 = 204
b3 = 204

red_lower = np.array([a1,a2,a3], np.uint8)
red_upper = np.array([b1,b2,b3], np.uint8)

img = cv2.imread(img_path)
crop = img[y:y+h, x:x+w]
blur = cv2.blur(crop,(2,2))
hsv = cv2.cvtColor(blur,cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, red_lower, red_upper)
mask_copy = mask.copy()
cnts = cv2.findContours(mask_copy,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

The problematic picture:

enter image description here

The problematic picture in HSV:

enter image description here

The problematic picture after segmentation:

enter image description here

2
  • 1
    You set lower_color and upper_color but then in the inRange function you use red_lower and red_upper. Is this correct? What are those two set to? Commented Dec 23, 2020 at 14:47
  • sorry, I pasted together a short summary code from different versions, I edited the OP, the question is still relevant. The y, x, w, h are used to crop the image, they are not really important. Commented Dec 23, 2020 at 14:49

1 Answer 1

1

you specify the valid hue range as 0 to 187.

in OpenCV, the hue circle, when expressed in uint8 values, has a range of 0 to 179 (360 degrees in 2-degree increments).

that means you just accepted ALL hue values.

Sign up to request clarification or add additional context in comments.

1 Comment

OK, now I understand. When I took the pictures in darkness, most of the light from the flash was reflected from the red squares and that's why it worked. In the second case, the light was also reflected from other parts of the test specimen and my segmentation values picked up everything. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.