2013-02-12 177 views
1

我是OpenCV的新手,請耐心等待。我正在做一個Android應用程序來測量葉色的相似性。我試過使用canny來檢測只有葉面積,但我認爲它需要一個更長的過程。這裏是代碼,僅用於檢測葉面積。裁剪圖像的中心

public class editImage extends Activity { 
//private static final int CV_64FC1 = 0; 
protected ImageView im; 
protected String msg_path; 
private Mat mMatriximg; 

private Mat mMatriximgBW; 
private int CV_64FC1; 

//private Mat mMatriximgBW; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.image_layout); 

    im = (ImageView) findViewById(R.id.imV_ed); 
    Intent iIdentify = getIntent(); 
    msg_path = iIdentify.getStringExtra("path"); 

    //to grayscale 
    int im_gray = Highgui.CV_LOAD_IMAGE_GRAYSCALE; 
    mMatriximg = Highgui.imread(msg_path, im_gray); 

    // call library OpenCV 
    //Imgproc.equalizeHist(mMatriximg, mMatriximg); 
    Imgproc.Canny(mMatriximg, mMatriximg, 50, 150); 

    mMatriximgBW = Mat.zeros(mMatriximg.height(), mMatriximg.width(), CV_64FC1); 

    double y; 
    double threshold=190; 

    for(int i=0; i<mMatriximg.height(); i++){ 
     for(int j=0; j<mMatriximg.width(); j++){ 
      if(mMatriximg.get(i, j) [0]>=threshold){ 
       y=255; 
      }else{ 
       y=0;   
      } 
      mMatriximgBW.put(i, j, new double[] {y}); 
     } 
    } 

    //result mat to Grayscale 
    Imgproc.cvtColor(mMatriximgBW, mMatriximgBW, Imgproc.COLOR_GRAY2RGBA, 4); 

    Bitmap bmpOut = Bitmap.createBitmap(mMatriximgBW.cols(), mMatriximgBW.rows(), Bitmap.Config.ARGB_8888); 

    Utils.matToBitmap(mMatriximgBW, bmpOut); 
    im.setImageBitmap(bmpOut); 

} 

public void bckHome(View v){ 
    Intent iIden = new Intent(this, MBWDActivity.class); 
    setResult(RESULT_OK, iIden); 
    startActivityForResult(iIden, 99); 
} 
} 

所以我認爲如果我將葉子的中心作爲樣本數據(矩陣)切出,效率會更高。

有誰可以分享如何實現cvSetImageROI或其他方法?

感謝您的幫助

回答

1

我已經闡述過這一議題on this answer

您需要創建具有ROI維度的Rect。然後,創建一個新的Mat,並將其原始圖像作爲第一個參數傳遞給其構造函數,將ROI作爲第二個參數傳遞給該矩形。

+0

可以請你通知顯示完整簡單代碼的線程,並附上簡要說明..非常感謝你 – dzikri 2013-02-14 08:55:28

+0

我覺得這就是[答案](http://stackoverflow.com/a/14696576/176769)正在顯示。您需要將手放在OpenCV的文檔中,並檢查「Rect」和「Mat」的構造函數作爲參數。 – karlphillip 2013-02-14 16:27:48