简体   繁体   English

如何将此 HSV 转换为 RGB

[英]How to convert this HSV into RGB

How to convert this HSV into RGB I would like it using cv2.color cv2.COLOR_BGR2RGB but its not same result.如何将此 HSV 转换为 RGB 我想使用 cv2.color cv2.COLOR_BGR2RGB 但结果不一样。

This is the image i've used enter image description here这是我使用过的图像 在此处输入图像描述

Heres' the result that I want but using the cv2.COLOR_BGR2RGB but the code below is using the cv2.COLOR_BGR2HSV enter image description here这是我想要的结果,但使用 cv2.COLOR_BGR2RGB 但下面的代码使用 cv2.COLOR_BGR2HSV在此处输入图像描述

import cv2
import numpy as np

## Read
img = cv2.imread("ni.jpg")

## convert to hsv
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

## mask of green (36,25,25) ~ (86, 255,255)
# mask = cv2.inRange(hsv, (36, 25, 25), (86, 255,255))
mask = cv2.inRange(hsv, (7, 25, 25), (70, 255,255))

## slice the green
imask = mask>0
green = np.zeros_like(img, np.uint8)
green[imask] = img[imask]

## save 
cv2.imwrite("green.png", green)

To convert an image from HSV to RGB you can do:要将图像从 HSV 转换为 RGB,您可以执行以下操作:

rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)

and to do HSV to BGR it is并且对 BGR 做 HSV 是

bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

You have to know that OpenCV is using BGR when reading/saving images.您必须知道 OpenCV 在读取/保存图像时使用 BGR。 You can convert between RGB and BGR with cvtColor and cv2.COLOR_RGB2BGR, or cv2.COLOR_BGR2RGB.您可以使用 cvtColor 和 cv2.COLOR_RGB2BGR 或 cv2.COLOR_BGR2RGB 在 RGB 和 BGR 之间进行转换。

EDIT:编辑:

However, if what you want is having a mask of green bananas (or yellow bananas), The issue is the way you defined green color : it is including a lot of other colors right now, including yellow.然而,如果你想要的是一个绿色香蕉(或黄色香蕉)的面具,问题是你定义绿色的方式:它现在包括很多其他颜色,包括黄色。

What you can do with the HSV, is to only look at the first channel, the hue :你可以用 HSV 做的是只看第一个通道,色调:

香蕉的色调

Here you can see that green and yellow can be differentiated : green bananas have pixel value roughly between 30 and 50, and yellow between 20 and 30.在这里你可以看到绿色和黄色是可以区分的:绿色香蕉的像素值大约在 30 到 50 之间,黄色在 20 到 30 之间。

You can do a mask with that.你可以用它做一个面具。 I used another library to do the cleanup of pixel we don't want.我使用另一个库来清理我们不想要的像素。 It is Scikit-image.它是 Scikit 图像。 This can be done in OpenCV as well, but it takes a bit more time...这也可以在 OpenCV 中完成,但需要更多时间......

SO here is my code :所以这是我的代码:

import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage.morphology import remove_small_objects, remove_small_holes
## Read
img = cv2.imread("ni.jpg")

## convert to hsv
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
hue = hsv[:,:,0]

# plt.imshow(hue) # this show the figure in my post
# plt.show()

# mask = np.bitwise_and(hue > 20, hue < 35) # for yellow
mask = np.bitwise_and(hue > 30, hue < 50)  # for green
mask = remove_small_objects(mask, 1000)
mask = remove_small_holes(mask, 1000)

green = np.zeros_like(img, np.uint8)
green[mask] = img[mask]

## save
cv2.imwrite("green.png", green)

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

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