I try use that code to segment an image, and it work in visual studio, but I need to translate to Java in Android Studio.
When I translate, I have that program:
public Mat watershedVisual(Bitmap bmp){
Mat img = new Mat();
Mat rgba = new Mat();
Utils.bitmapToMat(bmp, img);
img.convertTo(img, CvType.CV_8UC3);
Imgproc.cvtColor(img, rgba, Imgproc.COLOR_RGBA2RGB);
Mat dest = new Mat();
Mat markers = new Mat(rgba.size(),CvType.CV_8UC3, new Scalar(-1));
markers.submat(new Rect(0,0,rgba.cols(), 5)).setTo(new Scalar(1,1,1));
markers.submat(new Rect(0,rgba.rows()-5,rgba.cols(), 5)).setTo(new Scalar(1,1,1));
markers.submat(new Rect(0,0,5, rgba.rows())).setTo(new Scalar(1,1,1));
markers.submat(new Rect(rgba.cols()-5,0,5, rgba.rows())).setTo(new Scalar(1,1,1));
int centreW = rgba.cols()/4;
int centreH = rgba.rows()/4;
markers.submat(new Rect((rgba.cols()/2)-(centreW/2), (rgba.rows()/2)-(centreH/2), centreW, centreH)).setTo(new Scalar(2,2,2));
markers.convertTo(markers, Imgproc.COLOR_BGR2GRAY);
WatershedSegmenter segmenter = new WatershedSegmenter();
Mat wshedMask = new Mat();
segmenter.setMarkers(markers);
wshedMask = segmenter.process(rgba);
Mat mask = new Mat();
Core.convertScaleAbs(wshedMask, mask, 1, 0);
double thresh = Imgproc.threshold(mask, mask, 1, 255, Imgproc.THRESH_BINARY);
Core.bitwise_and(rgba, rgba, dest, mask);
dest.convertTo(dest, CvType.CV_8UC3);
return dest;
}
The problem is when I execute it, I have that problem:
E/cv::error(): OpenCV(3.4.11) Error: Assertion failed (src.type() == CV_8UC3 && dst.type() == CV_32SC1) in void cv::watershed(cv::InputArray, cv::InputOutputArray), file /build/3_4_pack-android/opencv/modules/imgproc/src/segmentation.cpp, line 161
I convert the image like stackoverflow, but in android there are a problem with that and I don’t know how fix it.
class WatershedSegmenter {
public Mat markers=new Mat();
public void setMarkers(Mat markerImage)
{
markerImage.convertTo(markers, CvType.CV_32SC1);
}
public Mat process(Mat image)
{
image.convertTo(image, CvType.CV_8UC3);
markers.convertTo(markers,CvType.CV_32SC1);
Imgproc.watershed(image,markers);
//Log.i(TAG, "LLEGA AQUI");
markers.convertTo(markers,CvType.CV_8UC3);
return markers;
}
};
Any help please?