PYTHON+OPENCV Select a frame in the video and then use the mouse callback to implement the picture rectangle frame

Source: Internet
Author: User

Recently, because you want to implement a template match, you need to select a target in the video and then frame it (that is, as a template) to detect it using a template matching method. So we need to first select a frame in the video, but the only way I can think of it in the process of using the camera to read the video is:

1. In the process of video playback, when you want to select which map to mark the target, press the pause key.

2. Frame the target you want to detect.

How can that be achieved?

The first is the pause how to implement it? In the camera live, we will first copy the image and display it in another window. Then turn off the camera and finish the pause. After marking the target and then turning on the camera. How does it come true?

Importcv2drawing=Falseix, iy=-1,-1Tempflag=Falsedefdraw_circle (event, x, Y, Flags, param):GlobalIX, iy, drawing, mode, cap, template, Tempflagifevent = =Cv2. Event_lbuttondown:tempflag=True Drawing=True IX, iy= x, y#press the left mouse button to record the current coordinate point with the global variable Ix,iy    elifevent = =Cv2. Event_lbuttonup:ifDrawing = =true:drawing= False#the left mouse button is lifted to draw a rectangular boxCv2.rectangle (frame, (ix, IY), (x, y), (0, 255, 0), 1) Template= Frame[iy:y, ix:x,:]#target image in the Intercept boxCap = Cv2. Videocapture (-1)#turn on the cameraCv2.imshow ('img', frame)#image after displaying a picture frameCv2.namedwindow ('img') Cv2.setmousecallback ('img', draw_circle) Cap= Cv2. Videocapture (-1) while(True): ret, Frame=Cap.read () cv2.imshow ('vedio', frame)ifTempflag = = True:#If the target is framed, the target is displayedCv2.imshow ('Temp', template) K= Cv2.waitkey (33)    ifK = = 27:#Exit Video         Break    elifK = = 32:#If you press the space bar         while(1): Cap.release ()#turn off the cameraImgcopy = Frame#show the last image before turning off the cameraCv2.imshow ('img', frame) K= Cv2.waitkey (0)#waiting to call the mouse callback function box out of the target            ifK = = 32:#after the box is finished, press the SPACEBAR again to replay the camera capture screen.                 Break

Results

For the frame in the image of the target, I actually read the online code, such as http://blog.csdn.net/szfhy/article/details/51087301

The picture after my frame is like this

The heart is broken, the baby just want a rectangular box ~

Originally I want to draw a rectangle box, there is a rectangle box with the mouse drag size changes, and only a rectangular box, the mouse lifted, the rectangle is determined. Temporarily do not know how to achieve, have to know can exchange, hehe ~

PYTHON+OPENCV Select a frame in the video and then use the mouse callback to implement the picture rectangle frame

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.