利用OPENCV寫的從視頻中提取圖片樣本的小程式

來源:互聯網
上載者:User

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

 

利用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;
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.