Java- OpenCV 错误:OpenCV 错误:断言失败(dims <= 2 && step[0] > 0)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42615240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:45:58  来源:igfitidea点击:

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

javaopencv

提问by Juan M. Guevara Jiménez

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.

我有这个 opencv 代码。这对我在页面中找到的图像进行了卷积。我想尝试一下,但它给出了以下错误,而且我对 openCV 了解不多。我需要帮助。

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

错误:OpenCV 错误:断言失败(dims <= 2 && step[0] > 0)在 cv::Mat::locateROI,文件 C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\core \src\matrix.cpp,第 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());
  }
   }

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

}

回答by EikeMike

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.

有同样的错误,并遵循了@Miki 的非常有价值的提示。在我的情况下,由于位深不合适,图像没有正确加载。32 位而不是灰度图像的 8 位。

回答by fzk

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

我有同样的错误,我在文件名中写了 png 而不是 jpg。确保使用正确的扩展名/名称加载图像

回答by Bea

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)

回答by Quang Hoang

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

看起来您的源和目标是单通道,而您的内核是 3 个通道。