源代碼如下:
#include<iostream>
#include<fstream>
#include<opencv2\core\core.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
using namespace cv;
using namespace std;
const unsigned char FORE_GROUD = 255;
int thresh = 7;
Mat src,dst,dst1;
//ofstream fout;
int main()
{
string outfilename("test.txt");
ofstream fout(outfilename.c_str());
namedWindow("result", 1);
Mat src = imread("1.jpg");
Mat dst,src1;
cvtColor(src,src1,COLOR_BGR2GRAY);
Point root_points[1][8];
root_points[0][0] = Point(0,0);
root_points[0][1] = Point(src.cols,0);
root_points[0][2] = Point(src.cols,100);
root_points[0][3] = Point(140,100);
root_points[0][4] = Point(0,205);
root_points[0][5] = Point(src.cols,205);
root_points[0][6] = Point(src.cols,src.rows);
root_points[0][7] = Point(0,src.rows);
const Point* ppt[1] = {root_points[0]};
int npt[] = {8};
polylines(src1, ppt, npt, 1, 1, Scalar(0),1,8,0);
fillPoly(src1, ppt, npt, 1, Scalar(255));
medianBlur( src1, src1, 3);
equalizeHist(src1 ,src1);
threshold( src1,dst,double(thresh),double(FORE_GROUD),THRESH_BINARY);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
vector<vector<Point>> contours_poly( contours.size() );
vector<Rect> boundRect( contours.size() );
findContours ( dst,contours,hierarchy,CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE) ; // 獲取每個輪廓的每個像素
for(int i = 0;i < contours.size() ;i++)
{
approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
boundRect[i] = boundingRect( Mat(contours_poly[i]) );
//Rect rect = boundingRect( Mat(contours_poly[index]) );
drawContours( src, contours_poly, index, Scalar(255,255,255), 1, 8, hierarchy, 0, Point() );
rectangle(src,boundRect[i].tl(),boundRect[i].br(),Scalar(255,255,255),2);
}
//fout.close();
imshow("result",dst);
imshow("show",src);
waitKey(0);
return(0);
}
斷點運行到approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true ); 時出現報錯

求大神指點
uj5u.com熱心網友回復:
下標溢位?Debug下打開call stack看看函式呼叫堆疊,找到出錯的函式先。
uj5u.com熱心網友回復:
崩潰的時候在彈出的對話框按相應按鈕進入除錯,按Alt+7鍵查看Call Stack即“呼叫堆疊”里面從上到下列出的對應從里層到外層的函式呼叫歷史。雙擊某一行可將游標定位到此次呼叫的源代碼或匯編指令處,看不懂時雙擊下一行,直到能看懂為止。uj5u.com熱心網友回復:
你訪問vector的時候,下標有越界操作了,注意你小標的范圍轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/102567.html
標籤:圖形處理/算法
