利用OPENCV写的从视频中提取图片样本的小程序

做机器学习的,经常要提取样本图片,所有写了这个小工具

/********************************************************************************************************************************************

//从视频中提取样本图片的工具小程序

//lian 2011.7.12

*******************************************************************************************************************************************/

#include <opencv2/video/tracking.hpp>

#include <opencv2/imgproc/imgproc.hpp>

#include <opencv2/highgui/highgui.hpp>

#include <iostream>

#include <ctype.h>

using namespace cv;

using namespace std;

//全局变量

Mat image;

bool selectObject = false;

int trackObject = 0;

bool showHist = true;

Point origin;

Rect selection;

//图片计数

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* )

//响应鼠标拖动,获得矩形区域

{

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)

//保存截图,根据按键给图像命名

{

string filename;

char s[10];

char c = (char)waitKey(0);

if( c == 27 )

return 0;

switch(c)

{

case 'p'://行人

++imgPersonNum;

sprintf(s,"%ld",imgPersonNum);

filename = "d:\\测试图片http://www.cnblogs.com/seacode/admin/file://PERSON//per"+(string) s;

break;

case 'c'://车

++imgCarNum;

sprintf(s,"%ld",imgCarNum);

filename = "d:\\测试图片http://www.cnblogs.com/seacode/admin/file://CAR//car" + (string) s;

break;

case 's'://人群

++imgPerGroupNum;

sprintf(s,"%ld",imgPerGroupNum);

filename = "d:\\测试图片http://www.cnblogs.com/seacode/admin/file://PERSONGROUP//perGroups" +(string) s;

break;

default:

;

}

filename +=".png";

imwrite(filename,img); //保存图片

}//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) //指定图片文件序号

{

cap.open(argv[1]);

imgCarNum = atoi(argv[2]);

imgPersonNum = atoi(argv[3]);

imgPerGroupNum = atoi(argv[4]);

}

if( !cap.isOpened() )

{

help();

cout << "***Could 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);//获取图片

cvtColor(image, hsv, CV_BGR2HSV);

if( trackObject )

{

if( trackObject < 0 )

{

Mat roi(image, selection); //获取截图

imshow( "ROI", roi );//显示截图

saveImage(roi);//保存截图

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); //逐帧运行

if( c == 27 )

break;

}

return 0;

}