I am trying to detect red color from the video that\'s being taken from my webcam. The following code example given below is taken from OpenCV Documentation. The code is gi
Here is a program to determine color you need by choosing the 6 arrays parameters.(work on Opencv 3.2). You chose your image or a "color range barre" input image and you move cursors and see which arrays values are the ones you need to isolate your color! Color range program screen pic
here is the code:(can easily be adapted for video input). image.jpg->(your image) color_bar.jpg->(any image you want just to display a windows,try anything)
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()