opencv从摄像头读取图像怎么改为读取保存好的jpg图片
下面是代码人脸识别的,怎么把从摄像头读取改为从读取保存好的图片#include"opencv2/objdetect/objdetect.hpp"#include"open...
下面是代码人脸识别的,怎么把从摄像头读取改为从读取保存好的图片
#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;
/** Function Headers */
void detectAndDisplay( Mat frame );
/** Global variables */
//-- Note, either copy these two files from opencv/data/haarscascades to your current folder, or change these locations
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
string window_name = "Capture - Face detection";
RNG rng(12345);
/**
* @function main
*/
int main( int argc, const char** argv )
{
CvCapture* capture;
Mat frame;
//-- 1. Load the cascades
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; };
//-- 2. Read the video stream
capture = cvCaptureFromCAM( -1 );
//capture = cvCreateCameraCapture(0);//
if( capture )
{
while( true )
{
frame = cvQueryFrame( capture );
//-- 3. Apply the classifier to the frame
if( !frame.empty() )
{ detectAndDisplay( frame ); }
else
{ printf(" --(!) No captured frame -- Break!"); break; }
int c = waitKey(10);
if( (char)c == 'c' ) { break; }
}
}
cvReleaseCapture( &capture );//释放内存;*/
detectAndDisplay( frame );
return 0;
} 展开
#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;
/** Function Headers */
void detectAndDisplay( Mat frame );
/** Global variables */
//-- Note, either copy these two files from opencv/data/haarscascades to your current folder, or change these locations
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
string window_name = "Capture - Face detection";
RNG rng(12345);
/**
* @function main
*/
int main( int argc, const char** argv )
{
CvCapture* capture;
Mat frame;
//-- 1. Load the cascades
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; };
//-- 2. Read the video stream
capture = cvCaptureFromCAM( -1 );
//capture = cvCreateCameraCapture(0);//
if( capture )
{
while( true )
{
frame = cvQueryFrame( capture );
//-- 3. Apply the classifier to the frame
if( !frame.empty() )
{ detectAndDisplay( frame ); }
else
{ printf(" --(!) No captured frame -- Break!"); break; }
int c = waitKey(10);
if( (char)c == 'c' ) { break; }
}
}
cvReleaseCapture( &capture );//释放内存;*/
detectAndDisplay( frame );
return 0;
} 展开
1个回答
展开全部
没有看见void detectAndDisplay( Mat frame );的实现,就默认这个函数做人脸检测了...
将“//-- 2. Read the video stream”之后的if块和释放capture的语句删除,添加
frame=imread("D:/image/1.jpg",IMREAD_COLOR);
D:/image/1.jpg替换为所需图片的路径,如果opencv为2.3.1及以下版本,IMREAD_COLOR换为CV_LOAD_IMAGE_COLOR;
另外吐槽下,看头文件应该用的是opencv2,却仍然使用opencv1的CvCapture,虽然ok,但感觉不彻底...
将“//-- 2. Read the video stream”之后的if块和释放capture的语句删除,添加
frame=imread("D:/image/1.jpg",IMREAD_COLOR);
D:/image/1.jpg替换为所需图片的路径,如果opencv为2.3.1及以下版本,IMREAD_COLOR换为CV_LOAD_IMAGE_COLOR;
另外吐槽下,看头文件应该用的是opencv2,却仍然使用opencv1的CvCapture,虽然ok,但感觉不彻底...
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询