簡體   English   中英

從OpenCV Canny邊緣探測器獲取角度

[英]Get angle from OpenCV Canny edge detector

我想使用OpenCV的Canny邊緣檢測器,如本問題中所述 例如:

cv::Canny(image,contours,10,350); 

但是,我希望不僅能夠獲得最終的閾值圖像,而且還希望在每個像素處獲得檢測到的邊緣角度。 這在OpenCV中是否可行?

canny不直接給你這個。 但是,您可以從Sobel變換計算角度,該變換在canny()內部使用。

偽代碼:

    cv::Canny(image,contours,10,350);
    cv::Sobel(image, dx, CV_64F, 1, 0, 3, 1, 0, cv::BORDER_REPLICATE);
    cv::Sobel(image, dy, CV_64F, 0, 1, 3, 1, 0, cv::BORDER_REPLICATE);

    cv::Mat angle(image.size(), CV_64F)

    foreach (i,j) such that contours[i, j] > 0
    {
        angle[i, j] = atan2(dy[i,j], dx[i , j])
    }

除了使用for循環,您還可以為phase函數提供dxdy漸變,返回角度方向的灰度圖像,然后將其傳遞給applyColorMap函數,然后用邊緣遮蓋它,因此背景為黑色。

這是工作流程:

  1. 獲得角度

     Mat angles; phase(dx, dy, angles, true); 

    true參數表示角度以度為單位返回。

  2. 將角度范圍更改為0-255,這樣您就可以轉換為CV_8U而不會丟失數據

     angles = angles / 360 * 255; 

    請注意, angles仍然是CV_64F類型,因為它來自Sobel功能

  3. 轉換為CV_8U

     angles.convertTo(angles, CV_8U); 
  4. 應用您選擇的顏色圖

     applyColorMap(angles, angles, COLORMAP_HSV); 

    在這種情況下,我選擇HSV色彩映射。 有關詳細信息,請參閱此處: https//www.learnopencv.com/applycolormap-for-pseudocoloring-in-opencv-c-python/

  5. 應用邊緣蒙版,使背景為黑色

     Mat colored; angles.copyTo(colored, contours); 
  6. 最后顯示圖像:D

     imshow("Colored angles", colored); 

如果您的來源是視頻或網絡攝像頭,在應用邊緣遮罩之前,您必須清除colored圖像,以防止聚合:

colored.release();
angles.copyTo(colored, contours);

完整代碼:

Mat angles, colored;

phase(dx, dy, angles, true);
angles = angles / 360 * 255;
angles.convertTo(angles, CV_8U);
applyColorMap(angles, angles, COLORMAP_HSV);
colored.release();
angles.copyTo(colored, contours);
imshow("Colored angles", colored);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM