user2882635
user2882635

Reputation: 163

Python OpenCV segmentation problems

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

Upvotes: 0

Views: 154

Answers (1)

Christoph Rackwitz
Christoph Rackwitz

Reputation: 15354

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.

Upvotes: 1

Related Questions