![](/img/trans.png)
[英]Why does Python ctype c_char_p return different values on OSX vs. Windows?
[英]Why Does OpenCV Resize Give Different Pixel Values When Specifying Dsize vs. fx/fy?
今天我惊讶地发现,在使用 OpenCV-Python cv2.resize
时,我得到的结果与我指定fx
和fy
时,或者我自己进行dsize
计算并输入时不同。
请注意,这与图像大小不匹配无关,而是实际的单个像素值差异。
import cv2
import numpy as np
scale = 0.55
image = np.arange(100).reshape(10, 10).astype(np.float)
resized_fx_fy = cv2.resize(image, None, fx=scale, fy=scale, interpolation=cv2.INTER_LINEAR)
resize_height, resize_width = resized_fx_fy.shape
resized_dsize = cv2.resize(image, (resize_width, resize_height), interpolation=cv2.INTER_LINEAR)
print(np.abs(resized_fx_fy - resized_dsize).max())
7.499998092651367
print(resized_fx_fy - resized_dsize)
[[0.83333344 0.98484851 1.13636362 1.28787897 1.43939408 1.09090917]
[2.34848631 2.50000024 2.65151525 2.80303049 2.9545455 2.6060605 ]
[3.86363742 4.01515031 4.16666532 4.31818056 4.46969557 4.12121058]
[5.37879091 5.53030276 5.68181777 5.83333302 5.98484802 5.63636303]
[6.89394202 7.04545283 7.19696784 7.34848309 7.49999809 7.1515131 ]
[3.40909298 3.5606029 3.71211791 3.86363316 4.01514816 3.66666317]]
从对 opencv 文档的天真解释来看,这些应该是等效的,但显然它们不是。
一个可能的提示:当scale = 0.5
时,上面的代码没有区别
如果它是相关的:
Python 3.8.5
opencv-contrib-python 4.2.0.34 pypi_0 pypi
opencv-python 4.2.0.34 pypi_0 pypi
opencv-python-headless 4.5.1.48 pypi_0 pypi
cv::resize
是一种仿射变换,它将目标图像中的 integer 索引映射到源图像中的浮点索引,并使用插值方法计算值。 因为它是仿射变换,所以比例参数是确定准确 output 值的最重要参数。
According to the OpenCV source code for cv::resize() , if dsize
is provided, fx
and fy
(referred to as inv_scale_x
and inv_scale_y
in c++ source code) are overwritten with relative scale of output to input, regardless of whether fx
or fy
是否为零。 在您的示例中,第一个cv.resize()
使用inv_scale_x
和inv_scale_y
0.55。 第二个cv.resize()
对这两个比例参数使用 0.6。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.