我需要从该图像中分离出黄色。我尝试使用opencv cv2.inrange
和阈值设置,但没有得到令人满意的结果。每当照明条件改变时,颜色的阴影也会改变,因此请提出其他算法。
答案 0 :(得分:1)
HSV价值体系在这里发挥作用。正常的rgb值过滤可以在不同的光照条件下给出不同的结果。但是,HSV可以帮助您仅选择不太容易打结的特定颜色。
image=cv2.imread('image.jpg')
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_yellow = np.array([20,100,100])
upper_yellow = np.array([30,255,255])
# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_blue, upper_blue)
# Bitwise-AND mask and original image
res = cv2.bitwise_and(image,image, mask= mask)
cv2.imshow('res',res)
#press any key to quit
cv2.waitKey(0)
cv2.destroyAllWindows()
您可以在此处了解有关hsv及其应用程序的更多信息, https://www.kirupa.com/design/little_about_color_hsv_rgb.htm https://docs.opencv.org/3.4.2/df/d9d/tutorial_py_colorspaces.html
http://aishack.in/tutorials/tracking-colored-objects-opencv/
答案 1 :(得分:0)
#我尝试使用代码
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
threshold=0.667
#load image
img=plt.imread(r'C:\Users\Desktop\pbr\artik image\pbr2.jpg')
#extract each color channel
red, green, blue=img[:,:,0],img[:,:,1],img[:,:,2]
#total red+green+biue intensity
intensity=img.sum(axis=2)
add2=np.sum(intensity)
#function to calculate proportion of a certain channel
def color_frac(color):
return np.sum(color)/np.sum(intensity)
#calculate proportion of each color
red_fraction=color_frac(red)
green_fraction=color_frac(green)
blue_fraction=color_frac(blue)
sum_color_fraction=red_fraction+green_fraction+blue_fraction
print('Red fraction:{}'.format(red_fraction))
print('\nGreen fraction:{}'.format(green_fraction))
print('\nBlue fraction:{}'.format(blue_fraction))
print('\nRGB sum:{}'.format(sum_color_fraction))
print(red.shape==green.shape==blue.shape)
add1=red_fraction+green_fraction
green1=green.astype(float)
red1=red.astype(float)
yellow=green1+red1
check=yellow/intensity
j=check
df=pd.DataFrame.from_records(check)
Bad=df[df>threshold].count().sum()
df[df>threshold]=1
df[df<=threshold]=0
df=df.fillna(1)
data=df*255
data=df.astype(np.uint8)
plt.imshow(data)
#plt.imshow(green)
#plr.imshow(red+green)