【问题标题】:How to transform image opencv in android如何在android中转换图像opencv
【发布时间】:2014-07-17 02:38:35
【问题描述】:

我只是 Opencv 的 android 初学者。这是转换图像的简单代码。但我已经尝试了几天的图像转换。但我无法弄清楚什么是问题。我认为部分打击评论(src,dst)是错误的断言。 我现在需要你们帮忙。 (类似于Android OpenCV getPerspectiveTransform and warpPerspective)。

这是我的日志。

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 in function 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); 
} 

}

【问题讨论】:

    标签: android opencv


    【解决方案1】:

    您需要注意各种函数的参数类型和尺寸是否正确。例如,这里的调用:

       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。

    【讨论】:

    • 感谢您的帮助。我误解了使用 warpPerspective 函数。
    猜你喜欢
    • 2016-01-03
    • 1970-01-01
    • 2019-03-04
    • 2012-05-09
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多