我制作opencv程序,并在搜索中找到它这个程序让我在矩形中找到人脸
但是我有个错误即矢量下标超出范围错误消息我怎样才能解决问题??
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/contrib/contrib.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <vector>
using namespace cv;
using namespace std;
int main()
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
vector<Mat> images;
vector<int> labels;
// Get the height from the first image. We'll need this
// later in code to reshape the images to their original
// size AND we need to reshape incoming faces to this size:
int im_width = images[0].cols;
int im_height = images[0].rows;
// Create a FaceRecognizer and train it on the given images:
Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
model->train(images, labels);
// That's it for learning the Face Recognition model. You now
// need to create the classifier for the task of Face Detection.
// We are going to use the haar cascade you have specified in the
// command line arguments:
//
CascadeClassifier faceCascade;
faceCascade.load("C:\opencv\data\haarcascade\haarcascade_frontalface_alt.xml");
Mat image;
namedWindow("edges",1);
for(;;){
cap >> image; // get a new frame from camera
Mat original = image.clone();
Mat frame_gray;
cvtColor(original, frame_gray, CV_BGR2GRAY);
// equalizeHist( frame_gray, frame_gray );
/* GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);*/
//DITECT FACE
// Find the faces in the frame:
vector< Rect_<int> > faces;
faceCascade.detectMultiScale(frame_gray, faces);
// At this point you have the position of the faces in
// faces. Now we'll get the faces, make a prediction and
// annotate it in the video. Cool or what?
for(int i = 0; i < faces.size(); i++) {
Rect face_i = faces[i];
//rectangle(image, aRect, CV_RGB(0, 255,0), 1);
Mat face = frame_gray(face_i);
// Resizing the face is necessary for Eigenfaces and Fisherfaces. You can easily
// verify this, by reading through the face recognition tutorial coming with OpenCV.
// Resizing IS NOT NEEDED for Local Binary Patterns Histograms, so preparing the
// input data really depends on the algorithm used.
//
// I strongly encourage you to play around with the algorithms. See which work best
// in your scenario, LBPH should always be a contender for robust face recognition.
//
// Since I am showing the Fisherfaces algorithm here, I also show how to resize the
// face you have just found:
Mat face_resized;
cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);
// Now perform the prediction, see how easy that is:
int prediction = model->predict(face_resized);
// And finally write all we've found out to the original image!
// First of all draw a green rectangle around the detected face:
rectangle(original, face_i, CV_RGB(0, 255,0), 1);
// Create the text we will annotate the box with:
string box_text = format("Prediction = %d", prediction);
// Calculate the position for annotated text (make sure we don't
// put illegal values in there):
int pos_x = std::max(face_i.tl().x - 10, 0);
int pos_y = std::max(face_i.tl().y - 10, 0);
// And now put it into the image:
putText(original, box_text, Point(pos_x, pos_y), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0,255,0), 2.0);
}
faceCascade.detectMultiScale( frame_gray, faces, 1.1, 3, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
imshow("frame_gray", frame_gray);
if(waitKey(1000) >= 0)
break;
}
return 0;
}
images变量为空,因此访问0将具有未定义的行为。这应该得到纠正,首先你需要将数据添加到你的图像变量中,然后你就可以访问
vector<Mat> images;
int im_width = images[0].cols;
int im_height = images[0].rows;
这可能会导致某些实现出现超出范围的错误消息。然而,如果我们从std::vector访问无效索引,方法中的标准保证会出现此类异常
编辑赫伯·舒特在他的Gotw系列中写了一篇关于这方面的好文章。
http://www.gotw.ca/gotw/074.htm
矢量为空,因为您将它们定义为
vector<Mat> images;
vector<int> labels;
并且没有向它们添加元素。
但是,您正在尝试访问向量中尚不存在的向量的第一个元素。
// Get the height from the first image. We'll need this
// later in code to reshape the images to their original
// size AND we need to reshape incoming faces to this size:
int im_width = images[0].cols;
int im_height = images[0].rows;