Java – the watershed in opencv Android
•
Java
I try to implement the watershed function of OpenCV on Android But my program always crashes where the watershed function is called I can output the marked results perfectly But the watershed function always crashes This is my code:
Mat threeChannel = new Mat(); Imgproc.cvtColor(mRgba,threeChannel,Imgproc.COLOR_BGR2GRAY); Imgproc.threshold(threeChannel,100,255,Imgproc.THRESH_BINARY); Mat fg = new Mat(mRgba.size(),CvType.CV_8U); Imgproc.erode(threeChannel,fg,new Mat(),new Point(-1,-1),2); Mat bg = new Mat(mRgba.size(),CvType.CV_8U); Imgproc.dilate(threeChannel,bg,3); Imgproc.threshold(bg,1,128,Imgproc.THRESH_BINARY_INV); Mat markers = new Mat(mRgba.size(),CvType.CV_8U,new Scalar(0)); Core.add(fg,markers); WatershedSegmenter segmenter = new WatershedSegmenter(); segmenter.setMarkers(markers); Mat result = segmenter.process(mRgba); return result;
The watershedsegment calss is as follows:
public class WatershedSegmenter{ public Mat markers; public void setMarkers(Mat markerImage) { markerImage.convertTo(markers,CvType.CV_32S); } public Mat process(Mat image) { Imgproc.watershed(image,markers); markers.convertTo(markers,CvType.CV_8U); return markers; } }
Has anyone managed to make it run on Android? Before using this tutorial, I try to make it use QT: link. In Q But I have no luck on Android at present
Solution
I have now discovered the cause of the crash The watershed adopts 8-bit 3-channel data format, and RGBA adopts 4-channel data format I just convert it from RGBA to RGB. It solves all the problems
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码