2017-10-11 193 views
1

我正在将一个python脚本传输到MATLAB中。在Python脚本中的一个步骤是调整一个256 * 256的RGB图像,以40 * 40的RGB图像,使用cv2.resize功能,即为什么在RGB图像上使用python cv2.resize函数会产生与MATLAB imresize函数不同的结果?

import cv2 
img = cv2.imread('0.png') 
img_40 = cv2.resize(img, (40, 40)) # img rescaled to 40*40 

而且,我打印40 * 40的图像B信道的一些像素值。

print img_40[0:10, 0:10, 0] 
ans = 
[[ 0 0 1 2 1 3 0 21 96 128] 
[ 2 0 17 13 5 20 15 48 112 126] 
[ 0 0 6 0 2 3 80 107 122 129] 
[ 0 5 1 7 0 14 98 132 129 127] 
[ 1 2 0 0 0 16 100 151 138 134] 
[ 0 2 0 2 0 34 105 138 143 139] 
[ 0 3 0 0 0 54 96 29 51 79] 
[ 5 0 0 0 0 56 118 103 97 38] 
[ 3 0 0 0 2 44 132 95 93 89] 
[ 1 0 1 3 0 38 141 128 104 26]] 

但是,当我使用MATLAB imresize函数,我得到了一个稍微不同的结果。 PS:我已将AntiAliasing设置为false,如here所述。

img = imread('0.png'); 
img_40 = imresize(img,[40,40],'bilinear','AntiAliasing',false); 
img_40(1:10,1:10,3) 
ans = 
0 0 2 1 2 4 0 21 96 128 
2 0 18 13 5 20 15 48 112 127 
0 0 6 0 3 3 81 107 123 129 
0 5 1 7 0 14 99 133 129 127 
1 2 0 0 0 16 100 151 139 134 
0 2 0 2 0 34 105 139 144 140 
0 3 0 0 0 54 96 29 51 79 
6 0 0 0 0 57 119 104 97 39 
3 0 0 0 2 44 132 96 93 89 
1 0 1 3 1 38 141 129 104 26 

并提供测试图像0.png

期待任何解释,这将有助于我。

更新 - 二零一七年十月十二日

正如下面他的评论注意到安德Biguri,该问题的原因可能是:插值给浮点值,但我与uint8值工作,这可能会导致舍入错误。

并将图像矩阵转换为double类型,看起来是正确的。

与Python CV2:

img = cv2.imread('0.png') 
d_img = img.astype('float') 
img_40 = cv2.resize(d_img, (40, 40)) # img rescaled to 40*40 
print img_40[0:10, 0:10, 0] 
ans = 
[[ 3.00000000e-01 0.00000000e+00 1.40000000e+00 1.81000000e+00 
1.61000000e+00 3.49000000e+00 0.00000000e+00 2.13500000e+01 
9.62500000e+01 1.28080000e+02] 
[ 2.00000000e+00 0.00000000e+00 1.75000000e+01 1.31900000e+01 
4.87000000e+00 1.98900000e+01 1.52300000e+01 4.81500000e+01 
1.12420000e+02 1.26630000e+02] 
[ 0.00000000e+00 0.00000000e+00 5.75000000e+00 0.00000000e+00 
2.35000000e+00 2.80000000e+00 8.04000000e+01 1.06500000e+02 
1.22300000e+02 1.28800000e+02] 
[ 0.00000000e+00 5.28000000e+00 9.50000000e-01 7.19000000e+00 
0.00000000e+00 1.40300000e+01 9.85100000e+01 1.32200000e+02 
1.28990000e+02 1.27270000e+02] 
[ 7.90000000e-01 2.40000000e+00 1.50000000e-01 7.00000000e-02 
0.00000000e+00 1.65100000e+01 9.96300000e+01 1.50750000e+02 
1.38440000e+02 1.34300000e+02] 
[ 0.00000000e+00 2.13000000e+00 1.50000000e-01 1.90000000e+00 
0.00000000e+00 3.42900000e+01 1.04730000e+02 1.38400000e+02 
1.43560000e+02 1.39400000e+02] 
[ 0.00000000e+00 2.70000000e+00 0.00000000e+00 0.00000000e+00 
0.00000000e+00 5.43700000e+01 9.57400000e+01 2.88500000e+01 
5.05500000e+01 7.88600000e+01] 
[ 5.55000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 
1.50000000e-01 5.65500000e+01 1.18500000e+02 1.03000000e+02 
9.73000000e+01 3.81000000e+01] 
[ 3.30000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 
1.84000000e+00 4.40700000e+01 1.32280000e+02 9.53000000e+01 
9.27000000e+01 8.92400000e+01] 
[ 1.40000000e+00 0.00000000e+00 1.00000000e+00 3.03000000e+00 
7.00000000e-01 3.79900000e+01 1.40600000e+02 1.28600000e+02 
1.04270000e+02 2.61200000e+01]] 

用MATLAB:

img = imread('0.png'); 
d_img = single(img); 
img_40 = imresize(d_img,[40,40],'bilinear','AntiAliasing',false); 
img_40(1:10,1:10,3) 

0.3000   0 1.4000 1.8100 1.6100 3.4900   0 21.3500 96.2500 128.0800 
2.0000   0 17.5000 13.1900 4.8700 19.8900 15.2300 48.1500 112.4200 126.6300 
    0   0 5.7500   0 2.3500 2.8000 80.4000 106.5000 122.3000 128.8000 
    0 5.2800 0.9500 7.1900   0 14.0300 98.5100 132.2000 128.9900 127.2700 
0.7900 2.4000 0.1500 0.0700   0 16.5100 99.6300 150.7500 138.4400 134.3000 
    0 2.1300 0.1500 1.9000   0 34.2900 104.7300 138.4000 143.5600 139.4000 
    0 2.7000   0   0   0 54.3700 95.7400 28.8500 50.5500 78.8600 
5.5500   0   0   0 0.1500 56.5500 118.5000 103.0000 97.3000 38.1000 
3.3000   0   0   0 1.8400 44.0700 132.2800 95.3000 92.7000 89.2400 
1.4000   0 1.0000 3.0300 0.7000 37.9900 140.6000 128.6000 104.2700 26.1200 

PS:问题的答案How to use Matlab's imresize in python不提与uint8值工作时所造成的内插,舍入误差。

回答

1

这是由于舍入错误。请注意,所有差异仅为1个单位。

您正在使用uint8值,但插值几乎总会给出浮点值。像素值[0 - 3]之间的中点是1.5。取决于resize内部的数学运算符的确切顺序,结果可能是1.49999999999991.50000000000001,然后当四舍五入时,您会得到12

Read more about floating points maths

+1

非常感谢你,你的回答解决了我的问题。有关详细信息,请参阅问题中的更新。 – 7color

相关问题