簡體   English   中英

錯誤:調試斷言失敗。 C++ 向量下標超出范圍

[英]error : Debug assertion failed. C++ vector subscript out of range

我收到錯誤:Opencv.exe 中 0x00007FF89F84AFEC (ucrtbased.dll) 處出現未處理的異常:無效參數已傳遞給認為無效參數致命的 function。

#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

Mat imgOriginal, imgDialate, imgCanny, imgGray, imgBlur;
vector<Point> initialPoints, docPoints;

Mat preProcessing(Mat img)
{
    cvtColor(img, imgGray, COLOR_BGR2GRAY);
    GaussianBlur(imgGray, imgBlur, Size(3, 3), 3, 0);
    Canny(imgBlur, imgCanny, 25, 75);
    Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
    dilate(imgCanny, imgDialate, kernel);
    return imgDialate;
}

向量代碼在這里顯示錯誤

vector<Point> getContours(Mat image)
{
    vector< vector<Point >> contours;
    vector<Vec4i> hierarchy;

    findContours(image, contours, hierarchy ,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);

    vector<vector<Point >> conPoly(contours.size());
    vector<Rect> boundRect(contours.size());

    vector<Point> biggest;
    int maxArea = 0;

    for (int i = 0; i < contours.size(); i++)
    {
        int area = contourArea(contours[i]);
        //cout << area << endl;

        string objectType;
    
        if (area > 1000)
        {
            float peri = arcLength(contours[i], true);
            approxPolyDP(contours[i], conPoly[i], 0.02 * peri, true);

            if (area > maxArea && conPoly[i].size() == 4)
            {
                drawContours(imgOriginal, conPoly, i, Scalar(255, 0, 255, 5));
                biggest = { conPoly[i][0],conPoly[i][1] , conPoly[i][2] , conPoly[i][3] };
                maxArea = area;
            }           
        }
    }
    return biggest;
}
void drawPoints(vector<Point> points, Scalar color)
{
    for (int i = 0; i < points.size(); i++)
    {
        circle(imgOriginal, points[i], 10, color, FILLED);
        putText(imgOriginal, to_string(i), points[i], FONT_HERSHEY_PLAIN, 4, color, 4);
    }
}

在這里,我將向量起始索引設置為 0,但它在 Opencv.exe 中的 0x00007FF89EA7AFEC (ucrtbased.dll) 處顯示錯誤未處理的異常:無效參數已傳遞給認為無效參數致命的 function。

Opencv.exe 中 0x00007FF89EA7AFEC (ucrtbased.dll) 處的未處理異常:無效參數已傳遞給認為無效參數致命的 function。

vector<Point> reorder(vector<Point> points)
{
    vector<Point> newPoints;
    vector<int> sumPoints, subPoints;  //**vector subscript out of range**

    for (int i = 0; i < 4; i++)
    {
        sumPoints.push_back(points[i].x + points[i].y);
        subPoints.push_back(points[i].x - points[i].y);
    }
    newPoints.push_back(points[min_element(sumPoints.begin(), sumPoints.end()) - sumPoints.begin()]); 
    newPoints.push_back(points[max_element(subPoints.begin(), subPoints.end()) - subPoints.begin()]); 
    newPoints.push_back(points[min_element(subPoints.begin(), subPoints.end()) - subPoints.begin()]);
    newPoints.push_back(points[max_element(sumPoints.begin(), sumPoints.end()) - sumPoints.begin()]);

    return newPoints;
    
}
int main()
{
    string path = "resource/document2.jpg";
    imgOriginal = imread(path);
    resize(imgOriginal, imgOriginal,Size(360,360));

    //image processing 
    imgDialate = preProcessing(imgOriginal);

    //Get contours
    initialPoints = getContours(imgDialate);

    //draw points
    docPoints = reorder(initialPoints);
    drawPoints(docPoints, Scalar(0, 0, 255));

    imshow("imgOrignal", imgOriginal);
    imshow("imgDialate", imgDialate);
    waitKey(0);
    return 0;
}

您必須更改重新訂購代碼進行更改,例如 (0,0,255),5)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM