将 BGR 彩色图像转换为除一种颜色之外的灰度图像

发布于 2025-01-17 00:59:32 字数 284 浏览 0 评论 0原文

我想留下黑白(灰度)的彩色图像,以及原始颜色的感兴趣区域。我有一个彩色 BGR 图像,我想删除除一种颜色之外的所有颜色。

在此处输入图像描述

就像这张叶子图像一样,我想让整个图像变成黑白,并使用 OpenCV 和 python 保留原始颜色(绿色),或增强该图像中的黄色斑点。

我研究了 OpenCV 文档,但没有找到任何可用的东西。我研究了为此创建一个过滤器,但我也找不到任何东西。

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.

enter image description here

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.

I have studied the OpenCV documentation, but I don't find anything to use. I study about create a filter for this, but I couldn't find anything too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

李不 2025-01-24 00:59:32

HSV 颜色阈值听起来很适合这种情况。这个想法是将图像转换为 HSV 格式,然后定义下限和上限范围。这将使我们能够将图像中所需的对象分割到蒙版上,其中要保留的部分为白色,要丢弃的部分为黑色。

我们的想法是获得两张图像:一张代表彩色部分,另一张代表我们想要保留的反转灰度部分。然后我们只需将它们组合在一起即可得到我们的结果。

输入图像:

使用此 HSV 下/上范围,我们可以从图像中分割绿色

lower = np.array([35, 90, 35])
upper = np.array([179, 255, 255])

彩色 -> 灰色 -> 组合结果



如果您只想要浅绿色,您可以调整阈值范围以删除深绿色

lower = np.array([35, 90, 88])
upper = np.array([179, 255, 255])

-> 灰色 -> 组合结果



的结果

lower = np.array([0, 0, 128])
upper = np.array([29, 255, 255])

这是黄色代码

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()

要确定 HSV 下限/上限范围,您可以使用这个 HSV 阈值器带有滑块的脚本,因此您无需猜测和检查。只需更改图片路径即可

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()

HSV color thresholding sounds great for this situation. The idea is to convert the image into HSV format then define a lower and upper range. 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

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. 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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文