[英]Convert BGR colored image to grayscale except one color
I'd like to leave a color image in black and white (grayscale), and regions of interest in the original color.我想留下黑白(灰度)的彩色图像,以及原始颜色的感兴趣区域。 I have a colored BGR image and I want to remove all colors except one color.
我有一张彩色 BGR 图像,我想删除除一种颜色之外的所有 colors。
Like this leaf image, I want to make the entire image black and white, and leave the original color (green), or intensify the yellow spots in this image, using OpenCV and python.像这张树叶图像,我想把整个图像变成黑色和白色,并保留原来的颜色(绿色),或者加强这张图像中的黄色斑点,使用 OpenCV 和 python。
I have studied the OpenCV documentation, but I don't find anything to use.我研究了 OpenCV 文档,但没有找到任何有用的东西。 I study about create a filter for this, but I couldn't find anything too.
我研究了为此创建一个过滤器,但我也找不到任何东西。
HSV color thresholding sounds great for this situation. HSV 颜色阈值听起来很适合这种情况。 The idea is to convert the image into HSV format then define a lower and upper range.
这个想法是将图像转换为 HSV 格式,然后定义一个较低和较高的范围。 This will allow us to segment desired objects in the image onto a mask where sections to keep are in white and areas to throw away in black.
这将使我们能够将图像中所需的对象分割到一个蒙版上,其中要保留的部分为白色,要丢弃的区域为黑色。
The idea is to get two images: one representing the colored sections and another representing the inversed grayscale sections we want to keep.我们的想法是获得两张图像:一张代表彩色部分,另一张代表我们想要保留的反转灰度部分。 Then we simply combine them together to get our result.
然后我们简单地将它们组合在一起得到我们的结果。
Input image:输入图像:
Using this HSV lower/upper range, we can segment green from the image使用这个 HSV 下限/上限范围,我们可以从图像中分割出绿色
lower = np.array([35, 90, 35])
upper = np.array([179, 255, 255])
Colored ->
Gray ->
Combined result彩色
->
灰色->
组合结果
If instead you wanted only light green, you could adjust the threshold range to remove dark green相反,如果您只想要浅绿色,则可以调整阈值范围以去除深绿色
lower = np.array([35, 90, 88])
upper = np.array([179, 255, 255])
Colored ->
Gray ->
Combined result彩色
->
灰色->
组合结果
Here's the result for yellow这是黄色的结果
lower = np.array([0, 0, 128])
upper = np.array([29, 255, 255])
Code代码
import numpy as np
import cv2
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.merge([gray, gray, gray])
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([35, 90, 88])
upper = np.array([179, 255, 255])
mask = cv2.inRange(hsv, lower, upper)
colored_output = cv2.bitwise_and(image, image, mask=mask)
gray_output = cv2.bitwise_and(gray, gray, mask=255-mask)
result = cv2.add(colored_output, gray_output)
cv2.imshow('colored_output', colored_output)
cv2.imshow('gray_output', gray_output)
cv2.imshow('result', result)
cv2.waitKey()
To determine the HSV lower/upper ranges, you can use this HSV thresholder script with sliders so you don't need to guess and check.要确定 HSV 下限/上限范围,您可以将此 HSV 阈值脚本与滑块一起使用,这样您就无需猜测和检查。 Just change the image path
只需更改图像路径
import cv2
import numpy as np
def nothing(x):
pass
# Load image
image = cv2.imread('1.jpg')
# Create a window
cv2.namedWindow('image')
# Create trackbars for color change
# Hue is from 0-179 for Opencv
cv2.createTrackbar('HMin', 'image', 0, 179, nothing)
cv2.createTrackbar('SMin', 'image', 0, 255, nothing)
cv2.createTrackbar('VMin', 'image', 0, 255, nothing)
cv2.createTrackbar('HMax', 'image', 0, 179, nothing)
cv2.createTrackbar('SMax', 'image', 0, 255, nothing)
cv2.createTrackbar('VMax', 'image', 0, 255, nothing)
# Set default value for Max HSV trackbars
cv2.setTrackbarPos('HMax', 'image', 179)
cv2.setTrackbarPos('SMax', 'image', 255)
cv2.setTrackbarPos('VMax', 'image', 255)
# Initialize HSV min/max values
hMin = sMin = vMin = hMax = sMax = vMax = 0
phMin = psMin = pvMin = phMax = psMax = pvMax = 0
while(1):
# Get current positions of all trackbars
hMin = cv2.getTrackbarPos('HMin', 'image')
sMin = cv2.getTrackbarPos('SMin', 'image')
vMin = cv2.getTrackbarPos('VMin', 'image')
hMax = cv2.getTrackbarPos('HMax', 'image')
sMax = cv2.getTrackbarPos('SMax', 'image')
vMax = cv2.getTrackbarPos('VMax', 'image')
# Set minimum and maximum HSV values to display
lower = np.array([hMin, sMin, vMin])
upper = np.array([hMax, sMax, vMax])
# Convert to HSV format and color threshold
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
result = cv2.bitwise_and(image, image, mask=mask)
# Print if there is a change in HSV value
if((phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax) ):
print("(hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d)" % (hMin , sMin , vMin, hMax, sMax , vMax))
phMin = hMin
psMin = sMin
pvMin = vMin
phMax = hMax
psMax = sMax
pvMax = vMax
# Display result image
cv2.imshow('image', result)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.