邊緣檢測的原理:
檢測出圖像中所有灰度值變化較大的點,而且這些點連起來構成若干線條,這些線條就稱之為圖像的邊緣。
1986年,由John F. Canny 提出!
// Canny(Mat image, Mat edges, double threshold1, double threshold2, int
// apertureSize, boolean L2gradient)
// 第一個參數,InputArray類型的image,輸入圖像,即源圖像,填Mat類的對象即可,且需為單通道8位圖像。
// 第二個參數,OutputArray類型的edges,輸出的邊緣圖,需要和源圖片有一樣的尺寸和類型。
// 第三個參數,double類型的threshold1,第一個滯后性閾值。
// 第四個參數,double類型的threshold2,第二個滯后性閾值。
// 第五個參數,int類型的apertureSize,表示應用Sobel算子的孔徑大小,其有默認值3。
// 第六個參數,bool類型的L2gradient,一個計算圖像梯度幅值的標識,有默認值false。
public static void canny(String oriImg, String dstImg, int threshold) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat img = Imgcodecs.imread(oriImg); Imgproc.cvtColor(img, img, Imgproc.COLOR_BGR2GRAY); // Imgproc.Canny(img, img, threshold, threshold * 3, 3, true); // Imgcodecs.imwrite(dstImg, img); }
實例:

canny:

threshold越大,輪廓的要求越高(灰度值變化越明顯才能構成輪廓)
上圖,thrdshold=20,當設置成50,如下圖:

