3

Somebody can help me to solve it ? I have a binary image which i procced with Watershed Segmentation, My Problem is, how to findContours and CountObject in image ?

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        Bitmap bmp=BitmapFactory.decodeFile(picturePath);
          Log.i(TAG, picturePath);
         Mat img=Highgui.imread(picturePath);
        //Mat img=Imgcodecs.imread(picturePath);
        Mat result=new Mat();
        //Utils.bitmapToMat(bmp, img);
        //Imgproc.cvtColor(img,result,Imgproc.COLOR_BGRA2BGR);
         result=steptowatershed(img);
        //Imgproc.cvtColor(result, img,Imgproc.COLOR_BGR2BGRA,4);
        Utils.matToBitmap(result, bmp, true);
          Log.i(TAG, "all okay");
        imageView.setImageBitmap(bmp);

    }        
}
//in here i must place that code for findContours and Count object ? or where ?

public Mat steptowatershed(Mat img)
{
    Mat threeChannel = new Mat();

    Imgproc.cvtColor(img, threeChannel, Imgproc.COLOR_BGR2GRAY);
    Imgproc.threshold(threeChannel, threeChannel, 100, 255, Imgproc.THRESH_BINARY);

    Mat fg = new Mat(img.size(),CvType.CV_8U);
    Imgproc.erode(threeChannel,fg,new Mat());

    Mat bg = new Mat(img.size(),CvType.CV_8U);
    Imgproc.dilate(threeChannel,bg,new Mat());
    Imgproc.threshold(bg,bg,1, 128,Imgproc.THRESH_BINARY_INV);

    Mat markers = new Mat(img.size(),CvType.CV_8U, new Scalar(0));
    Core.add(fg, bg, markers);
    Mat result1=new Mat();
    WatershedSegmenter segmenter = new WatershedSegmenter();

    segmenter.setMarkers(markers);
    result1 = segmenter.process(img);
    return result1;
    //in here i must place that code for findContours and Count object ? or where ?
}

public class WatershedSegmenter
{
    public Mat markers=new Mat();
    public void setMarkers(Mat markerImage)
    {

        markerImage.convertTo(markers, CvType.CV_32SC1);
    }

    //in here i must place that code for findContours and Count object ? or where ?
    public Mat process(Mat image)
    {
        Imgproc.watershed(image,markers);
        markers.convertTo(markers,CvType.CV_8U);

        return markers;
    }
}
slavoo
  • 5,798
  • 64
  • 37
  • 39
MTStuart
  • 63
  • 9
  • i'm really in need of help with this question, can you help me, please? https://stackoverflow.com/questions/61216402/how-to-improve-image-segmentation-using-the-watershed – Carlos Diego Apr 28 '20 at 17:17

1 Answers1

0

Did you even read the reference documentation of the watershed function?

Before passing the image to the function, you have to roughly outline the desired regions in the image markers with positive (>0) indices. So, every region is represented as one or more connected components with the pixel values 1, 2, 3, and so on. Such markers can be retrieved from a binary mask using findContours

So you obviously have to use findContours befor you do the watershed transform. To count your objects it would obviously make sense to have segmented objects in the first place. So you should do that most likely after the watershed transform...

Just use google. There are countless tutorials and examples on how to achieve what you want.

Piglet
  • 27,501
  • 3
  • 20
  • 43
  • what shold i do @Piglet ? Where i must place that code whic can count object like that ? its right my source code above ? – MTStuart May 10 '16 at 11:40
  • can we chat @Piglet ? – MTStuart May 10 '16 at 11:46
  • 1
    sorry mate if you don't understand your source code well enough to know where to add those function calls you better start off with understanding what you already have, befor you add more... Read the documentation on all 3 functions. That's what every decent programmer would do first. I assume you don't even understand what you are doing here otherwise the order of calls would be clear. Start with commenting your entire code like you would explain it to your mother. what is going on? what data do you have?... – Piglet May 10 '16 at 12:10
  • so, what should i do now ? @Piglet – MTStuart May 10 '16 at 12:25
  • really sor, im newbie on Android OpenCV @Piglet – MTStuart May 10 '16 at 12:28
  • Then maybe you should start with some basics? Just a suggestion... As I told you the reference tells you that you should do findcontours befor watershed and counting objects makes no sense if you don't have objects yet. so the order is rather obvious if you are not braindead. So go and RTFM please. Its all there. You just have to read and apply what is written. If this is too much to understand, start with something simpler. Read a book on image processing. Do low-level OpenCV tutorials... – Piglet May 10 '16 at 12:36