简体   繁体   English

为什么 cv2.resize() 会扭曲我的图像?

[英]Why is cv2.resize() distorting my images?

I have the following image:我有以下图片:

Original Image原图

I am using the following code to resize this image to 1600x1200.我正在使用以下代码将此图像调整为 1600x1200。

img = cv2.imread('R.png')
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray_image.resize(1600,1200)

I am then returned the following image:然后我返回以下图像:

Final Image最终图像

I have tried to fix this by using different image formats (jpg, tif), but this does not seem to help.我试图通过使用不同的图像格式(jpg、tif)来解决这个问题,但这似乎没有帮助。 I also tried using different interpolation algorithms like INTER_NEAREST and INTER_LINEAR, and these produce the same results.我还尝试使用不同的插值算法,如 INTER_NEAREST 和 INTER_LINEAR,它们产生相同的结果。

Does anyone have an idea?有人有想法吗?

You are calling the resize() function on the numpy array that represents the grayscale image, which only changes the shape of the array.您在表示灰度图像的 numpy 数组上调用resize() function,这只会更改数组的形状。 You should use the resize() function from OpenCV:您应该使用 OpenCV 中的resize() function:

img = cv2.imread('R.png')
resized_image = cv2.resize(img, (1600, 1200), interpolation = cv2.INTER_LINEAR)

Besides of that, I think you have mistakenly swapped the width and height of the image, it should be 1200 x 1200 to keep the scale.除此之外,我认为你错误地交换了图像的宽度和高度,它应该是 1200 x 1200 以保持比例。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM