Java OpenCV-4.0.0 圖像處理23 圖像輪廓

Java OpenCV-4.0.0 圖像處理23 圖像輪廓

Java OpenCV-4.0.0 圖像輪廓

輸入圖像轉爲灰度圖像cvtColor
使用Canny進行邊緣提取,得到二值圖像
使用findContours尋找輪廓
使用drawContours繪製輪廓

/**
 * OpenCV-4.0.0  輪廓發現
 *
 * @return: void
 * @date: 2019年1月23日 下午10:24:56
 */
public static void contour() {
    //1 獲取原圖
    Mat src = Imgcodecs.imread("C:\\Users\\xuhya\\Pictures\\qq.jpg");
    //2 圖片灰度化
    Mat gary = new Mat();
    Imgproc.cvtColor(src, gary, Imgproc.COLOR_RGB2GRAY);
    //3 圖像邊緣處理
    Mat edges = new Mat();
    Imgproc.Canny(gary, edges, 200, 500, 3, false);
    //4 發現輪廓
    List<MatOfPoint> list = new ArrayList<MatOfPoint>();
    Mat hierarchy = new Mat();
    Imgproc.findContours(edges, list, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
    //5 繪製輪廓
    for (int i = 0, len = list.size(); i < len; i++) {
        Imgproc.drawContours(src, list, i, new Scalar(0, 255, 0), 1, Imgproc.LINE_AA);
    }
    HighGui.imshow("111", src);
    HighGui.waitKey(0);
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章