OPENCV调整大小不同于我自己的手动计算

发布于 2025-01-26 09:42:24 字数 773 浏览 2 评论 0原文

图像数组

[[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

客…行舟 2025-02-02 09:42:24

调整图像大小时,有几种插值方法。您可以使用interpolation cv2.resize的参数选择它。
此方法确定如何根据旧像素计算新像素的值。

与您手动实现的方法相似的方法是cv.inter_nearest。对于每个目标像素,它将选择最接近其的源像素并简单地复制其值,结果将在您的“手册”

img2 = cv2.resize(img, (h//2, w//2), interpolation=cv2.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 of cv2.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:

img2 = cv2.resize(img, (h//2, w//2), interpolation=cv2.INTER_NEAREST)

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) is cv2.INTER_LINEAR (not cv2.INTER_NEAREST). This explains your result. You can set the interpolation 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?.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文