3

有人可以帮我解决吗?我有一个使用分水岭分割处理的二进制图像,我的问题是,如何在图像中找到轮廓和计数对象?

@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;
    }
}
4

1 回答 1

0

您是否阅读过分水岭函数的参考文档?

在将图像传递给函数之前,您必须使用正 (>0) 索引粗略地勾勒出图像标记中所需的区域。因此,每个区域都表示为一个或多个具有像素值 1、2、3 等的连通分量。可以使用findContours从二进制掩码中检索此类标记

因此,在进行分水岭变换之前,您显然必须使用 findContours。要计算您的对象,显然首先将对象分段是有意义的。所以你最有可能在分水岭变换之后这样做......

只需使用谷歌。关于如何实现你想要的东西有无数的教程和例子。

于 2016-05-10T10:00:07.343 回答