如何使用OpenCV和蒙版从图像中选择合适的颜色? [英] How can I select the good colors from an image with OpenCV and mask?

查看:142
本文介绍了如何使用OpenCV和蒙版从图像中选择合适的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试选择图片中的颜色.我正在使用OpenCV这样做,并且正在从页面 https://realpython学习.com/python-opencv-color-spaces/.我正在处理的图像随即显示.

I'm trying to select a color in an picture. I'm using OpenCV to do so and I'm learning from the page https://realpython.com/python-opencv-color-spaces/ . The image I'm treating is shown just afterwards.

import cv2
import matplotlib.pyplot as plt
import numpy as np

greenhsv = (60, 255, 255)
green2hsv=(70,100,170)
g_square = np.full((10, 10, 3), greenhsv, dtype=np.uint8)/255.0
plt.imshow(hsv_to_rgb(g_square))
plt.show()
g1_square = np.full((10, 10, 3), green2hsv, dtype=np.uint8)/255.0
plt.imshow(hsv_to_rgb(g1_square))
plt.show()

nucl = cv2.imread('./Pictures/image_nucleation_essai0.png')
nucl = cv2.cvtColor(nucl, cv2.COLOR_BGR2RGB)
plt.imshow(nucl)
plt.show()

hsv_nucl = cv2.cvtColor(nucl, cv2.COLOR_RGB2HSV)

mask = cv2.inRange(hsv_nucl, greenhsv,green2hsv)
result = cv2.bitwise_and(nucl, nucl, mask=mask)
plt.imshow(mask, cmap="gray")
plt.show()
plt.imshow(result)
plt.show()

这就是我得到的:

显然,面具没有发挥作用……我不明白为什么.我玩过果岭的值,但是什么也没得到...

Obviously the mask didn't do its job... I don't get why. I played with the values of the green but I didn't get anything...

我的错误在哪里?

推荐答案

您的颜色范围还不太正确.同样,inRange()函数中的变量顺序错误.它是从头到尾的,因此必须先使用深色.将您的代码更改为cv2.inRange(hsv_nucl, green2hsv,greenhsv).您可以使用/调整下面代码中的值,该值有效.
结果:

Your color ranges are not quite right yet. Also the variables in the inRange() function are in the wrong order. It's from-to, so the darker color must be first. Change your code to cv2.inRange(hsv_nucl, green2hsv,greenhsv) You can use/tweak the values in the code below, that works.
Result:

有白色背景:

With white background:

import numpy as np 
import cv2

# load image
img = cv2.imread("Eding.png")
# convert to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 
# set lower and upper color limits
lower_val = np.array([50,100,170])
upper_val = np.array([70,255,255])
# Threshold the HSV image to get only green colors
mask = cv2.inRange(hsv, lower_val, upper_val)
# apply mask to original image - this shows the green with black blackground
only_green = cv2.bitwise_and(img,img, mask= mask)

# create a black image with the dimensions of the input image
background = np.zeros(img.shape, img.dtype)
# invert to create a white image
background = cv2.bitwise_not(background)
# invert the mask that blocks everything except green -
# so now it only blocks the green area's
mask_inv = cv2.bitwise_not(mask)
# apply the inverted mask to the white image,
# so it now has black where the original image had green
masked_bg = cv2.bitwise_and(background,background, mask= mask_inv)
# add the 2 images together. It adds all the pixel values, 
# so the result is white background and the the green from the first image
final = cv2.add(only_green, masked_bg)

#show image
cv2.imshow("img", final)
cv2.waitKey(0)
cv2.destroyAllWindows()

这篇关于如何使用OpenCV和蒙版从图像中选择合适的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆