[英]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-如-
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.