简体   繁体   English

为什么在指定 Dsize 与 fx/fy 时 OpenCV Resize 会给出不同的像素值?

[英]Why Does OpenCV Resize Give Different Pixel Values When Specifying Dsize vs. fx/fy?

Today I was surprised to discover that when using OpenCV-Python cv2.resize , I get different results from when I specify fx and fy , or when I do the dsize calculation myself and feed that.今天我惊讶地发现,在使用 OpenCV-Python cv2.resize时,我得到的结果与我指定fxfy时,或者我自己进行dsize计算并输入时不同。

Note that this is NOT about image size mismatch, but actual individual pixel value difference.请注意,这与图像大小不匹配无关,而是实际的单个像素值差异。

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]]

From a naive interpretation of the opencv docs, these should be equivalent but clearly they are not.从对 opencv 文档的天真解释来看,这些应该是等效的,但显然它们不是。

A possible hint: The above code yields no difference when scale = 0.5一个可能的提示:当scale = 0.5时,上面的代码没有区别

In case it's relevant:如果它是相关的:

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 is an affine transformation that maps integer indices from the destination image to floating point indices in the source image and uses the interpolation method to calculate the value. cv::resize是一种仿射变换,它将目标图像中的 integer 索引映射到源图像中的浮点索引,并使用插值方法计算值。 Because it is an affine transformation, the scale parameter is the most important parameter in determining the exact output values.因为它是仿射变换,所以比例参数是确定准确 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 are zero or not. 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是否为零。 In your example, the first cv.resize() uses inv_scale_x and inv_scale_y of 0.55.在您的示例中,第一个cv.resize()使用inv_scale_xinv_scale_y 0.55。 The second cv.resize() uses 0.6 for these two scale parameters.第二个cv.resize()对这两个比例参数使用 0.6。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Python ctype c_char_p在OSX和Windows上返回不同的值? - Why does Python ctype c_char_p return different values on OSX vs. Windows? 为什么epiplolar geometry/sfm在python opencv中没有给出正确的值 - Why epiplolar geometry/sfm does not give proper values in python opencv 为什么在不同的操作系统和版本中打印这些值会给出不同的值? - Why does printing these values give different values in different OS and versions? 列表上的 Python append() 与 + 运算符,为什么这些会给出不同的结果? - Python append() vs. + operator on lists, why do these give different results? 保存在python中时,为什么GIF像素值会更改? - Why does GIF pixel values change when saved in python? 为什么 opencv Videocapture function 会读取像素值错误的视频帧? - Why does opencv Videocapture function read video frames with wrong pixel values? OpenCV MergeMertens在Python Vs中给出不同的结果。 C ++ - OpenCV MergeMertens gives different results in Python Vs. C++ 为什么 AWS CLI 在 darwin (mac) 和 linux 上使用不同的 AWS 服务? rds describe-db-instances 命令什么时候使用STS? - Why the does AWS CLI use different AWS services on darwin (mac) vs. linux? When does the rds describe-db-instances command use STS? 使用opencv的matlplotlib提供具有相同像素值的不同图像 - matlplotlib with opencv gives a different image with the same pixel values 为什么Python的'for ... in'在值列表和字典列表上的工作方式不同? - Why does Python's 'for … in' work differently on a list of values vs. a list of dictionaries?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM