I extracted green color in image with cv2.inRange()
.
But, The colors of the extracted images were all different.
like this --> enter image description here
I want to change extracted image's color to same.
Please help me....
This is my code.
import numpy as np
import cv2
img = cv2.imread('test_img.jpg')
height, width = img.shape[:2]
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_green = (35,45,0,0)
upper_green = (80, 255, 255,0)
img_mask = cv2.inRange(img_hsv, lower_green, upper_green)
img_result = cv2.bitwise_and(img, img, mask = img_mask)
cv2.imshow('color',img_result)
Output
CodePudding user response:
You need to zero
the B
and R
dimensions of the image, as follows:
import numpy as np
import cv2
img = cv2.imread('test_img.jpg')
height, width = img.shape[:2]
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_green = (35,45,0,0)
upper_green = (80, 255, 255,0)
img_mask = cv2.inRange(img_hsv, lower_green, upper_green)
img_result = cv2.bitwise_and(img, img, mask = img_mask)[:, :, 1]
img[:, :, 0] = 0
img[:, :, 2] = 0
img[:, :, 1] = img_result
cv2.imshow('color', img)
cv2.waitKey(0)
or if you want a simpler way to output the image, you can use the matplotlib.pyplot
, as follows:
import matplotlib.pyplot as plt
plt.imshow(img)
Cheers