Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the code for OpenCV to implement the matting tool?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about what the code of OpenCV matting tool is, many people may not know much about it. In order to make you understand better, the editor summarizes the following content for you. I hope you can get something according to this article.

In the field of computer image, we often need to do some matting work to extract the region of interest from the image and eliminate other redundant background elements. in order to achieve various functions of computer vision (such as vehicle detection, face detection, etc.). If we purely use tool software such as Meitu, because the tool software integrates all kinds of functions that may be used in image processing, the efficiency of pure matting is very low. Now we use OpenCV to achieve a simple matting program, only need to select the region of interest of the target on the screen, the target will be automatically saved according to the sequence number.

The code is as follows, with easy-to-understand comments:

# include # include / / whether matting is a single target or a multi-target. If it is a single target, uncomment the following line of text. Otherwise, please comment this paragraph. / / # define SINGLE_OBJECT # define TRUE 1 / / logical True # define FALSE 0 / / logical false # define CODE_ESC 27 / / Encoding of the ESC key # define CODE_SPC 32 / / Encoding of the space bar # define STATUS_WAIT 0 / / matting waiting status # define STATUS_PROC 1 / / matting progress status # define STATUS_DONE 2 / / matting completion status # define VIDEO_ FILENAME "capture-1.mp4" / / Video stream file name static int m_x1 = 0 / / Mouse pointer coordinates (start x) static int m_x2 = 0; / / Mouse pointer coordinates (end x) static int m_y1 = 0; / / Mouse pointer coordinates (start y) static int m_y2 = 0; / / Mouse pointer coordinates (end y) static int m_status = STATUS_WAIT / / current matting status indicates static void on_mouse (int, void*); / / mouse callback / / main program int main (void) {int end = 0; / / indicates whether to end the program int next = 0; / / indicates whether to switch to the next picture int code = 0 / / Storage key coding int count = 0; / / Storage target count int frame = 0; / / Video frame number (for interval sampling) int maxCol = 0; / / maximum number of image columns (= image width-1) int maxRow = 0 / / maximum number of lines of image (= image height-1) CvCapture* pVideo = NULL; / / Video stream object IplImage* pFrame = NULL; / / Video frame image (for sample storage) IplImage* pFrmCp = NULL; / / Video frame image (for screen display) CvPoint pt1 = cvPoint (0,0) / / rectangle 1 CvPoint pt2 = cvPoint (0,0); / / rectangle diagonal coordinate point 2 CvRect r = cvRect (0,0,0,0); / / region of interest rectangle char seq [] = "- 2147483648" / / the string form of target count char fil [] = "data\\-2147483648.jpg"; / / file name string / / load video stream pVideo = cvCreateFileCapture (VIDEO_FILENAME); if (! pVideo) {return-1 } / / if (! pVideo) / / create a data storage directory if (_ access ("data", 0)! = 0) {system ("md data");} / / if (_ access ()) / / get the first image and create a copy, and get the maximum number of columns and rows at the same time. It is convenient to use pFrame = cvQueryFrame (pVideo). If (pFrame) {pFrmCp = cvCreateImage (cvGetSize (pFrame), 8, pFrame- > nChannels); maxCol = pFrmCp- > width-1; maxRow = pFrmCp- > height-1;} / / if (pFrame) else {cvReleaseCapture (& pVideo); return-1;} / / else / / set the display window and set the mouse callback cvNamedWindow ("Monitor", CV_WINDOW_AUTOSIZE) CvSetMouseCallback ("Monitor", on_mouse, NULL); / / other initialization end = FALSE; count = 0; frame = 0; while (! end & & pFrame) {next = FALSE While (! next & &! end) {/ / copies the original video image into the copy area (clearing lines, rectangles, etc.) that have contaminated the image) cvCopy (pFrame, pFrmCp, NULL); if (STATUS_WAIT = = m_status) {/ / waiting for matting status. Draw horizontal and vertical guides cvLine (pFrmCp, cvPoint (m_x1, 0), cvPoint (m_x1, maxRow), CV_RGB (0,255,0); cvLine (pFrmCp, cvPoint (0, m_y1), cvPoint (maxCol, m_y1), CV_RGB (0,255,0)) } / / if (STATUS_WAIT) else if (STATUS_PROC = = m_status) {/ / during matting. Draw the currently selected region of interest pt1 = cvPoint (m_x1, m_y1); pt2 = cvPoint (m_x2, m_y2); cvRectangle (pFrmCp, pt1, pt2, CV_RGB (0,255,0)) } / / else if (STATUS_PROC) else if (STATUS_DONE = = m_status) {/ / matting completed Get the region of interest and save the sample r = cvRect by number (m_x1, m_y1, m_x2-m_x1 + 1, m_y2-m_y1 + 1) / / rectangle region of interest if (r.width > 30 & & r.height > 30) {/ / region reaches a certain size, matting is effective, save region of interest sample + + count; cvSetImageROI (pFrame, r) Sprintf_s (seq, "% d", count); strcpy_s (fil, "data\"); strcat_s (fil, seq); strcat_s (fil, ".jpg"); cvSaveImage (fil, pFrame, 0) CvResetImageROI (pFrame); # ifdef SINGLE_OBJECT m_next = TRUE;#endif} / / if (r.width) / / restore matting waiting status m_status = STATUS_WAIT;} / / else if (STATUS_DONE) cvShowImage ("Monitor", pFrmCp) Code = cvWaitKey (10); if (CODE_SPC = = code) {next = TRUE;} / / if (CODE_SPC) else if (CODE_ESC = = code) {end = TRUE } / / else if (CODE_ESC)} / / while (! next) if (next) {do {pFrame = cvQueryFrame (pVideo); + + frame;} while (pFrame & & frame% 60! = 0) / / do...while} / / if (next)} / / while (! end) cvDestroyAllWindows (); cvReleaseImage (& pFrmCp); cvReleaseCapture (& pVideo); return 0 } / / main () / / Mouse event callback void on_mouse (int event, int x, int y, int flags, void* param) {switch (flags) {case CV_EVENT_MOUSEMOVE: if (STATUS_WAIT = = m_status) {/ / waiting status, determine the starting point of the region of interest m_x1 = x, m_y1 = y } / / if (STATUS_WAIT) else if (STATUS_PROC = = m_status) {/ / capture status, determine the end point of region of interest m_x2 = x, m_y2 = y;} / / else if (STATUS_PROC) break Case CV_EVENT_LBUTTONDOWN: if (STATUS_WAIT = = m_status) {/ / wait state press the mouse to enter the capture state, fixed starting point m_x1 = x, m_y1 = y; m_status = STATUS_PROC } / / if (STATUS_WAIT) else if (STATUS_PROC = = m_status) {/ / capture status is pressed, capture is completed, fixed end point m_x2 = x, m_y2 = y; m_status = STATUS_DONE;} / / else if (STATUS_PROC) break } / / switch} / / on_mouse () after reading the above, do you have any further understanding of the code for OpenCV to implement the matting tool? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 297

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report