gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:43:07 25 4
gpt4 key购买 nike

我在互联网上能找到的就是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;
}

最佳答案

问题在于您将图像加载为灰度图像,因此以后无需转换。在执行BGR2GRAY时,cvtColor希望输入垫为您的情况下的颜色垫

关于opencv - 图像处理opencv 3.0断言失败错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41488736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com