Python 错误:(-215) ssize.width > 0 && ssize.height > 0 in function resize
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47172770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: (-215) ssize.width > 0 && ssize.height > 0 in function resize
提问by Deepak Umredkar
I am building image processing classifier the whole code in right except this line -
我正在构建图像处理分类器的整个代码,除了这一行 -
input_img_resize=cv2.resize(input_img,(128,128))
input_img_resize=cv2.resize(input_img,(128,128))
This line givin me an error
这一行给我一个错误
('error: /io/opencv/modules/imgproc/src/imgwarp.cpp:3483: error: (-215) ssize.width > 0 && ssize.height > 0 in function resize')
My code -
我的代码 -
PATH = os.getcwd()
# Define data path
data_path = PATH + '/data'
data_dir_list = os.listdir(data_path)
img_rows=128
img_cols=128
num_channel=3
num_epoch=30
num_classes = 67
img_data_list=[]
for dataset in data_dir_list:
img_list=os.listdir(data_path+'/'+ dataset)
print ('Loaded the images of dataset-'+'{}\n'.format(dataset))
for img in img_list:
input_img=cv2.imread(data_path + '/'+ dataset + '/'+ img )
input_img_resize=cv2.resize(input_img,(128,128))
img_data_list.append(input_img_resize)
回答by Vu Gia Truong
Well, obviously this line
input_img=cv2.imread(data_path + '/'+ dataset + '/'+ img )
return a empty array.
好吧,显然这一行
input_img=cv2.imread(data_path + '/'+ dataset + '/'+ img )
返回一个空数组。
You should check whether image is exist first before reading. And please stop using string combination to join file path, use python os.path.join is better choice.
在阅读之前,您应该先检查图像是否存在。并且请停止使用字符串组合来连接文件路径,使用 python os.path.join 是更好的选择。
image_path = os.path.join(data_path, dataset, img)
if os.path.exist():
# Do stuff
回答by T.Antoni
It is because of one image.
这是因为一张图片。
To find the image I added a line of code that prints the name of the image before it enters the cv2.resize
and another line that prints the name after it is resized. It will automatically stop at the image with fault.
为了找到图像,我添加了一行代码,在图像进入之前打印图像的名称,cv2.resize
另一行在调整大小后打印名称。它会自动停在有故障的图像处。
回答by hafiz031
Make sure the input_img's resolution is not zero, i.e, (0,0,number_of_channels) which means it is not finding any image. So add the following checking before performing operations on it:
确保 input_img 的分辨率不为零,即 (0,0,number_of_channels) 这意味着它没有找到任何图像。所以在对其进行操作之前添加以下检查:
if input_img.shape[0]!=0 and input_img.shape[1]!=0:
#operations on input_img
回答by sammens19
If you are working with several images (for example, 1000 images), it may be difficult for you to identify which image is giving you problems. It may also be that, there are several others that are corrupt. In such a case, the code below can be useful.
如果您正在处理多个图像(例如,1000 个图像),您可能很难确定哪个图像给您带来了问题。也可能是,还有其他几个腐败。在这种情况下,下面的代码可能很有用。
for file in filenames:
try:
image = cv2.resize(cv2.imread(file), (size, size))
except:
print(file)
where filenames
is a list which contains names of the images.
其中filenames
是一个包含图像名称的列表。
回答by Ashwin Dhakal
You are facing this problem because the image might not have been read properly while scanning. So do make sure tat image is loaded.
您遇到此问题是因为扫描时可能没有正确读取图像。所以一定要确保加载了 tat 图像。
if input_img is not None:
img = cv2.resize(input_img, (IMG_SIZE,IMG_SIZE))
training_data.append([np.array(img), np.array(label)])
else:
print("image not loaded")
This skips the current image and then continue the scan. This breaks into two segments results as follows:
Hope this helps :)
回答by anonymous
This is because the region of image is not properly identified. Here's one way you can try:
这是因为未正确识别图像区域。您可以尝试以下一种方法:
gray = cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY)
r = cv2.selectROI(gray)
cv2.selectROI()
This will let you to manually select the region of image in each pic if its not detected.
如果未检测到,这将让您手动选择每张图片中的图像区域。