Re: [問題] OpenCV中尋找指定長度的線段

看板C_and_CPP (C/C++)作者 (のヮの)時間15年前 (2011/01/25 04:20), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《eggheadlai (賴蛋蛋)》之銘言: : 開發平台(Platform): VC++ : 額外使用到的函數庫(Library Used): 無 : 問題(Question): : 小弟想要在image中經過Canny後所得到的edge中刪去太長或太短的曲線 : 翻了書和網站上知道要用cvHoughLines2這個函式 : 但是cvHoughLines2好像只能刪去太短的曲線不能刪去太大的曲線 : 而且裡面的參數書上寫的不是很清楚 : 不知道有沒有板上的大大可是稍微指點一下迷津或簡單的範例 : 或是有其他更好的方法 : 感激不盡 因為你說用 cvHoughLines2,所以應該是針對直線吧?? 你可以試著自己實作那個 accumulator IplImage *canny = 你的 canny image const int diagonal = cvRound(sqrt( (double)(canny->width*canny->width + canny->height*canny->height))); // accumulator[2 * diagonal][180] int *accumulator = (int*)malloc(sizeof(int) * 180 * diagonal * 2); memset(accumulator, 0, sizeof(int) * 180 * diagonal * 2); for (int y = 0; y < canny->height; y++) { for (int x = 0; x < canny->width; x++) { if (CV_IMAGE_ELEM(canny, unsigned char, y, x) > 128) { for (int angle = 0; angle < 180; angle++) { const int dist = cvRound( ((double)x * cos((double)angle * CV_PI / 180.0)) + ((double)y * sin((double)angle * CV_PI / 180.0))); if ((dist > -diagonal) && (dist < diagonal)) { // accumulator[dist + diagonal][angle]++ accumulator[(dist + diagonal) * 180 + angle]++; } } } } } diagonal 是畫面對角線長度,也是畫面上的線離原點的最遠距離 accumulator 的 dimension 是 (2 * diagonal)x180 因為角度是 0~180,所以距離會有正有負,dimension 要用 2 * diagonal 關於 hough transform 網路上有很多資料 CV_IMAGE_ELEM(...) > 128 是判斷這個 pixel 是不是在偵測到的 edge 上 我記得 canny detector 結果是存成 binary image 所以這樣設 然後你要過濾就很容易了 for (int angle = 0; angle < 180; angle++) { for (int dist = -diagonal; dist < diagonal; dist++) { const int n = accumulator[(dist + diagonal) * 180 + angle]; if ((n > n_min) && (n < n_max)) { // (angle, dist) 就是這條 hough line 的 parameter } } } 比較需要小心的是 IplImage 的座標系 y 是往下長 但是 hough transform 是在一般的座標系運作,也就是 y 往上長 所以算的時候 y 可能都要轉成 canny->height - y - 1 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 1.161.198.237
文章代碼(AID): #1DFTy978 (C_and_CPP)
文章代碼(AID): #1DFTy978 (C_and_CPP)