簡體   English   中英

為什么在指定 Dsize 與 fx/fy 時 OpenCV Resize 會給出不同的像素值?

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

今天我驚訝地發現,在使用 OpenCV-Python cv2.resize時,我得到的結果與我指定fxfy時,或者我自己進行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_xinv_scale_y 0.55。 第二個cv.resize()對這兩個比例參數使用 0.6。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM