2014-12-14 7 views
-1

다음 코드를 실행하면 VECTOR SUBSCRIPT OUT OF RANGE로 오류가 발생합니다. boundingRect를 호출 할 때 오류가 발생합니다. 디버거는 벡터 윤곽선을 인쇄 할 때 "벡터 첨자가 범위를 벗어났습니다."라고 말하면 크기가 0입니다. 나는 그것을 이해할 수 없다.OpenCV/Visual C++ : 벡터 첨자가 범위를 벗어났습니다.

저는 Visual C++와 OpenCV를 처음 사용합니다. 누구든지 나에게이 오류에 대한 이유와 해결책을 줄 수 있다면 도움이 될 것입니다.

#include "opencv2/opencv.hpp" 
#include "opencv2\imgproc\imgproc.hpp" 
#include "opencv2\core\core.hpp" 
#include <iostream> 
#include <Windows.h> 

using namespace cv; 
using namespace std; 

/// Global variables 
Mat src, src_hsv, src_bin, src_edge, src_drawing; 
Mat srcclone, srcclone1; 
Mat cropped, cropped_hsv, cropped_bin, cropped_edge, cropped_drawing; 


///-HSV parameters -// 
Scalar min_src_hsv = Scalar(49, 14, 154); 
Scalar max_src_hsv = Scalar(107, 85, 255); 

Scalar min_cropped_hsv = Scalar(61, 0, 235); 
Scalar max_cropped_hsv = Scalar(255, 255, 255); 

///Canny parameters 
int lowThreshold = 45; 
int ratio = 3; 
int kernel_size = 3; 

int lowThresholdCropped = 120; 
int ratioCropped = 3; 
int kernel_sizeCropped = 3; 

///Contour detection 
cv::vector<vector<Point> > contours; 
cv::vector<Vec4i> hierarchy; 
double largest_Perimeter = 0; 
int largest_Perimeter_contIndex; 

Rect boundingrect; 

RNG rng(12345); 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
void ProcessingFunction(int, void*) 
{ 
    srcclone = src.clone(); 
    /// Convert the image to HSV 
    cvtColor(src, src_hsv, CV_BGR2HSV); 
    inRange(src_hsv, min_src_hsv, max_src_hsv, src_bin); 

    //namedWindow("Binary Image", CV_WINDOW_AUTOSIZE); 
    imshow("Binary Image", src_bin); 

    /// Reduce noise of the input image with a kernel 3x3 many times as possible :D 
    blur(src_bin, src_bin, Size(3, 3)); 

    /// Canny edge detection for the blurred src_bin image 
    Canny(src_bin, src_edge, lowThreshold, lowThreshold*ratio, kernel_size); 

    ///Morphological opening for the edges, apply this accordingly :D 
    dilate(src_edge, src_edge, getStructuringElement(MORPH_ELLIPSE, Size(5, 5))); 
    erode(src_edge, src_edge, getStructuringElement(MORPH_ELLIPSE, Size(5, 5))); 

    /// Find contours of the edge image 
    findContours(src_edge, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); 

    ///Find contour with the max perimeter 
    cout << "Contour Size : " << contours.size() << endl; 
    for (int i = 0; i < contours.size(); i++) 
    { 
     double a = arcLength(contours[i], false); 

     if (a > largest_Perimeter) 
     { 
      largest_Perimeter = a; 
      largest_Perimeter_contIndex = i; 
     } 
    } 
    cout << "Largest Contour Index : " << largest_Perimeter_contIndex << endl; 
    cout << "largest perimeter :" << largest_Perimeter << endl; 

    ///Draw largest perimeter 
    src_drawing = Mat::zeros(src_edge.size(), CV_8UC3); 
    Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); 
    drawContours(src_drawing, contours, largest_Perimeter_contIndex, color, 2, 8, hierarchy, 0, Point()); 
    Mat src_drawingclone = src_drawing.clone(); 

    ///Bounding Rectangle 
    boundingrect = boundingRect(contours[largest_Perimeter_contIndex]); 
    rectangle(src_drawing, boundingrect, Scalar(255, 255, 0), 2); 
    cout << "x:" << boundingrect.x << " y:" << boundingrect.y << endl; 
    cout << "h:" << boundingrect.height << " w:" << boundingrect.width; 

    Sleep(3000); 
} 


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 


int main() 
{ 
    VideoCapture cap(0); // open the default camera 
    if (!cap.isOpened()) // check if we succeeded 
    { 
     cout << "Cap not opened" << endl; 
    } 

    bool readSuccess = true; 

    while (readSuccess) 
    { 
     readSuccess = false; 

     while (!readSuccess) 
     { 
      readSuccess = cap.read(src); // get a new frame from camera 
     } 


     if (!src.data) 
     { 
      return -1; 
     } 

     if (readSuccess) 
     { 
      cout << "Image read" << endl; 
      ProcessingFunction(0, 0); 
     } 

     if (waitKey(30) == 27) break; 
    } 
    // the camera will be deinitialized automatically in VideoCapture destructor 
    return 0; 
} 
+0

경우 해당 에러가 발생합니까? 1 번 라인? 디버거에서 무엇을 말하고 있습니까? – rene

+1

boundingRect를 호출 할 때 오류가 발생합니다. 디버거가 벡터 범위를 벗어난 벡터 아래 첨자를 말합니다. 벡터 윤곽선을 인쇄 할 때 크기가 0입니다. 나는 그것을 이해할 수 없다. – DDD3

+1

질문에 대한 정보 수정은 어떻게하나요? – rene

답변

1

당신은 어떤 등고선도 찾지 못했습니다.

너무 :

// 
// please, get rid of the global variables !! 
// this has to go *inside* void ProcessingFunction(int, void*) 
// you need to re-initialize it for every call ! 
// 

int largest_Perimeter_contIndex = -1; // something invalid 

... 

if (largest_Perimeter_contIndex > -1) 
{ 
    ///Draw largest perimeter 
    src_drawing = Mat::zeros(src_edge.size(), CV_8UC3); 
    Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); 
    drawContours(src_drawing, contours, largest_Perimeter_contIndex, color, 2, 8, hierarchy, 0, Point()); 
    Mat src_drawingclone = src_drawing.clone(); 

    ///Bounding Rectangle 
    boundingrect = boundingRect(contours[largest_Perimeter_contIndex]); 
    rectangle(src_drawing, boundingrect, Scalar(255, 255, 0), 2); 
    cout << "x:" << boundingrect.x << " y:" << boundingrect.y << endl; 
    cout << "h:" << boundingrect.height << " w:" << boundingrect.width; 
} 
+0

일하고있는 것 같습니다! 고마워 ... !!! – DDD3

관련 문제