我已经使用 wiki 上提供的说明使用 cmake 安装了 OpenCV。我已经在我的桌面上构建了这些文件。我尝试运行的第一个程序是从相机程序中简单捕获。它的来源是
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
int main() {
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
if ( !capture ) {
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
return -1;
}
cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
while ( 1 ) {
IplImage* frame = cvQueryFrame( capture );
if ( !frame ) {
fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}
cvShowImage( "Cam WIndow", frame );
if ( (cvWaitKey(10) & 255) == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "Cam WIndow" );
return 0;
}
它使用 g++ 命令和 pkg-config 正确编译,如下所示:
g++ -o cam Camera.cpp
pkg-config opencv --cflags --libs
我尝试运行的第二个程序也是来自 wiki,人脸识别程序,其源代码如下。
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
void detectAndDisplay( Mat frame );
String face_cascade_name = "lbpcascade_frontalface.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
string window_name = "Capture - Face detection";
RNG rng(12345);
int main( int argc, const char** argv )
{
CvCapture* capture;
Mat frame;
if( !face_cascade.load( face_cascade_name ))
{
printf("--(!)Error loading\n");
return -1;
};
if( !eyes_cascade.load( eyes_cascade_name ))
{
printf("--(!)Error loading\n");
return -1;
};
capture = cvCaptureFromCAM( -1 );
if( capture )
{
while( true )
{
frame = cvQueryFrame( capture );
if( !frame.empty() )
{ detectAndDisplay( frame ); }
else
{ printf(" --(!) No captured frame -- Break!"); break; }
int c = waitKey(10);
if( (char)c == 'c' ) { break; }
}
}
return 0;
}
void detectAndDisplay( Mat frame )
{
std::vector<Rect> faces;
Mat frame_gray;
cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0, Size(80, 80) );
for( int i = 0; i < faces.size(); i++ )
{
Mat faceROI = frame_gray( faces[i] );
std::vector<Rect> eyes;
eyes_cascade.detectMultiScale(faceROI,eyes,1.1,2,0|CV_HAAR_SCALE_IMAGE,Size(30, 30));
if( eyes.size() == 2)
{
Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
ellipse(frame,center,Size(faces[i].width*0.5,faces[i].height*0.5),0,0,360,Scalar(255,0,0),2,8,0);
for( int j = 0; j < eyes.size(); j++ )
{ \
Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
circle( frame, center, radius, Scalar( 255, 0, 255 ), 3, 8, 0 );
}
}
}
imshow( window_name, frame );
}
现在我使用下面给出的命令编译了这个程序:
g++ -o cam Cam1.cpp -lopencv_core -lopencv_imgproc -lopencv_calib3d -lopencv_video -lopencv_features2d -lopencv_ml -lopencv_highgui -lopencv_objdetect -lopencv_contrib -lopencv_legacyD
此编译命令给出错误
fatal error: opencv2/objdetect/objdetect.hpp: No such file or directory
该怎么办?