如何在android中转换图像opencv

时间:2014-05-28 02:47:28

标签: android opencv

我只是Opencv的初学者。这是转换图像的简单代码。但我试图做几天的图像转换。但我无法弄清楚是什么问题。我认为部分评论(src,dst)是错误的论文。 我现在需要你们的帮助。 (像Android OpenCV getPerspectiveTransform and warpPerspective)。

这是我的logcat。

E / AndroidRuntime(9880):CvException [org.opencv.core.CvException:/home/reports/ci/slave_desktop/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp:97 :错误:(-215)src.dims == 2&& info.height ==(uint32_t)src.rows&& info.width ==(uint32_t)函数中的src.cols void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv *,jclass,jlong​​,jobject,jboolean)

公共类MainActivity扩展了Activity实现OnClickListener {

private Button btnProc; 
 private ImageView imageView; 
 private Bitmap bmp,bmp1; 

 private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) { 
     @Override 
     public void onManagerConnected(int status) { 
         switch (status) { 
            case LoaderCallbackInterface.SUCCESS:{ 
            } break; 
            default:{ 
             super.onManagerConnected(status); 
             } break; 
         } 
     } 
 }; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.activity_main);

    btnProc = (Button) findViewById(R.id.btn_gray_process); 
    imageView = (ImageView) findViewById(R.id.image_view);
    btnProc.setOnClickListener(this);
}

public void onClick(View v) { 

    bmp = BitmapFactory.decodeResource(getResources(),R.drawable.a2);
    Mat original_image = new Mat();     
    Utils.bitmapToMat(bmp, original_image);     

    Mat dst = new Mat(4,1,CvType.CV_32FC2);
    Mat src = new Mat(4,1,CvType.CV_32FC2);
    src.put(0,0,407.0,74.0,1606.0,74.0,420.0,2589.0,1698.0,2589.0);
        dst.put(0,0,0.0,0.0,1600.0,0.0, 0.0,2500.0,1600.0,2500.0);      
    Mat perspectiveTransform = Imgproc.getPerspectiveTransform(src, dst);
    Mat cropped_image = original_image.clone();
    Imgproc.warpPerspective(src, cropped_image, perspectiveTransform, new Size(512,512));    
    bmp1 = Bitmap.createBitmap(400, 400, Config.RGB_565);
    Utils.matToBitmap(dst, bmp1);

    imageView.setImageBitmap(bmp1);
}

public void onResume(){ 
     super.onResume();       
     OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this,mLoaderCallback); 
} 

}

1 个答案:

答案 0 :(得分:0)

您需要注意各种功能的参数是正确的类型和尺寸。例如,这里打电话:

   Imgproc.warpPerspective(src, cropped_image, perspectiveTransform, new Size(512,512));    

我怀疑你真的不想在这里使用src,因为它是4x1 CV_32FC2。这应该是original_image。

另外,这里:

  Utils.matToBitmap(dst, bmp1);

再次,dst是4x1 CV_32FC2。我怀疑你应该尝试使用cropped_image。