图像处理 opencv 3.0 断言失败错误

Posted

技术标签:

【中文标题】图像处理 opencv 3.0 断言失败错误【英文标题】:image processing opencv 3.0 assertion failed error 【发布时间】:2017-01-05 15:28:28 【问题描述】:

我在互联网上能找到的只有 opencv 2.x java 代码示例。我正在使用 opencv 3.2 并尝试加载图像并将所有长于 x 像素的黑线变为白色(删除它们)。这是我从一个opencv 2.4版本的霍夫变换示例开始的地方......

  Mat img = Imgcodecs.imread("C:/Users/user1/Desktop/topdown-6.jpg");
   // Mat img = Imgcodecs.imread(fileName)

    // generate gray scale and blur
    Mat gray = new Mat();
    Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);
    Imgproc.blur(gray, gray, new Size(3, 3));

    // detect the edges
    Mat edges = new Mat();
    int lowThreshold = 50;
    int ratio = 3;
    Imgproc.Canny(gray, edges, lowThreshold, lowThreshold * ratio);

    Mat lines = new Mat();
    Imgproc.HoughLinesP(edges, lines, 1, Math.PI / 180, 50, 50, 10);

    for(int i = 0; i < lines.cols(); i++) 
        double[] val = lines.get(0, i);
        Imgproc.line(img, new Point(val[0], val[1]), new Point(val[2], val[3]), new Scalar(0, 0, 255), 2);
    

    Image edgesImg = toBufferedImage(edges);
    Image linesImg = toBufferedImage(lines);
    Image imgg = toBufferedImage(img);

我得到了错误

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor, file 

C:\build\master_winpack-bindings-win32-vc14-static\opencv\modules\imgproc\src\color.cpp, line 9748
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: C:\build\master_winpack-bindings-win32-vc14-static\opencv\modules\imgproc\src\color.cpp:9748: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor
]
    at org.opencv.imgproc.Imgproc.cvtColor_1(Native Method)
    at org.opencv.imgproc.Imgproc.cvtColor(Imgproc.java:1778)
    at Main.main(Main.java:174)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

对我的目标的任何帮助都会很棒。我应该只使用 opencv 2.x 版吗?

编辑:

    public Image toBufferedImage(Mat m)
    int type = BufferedImage.TYPE_BYTE_GRAY;
    if ( m.channels() > 1 ) 
        type = BufferedImage.TYPE_3BYTE_BGR;
    
    int bufferSize = m.channels()*m.cols()*m.rows();
    byte [] b = new byte[bufferSize];
    m.get(0,0,b); // ERROR HAPPENING HERE
    BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);
    final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
    System.arraycopy(b, 0, targetPixels, 0, b.length);
    return image;

【问题讨论】:

【参考方案1】:

问题是您将图像加载为灰度,因此您以后不需要转换它。在执行 BGR2GRAY 时,cvtColor 期望输入垫是颜色垫

【讨论】:

如果保留参数并删除 cvtColor 行会怎样? (同时更改模糊以使用 img 作为输入) 我搞定了。解决方案是制作参数 Imgcodecs.IMREAD_COLOR 。现在我在将垫子转换为缓冲图像的代码中出现“垫子数据类型不兼容:28”错误。 toBufferedImage(mat); 的编辑和发布代码

以上是关于图像处理 opencv 3.0 断言失败错误的主要内容,如果未能解决你的问题,请参考以下文章

java opencv错误内存不足和断言失败

OpenCV 错误断言在某些像素值上失败

OpenCV 人脸检测 ROI 断言失败

haar 训练 OpenCV 断言失败

OpenCV 错误:调用 detectMultiScale 时断言失败

是啥导致“OpenCV(4.0.1)错误:断言失败(m.dims <= 2)”