6

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());
  }
   }

}

2
  • 4
    Are you sure that the image is loaded correctly? Commented Mar 6, 2017 at 0:35
  • Exactly my image was 'Final.jpg' but instead i loaded 'final.jpg' so take a look at that too! Commented Oct 20, 2017 at 17:16

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

I had the same error, I wrote png instead of jpg in the file name. Make sure you load image with the proper extension/name

Comments

0

It looks like your source and destination are single channel while your kernel is 3 channels.

2 Comments

Thanks and what would be the change?
I'm not familiar with Java, but how about ` put(0,0,0); put(0,1,0); put(0,2,0);` to kernel only?
0

I had the same exact error before so when I wrote the whole path of the picture, My code worked very well , so be careful with the extension of the image and make sure that your picture exists

Here is what I did :

pic = cv2.imread('C:\Users\WSI\Desktop\python_scripts\hakuoki.jpg',cv2.IMREAD_COLOR)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.