Search code examples
androidopencvimage-processingjava-native-interface

Android OpenCV Error


I am new to OpenCV. I integrated OpenCV in Android Studio following This Post

Version of OpenCV SDK used is : 3.4.0

Version of Android Studio is : 3.0.1

My Project Structure is as follows:

Project Structure

While accessing the Imgproc library while Canny function is working remaining functions like HoughLines, cornerHarris,.. detect() method in LineSegmentDetector class all throw a same type of exception which I am unable to figure out why it is thrown.

My code:

public class MainActivity extends AppCompatActivity {
static{ System.loadLibrary("opencv_java3"); }
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView image=findViewById(R.id.image1);
    ImageView image2=findViewById(R.id.image2);
    Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.wall);
    image.setImageBitmap(bitmap);
    Mat imageMat=new Mat();
    Utils.bitmapToMat(bitmap,imageMat);
    Bitmap newBitmap=Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Mat lines=new Mat();
    LineSegmentDetector lsd= Imgproc.createLineSegmentDetector(Imgproc.LSD_REFINE_STD,0.75,2,0.6,10,0.6,0.1,2);
    lsd.detect(imageMat,lines);
    //Imgproc.Canny(imageMat,lines,1,60,3,false);
    //Imgproc.HoughLines(imageMat,lines,20,20,10);
    //Imgproc.cornerHarris(imageMat,lines,2,3,0.04);
    //Imgproc.GaussianBlur(imageMat,lines,new Size(3,4),2);
    //Utils.matToBitmap(lines,newBitmap);
    image2.setImageBitmap(newBitmap);}
  }

Error StackTrace:

02-26 17:51:54.845 18690-18690/com.example.bssakala.opencvsample E/cv::error(): OpenCV Error: Assertion failed (!image.empty() && image.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3))) in virtual void cv::LineSegmentDetectorImpl::detect(cv::InputArray, cv::OutputArray, cv::OutputArray, cv::OutputArray, cv::OutputArray), file /build/master_pack-android/opencv/modules/imgproc/src/lsd.cpp, line 416 02-26 17:51:54.854 18690-18690/com.example.bssakala.opencvsample E/org.opencv.imgproc: imgproc::detect_11() caught cv::Exception: /build/master_pack-android/opencv/modules/imgproc/src/lsd.cpp:416: error: (-215) !image.empty() && image.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3)) in function virtual void cv::LineSegmentDetectorImpl::detect(cv::InputArray, cv::OutputArray, cv::OutputArray, cv::OutputArray, cv::OutputArray) 02-26 17:51:54.856 18690-18690/com.example.bssakala.opencvsample E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.bssakala.opencvsample, PID: 18690 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bssakala.opencvsample/com.example.bssakala.opencvsample.MainActivity}: CvException [org.opencv.core.CvException: cv::Exception: /build/master_pack-android/opencv/modules/imgproc/src/lsd.cpp:416: error: (-215) !image.empty() && image.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3)) in function virtual void cv::LineSegmentDetectorImpl::detect(cv::InputArray, cv::OutputArray, cv::OutputArray, cv::OutputArray, cv::OutputArray) ] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2706) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1514) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6205) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) Caused by: CvException [org.opencv.core.CvException: cv::Exception: /build/master_pack-android/opencv/modules/imgproc/src/lsd.cpp:416: error: (-215) !image.empty() && image.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3)) in function virtual void cv::LineSegmentDetectorImpl::detect(cv::InputArray, cv::OutputArray, cv::OutputArray, cv::OutputArray, cv::OutputArray) ] at org.opencv.imgproc.LineSegmentDetector.detect_1(Native Method) at org.opencv.imgproc.LineSegmentDetector.detect(LineSegmentDetector.java:59) at com.example.bssakala.opencvsample.MainActivity.onCreate(MainActivity.java:34) at android.app.Activity.performCreate(Activity.java:6864) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659) ... 9 more

What does this line indicate??

error: (-215) !image.empty() && image.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3)) in function virtual void cv::LineSegmentDetectorImpl::detect(cv::InputArray, cv::OutputArray, cv::OutputArray, cv::OutputArray, cv::OutputArray)


Solution

  • The error message tells you that a certain assertation failed when you called detect

    From https://github.com/opencv/opencv/blob/master/modules/imgproc/src/lsd.cpp line 416:

     CV_Assert(!image.empty() && image.type() == CV_8UC1);
    

    Which translates to: make sure that the provided input image is not empty and that its type is CV_8UC1 or throw an ugly error message at the callers face.

    So make sure you provide a proper input image to detect as anything else won't work.

    This can also be found in the OpenCV reference manual

    https://docs.opencv.org/3.1.0/db/d73/classcv_1_1LineSegmentDetector.html

    Where it says:

    Parameters

    _image A grayscale (CV_8UC1) input image.