OPENCV调整大小不同于我自己的手动计算
图像数组
[[2, 2, 2, 2],
[2, 3, 3, 3],
[2, 4, 4, 4],
[5, 5, 5, 5]]
h = 4,w = 4 使用cv2.Resize(img,(h // 2,w // 2))
,结果是
[[2, 3],
[4, 5]]
还原因子是 2 ,当我手动计算时,
newImage(0,0) -> oldImage(2*0,2*0) = oldImage(0,0) = 2
newImage(0,1) -> oldImage(2*0,2*1) = oldImage(0,2) = 2
newImage(1,0) -> oldImage(2*1,2*0) = oldImage(2,0) = 2
newImage(1,1) -> oldImage(2*1,2*1) = oldImage(2,2) = 4
结果我的手册计算应该是:
[[2, 2],
[2, 4]]
我认为我的逻辑不是错误的,为什么OPENCV计算会有所不同
The Image array
[[2, 2, 2, 2],
[2, 3, 3, 3],
[2, 4, 4, 4],
[5, 5, 5, 5]]
h = 4, w = 4
use cv2.resize(img,(h//2,w//2))
, the result is
[[2, 3],
[4, 5]]
The reduction factor is 2, when I calculate manually,
newImage(0,0) -> oldImage(2*0,2*0) = oldImage(0,0) = 2
newImage(0,1) -> oldImage(2*0,2*1) = oldImage(0,2) = 2
newImage(1,0) -> oldImage(2*1,2*0) = oldImage(2,0) = 2
newImage(1,1) -> oldImage(2*1,2*1) = oldImage(2,2) = 4
The result of my manual calculation should be:
[[2, 2],
[2, 4]]
I think my logic is not wrong ah, why would there be a difference with the opencv calculation it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调整图像大小时,有几种插值方法。您可以使用
interpolation
cv2.resize
的参数选择它。此方法确定如何根据旧像素计算新像素的值。
与您手动实现的方法相似的方法是
cv.inter_nearest
。对于每个目标像素,它将选择最接近其的源像素并简单地复制其值,结果将在您的“手册”调整大小中,例如
cv2.inter_linear
,<代码, > cv.inter_cubic 等。执行更复杂的计算,可能考虑到目标像素附近的几个源像素。默认方法如果您未指定
interpolation
参数(如上上面的代码中)为cv2.inter_linear
( not> cv2.inter_nearest
)。这解释了您的结果。您可以将插值设置为不同的值和实验。
请参阅
cv2.Resize
:。有一些经验法则,插值方法对不同情况下表现最好。请参阅此处:哪种插值最适合调整图像? 。
When resizing an image there are several interpolation methods. You select it with the
interpolation
parameter ofcv2.resize
.This method determine how to calculate value for the new pixels based on the old ones.
The method which behaves similarly to the one you implemented manually is
cv.INTER_NEAREST
. For each destination pixel, it will select the source pixel closest to it and simply copy it's value, and the result will be like in your "manual" resize:Other interpolation methods like
cv2.INTER_LINEAR
,cv.INTER_CUBIC
etc. perform a more sophisticated calculation, possibly taking into account several source pixels in the neighborhood of the destination pixel.The default method in case you don't specify the
interpolation
parameter (like in your code above) iscv2.INTER_LINEAR
(notcv2.INTER_NEAREST
). This explains your result. You can set theinterpolation
parameter to different values and experiment.See the documentation for
cv2.resize
: cv.resize, and the list of interpolation methods:InterpolationFlags.There are some rules of thumb which interpolation method performs the best for different scenarios. See here: Which kind of interpolation best for resizing image?.