这里是一个程序,以确定需要通过选择6个阵列参数的颜色。 (关于opencv的3.2工作)。 你选择你的图像或“色彩范围巴雷”输入图像和你移动光标,看看哪些数组值是您需要隔离的颜色的! Color range program screen pic
这里是代码:(可以很容易地适应视频输入) image.jpg - >(您的图片) color_bar.jpg - >(任何图像你只想显示一个窗口,尝试任何事情)
import cv2
import numpy as np
from matplotlib import pyplot as plt
def nothing(x):
pass
def main():
window_name='color range parameter'
cv2.namedWindow(window_name)
# Create a black image, a window
im = cv2.imread('image.jpg')
cb = cv2.imread('color_bar.jpg')
hsv = cv2.cvtColor(im,cv2.COLOR_BGR2HSV)
print ('lower_color = np.array([a1,a2,a3])')
print ('upper_color = np.array([b1,b2,b3])')
# create trackbars for color change
cv2.createTrackbar('a1',window_name,0,255,nothing)
cv2.createTrackbar('a2',window_name,0,255,nothing)
cv2.createTrackbar('a3',window_name,0,255,nothing)
cv2.createTrackbar('b1',window_name,150,255,nothing)
cv2.createTrackbar('b2',window_name,150,255,nothing)
cv2.createTrackbar('b3',window_name,150,255,nothing)
while(1):
a1 = cv2.getTrackbarPos('a1',window_name)
a2 = cv2.getTrackbarPos('a2',window_name)
a3 = cv2.getTrackbarPos('a3',window_name)
b1 = cv2.getTrackbarPos('b1',window_name)
b2 = cv2.getTrackbarPos('b2',window_name)
b3 = cv2.getTrackbarPos('b3',window_name)
# hsv hue sat value
lower_color = np.array([a1,a2,a3])
upper_color = np.array([b1,b2,b3])
mask = cv2.inRange(hsv, lower_color, upper_color)
res = cv2.bitwise_and(im, im, mask = mask)
cv2.imshow('mask',mask)
cv2.imshow('res',res)
cv2.imshow('im',im)
cv2.imshow(window_name,cb)
k = cv2.waitKey(1) & 0xFF
if k == 27: # wait for ESC key to exit
break
elif k == ord('s'): # wait for 's' key to save and exit
cv2.imwrite('Img_screen_mask.jpg',mask)
cv2.imwrite('Img_screen_res.jpg',res)
break
cv2.destroyAllWindows()
#Run Main
if __name__ == "__main__" :
main()