简体   繁体   English

使用 cv2 调整图像大小

[英]Resizing image with cv2

I'm trying resize images retrieved from cifar10 in the original 32x32 to 96x96 for use with MobileNetV2, howevery I'm running into this error.我正在尝试将原始 32x32 中从 cifar10 检索到的图像大小调整为 96x96,以便与 MobileNetV2 一起使用,但是我遇到了这个错误。 Tried a variety of solutions but nothing seems to work.尝试了各种解决方案,但似乎没有任何效果。

My code:我的代码:

for a in range(len(train_images)):
    train_images[a] = cv2.resize(train_images[a], dsize=(minSize, minSize), interpolation=cv2.INTER_CUBIC)

Error I'm getting:我得到的错误:

----> 8     train_images[a] = cv2.resize(train_images[a], dsize=(minSize, minSize), interpolation=cv2.INTER_CUBIC)
ValueError: could not broadcast input array from shape (96,96,3) into shape (32,32,3)

Sometimes you have to convert the image from RGB to grayscale.有时您必须将图像从 RGB 转换为灰度。 If that is the problem, the only thing you should do is gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) , resize the image and then again resized_image = cv2.cvtColor(gray_image, cv2.COLOR_GRAY2RGB)如果这是问题所在,您唯一应该做的就是gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)调整图像大小,然后再次resized_image = cv2.cvtColor(gray_image, cv2.COLOR_GRAY2RGB)

I have never run into this error but if the first option doesn't work, you can try and resize image with pillow like this:我从来没有遇到过这个错误,但是如果第一个选项不起作用,您可以尝试使用枕头调整图像大小,如下所示:

from PIL import Image

im = Image.fromarray(cv2_image)
nx, ny = im.size
im2 = im.resize((nx*2, ny*2), Image.LANCZOS)
cv2_image = cv2.cvtColor(numpy.array(im2), cv2.COLOR_RGB2BGR)

You can make this into a function and call it in the list comprehension.你可以把它变成一个函数并在列表理解中调用它。 I hope this solves your problem :)我希望这能解决你的问题:)

This is simply because you are reading the 32x32 image from train_images and trying to save the reshaped image (96x96) in the same array which is impossible!这仅仅是因为您正在从 train_images 读取 32x32 图像并尝试将重塑后的图像 (96x96) 保存在同一个数组中,这是不可能的! Try something like:尝试类似:

train_images_reshaped = np.array((num_images, 96, 96, 3))
for a in range(len(train_images)):
    train_images_reshaped[a] = cv2.resize(train_images[a], dsize=(minSize, minSize), interpolation=cv2.INTER_CUBIC)

There are some interpolation algorithms in OpenCV. OpenCV 中有一些插值算法。 Such as-如-

  • INTER_NEAREST – a nearest-neighbor interpolation INTER_NEAREST – 最近邻插值
  • INTER_LINEAR – a bilinear interpolation (used by default) INTER_LINEAR – 双线性插值(默认使用)
  • INTER_AREA – resampling using pixel area relation. INTER_AREA – 使用像素区域关系重新采样。 It may be a preferred method for image decimation, as it gives moire'-free results.它可能是图像抽取的首选方法,因为它提供无摩尔纹的结果。 But when the image is zoomed, it is similar to the INTER_NEAREST method.但是当图像放大时,它类似于 INTER_NEAREST 方法。
  • INTER_CUBIC – a bicubic interpolation over 4×4 pixel neighborhood INTER_CUBIC – 4×4 像素邻域上的双三次插值
  • INTER_LANCZOS4 – a Lanczos interpolation over 8×8 pixel neighborhood INTER_LANCZOS4 – 8×8 像素邻域上的 Lanczos 插值

Code:代码:

image_scaled=cv2.resize(image,None,fx=.75,fy=.75,interpolation = cv2.INTER_LINEAR)
img_double=cv2.resize(image,None,fx=2,fy=2,interpolation=cv2.INTER_CUBIC)
image_resize=cv2.resize(image,(200,300),interpolation=cv2.INTER_AREA)
image_resize=cv2.resize(image,(500,400),interpolation=cv2.INTER_LANCZOS4)

You can find the details about python implementation here as well: How to resize images in OpenCV python您也可以在此处找到有关python 实现的详细信息: 如何在 OpenCV python 中调整图像大小

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

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