2016-05-09 13 views
3

誰かが私にそれを解決するのを助けることができますか?私は、Watershed Segmentation、私の問題、Image内のContourとCountObjectの発見方法についてバイナリイメージを持っていますか?バイナリイメージのFindContoursと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)インデックスを有する画像マーカーの 所望の領域の輪郭を有しています。 したがって、すべての領域は1つ以上の接続されたコンポーネントとして表示されます。 ピクセル値1,2,3などがあります。このようなマーカーは、それで、あなたは明らかにあなたが流域変換しますbefor findContoursを使用する必要がfindContours

を使用して、バイナリマスクから を取得することができます。 オブジェクトを数えるには、最初にセグメント化されたオブジェクトを持つことは明らかです。 これは、流域を変換した後で最もそうする必要があります。

ちょうどGoogleを使用してください。あなたが望むものを達成するための数多くのチュートリアルや例があります。

+0

@pigletは何ですか?どこでそのようなオブジェクトを数えることができるコードを置く必要がありますか?上の私のソースコードですか? – MTStuart

+0

私たちは@Pigletをチャットできますか? – MTStuart

+1

あなたのソースコードを十分に理解できない場合は、それらの関数呼び出しを追加する場所を知っていなければ、既に何を持っているかを理解することができます。 3つの関数すべてのドキュメントをお読みください。これはまともなプログラマが最初にやることです。私はあなたがここで何をしているのか理解していないと仮定します。そうでなければ、呼び出しの順序ははっきりします。あなたの母親に説明するように、あなたのコード全体にコメントを書くことから始めます。何が起こっている?あなたはどのようなデータを持っていますか... ... – Piglet

関連する問題