向量下标超出范围错误message3

时间:2014-04-12 14:16:36

标签: c++ opencv vector

我制作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;

}

2 个答案:

答案 0 :(得分:1)

images 变量为空,因此访问0将具有未定义的行为。这应该被纠正,首先你需要将数据添加到您的图像变量,然后您可以访问 它。

vector<Mat> images;
int im_width = images[0].cols;
int im_height = images[0].rows;

这可能会导致某些实现上出现超出范围的错误消息。但是,如果我们从 at

访问无效索引,标准 std::vector 方法会保证此类例外

修改 Herb Shutter在他的Gotw系列中写了很好的文章。

http://www.gotw.ca/gotw/074.htm

答案 1 :(得分:0)

向量为空,因为您将它们定义为

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;