I have a problem when I traslate my code to Android Studio. The code in C++ is:
String folder = "images/categoria4/";
String filename = "ischaemic-leg-ulcer-with-tendon-visible";
String extension = ".jpg";
cv::Mat original = imread(folder + filename + extension);
cv::Mat frame_HSV, frame_threshold, final, res, diferencia, outImage, prueba, prueba2;
while (true) {
cv::Mat dest, hsv;
// Create markers image
cv::Mat markers(original.size(), CV_8U, cv::Scalar(-1));
//top rectangle
markers(Rect(0, 0, original.cols, 5)) = Scalar::all(1);
//bottom rectangle
markers(Rect(0, original.rows - 5, original.cols, 5)) = Scalar::all(1);
//left rectangle
markers(Rect(0, 0, 5, original.rows)) = Scalar::all(1);
//right rectangle
markers(Rect(original.cols - 5, 0, 5, original.rows)) = Scalar::all(1);
//centre rectangle
int centreW = original.cols / 4;
int centreH = original.rows / 4;
markers(Rect((original.cols / 2) - (centreW / 2), (original.rows / 2) - (centreH / 2), centreW, centreH)) = Scalar::all(2);
markers.convertTo(markers, COLOR_BGR2GRAY);
imshow("markers", markers);
//Create watershed segmentation object
WatershedSegmenter segmenter;
segmenter.setMarkers(markers);
cv::Mat wshedMask = segmenter.process(original);
cv::Mat mask;
convertScaleAbs(wshedMask, mask, 1, 0);
double thresh = threshold(mask, mask, 1, 255, THRESH_BINARY);
bitwise_and(original, original, dest, mask);
dest.convertTo(dest, CV_8U);
imshow("Watershed", mask);
imwrite(folder + filename + "_W" + extension, dest);
If I execute that program, the image Mat markers look like a black picture with a square on centre. I use it to localize the section in the original image.
The problem is when I try do it on Android Studio, the image markers don’t look like the previous code. It’s looks like a black image. This is the code in Java:
public Mat watershedGithub2(Bitmap bmp){
Mat img = new Mat();
Mat rgba = new Mat();
Mat dest = new Mat();
Utils.bitmapToMat(bmp, img);
Mat threeChannel = new Mat();
img.convertTo(img, CvType.CV_8UC3);
Imgproc.cvtColor(img, rgba, Imgproc.COLOR_RGBA2RGB);
Imgproc.cvtColor(rgba, threeChannel, Imgproc.COLOR_BGR2GRAY);
Imgproc.threshold(threeChannel, threeChannel, 100, 255, Imgproc.THRESH_BINARY);
Mat fg = new Mat(rgba.size(),CvType.CV_8UC3);
Imgproc.erode(threeChannel,fg,new Mat());
Mat bg = new Mat(rgba.size(),CvType.CV_8UC3);
Imgproc.dilate(threeChannel,bg,new Mat());
Imgproc.threshold(bg,bg,1, 128,Imgproc.THRESH_BINARY_INV);
Mat markers = new Mat(rgba.size(),CvType.CV_8UC3, new Scalar(-1));
Imgproc.rectangle(markers, new Point(0,0), new Point(rgba.cols(),5), new Scalar(255), -1);
Imgproc.rectangle(markers, new Point(0,rgba.rows()-5), new Point(rgba.cols(),5), new Scalar(255), -1);
Imgproc.rectangle(markers, new Point(0,0), new Point(5,rgba.rows()), new Scalar(255), -1);
Imgproc.rectangle(markers, new Point(rgba.cols()-5,0), new Point(5,rgba.rows()), new Scalar(255), -1);
int centreW = rgba.cols()/4;
int centreH = rgba.rows()/4;
Imgproc.rectangle(markers, new Point((rgba.cols()/2)-(centreW/2),(rgba.rows()/2)-(centreH/2)), new Point(centreW,centreH), new Scalar(255), -1);
markers.convertTo(markers, Imgproc.COLOR_BGR2GRAY);
Core.add(fg, bg, markers);
Mat result1 = new Mat();
WatershedSegmenter segmenter = new WatershedSegmenter();
segmenter.setMarkers(markers);
result1 = segmenter.process(rgba);
Mat mask = new Mat();
Core.convertScaleAbs(result1, 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;
}
How can I fix it?