C++ 在opencv 2DFilter中使用自定义内核-导致崩溃。。。卷积如何?

C++ 在opencv 2DFilter中使用自定义内核-导致崩溃。。。卷积如何?,c++,opencv,image-processing,convolution,C++,Opencv,Image Processing,Convolution,我想今天在openCV中尝试一下(自动)相关/卷积,并制作自己的2D过滤器内核 在openCV之后,我发现为openCV制作自己的内核可能并不难。然而,当我尝试使用一个异常时,我得到了未处理的异常 代码及与此问题相关的注释如下: #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include <stdlib.h> #include <stdio.h> #inclu

我想今天在openCV中尝试一下(自动)相关/卷积,并制作自己的2D过滤器内核

在openCV之后,我发现为openCV制作自己的内核可能并不难。然而,当我尝试使用一个异常时,我得到了未处理的异常

代码及与此问题相关的注释如下:

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv) {

    //Loading the source image
    Mat src;
    src = imread( "1.png" );

    //Output image of the same size and the same number of channels as src.
    Mat dst;
    //Mat dst = src.clone();   //didn't help...

    //desired depth of the destination image
    //negative so dst will be the same as src.depth()
    int ddepth = -1;        

    //the convolution kernel, a single-channel floating point matrix:
    Mat kernel = imread( "kernel.png" );
    kernel.convertTo(kernel, CV_32F);     //<<not working
    //normalize(kernel, kernel, 1.0, 0.0, 4, -1, noArray());  //doesn't help

    //cout << kernel.size() << endl;  // ... gives 11, 11

    //however, the example from tutorial that does work:
    //kernel = Mat::ones( 11, 11, CV_32F )/ (float)(11*11);

    //default value (-1,-1) here means that the anchor is at the kernel center.
    Point anchor = Point(-1,-1);

    //value added to the filtered pixels before storing them in dst.
    double delta = 0;

    //alright, let's do this...
    filter2D(src, dst, ddepth , kernel, anchor, delta, BORDER_DEFAULT );

    imshow("Source", src);     //<< unhandled exception here
    imshow("Kernel", kernel);
    imshow("Destination", dst);
    waitKey(1000000);

    return 0;
}
#包括“opencv2/imgproc/imgproc.hpp”
#包括“opencv2/highgui/highgui.hpp”
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
int main(int argc,字符**argv){
//加载源图像
Mat-src;
src=imread(“1.png”);
//输出与src大小和通道数相同的图像。
Mat-dst;
//Mat dst=src.clone();//没有帮助。。。
//目标图像的所需深度
//负值,因此dst将与src.depth()相同
int-ddepth=-1;
//卷积内核,单通道浮点矩阵:
Mat kernel=imread(“kernel.png”);

convertTo(kernel,CV_32F);//应该发布断言错误,这将有助于回答您的问题,而不是询问崩溃的原因。无论如何,我已经在下面发布了卷积过滤器2D的可能错误和解决方案

错误1:

OpenCV错误:cv::countNo中的断言失败(src.channels()==1&&func!=0) nZero,文件C:\builds\2\u 4\u PackSlave-win32-vc12-shared\opencv\modules\core\src\st at.cpp,第549行

解决方案:您的输入图像和内核应为灰度。您可以在imread中使用标志0。(例如,cv::imread(“kernel.png”,0)将图像读取为灰度)。如果要将不同的内核应用于不同的通道,请使用split()将图像分割为单独的颜色平面,并分别进行处理


除了可能崩溃的obove错误,我看不到任何东西。内核大小应该是奇数,您的内核映像是11X11,这很好。如果它仍然崩溃,请提供更多信息以帮助您解决问题。

完全正确!我甚至在我的评论中写了“单通道浮点矩阵”…但是,在我的辩护中,唯一的提要是我从VS得到的是“OpenCVest.Exe中的0x000”FF88C6EA8B9C未处理的异常:微软C++异常:CV::内存位置0x00,000,521B2De30。“没有提到”(SRC.ChhannSes()= 1和& Func=0)你如何访问它?而且,内核不是src需要一个通道,src需要三个通道工作…当然,感谢你的帮助,现在正在工作(但不像我希望的那样,但这是另一个讨论:)