我正在使用VideoWriter,VideoCapture和imShow从笔记本电脑摄像头录制视频。我正在使用faceCascade.detectMultiScale( frameGray2, faces, 1.1, 2, 0, cv::Size(80,80))
来检测视频中的人脸。我正在使用faces.size()打印面孔的数量。我正在使用
cv::CascadeClassifier faceCascade;
if (!faceCascade.load( faceCascadeName ))
{
std::cerr << "Could not load face classifier" << std::endl;
return 2;
}
其中
string faceCascadeName = "/home/sandeep/git-rectify/opencv/data/lbpcascades/lbpcascade_frontalface.xml";
我对使用的xml文件类型感到怀疑。请检查该值是否正确
整个代码如下
int main(int, char**)
{
//Preprocess the image
cv::Mat frame;
cv::Mat frameGray;
cv::Mat frameGray2;
cv::CascadeClassifier faceCascade;
//This will contain the output of the face detector
std::vector<cv::Rect> faces;
const char *colorFrame = "Color frame";
const char *GrayFrame1 = "Gray Frame1";
const char *GrayFrame2 = "Gray Frame2";
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
// Default resolution of the frame is obtained.The default resolution is system dependent.
int frame_width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
int frame_height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
/** Define the codec(CV_FOURCC method, other samples like MJPG are present in http://www.fourcc.org/codecs.php)
*** and create VideoWriter object.The output is stored in 'outcpp.avi' file.100 is the frame rate per second.
**/
VideoWriter video("outcpp.avi",CV_FOURCC('M','J','P','G'),100, Size(frame_width,frame_height));
string faceCascadeName = "/home/sandeep/git-rectify/opencv/data/lbpcascades/lbpcascade_frontalface.xml";
//Load the classifiers
if (!faceCascade.load( faceCascadeName ))
{
std::cerr << "Could not load face classifier" << std::endl;
return 2;
}
for(;;)
{
cap >> frame; // get a new frame from camera
if (frame.empty())
break;
// cvtColor is used to transform image from one color space to another. Here it converts to Grayscale Image.
cv::cvtColor( frame, frameGray, cv::COLOR_RGB2GRAY);
//It is a method that improves the contrast in an image, in order to stretch out the intensity range.
cv::equalizeHist( frameGray, frameGray2 );
//Detect the face
faceCascade.detectMultiScale( frameGray2, faces, 1.1, 2, 0, cv::Size(80,80));
cout << "Number of detected faces: " << faces.size() << std::endl;
video.write(frame);
imshow( colorFrame, frame );
//namedWindow( GrayFrame1, CV_WINDOW_AUTOSIZE );
//namedWindow( GrayFrame2, CV_WINDOW_AUTOSIZE );
//imshow( GrayFrame1, frameGray );
//imshow( GrayFrame2, frameGray2 );
char c=(char)waitKey(1);
if(c=='1')
break;
}
// When everything done, release the video capture object
cap.release();
video.release();
// Closes all the frames
destroyAllWindows();
return 0;
}
1>我是镜头前唯一的人,但是在set = {0,1,2,3}中,面孔的数量是1,这是错误的,应该只打印1。
2>还有1个问题,我是否可以使用此代码进行一些修改来检测蔬菜,获取蔬菜名称?