矢量下标超出范围错误消息 3

Posted

技术标签:

【中文标题】矢量下标超出范围错误消息 3【英文标题】:vector subscript out of range error message3 【发布时间】:2014-04-12 14:16:35 【问题描述】:

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

【问题讨论】:

【参考方案1】:

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

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

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

访问无效索引,标准 at 方法会保证此类异常

编辑 Herb Shutter 在他的 Gotw 系列中有一篇很好的文章。

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

【讨论】:

那么,我怎样才能得到正确的答案呢?我不知道该怎么办 @user3455031:首先你需要在images和labels变量中添加成员,然后访问他们的信息。 @user3455031 "Get the height from the first image" 把你从来没有放入过的图像中,高度应该从哪里来? @user3455031:我认为通过查看您的代码,您需要先调用模型->训练(图像,标签)。这可能会在您的矢量对象中添加数据,然后您应该输入访问它的代码。【参考方案2】:

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

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;

【讨论】:

以上是关于矢量下标超出范围错误消息 3的主要内容,如果未能解决你的问题,请参考以下文章

矢量调整大小后下标超出范围

重命名两个目录中的文件名如果它们之间的某些字符匹配 - 矢量下标超出范围

调试断言失败,向量下标超出范围

调试错误 - 向量下标超出范围 - PCL

c++和opencv中的向量下标超出范围错误

二维向量下标超出范围