2017-01-03 79 views
1

把mat轉換成位圖和位圖來處理。我得到了黑色的場景。可能是什麼原因,有人請幫助我如何將mat轉換爲位圖和位圖來處理?

package com.example.alper.myapplication; 

      import android.graphics.Bitmap; 
      import android.graphics.BitmapFactory; 
      import android.os.Environment; 
      import android.support.v7.app.AppCompatActivity; 
      import android.os.Bundle; 
      import android.util.Log; 
      import android.view.View; 
      import android.widget.Button; 
      import android.widget.ImageView; 
      import android.widget.Toast; 

      import org.opencv.android.Utils; 
      import org.opencv.core.CvException; 
      import org.opencv.core.CvType; 
      import org.opencv.core.Mat; 
      import org.opencv.core.Scalar; 
      import org.opencv.imgproc.Imgproc; 

      import static org.opencv.imgproc.Imgproc.cvtColor; 

    public class Main2Activity extends AppCompatActivity { 
     ImageView iv; 
     Bitmap bmpInput, bmpOutput; 
     Mat matInput , matOutput; 
     Button bt; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main2); 
      iv = (ImageView) findViewById(R.id.imageView2); 
      bt= (Button) findViewById(R.id.button2); 

      String photoPath = Environment.getExternalStorageDirectory()+"/frames/frame.png"; 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
      bmpInput = BitmapFactory.decodeFile(photoPath,options); 

      iv.setImageBitmap(bmpInput); 
      matInput = convertBitMap2Mat(bmpInput); 
      matOutput = new Mat(matInput.rows(), matInput.cols(), CvType.CV_8UC3); 

      bt.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 

        bmpOutput = converMat2Bitmat(matOutput); 
        iv.setImageBitmap(bmpOutput); 
       } 
      }); 



     } 
     Bitmap converMat2Bitmat (Mat img) { 
      int width = img.width(); 
      int hight = img.height(); 


      Bitmap bmp; 
      bmp = Bitmap.createBitmap(width, hight, Bitmap.Config.ARGB_8888); 
      Mat tmp; 
      tmp = img.channels()==1? new Mat(width, hight, CvType.CV_8UC1, new Scalar(1)): new Mat(width, hight, CvType.CV_8UC3, new Scalar(3)); 
      try { 
       if (img.channels()==3) 
        cvtColor(img, tmp, Imgproc.COLOR_RGB2BGRA); 
       else if (img.channels()==1) 
        cvtColor(img, tmp, Imgproc.COLOR_GRAY2RGBA); 
       Utils.matToBitmap(tmp, bmp); 
      } 
      catch (CvException e){ 
       Log.d("Expection",e.getMessage()); 
      } 
      return bmp; 
     } 


     Mat convertBitMap2Mat (Bitmap rgbaImage){ 
      Mat rgbaMat = new Mat(rgbaImage.getHeight(), rgbaImage.getWidth(),CvType.CV_8UC4); 
      Bitmap bmp32 = rgbaImage.copy(Bitmap.Config.ARGB_8888, true); 
      Utils.bitmapToMat(bmp32, rgbaMat); 

      Mat rgbMat = new Mat(rgbaImage.getHeight(), rgbaImage.getWidth(), CvType.CV_8UC3); 
      cvtColor(rgbaMat,rgbMat,Imgproc.COLOR_RGBA2BGR, 3); 
      return rgbMat; 
     } 

我有一個位圖圖片。 ı想從位圖轉換爲mat,然後再應用一些圖像處理再轉換成matmap中的位圖,而不是在圖像視圖中顯示,但是會得到黑色的場景。您認爲可能是什麼原因?幫我。

回答

1

這個功能在opencv中有用Utils class。嘗試使用these

0

沒有問題與您的代碼。錯誤在您的圖像處理代碼中。 如果您嘗試將bmpInput轉換爲mat,然後將其重新轉換爲位圖,它將與您的代碼一起使用。