简体   繁体   English

OpenCV Python 如何在将图像转换为灰度时保持一种颜色

[英]OpenCV Python how to keep one color as is converting an image to Grayscale

锥体的图像

How do I make it so everything in the image is in gray-scale except the orange cone.我怎么做才能使图像中的所有内容都是灰度的,除了橙色圆锥。 Using opencv python.使用 opencv python。

You can achieve your goal by using bitwise_and() function and thresholding .您可以通过使用bitwise_and() function 和thresholding来实现您的目标。 Steps:脚步:

  • generate mask for the required region.(here thresholding is used but other methods can also be used)为所需区域生成mask 。(此处使用thresholding ,但也可以使用其他方法)
  • extract required regions using bitwise_and (image & mask).使用bitwise_and (图像和掩码)提取所需regions
  • Add masked regions to get output.添加masked regions以获得 output。

Here's sample code:这是示例代码:

import cv2
import numpy as np

img = cv2.imread('input.jpg')

# creating mask using thresholding over `red` channel (use better use histogram to get threshoding value)
# I have used 200 as thershoding value it can be different for different images
ret, mask = cv2.threshold(img[:, :,2], 200, 255, cv2.THRESH_BINARY)

mask3 = np.zeros_like(img)
mask3[:, :, 0] = mask
mask3[:, :, 1] = mask
mask3[:, :, 2] = mask

# extracting `orange` region using `biteise_and`
orange = cv2.bitwise_and(img, mask3)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img  = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)

# extracting non-orange region
gray = cv2.bitwise_and(img, 255 - mask3)

# orange masked output
out = gray + orange

cv2.imwrite('orange.png', orange)
cv2.imwrite('gray.png', gray)
cv2.imwrite("output.png", out)

Results:结果:

masked orange image蒙面的橙色图像

蒙面的橙色图像

masked gray image蒙版灰度图像

在此处输入图像描述

output image output 图像

在此处输入图像描述

Here is an alternate way to do that in Python/OpenCV.这是在 Python/OpenCV 中执行此操作的另一种方法。

  • Read the input读取输入
  • Threshold on color using cv2.inRange()使用 cv2.inRange() 的颜色阈值
  • Apply morphology to clean it up and fill in holes as a mask应用形态学将其清理并填充孔作为掩码
  • Create a grayscale version of the input创建输入的灰度版本
  • Merge the input and grayscale versions using the mask via np.where()通过 np.where() 使用掩码合并输入和灰度版本
  • Save the results保存结果

Input:输入:

在此处输入图像描述

import cv2
import numpy as np

img = cv2.imread("orange_cone.jpg")

# threshold on orange
lower = (0,60,200)
upper = (110,160,255)
thresh = cv2.inRange(img, lower, upper)

# apply morphology and make 3 channels as mask
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5))
mask = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
mask = cv2.merge([mask,mask,mask])

# create 3-channel grayscale version
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)

# blend img with gray using mask
result = np.where(mask==255, img, gray)

# save images
cv2.imwrite('orange_cone_thresh.jpg', thresh)
cv2.imwrite('orange_cone_mask.jpg', mask)
cv2.imwrite('orange_cone_result.jpg', result)

# Display images
cv2.imshow("thresh", thresh)
cv2.imshow("mask", mask)
cv2.imshow("result", result)
cv2.waitKey(0)

Threshold image:阈值图像:

在此处输入图像描述

Mask image:蒙版图片:

在此处输入图像描述

Merged result:合并结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM