c++ - Matlab 到 Opencv 代码

标签 c++ matlab opencv image-processing

I = imread('C:\\Users\\20055316\\Desktop\\blurMetric\\Illumination_Changes\\z_0001.bmp');
I = double(I);
[y, x] = size(I);

Hv = [1 1 1 1 1 1 1 1 1]/9;
Hh = Hv';

B_Ver = imfilter(I,Hv);%blur the input image in vertical direction
B_Hor = imfilter(I,Hh);%blur the input image in horizontal direction

我需要将上面的Matlab代码转换成OpenCV, 我尝试使用 Filter2D 但没有得到想要的结果。 openCV代码如下图

Mat oMatTempImage = imread("C:\\Users\\20055316\\Desktop\\blurMetric\\Illumination_Changes\\z_0001.bmp");
    cvtColor(oMatTempImage, oMatTempImage, CV_BGR2RGB);
    oMatTempImage.convertTo(oMatTempImage, CV_32FC3);
Mat oMatVerticalKernel = Mat(1, 9, CV_32FC1);
    for (int nColItr = 0; nColItr < 9; nColItr++)
    {
        oMatVerticalKernel.at<float>(0, nColItr) = 1.0f / 9.0f;
    }
    Mat oMatHorizontalKernel;
    transpose(oMatVerticalKernel, oMatHorizontalKernel);
Mat oMatVeriticalBlurr;
    filter2D(oMatTempImage, oMatVeriticalBlurr, -1, oMatVerticalKernel);
Mat oMatHorizontalBlurr;
    filter2D(oMatTempImage, oMatHorizontalBlurr, -1, oMatHorizontalKernel);

我没有得到类似的结果,请帮助我。

编辑: 谢谢大家的回复, 我知道出了什么问题。 当调用 imfilter() 函数时,Matlab 在内部执行零填充,而 opencv 不这样做。我必须在应用滤镜之前进行零填充,然后再删除零填充像素。这解决了问题。

最佳答案

关于c++ - Matlab 到 Opencv 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26096110/

相关文章:

c++ - 是否有 C++ 等价于逻辑 when?

c++ - VS2013 : compiler bug with float and/EHa +/fp:strict?

database - 使用 matlab 连接到远程 postgreSQL 数据库

c - 从 C-DLL 函数读取 MATLAB 中的 int 和字符串数组

opencv - 如何将 SDL1 表面转换为 opencv Mat

c++ - 如果用 C++ 将整个类写在一个文件中会发生什么样的坏事?

matlab - 函数等待执行

opencv - bash 中的人脸检测 : Simply return number of faces found

c++ - 多 channel 核图像卷积

c++ - C++ 对 int 的读写是原子的吗?