BounceBall#02 OpenCVで四角形を検出する その2

前回、カメラ画像を2値化しました。今回は2値化した画像から四角形の座標を求めます。

輪郭を取得する

2値化した際、白になった領域の輪郭を取得します。

List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat hierarchy = new Mat();
Imgproc.findContours(tempMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_TC89_L1);
Imgproc.drawContours(rgbaMat, contours, -1, new Scalar(0, 255, 0), 1);

findContours()で輪郭を取得します。tempMatは2値化した画像です。contoursに輪郭のデータが格納されるので、contoursをdrawContours()で描画しています。

2値化画像です。

2値化画像から輪郭を取得して描画した画像です。