1

I am doing a tutorial on Image Segmentation using Watershed Algorithm and I want to know if I am getting the right result after segmenting the image into foreground and background.

The requirement is to view my finalMat object's foreground back to color mode using an imageView. I am using 'a leaf' photo as an example and I want to see the green back to please myself that I am getting the object out of its background.

Here is my code (Please feel free to comment on what am I doing wrong, or a better solution that I came up with. I am new to android development and OpenCV):

    Mat finalMat = markersMat;
    finalMat.convertTo(markersMat, CvType.CV_32S);
    Imgproc.watershed(originalPicMat, finalMat);

    //then return as CV_8U:
    finalMat.convertTo(finalMat, CvType.CV_8U);
    //test at final output:
    Bitmap testBmp = Bitmap.createBitmap(grayscaleMat.cols(), grayscaleMat.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(finalMat, testBmp);
    imgView_finalOutput.setImageBitmap(testBmp);

Here is my code for the marker:

    //Markers image:
    Mat markersMat = new Mat(grayscaleMat.size(), CvType.CV_8U, new Scalar(0));
    Core.add(foregroundMat, backgroundMat, markersMat);
    //show markers:
    Bitmap markersBmp = Bitmap.createBitmap(grayscaleMat.cols(), grayscaleMat.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(markersMat, markersBmp);
    imgView_markers.setImageBitmap(markersBmp);

Again, my question is how can I set the finalMat in color mode(return the color of the leaf)?

Here is my full code for the activity:

        //Set Original pic:
    String originalFilePathStr= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) +"/healthy1.jpg";
    Mat originalPicMat = Imgcodecs.imread(originalFilePathStr);
    //set imageview:
    //File originalPicFilePath = new File(originalFilePathStr+"/healthy.jpg");
    Uri originalPicUriPath = Uri.parse(originalFilePathStr);
    imgView_original.setImageURI(originalPicUriPath);

    /* IMAGE SEGMENTATION USING WATERSHED ALGORITHM */
    //Create a Mat Object using originalPicture as is:

    /* OTSU'S BINARIZATION */
    Mat grayscaleMat = new Mat();
    //check if i dont need to load_image as grayscale, if cvtColor does does work or vice versa:
    Imgproc.cvtColor(originalPicMat, grayscaleMat, Imgproc.COLOR_BGR2GRAY);
    Imgproc.threshold(grayscaleMat, grayscaleMat, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU);
    //test grayscale: WORKS:
    Bitmap grayscaleBmp = Bitmap.createBitmap(grayscaleMat.cols(), grayscaleMat.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(grayscaleMat, grayscaleBmp);
    imgView_grayscale.setImageBitmap(grayscaleBmp);

    //Part 2: Create marker: foregroun dnd background
    //Sure fg area:
    Mat foregroundMat = new Mat();
    Imgproc.erode(grayscaleMat, foregroundMat, new Mat(),new Point(-1,-1),2);
    //Show fg area:
    Bitmap foregroundBmp = Bitmap.createBitmap(grayscaleMat.cols(), grayscaleMat.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(grayscaleMat, foregroundBmp);
    imgView_foreground.setImageBitmap(foregroundBmp);


    //Mat Bg:
    Mat backgroundMat = new Mat();
    Imgproc.dilate(grayscaleMat, backgroundMat, new Mat(), new Point(-1,-1), 3);
    Imgproc.threshold(backgroundMat, backgroundMat, 1,128, Imgproc.THRESH_BINARY_INV);
    Bitmap backgroundBmp = Bitmap.createBitmap(grayscaleMat.cols(), grayscaleMat.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(backgroundMat, backgroundBmp);
    imgView_background.setImageBitmap(backgroundBmp);

    //Markers image:
    Mat markersMat = new Mat(grayscaleMat.size(), CvType.CV_8U, new Scalar(0));
    Core.add(foregroundMat, backgroundMat, markersMat);
    //show markers:
    Bitmap markersBmp = Bitmap.createBitmap(grayscaleMat.cols(), grayscaleMat.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(markersMat, markersBmp);
    imgView_markers.setImageBitmap(markersBmp);
    /*Watershed: Finale */

    //test watershed:
    Mat finalMat = markersMat;
    finalMat.convertTo(markersMat, CvType.CV_32S);
    Imgproc.watershed(originalPicMat, finalMat);

    //then return as CV_8U:
    finalMat.convertTo(finalMat, CvType.CV_8U);
    //test at final output:
    Bitmap testBmp = Bitmap.createBitmap(grayscaleMat.cols(), grayscaleMat.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(finalMat, testBmp);
    imgView_finalOutput.setImageBitmap(testBmp);

Here is a screenshot of my activity result (Not enough reputation points to post a screenshot).

UPDATE: It's now working, I can mark now the leaf. The only problem that I have now is that it returns the leaf in a different color. Please refer to this photo.

The final photo returns a different shade of green and yellow turns to blue. The Problem is that I don't know where the bug came from. If you guys have an idea on where is the problem please tell me so.

4

0 回答 0