import cv2 import numpy as np frameWidth = 640 frameHeight = 480 cap = cv2.VideoCapture(0) cap.set(3, frameWidth) cap.set(4, frameHeight) cap.set(10, 150) #blue, green, red #From our previous work (Color Picker) find the values of these colors myColors =[[44,61,36,92,205,255], [80, 193,115,109,255,255], [138,106,115,179,255,255]] #BGR FORMAT myColorValues = [[0, 102, 0], [204, 51, 51], [0, 0, 255]] ##[x, y, colorId] myPoints = [] def findColor(img, myColors, myColorValues): imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) count = 0 newPoints=[] for color in myColors: lower = np.array(color[0:3]) upper = np.array(color[3:6]) mask = cv2.inRange(imgHSV, lower, upper) x,y = getContours(mask) cv2.circle(imgResult,(x,y),5,myColorValues[count],cv2.FILLED) if x!=0 and y!=0: newPoints.append([x,y,count]) count+=1 return newPoints def getContours(img): contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) x, y, w, h = 0,0,0,0 for cnt in contours: area = cv2.contourArea(cnt) if area>500: # cv2.drawContours(imgResult, cnt, -1, (255, 0, 0), 3) peri = cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, 0.02*peri, True) x, y, w, h = cv2.boundingRect(approx) return x+w//2,y def drawOnCanvas(myPoints, myColorValues): for point in myPoints: cv2.circle(imgResult, (point[0], point[1]), 5, myColorValues[point[2]], cv2.FILLED) while True: success, img = cap.read() imgResult = img.copy() newPoints = findColor(img, myColors, myColorValues) if len(newPoints)!=0: for newP in newPoints: myPoints.append(newP) if len(myPoints)!=0: drawOnCanvas(myPoints, myColorValues) cv2.imshow("Result", imgResult) if cv2.waitKey(1) & 0xFF == ord('q'): break
Popular Posts
-
Serilog is a popular logging library for .NET applications. It provides a flexible and efficient logging framework that allows developers ...
-
original image: import cv2 as cv import numpy as np img = cv.imread( 'sudoku.png' , 0 ) _ , th1 = cv.threshold(img ,...
-
Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no i...
Jul 8, 2020
Python OpenCV Project: virtual paint
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment