Resize image in openCV is not working

时间:2017-07-10 15:17:36

标签: java android opencv

I have tried searching for solutions and nothing seems to be helping. Not sure what I should be doing.

Here is my Code

//The imagePath consist the path of the image from camereFunction()        
public void runImg(Uri imagePath){

     //my image file   
    Bitmap image = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imagePath);

    //For Testing Purpose 
    int image_w = image.getWidth();
    int image_h = image.getHeight();
    Log.d("Captured Image Prop: ", "Height = " + image_h + " Width = " + image_w );

    if (image != null ){


                    Mat imageMat = new Mat();
                    Mat image_res = new Mat();
                    //changes bitmap to Mat   
                    Utils.bitmapToMat(image, imageMat);

                    //Resize Image
                    Imgproc.resize(imageMat,image_res,new Size(400, 500),0.5,0.5,Imgproc.INTER_AREA);
                    //Create a rectangle
                    Imgproc.rectangle(image_res,new  Point(15,20), new Point(45,55), new Scalar(255, 0, 255),2);


                      // For Testing purpose ! To display proccessed image to view the outpts
                     Utils.matToBitmap(image_res,image);
                     imageView.setImageBitmap(image);


                    //To check the resized image height and width
                     image_w = image.getWidth();
                     image_h = image.getHeight();
                     Log.d("Resize Image Prop: ", "Height = " + image_h + " Width = " + image_w );
            }

This is the error that I am getting:

07-10 22:40:54.077 18490-18490/com.hariicomp.hariivel.ichariitest E/cv::error(): OpenCV Error: Assertion failed (src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols) in void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean), file /build/master_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp, line 97
07-10 22:40:54.077 18490-18490/com.hariicomp.hariivel.ichariitest E/org.opencv.android.Utils: nMatToBitmap catched cv::Exception: /build/master_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp:97: error: (-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)
07-10 22:40:54.083 18490-18490/com.hariicomp.hariivel.ichariitest E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.hariicomp.hariivel.ichariitest, PID: 18490
Theme: themes:{com.cyanogenmod.trebuchet=overlay:system, com.android.settings=overlay:system, default=overlay:org.cyanogenmod.hexolibre, iconPack:com.zavukodlak.candycons}
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=203, result=-1, data=Intent { (has extras) }} to activity {com.hariicomp.hariivel.ichariitest/com.hariicomp.hariivel.ichariitest.MainActivity}: CvException [org.opencv.core.CvException: /build/master_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp:97: error: (-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)
]
  at android.app.ActivityThread.deliverResults(ActivityThread.java:3733)
  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)
  at android.app.ActivityThread.-wrap16(ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:148)
  at android.app.ActivityThread.main(ActivityThread.java:5461)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  Caused by: CvException [org.opencv.core.CvException: /build/master_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp:97: error: (-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)
]
at org.opencv.android.Utils.nMatToBitmap2(Native Method)
at org.opencv.android.Utils.matToBitmap(Utils.java:123)
at com.hariicomp.hariivel.ichariitest.MainActivity.runOCR(MainActivity.java:199)
at com.hariicomp.hariivel.ichariitest.MainActivity.onActivityResult(MainActivity.java:128)
at android.app.Activity.dispatchActivityResult(Activity.java:6456)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3729)
... 9 more*

Can anybody please guide me ? Thank you.

1 个答案:

答案 0 :(得分:3)

错误表明您的imresize正在运作,但Utils.matToBitmap却没有。这是因为您需要重新初始化Bitmap image

您可以使用:

// before matToBitmap
 image = Bitmap.createBitmap(image_res.cols(), image_res.rows(), Bitmap.Config.ARGB_8888); 

在聊天中讨论后,它有效!很乐意提供帮助!