有人可以帮我解决吗?我有一个二进制图像,我使用Watershed Segmentation,我的问题是,如何在图像中找到Contours和CountObject?
@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;
}
}
答案 0 :(得分:0)
您是否阅读过分水岭功能的参考文档?
在将图像传递给函数之前,您必须大致勾勒出轮廓 具有正(> 0)指数的图像标记中的所需区域。 因此,每个区域都表示为一个或多个连接组件 像素值为1,2,3等。这样的标记可以是 使用 findContours
从二进制掩码中检索
所以你显然必须使用findContours,因为你做了分水岭变换。 为了计算你的对象,显然首先要有分段对象是有意义的。 所以你应该在分水岭改造后最有可能做到这一点......
只需使用谷歌。关于如何实现你想要的东西,有无数的教程和例子。