Small programs written by opencv to extract image samples from videos

Source: Internet
Author: User

Http://www.cnblogs.com/seacode/archive/2011/07/16/2108296.html

 

Small programs written by opencv to extract image samples from videos

For machine learning, we often need to extract sample images.

 

/*************************************** **************************************** **************************************** *********************
// Tool applet for extracting sample images from videos
// Lian 2011.7.12
**************************************** **************************************** **************************************** *******************/

# Include <opencv2/Video/tracking. HPP>
# Include <opencv2/imgproc. HPP>
# Include <opencv2/highgui. HPP>

# Include <iostream>
# Include <ctype. h>

Using namespace CV;
Using namespace STD;

// Global variable
Mat image;

Bool SelectObject = false;
Int trackobject = 0;
Bool showhist = true;
Point origin;
Rect selection;

// Image count
Int imgcarnum = 0;
Int imgpersonnum = 0;
Int imgpergroupnum = 0;

Void help ()
{
Cout <"\ nthis is a demo get object picture from video \ n"
<Endl;

Cout <"\ nusage: \ n"
"Program videoname imgcarnum imgpersonnum imgpergroupnum \ n" <Endl;

Cout <"\ n \ nhot keys: \ n"
"\ Tesc-T \ n"
"\ TC-save object type is car \ n"
"\ TP-save object type is People \ n"
"\ Ts-save object type is people group \ n"
"To initialize, select the object with mouse \ n" <Endl;
}

Void onmouse (INT event, int X, int y, Int, void *)
// Respond to the mouse drag to obtain the rectangular area
{
If (SelectObject)
{
Selection. x = min (x, origin. X );
Selection. Y = min (Y, origin. y );
Selection. width = STD: ABS (X-origin. X );
Selection. Height = STD: ABS (Y-origin. y );

Selection & = rect (0, 0, image. cols, image. Rows );
}
Switch (Event)
{
Case cv_event_lbuttondown:
Origin = point (x, y );
Selection = rect (X, Y, 0, 0 );
SelectObject = true;
Break;
Case cv_event_lbuttonup:
SelectObject = false;
If (selection. width> 0 & selection. Height> 0)
Trackobject =-1;
Break;
}
} // Onmouse

Int saveimage (MAT & IMG)
// Save and name the Image Based on the buttons
{
String filename;
Char s [10];
Char c = (char) waitkey (0 );
If (C = 27)
Return 0;
Switch (c)
{
Case 'p': // pedestrian
++ Imgpersonnum;
Sprintf (S, "% lD", imgpersonnum );
Filename = "d :\\ test image http://www.cnblogs.com/seacode/admin/file://PERSON//per" + (string) S;
Break;
Case 'C': // car
++ Imgcarnum;
Sprintf (S, "% lD", imgcarnum );
Filename = "d :\\ test image http://www.cnblogs.com/seacode/admin/file://CAR//car" + (string) S;
Break;
Case's ': // audience
++ Imgpergroupnum;
Sprintf (S, "% lD", imgpergroupnum );
Filename = "d :\\ test image http://www.cnblogs.com/seacode/admin/file://PERSONGROUP//perGroups" + (string) S;
Break;
Default:
;
}
Filename + = ". PNG ";
Imwrite (filename, IMG); // Save the image

} // Saveimage

Int main (INT argc, char ** argv)
{
Videocapture cap;
Rect trackwindow;
Rotatedrect trackbox;
Int hsize = 16;
Float hranges [] ={ 0,180 };
Const float * phranges = hranges;

If (argc = 1 | (argc = 2 & strlen (argv [1]) = 1 & isdigit (argv [1] [0])
Cap. Open (argc = 2? Argv [1] [0]-'0': 0 );
Else if (argc = 2)
Cap. Open (argv [1]);
Else if (argc> 2) // specify the image file number
{
Cap. Open (argv [1]);
Imgcarnum = atoi (argv [2]);
Imgpersonnum = atoi (argv [3]);
Imgpergroupnum = atoi (argv [4]);
}

If (! Cap. isopened ())
{
Help ();
Cout <"*** cocould not initialize capturing... *** \ n ";
Return 0;
}

Help ();

Namedwindow ("ROI", 1 );
Namedwindow ("getimage Demo", 1 );
Setmousecallback ("getimage Demo", onmouse, 0 );

Mat HSV, hue, mask, Hist, histimg = mat: zeros (200,320, cv_8uc3), backproj;

For (;;)
{
Mat frame;
Cap> frame;
If (frame. Empty ())
Break;

Frame. copyto (image); // get the image
Cvtcolor (image, HSV, cv_bgr2hsv );

If (trackobject)
{

If (trackobject <0)
{
Mat ROI (image, selection); // get
Imshow ("ROI", ROI); // display
Saveimage (ROI); // save

Trackwindow = selection;
Trackobject = 1;
}

}

If (SelectObject & selection. width> 0 & selection. Height> 0)
{
Mat ROI (image, selection );
Bitwise_not (ROI, ROI );

}
Imshow ("getimage Demo", image );

Char c = (char) waitkey (300); // run the task frame by frame
If (C = 27)
Break;
 
}

Return 0;
}

 

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.