看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: Win10, Linux, ...) Linux Ubumtu 16.04 QT5.5.1 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) QT GCC 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) OpenCV 4.0.0 問題(Question): 在編譯過程中,若是要修改影像內的pixel值就會出現如下錯誤: The program has unexpectedly finished. 可是將 /*pchRowData[j] = 0;*/ 註解掉,就可以解決問題。 我查詢此錯誤代碼可能會發生的情況,大多都解釋為資料沒有new的情況。 可是我這也不是要new的資料(是cv::Mat cvEdgeImg) 餵入的資料(Input): uchar* pchRowData = NULL; pchRowData = cvEdgeImg.ptr<uchar>(cvEdgeImg.cols); for(int j=cvEdgeImg.rows-1 ; j>=0 ; j--) { if(!bEverPaint) { if(pchRowData[j]==255) { bEverPaint = true; } } else { pchRowData[j] = 0; /*此行*/ } } 預期的正確結果(Expected Output): 編譯可以通過 錯誤結果(Wrong Output): The program has unexpectedly finished. 程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔) using namespace cv; Mat cvEdgeImg; . . . Canny( cvDilaImg, cvEdgeImg, 50, 150, 3); for(int i=0 ; i<cvEdgeImg.cols ; i++) { uchar* pchRowData = NULL; pchRowData = cvEdgeImg.ptr<uchar>(cvEdgeImg.cols); bool bEverPaint = false; for(int j=cvEdgeImg.rows-1 ; j>=0 ; j--) { if(!bEverPaint) { if(pchRowData[j]==255) { bEverPaint = true; } } else { // pchRowData[j] = 0; //上行註解掉編譯就會產生The program has unexpectedly finished. } } } imwrite("OutputEdgeImg.bmp",cvEdgeImg); 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.117.38.5 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1553752636.A.9F6.html ※ 編輯: chuinzong (122.117.38.5), 03/28/2019 14:00:08
nh60211as: 都用opencv4了就別用那麼舊的語法,改用.at<uchar> 03/28 14:14
nh60211as: 或是抓submatrix來存取吧 03/28 14:14
chuinzong: 由於我用cvEdgeImg.at<char>(i,j) = 255; 還是會有狀況 03/28 14:27
chuinzong: 那我用看submatrix試試看 03/28 14:27
chuinzong: The program has unexpectedly finished. 03/28 14:32
chuinzong: .../ProjectIamge crashed 03/28 14:33
nh60211as: pchRowData = cvEdgeImg.ptr<uchar>(cvEdgeImg.cols); 03/28 14:49
nh60211as: 這一行是代表你每一個迴圈都固定抓影像的同一個row 03/28 14:50
nh60211as: 這是你要的寫法嗎?另外用.ptr應該還要先檢查這個matri 03/28 14:51
nh60211as: 是不是連續的 03/28 14:52
nh60211as: 更正,是每一個迴圈抓固定的起始點 03/28 14:57
chuinzong: pchRowData = cvEdgeImg.ptr<uchar>(i);已經改成i還是 03/28 15:18
chuinzong: 會有一樣狀況 03/28 15:18
nh60211as: 正確的輸入方式是.at<char>(row,col),我覺得你應該先 03/28 15:21
nh60211as: 學一下opencv的語法或去看documentation,這個程式碼 03/28 15:22
nh60211as: 太難了 03/28 15:22
chuinzong: Ok,我知道問題在哪了!那我在看下文件,謝謝~ 03/28 15:32
flyfoxy: j>=0;j— 表示j有可能是-1 03/28 22:10