Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Detect and Recognize Car License Plate from a video in real time
Next article icon

Let us see how to draw the movement of objects captured by the webcam using OpenCV. Our program takes the video input from the webcam and tracks the objects we are moving. After identifying the objects, it will make contours precisely. After that, it will print all your drawing on the output screen.
 

python3
# importing the modulesimportcv2importnumpyasnp# set Width and Height of output ScreenframeWidth=640frameHeight=480# capturing Video from Webcamcap=cv2.VideoCapture(0)cap.set(3,frameWidth)cap.set(4,frameHeight)# set brightness, id is 10 and# value can be changed accordinglycap.set(10,150)# object color valuesmyColors=[[5,107,0,19,255,255],[133,56,0,159,156,255],[57,76,0,100,255,255],[90,48,0,118,255,255]]# color values which will be used to paint# values needs to be in BGRmyColorValues=[[51,153,255],[255,0,255],[0,255,0],[255,0,0]]# [x , y , colorId ]myPoints=[]# function to pick color of objectdeffindColor(img,myColors,myColorValues):# converting the image to HSV formatimgHSV=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)count=0newPoints=[]# running for loop to work with all colorsforcolorinmyColors:lower=np.array(color[0:3])upper=np.array(color[3:6])mask=cv2.inRange(imgHSV,lower,upper)x,y=getContours(mask)# making the circlescv2.circle(imgResult,(x,y),15,myColorValues[count],cv2.FILLED)ifx!=0andy!=0:newPoints.append([x,y,count])count+=1returnnewPoints# contours function used to improve accuracy of paintdefgetContours(img):_,contours,hierarchy=cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)x,y,w,h=0,0,0,0# working with contoursforcntincontours:area=cv2.contourArea(cnt)ifarea>500:peri=cv2.arcLength(cnt,True)approx=cv2.approxPolyDP(cnt,0.02*peri,True)x,y,w,h=cv2.boundingRect(approx)returnx+w//2,y# draws your action on virtual canvasdefdrawOnCanvas(myPoints,myColorValues):forpointinmyPoints:cv2.circle(imgResult,(point[0],point[1]),10,myColorValues[point[2]],cv2.FILLED)# running infinite while loop so that# program keep running until we close itwhileTrue:success,img=cap.read()imgResult=img.copy()# finding the colors for the pointsnewPoints=findColor(img,myColors,myColorValues)iflen(newPoints)!=0:fornewPinnewPoints:myPoints.append(newP)iflen(myPoints)!=0:# drawing the pointsdrawOnCanvas(myPoints,myColorValues)# displaying output on Screencv2.imshow("Result",imgResult)# condition to break programs execution# press q to stop the execution of programifcv2.waitKey(1)and0xFF==ord('q'):break

Output : 
 


 


Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp