5

I'm trying to select the green color in an image using OpenCV (the method to do it comes from this website. The image I'm treating is :

enter image description here

Here is the code I tried to write.

import cv2 import matplotlib.pyplot as plt import numpy as np greenhsv = (60, 255, 255) green2hsv=(70,100,170) g_square = np.full((10, 10, 3), greenhsv, dtype=np.uint8)/255.0 plt.imshow(hsv_to_rgb(g_square)) plt.show() g1_square = np.full((10, 10, 3), green2hsv, dtype=np.uint8)/255.0 plt.imshow(hsv_to_rgb(g1_square)) plt.show() nucl = cv2.imread('./Pictures/image_nucleation_essai0.png') nucl = cv2.cvtColor(nucl, cv2.COLOR_BGR2RGB) plt.imshow(nucl) plt.show() hsv_nucl = cv2.cvtColor(nucl, cv2.COLOR_RGB2HSV) mask = cv2.inRange(hsv_nucl, greenhsv,green2hsv) result = cv2.bitwise_and(nucl, nucl, mask=mask) plt.imshow(mask, cmap="gray") plt.show() plt.imshow(result) plt.show() 

The result is :

enter image description here enter image description here

So the mask did not work.

1
  • @OznOg In the tutorials, people always convert into HSV... So I did the same... Commented Dec 31, 2018 at 14:42

1 Answer 1

6

Your color ranges are not quite right yet. Also the variables in the inRange() function are in the wrong order. It's from-to, so the darker color must be first. Change your code to cv2.inRange(hsv_nucl, green2hsv,greenhsv) You can use/tweak the values in the code below, that works.
Result:
enter image description here

With white background:
enter image description here

import numpy as np import cv2 # load image img = cv2.imread("Eding.png") # convert to HSV hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # set lower and upper color limits lower_val = np.array([50,100,170]) upper_val = np.array([70,255,255]) # Threshold the HSV image to get only green colors mask = cv2.inRange(hsv, lower_val, upper_val) # apply mask to original image - this shows the green with black blackground only_green = cv2.bitwise_and(img,img, mask= mask) # create a black image with the dimensions of the input image background = np.zeros(img.shape, img.dtype) # invert to create a white image background = cv2.bitwise_not(background) # invert the mask that blocks everything except green - # so now it only blocks the green area's mask_inv = cv2.bitwise_not(mask) # apply the inverted mask to the white image, # so it now has black where the original image had green masked_bg = cv2.bitwise_and(background,background, mask= mask_inv) # add the 2 images together. It adds all the pixel values, # so the result is white background and the the green from the first image final = cv2.add(only_green, masked_bg) #show image cv2.imshow("img", final) cv2.waitKey(0) cv2.destroyAllWindows() 
Sign up to request clarification or add additional context in comments.

3 Comments

green2hsv,greenhsv is still not the right order, because the first array element then is out of order. Your example code is correct, though.
@J.D. Thx !! I'm still wondering if it's possible instead of the background in black it was possible to color it in white plz ?
Sure, I've updated the code to do that. Also take a look at the intermediate images, it helps you understand what happens.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.