【发布时间】:2019-02-24 03:58:39
【问题描述】:
我有一张原图:
然后我阅读它,创建一个 PSF,并在 Matlab 中对其进行模糊处理:
lenawords1=imread('lenawords.bmp');
%create PSF
sigma=6;
PSFgauss=fspecial('gaussian', 8*sigma+1, sigma);
%blur it
lenablur1=imfilter(lenawords1, PSFgauss, 'conv');
lenablurgray1=mat2gray(lenablur1);
PSFgaussgray = mat2gray(PSFgauss);
我保存了模糊的图像:
imwrite(lenablurgray1, 'lenablur.bmp');
当我在其中显示一些值时,我得到
disp(lenablurgray1(91:93, 71:75))
0.5556 0.5778 0.6000 0.6222 0.6444
0.6000 0.6444 0.6667 0.6889 0.6889
0.6444 0.6889 0.7111 0.7333 0.7333
然后我在 OpenCV 中打开该模糊图像并在相同的索引处显示其值:
Mat img = imread("lenablur.bmp");
for (int r = 91; r < 94; r++) {
for (int c = 71; c < 76; c++) {
cout << img.at<double>(r, c) << " ";
}
cout << endl;
}
cout << endl;
我得到的结果与上面的值不匹配:
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
这是为什么?
编辑:img.at<unsigned int>(r, c)给
1903260029 1533437542 ...
2004318088 ...
....
如果我将模糊图像保存为 png 文件:
imwrite(lenablurgray1, 'lenablur.png');
然后当我在 OpenCV 中阅读时:
Mat img = imread("lenablur.png");
img.convertTo(img, CV_64F);
然后img.at<double>(r, c) 给出
17 11 11 11 6
17 11 11 11 6
17 11 11 11 11
仍然与 Matlab 中的值不匹配
EDIT2:我现在看到内核的值是错误的。在 Matlab 中,我得到了
imwrite(PSFgaussgray, 'PSFgauss.bmp');
disp(PSFgaussgray(7:9, 7:9)*256)
.0316 .0513 .0812
.0513 ...
...
而在 OpenCV 中:
Mat kernel = imread("PSFgauss.bmp");
cvtColor(kernel, kernel, cv::COLOR_BGR2GRAY);
kernel.convertTo(kernel, CV_64F);
for (int r = 6; r < 9 ; r++) {
for (int c = 6; c < 9; c++) {
cout << kernel.at<double>(r, c) << " ";
}
cout << endl;
}
cout << endl;
我得到的结果与上面的值不匹配:
0 0 0
0 0 0
0 0 0
【问题讨论】:
-
如果您在图像中读取为
unsigned char,则不能执行img.at<double>(r, c)(BMP 始终是 8 位无符号整数)。 -
那我该怎么办?
-
类型必须与
img对象中存储的数据类型相匹配。请参阅文档:docs.opencv.org/trunk/d3/d63/… -
是的,我知道。我在 EDIT 中尝试了一些更改,但这些都不起作用
-
还有一个更改:MATLAB 索引从 1 开始,OpenCV 的索引从 0 开始。您需要调整您使用的索引。
标签: image matlab opencv image-processing