2016-02-28 104 views
0

我已經在我的源圖像上應用Canny邊緣檢測,然後使用findContours函數來查找最長的輪廓。但在此之後,我想使用OpenCV中的線擬合算法(fitline函數)在源圖像中沿着天空 - 海岸線繪製一條線。線擬合算法OpenCV

源圖像:

Source image

Canny邊緣檢測之後,找到最長的輪廓:

After Canny edge detection and finding the longest contour

結果預期線擬合後:

Result after line fitting

任何幫助將深表謝意。

回答

0

您不只是使用坎尼邊緣檢測器的地平線。

(1)應用足夠大的中值濾波器來隱藏小尺度特徵。 (2)計算梯度幅度。(3)計算梯度幅度。例如。使用醜陋的向前差異如下:

cv::Mat m; 
blurred.convertTo(m,CV_32F); 
cv::Mat crop = m(cv::Rect(0,0,m.cols-1,m.rows-1)); 
const cv::Mat shiftx = m(cv::Rect(1,0,m.cols-1,m.rows-1)); 
const cv::Mat shifty = m(cv::Rect(0,1,m.cols-1,m.rows-1)); 
cv::Mat dx,dy; 
cv::subtract(shiftx, crop, dx); 
cv::subtract(shifty, crop, dy); 
cv::pow(dx, 2.0, dx); 
cv::pow(dy, 2.0, dy); 
cv::add(dx, dy, dx); 
m.setTo(0); 
cv::sqrt(dx, crop); 

(3)收集點的梯度幅度高於---說---最大的一半。

cv::minMaxLoc(m, &mi, &ma); 
const float thr = ma/2.0; 
std::vector<cv::Point2f> pts; 
for (int y=0; y<m.rows;++y) 
// ... 

(4)並適合他們的路線。

你應該很適合。