有人可以帮我解决吗?我有一个使用分水岭分割处理的二进制图像,我的问题是,如何在图像中找到轮廓和计数对象?
@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;
}
}