10

I am building an image processing classifier. This line is giving me an error:

input_img_resize=cv2.resize(input_img,(128,128))

The 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)
Salvatore
  • 10,815
  • 4
  • 31
  • 69
Dexter
  • 630
  • 1
  • 11
  • 24
  • After `input_img=cv2.imread(data_path + '/'+ dataset + '/'+ img )` please confirm if the image was loaded properly, using `if input_img is not None:` – ZdaR Nov 08 '17 at 06:35
  • 1
    yeah you are right now what should I do now ? – Dexter Nov 08 '17 at 06:41
  • 2
    You should make sure that the path is correct, try printing the path and then use `ls -la` in terminal to check if the path exists and also make sure you are trying to read a image extension supported by the `OpenCV` such as `png`, `jpg`, `jpeg`, `bmp`, etc. Also use `os.path.join(data_path, dataset, img)` instead of `cv2.imread(data_path + '/'+ dataset + '/'+ img)`. – ZdaR Nov 08 '17 at 07:21

7 Answers7

15

Well, obviously this line input_img=cv2.imread(data_path + '/'+ dataset + '/'+ img ) returns an empty array.

You should check whether the image exists first before reading. And it is better not to use string combination to join file paths, use python os.path.join instead.

image_path = os.path.join(data_path, dataset, img)
if os.path.exist():
    # Do stuff
Salvatore
  • 10,815
  • 4
  • 31
  • 69
Vu Gia Truong
  • 1,022
  • 6
  • 14
3

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.

Marcello B.
  • 4,177
  • 11
  • 45
  • 65
T.Antoni
  • 31
  • 2
1

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:

if input_img.shape[0]!=0 and input_img.shape[1]!=0:
    #operations on input_img
hafiz031
  • 2,236
  • 3
  • 26
  • 48
1

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.

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.

sammens19
  • 161
  • 2
  • 4
0

You are facing this problem because the image might not have been read properly while scanning. So do make sure tat image is loaded.

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: enter image description here
Hope this helps :)

Ashwin Dhakal
  • 31
  • 1
  • 5
0

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.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
anonymous
  • 1
  • 1
0

For this error, you can simply use this code. It definitely runs and resolves your problem. I was also facing this error and fixed it with this code.

img = cv2.imread(path + '\\' + img_name)
Raguel
  • 595
  • 8
  • 25