最終更新:
edward_black 2012年12月24日(月) 21:59:13履歴
- DirectShowを用いてキャプチャした後, OpenCVのIplImageに画像データを渡す.
- OpenCVのキャプチャ周りに問題がある場合におすすめ.
- Easy Web Camera LIBraryを使用させて頂く.
//=======================================================================// //=======================================================================// //DirectShowでキャプチャ->Saveするだけ. //コンパイル::VS2008, OpenCV2.4.3 //Dディレクトリ直下に"yyyymmdd_hhmm"形式でディレクトリ作成した後, "img%05d.png/ppm"形式で画像を連番で保存. //ex.2012年12月24日13時26分に実行:"D:\20121224_1326\img00000.png - img99999.png //=======================================================================// //=======================================================================// #include "iostream" #include "direct.h" #include "string" #include "sstream" #include "string.h" #include "time.h" #include "DirectShow.h" #include "Windows.h" #include "opencv2\\opencv.hpp" #ifdef _DEBUG //Debugモードの場合 #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_core243d.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_imgproc243d.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_highgui243d.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_objdetect243d.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_contrib243d.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_features2d243d.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_flann243d.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_gpu243d.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_haartraining_engined.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_legacy243d.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_ts243d.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_video243d.lib") #else //Releaseモードの場合 #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_core243.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_imgproc243.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_highgui243.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_objdetect243.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_contrib243.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_features2d243.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_flann243.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_gpu243.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_haartraining_engined.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_legacy243.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_ts243.lib") #pragma comment(lib,"C:\\opencv\\build\\x86\\vc10\\lib\\opencv_video243.lib") #endif using namespace std; #define VGA_X 640 #define VGA_Y 480 #define QVGA_X 320 #define QVGA_Y 240 #define SAVE 1 #define PNG 0 #define PPM 1 //2台のカメラを接続する場合 片方だけ保存中 int main() { //DirectShowクラスを生成 //DirectShow オブジェクト名(WIDTH,HEIGHT,CAMERA NUMBER) DirectShow capture1(VGA_X, VGA_Y,0); DirectShow capture2(VGA_X, VGA_Y,1); char* window_name1 = "Capture for DirectShow1"; char* window_name2 = "Capture for DirectShow2"; const char* d_pass; //buff: ループ内でファイル連番保存のために使用するbuff領域 //num: ループ内カウント用変数 char buff[100] = ""; char buff2[100] = ""; int num=0; stringstream ss; string pass; //処理速度表示用 CvFont dfont; char message[64] = ""; //フォント cvInitFont(&dfont, CV_FONT_HERSHEY_SIMPLEX , 1.0f, 1.0f, 0.0f, 2, CV_AA); //OpenCV用の画像領域確保 IplImage *frame1 = cvCreateImage(cvSize(VGA_X, VGA_Y), IPL_DEPTH_8U,3); IplImage *image1 = cvCreateImage(cvSize(VGA_X, VGA_Y), IPL_DEPTH_8U,3); IplImage *frame2 = cvCreateImage(cvSize(VGA_X, VGA_Y), IPL_DEPTH_8U,3); IplImage *image2 = cvCreateImage(cvSize(VGA_X, VGA_Y), IPL_DEPTH_8U,3); //ウインドウを開く cvNamedWindow(window_name1, CV_WINDOW_AUTOSIZE); cvNamedWindow(window_name2, CV_WINDOW_AUTOSIZE); if(SAVE == 1) { time_t now = time(NULL); struct tm *date = localtime(&now); ss << "D:" << date->tm_year+1900 << date->tm_mon+1 << date->tm_mday << "_" << date->tm_hour << date->tm_min; cout << ss.str(); pass = ss.str(); //const char*へキャスト d_pass = pass.c_str(); mkdir(d_pass); //明示的に確保された領域へ行く strcpy(buff, pass.c_str()); if(PNG == 1) strcat(buff,"\\img%05d.png"); if(PPM == 1) strcat(buff,"\\img%05d.ppm"); } while(1) { //処理時間: image1に表示 double t = (double)cvGetTickCount(); frame1 = capture1.capture(); cvCopy(frame1, image1); frame2 = capture2.capture(); cvCopy(frame2, image2); //30ms待ち, 27(=ESC)でbreak int c = cvWaitKey(30); if(c == 27 ) break; //cvGetTickCount()からの経過時間 t = (double)cvGetTickCount() - t; sprintf(message, "%gms", t/((double)cvGetTickFrequency()*1000.) ); //表示[時間オーバーレイ] cvPutText(image1, message, cvPoint(50, 50), &dfont, CV_RGB(255, 255, 255)); //表示[IMAGE] cvShowImage(window_name1, image1); cvShowImage(window_name2, image2); sprintf(buff2, buff, num++); cvSaveImage(buff2, image2); } //解放 cvDestroyWindow(window_name1); cvReleaseImage(&image1); cvDestroyWindow(window_name2); cvReleaseImage(&image2); }
このページへのコメント
Thanks for every other great post. Where else maay just anyone get that type
of info in such a perfect meanns of writing? I've a presentation subsequent week,
and I am at the search for such info.
http://www.theinsaneworkoutreview.com/
What's up friends, pleasant post and fastidious arguments commented at this place, I am
actually enjoying by these.
http://www.undeadly.org/cgi?action=article&sid...
Incredible points. Soolid arguments. Keep up the great effort.
http://www.unspoilednews.com/story/63487/strengtho...
Great topic and well written. Do you have any more resources about this that you reccommend?
Great topic and well written. Do you have any more resources about this that you reccommend?