[問題] connected components 相關錯誤

看板C_and_CPP作者 (哈囉)時間9年前 (2015/06/14 11:38), 9年前編輯推噓1(101)
留言2則, 1人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Visual Studio C++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) OpenCV 問題(Question): 自己寫了一個跑connected area的簡單C++ 程式 沒想到出現了google後仍完全不知道從何解起的錯誤... 不過從錯誤碼看起來感覺是vector部分的問題 有請神人前輩們解答了! 預期的正確結果(Expected Output): 將各connected area重新配色,但區域不變 錯誤結果(Wrong Output): error視窗裡的訊息不適每次都一樣 極為神奇 好像都是存取違規、stack overflow之類的狀況 http://i.imgur.com/WQXxgIU.png
http://i.imgur.com/uHVc4Mi.png
程式碼(Code):(請善用置底文網頁, 記得排版) #include <opencv2/opencv.hpp> #include <iostream> #include <fstream> #include <vector> using namespace cv; using namespace std; #define vec2db vector<vector<bool> > vec2db istaken; int h = 0, w = 0; const int dx4[4] = { -1, 0, 1, 0 }; const int dy4[4] = { 0, -1, 0, 1 }; const int dx8[8] = { -1, -1, 0, 1, 1, 1, 0, -1 }; const int dy8[8] = { 0, -1, -1, -1, 0, 1, 1, 1 }; Mat drawblob(Mat &img); int main(int argc, char **argv) { Mat src = cv::imread("Newcolor2.png", 1); cv::imshow("src", src); h = src.rows, w = src.cols; // 二維vector "istaken" 初始化 printf("starts istaken initializing.\n"); for (int i = 0; i < w; i++) { vector<bool> nb; for (int j = 0; j < h; j++) { nb.push_back(false); } istaken.push_back(nb); } printf("image(w*h)=(%d*%d).\n", w, h); Mat cln = src.clone(); cln = drawblob(cln); cv::imshow("srcclone", cln); printf("Done.\n"); cv::waitKey(0); return 0; } void neighborsearch(Mat &img, int i, int j, int r, int g, int b){ //int h = img.rows, w = img.cols; // 標記該點已走過 istaken[i][j] = true; // 儲存該點的顏色 int r0 = img.at<cv::Vec3b>(j, i)[0]; int g0 = img.at<cv::Vec3b>(j, i)[1]; int b0 = img.at<cv::Vec3b>(j, i)[2]; // 存成與主點相同的顏色 img.at<cv::Vec3b>(j, i)[0] = r; img.at<cv::Vec3b>(j, i)[1] = g; img.at<cv::Vec3b>(j, i)[2] = b; // access該點的相鄰四點 for (int k = 0; k < 4; k++) { int x = i + dx4[k], y = j + dy4[k]; if (x >= 0 && x < w && y >= 0 && y < h && istaken[x][y] == false){ if ((img.at<cv::Vec3b>(y, x)[0] == r0) && (img.at<cv::Vec3b>(y, x)[1] == g0) && (img.at<cv::Vec3b>(y, x)[2] == b0)) { neighborsearch(img, x, y, r, g, b); } } } } Mat drawblob(Mat &img){ for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { if (istaken[i][j] == false){ // 該相鄰區塊顏色 int arear1 = rand() % 256; int areag1 = rand() % 256; int areab1 = rand() % 256; neighborsearch(img, i, j, arear1, areag1, areab1); } } } return img.clone(); } 補充說明(Supplement): 有請各位神人前輩們幫忙指點錯誤之處了!!>< -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.112.25.105 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1434253133.A.3DE.html

06/14 15:55, , 1F
code簡單看來沒有問題,應該是遞迴過多堆疊爆了,減小w及h
06/14 15:55, 1F

06/14 15:57, , 2F
的值試試,若正常則加大堆疊或不要用遞迴
06/14 15:57, 2F
感謝 似乎真的是記憶體爆了 改為BST的方法就行了 ※ 編輯: AceID (140.112.25.105), 07/01/2015 14:42:46
文章代碼(AID): #1LVFTDFU (C_and_CPP)