第十三章 OpenCV

      在〈第十三章 OpenCV〉中尚無留言

CMake

CMake 為C/C++ Makefile的自動產生器, 可以由下方網址下載
cmake : https://cmake.org/files/v3.5/cmake-3.5.2-win32-x86.zip

  1. opencv-2.4.10 下載,並解開到C:\OpenCV2410

將source & build 移到OpenCV2410之下, 並把build 改為devc_x64

  1. 環境變數的path加入 C:\Program Files (x86)\Dev-Cpp\MinGW64\bin
  2. 開啟cmak/bin裏的cmake-gui
  3. source選c:\OpenCV2410\source
  4. build選:\OpenCV2410\devc_x64
  5. configure
  6. Specify the generator for this project : MinGW Makefiles
  7. Specify native compilers /next
  8. C : C:/Program Files (x86)/Dev-Cpp/MinGW64/bin/gcc.exe
  9. C++ : C:/Program Files (x86)/Dev-Cpp/MinGW64/bin/g++.exe
  10. Finish
  11. Generate
  12. 開始/執行 cmd
  13. cd \OpenCV2410\devc_x64
  14. mingw32-make
  15. 進入Dev C++/工具/編譯器選項/TDM-GCC 4.9.2 64-bit Debug/目錄
  16. bin : C:\OpenCV2410\devc_x64\bin
  17. lib : C:\OpenCV2410\devc_x64\lib
  18. c include

C:\OpenCV2410\devc_x64\include

C:\OpenCV2410\devc_x64\include\opencv

C:\OpenCV2410\devc_x64\include\opencv2

  1. c++ include

C:\OpenCV2410\devc_x64\include

C:\OpenCV2410\devc_x64\include\opencv

C:\OpenCV2410\devc_x64\include\opencv2

  1. 一般/連結加入

-llibopencv_calib3d2410 -llibopencv_contrib2410 -llibopencv_core2410 -llibopencv_features2d2410 -llibopencv_flann2410 -llibopencv_gpu2410 -llibopencv_highgui2410 -llibopencv_imgproc2410 -llibopencv_legacy2410 -llibopencv_ml2410 -llibopencv_nonfree2410 -llibopencv_objdetect2410 -llibopencv_ocl2410 -llibopencv_photo2410 -llibopencv_stitching2410 -llibopencv_superres2410 -llibopencv_video2410 -llibopencv_videostab2410

 

程式碼

#include <iostream>

#include <cv.h>

#include <highgui.h>

#include <stdio.h>

#include “opencv2/objdetect/objdetect.hpp”

#include “opencv2/highgui/highgui.hpp”

#include “opencv2/imgproc/imgproc.hpp”

int min_face_height = 50;

int min_face_width = 50;

float scale=5.0f;

int g_slider_position=0;

CvCapture *capture;

using namespace std;

void CvTrackcalllback(int pos){

cvSetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES, pos );

}

int  main(){

CvSeq* faces;

CvSize size;

int count=0;

char AviFileName[]=”test.mp4″;

char cascade_name[]=”C:/OpenCV2410/sources/data/haarcascades/haarcascade_frontalface_alt.xml”;

CvHaarClassifierCascade* classifier=(CvHaarClassifierCascade*)cvLoad(cascade_name, 0, 0, 0);

if(!classifier){

cerr<<“ERROR: Could not load classifier cascade.”<<endl;

return -1;

}

capture = cvCaptureFromAVI(AviFileName);

cvNamedWindow(“AVI Player”,0);

CvMemStorage* facesMemStorage=cvCreateMemStorage(0);

int frames = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT);

if(frames!=0){

cvCreateTrackbar(“Test”,”AVI Player”,&g_slider_position,frames,CvTrackcalllback);

}

 

while(true){

IplImage *frame, *tempFrame, *tFrame;

if(cvGrabFrame(capture)){

frame=cvRetrieveFrame(capture);

tempFrame=cvCreateImage(cvSize(frame->width, frame->height), IPL_DEPTH_8U, frame->nChannels);

if(frame->origin==IPL_ORIGIN_TL){

cvCopy(frame, tempFrame, 0);

}

else{

cvFlip(frame, tempFrame, 0);

}

 

size.width = tempFrame->width /scale;

size.height = tempFrame->height /scale;

tFrame = cvCreateImage( size, tempFrame->depth, tempFrame->nChannels);

cvResize(tempFrame, tFrame, CV_INTER_LINEAR);

cvClearMemStorage(facesMemStorage);

faces=cvHaarDetectObjects(tFrame, classifier, facesMemStorage, 1.1, 3, CV_HAAR_DO_CANNY_PRUNING, cvSize(min_face_width, min_face_height));

 

if(faces){

for(int i=0; i<faces->total; ++i){

CvPoint point1, point2;

CvRect* rectangle = (CvRect*)cvGetSeqElem(faces, i);

point1.x = (rectangle->x)*scale;

point2.x = (rectangle->x + rectangle->width)*scale;

point1.y = (rectangle->y)*scale;

point2.y = (rectangle->y + rectangle->height)*scale;

cvRectangle(tempFrame, point1, point2, CV_RGB(255,0,0), 3, 8, 0);

}

}

 

cvShowImage(“AVI Player”,tempFrame);

if(cvWaitKey(10)>=0) break;

cvReleaseImage(&tempFrame);

cvReleaseImage(&tFrame);

//cvReleaseImage(&frame);

}

else{

break;

}

}

cvReleaseCapture(&capture);

cvDestroyWindow(“AVI Player”);

}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *