will
will

Reputation: 1

Obtain all R-G-B in an image

I'm new to image processing and machine vision and I'm a bit confused about the HSV thresholding. I have created a python based code which will use HSV of the image to obtain the triangles what I did was set specific ranges for the RGB but I'm not sure how much is blue or green and what specific range I need to set this to.

Here is a part of my code which I'm confused on

 lower_red = np.array([0,50,50])
  upper_red = np.array([2,255,255])
  mask0 = cv2.inRange(hsv, lower_red, upper_red)

  # upper mask (170-180) RED
  lower_red = np.array([170,50,50])
  upper_red = np.array([180,255,255])
  mask1 = cv2.inRange(hsv, lower_red, upper_red)

  # lower mask (0-10) BLUE
  lower_blue = np.array([20,150,0])
  upper_blue = np.array([220,255,255])
  mask2 = cv2.inRange(hsv, lower_blue, upper_blue)

  # upper mask (170-180) BLUE
  lower_blue = np.array([110,50,50])
  upper_blue = np.array([130,255,255])
  mask3 = cv2.inRange(hsv, lower_blue, upper_blue)

  # lower mask (0-10) GREEN
  lower_green = np.array([30,0,0])
  upper_green = np.array([90,255,255])
  mask4 = cv2.inRange(hsv, lower_green, upper_green)

  # upper mask (170-180)GREEN
  lower_green = np.array([40,0,0])
  upper_green = np.array([80,255,255])
  mask5 = cv2.inRange(hsv, lower_green, upper_green)

  # join my masks
  mask = mask0+mask1+mask2+mask3+mask4+mask5

  # set my output img to zero everywhere except my mask
  output_img = img2_fg.copy()
  output_img = cv2.bitwise_and(output_img, output_img, mask= mask)

  # or your HSV image, which I *believe* is what you want
  output_hsv = hsv.copy()
  output_hsv = cv2.bitwise_and(output_img, output_img, mask= mask)

  cv2.imshow('first ttmask',output_img)

here is the initial image https://i.sstatic.net/k9eVq.png

here is my output image after that code https://i.sstatic.net/ARDKi.png

Upvotes: 0

Views: 270

Answers (2)

MK 62665
MK 62665

Reputation: 123

In machine vision, using absolute threshold operations to locate or identify an object might not be a good idea unless you have a wider tolerance in color for your application or your illumination is consistent. The color intensities might vary depending on ambient lighting, glare and what not, resulting as blobs in the object of interest.

If you want to try and understand, try using RGB color space for starters if you are comfortable with it. Note that OpenCV returns BGR and not RGB. That is,

a=cv2.imread('sample.jpg')

blue_plane = a[:,:,0]

green_plane = a[:,:,1]

red_plane = a[:,:,2]

...

Hope it helped you!

Upvotes: 0

Piglet
Piglet

Reputation: 28974

Please educate yourself about the HSV colour model. Searching for it online should give all necessary information.

Hue maps colours to a 0-360° range. enter image description here

Hue alone may not be sufficient as the sourrounding area may also have colours that exist in your templates. So think about also creating masks for the Saturation and Value (brightness) channels and combine them.

Colour image processing is always difficult. Consider using black&white features.

Google "fiducial markers" to see some examples.

Upvotes: 2

Related Questions