下のようなエラーが出ました。
opencv/modules/imgproc/src/contours.cpp:199: error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function 'cvStartFindContours_Impl'
ここで改めてログを見て見ると
[Start]FindContours supports only CV_8UC1 images
TRY1
とあるので、ソースを下のようなものから(テスト中で。。。)
Mat charactor = Imgcodecs.imread(url.getPath(), Imgcodecs.IMREAD_UNCHANGED); // 背景除去 Mat hierarchy = Mat.zeros(new Size(200, 200), CvType.CV_8UC1); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Imgproc.findContours(charactor, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_TC89_L1); Mat dstContuor = Mat.zeros(new Size(charactor.width(),charactor.height()),CvType.CV_8UC3); Scalar color=new Scalar(255,255,255); Imgproc.drawContours(dstContuor, contours, -1, color,1);
描画モード(引数に渡している定数を「CvType.CV_8UC1」統一しました。
しかし、また。。。
opencv/modules/imgcodecs/src/loadsave.cpp:925: error: (-215:Assertion failed) code in function 'imencode'
Try2
調べて見ると、画像はグレースケースでないとけ内容だ。。。
読み取りを下のように修正
Mat charactor = Imgcodecs.imread(url.getPath(), Imgcodecs.IMREAD_GRAYSCALE); Mat gray = new Mat(); Imgproc.cvtColor(charactor, gray, Imgproc.COLOR_BGR2GRAY);
駄菓子菓子またもエラー
Invalid number of channels in input image:
'VScn::contains(scn)'
where
'scn' is 1
]
こんな感じのエラー。。。
TRY3
色々と試して見るが、どれもエラーになる。。。
ソースを少し見直して見た!
// 表示イメージを読み取る Mat charactor = Imgcodecs.imread(url.getPath(), Imgcodecs.IMREAD_COLOR); Mat gray = new Mat(); Imgproc.cvtColor(charactor, gray, Imgproc.COLOR_BGR2GRAY); // 背景除去 Mat hierarchy = Mat.zeros(new Size(200, 200), CvType.CV_8UC1); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Imgproc.findContours(gray, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); Mat dstContuor = Mat.zeros(new Size(charactor.width(),charactor.height()),CvType.CV_8UC1); Scalar color=new Scalar(255,255,255); Imgproc.drawContours(dstContuor, contours, -1, color,1); // Size resize = new Size(before.getWidth() / 3, before.getHeight() / 3); // Mat reSizeChar = new Mat(); // Imgproc.resize(charactor, reSizeChar, resize); MatOfByte charaByteBefore = new MatOfByte(); // 表示イメージをcharaByteに書き込む Imgcodecs.imencode(".png", dstContuor, charaByteBefore); Mat dst = new Mat(); // Imgproc.threshold(reSizeChar, dst, 100, 255, Imgproc.THRESH_BINARY); MatOfByte charaByte = new MatOfByte(); // この行で落ちていた。。。。 ☆Imgcodecs.imencode(".png", dst, charaByte);
上の☆マーク部分で落ちていたのですが、これは不要な処理でした。。。(色々と試した時の残骸です(笑))
そんなわけで実行することができました。
変更前は下のような感じです。ハナっからグレースケール(笑)
でわでわ。。。