将 BGR 彩色图像转换为除一种颜色之外的灰度图像
我想留下黑白(灰度)的彩色图像,以及原始颜色的感兴趣区域。我有一个彩色 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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HSV 颜色阈值听起来很适合这种情况。这个想法是将图像转换为 HSV 格式,然后定义下限和上限范围。这将使我们能够将图像中所需的对象分割到蒙版上,其中要保留的部分为白色,要丢弃的部分为黑色。
我们的想法是获得两张图像:一张代表彩色部分,另一张代表我们想要保留的反转灰度部分。然后我们只需将它们组合在一起即可得到我们的结果。
输入图像:
使用此 HSV 下/上范围,我们可以从图像中分割绿色
彩色
->
灰色->
组合结果如果您只想要浅绿色,您可以调整阈值范围以删除深绿色
->
灰色->
组合结果的结果
这是黄色代码
要确定 HSV 下限/上限范围,您可以使用这个 HSV 阈值器带有滑块的脚本,因此您无需猜测和检查。只需更改图片路径即可
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
Colored
->
Gray->
Combined resultIf instead you wanted only light green, you could adjust the threshold range to remove dark green
Colored
->
Gray->
Combined resultHere's the result for yellow
Code
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