Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java- OpenCV Error: OpenCV Error: Assertion failed (dims <= 2 && step[0] > 0)

Tags:

java

opencv

I have this opencv code. Which makes a convolution to an image that I found in a page. I wanted to try it, but it gives the following error and I do not know much about openCV. I need help.

Error: OpenCV Error: Assertion failed (dims <= 2 && step[0] > 0) in cv::Mat::locateROI, file C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\core\src\matrix.cpp, line 949

public class main {

public static void main (String [ ] args) {

System.out.println ("hola");

 try {

     int kernelSize = 3;

     System.loadLibrary( Core.NATIVE_LIBRARY_NAME );

     Mat source = Imgcodecs.imread("logo.png", Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);

     Mat destination = new Mat(source.rows(),source.cols(),source.type());

     Mat kernel = new Mat(kernelSize,kernelSize, CvType.CV_32F){
        {
           put(0,0,0);
           put(0,1,0);
           put(0,2,0);

           put(1,0,0);
           put(1,1,1);
           put(1,2,0);

           put(2,0,0);
           put(2,1,0);
           put(2,2,0);
        }
     };

     Imgproc.filter2D(source, destination, -1, kernel);

     Imgcodecs.imwrite("original.jpg", destination);

  } catch (Exception e) {

      System.out.println("Error: " + e.getMessage());
  }
   }

}

like image 724
Juan M. Guevara Jiménez Avatar asked Mar 05 '17 23:03

Juan M. Guevara Jiménez


1 Answers

Had the same error and followed the very valuable hint of @Miki. In my case the image wasn't loaded properly because of unsuitable bit-depth. 32 instead of 8 bit for a greyscale image.

like image 105
EikeMike Avatar answered Oct 21 '22 03:10

EikeMike