3

I'm a newbei at Data Visualization, Machine Learning and Deep Learning topics. I'm trying to improve myself, but I stucked a topic when I try to implement a visualization.

In details, I tried to implement a kernel on 'Kaggle', my data is Cat and Dog images. I have train and test datas. The problem is that my train data has too many pictures that are different size, pixels and shapes. I want to make all of them in a 1 shape type.(For example, I want to make all of the pictures as 64x64 pixels, or 128x128 etc.)

""" I tried different codes to adjust its shapes and create plot:
I tried to reach 2 goals:
1. Convert the dog and cat images from RGB to Grayscale
2. Make all of the images 128x128, or 64x64 """


# One of the codes I've tried
img_size = 128
basewidth = 128
for image in tqdm(os.listdir(train_cat)): 
    path = os.path.join(train_cat, image)
    img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
    try:
       wpercent = (basewidth / float(img.size[0]))
       hsize = int((float(img.size[1]) * float(wpercent)))
       img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
    except:
        pass
    np_img=np.asarray(img)

for image2 in tqdm(os.listdir(train_dog)): 
    path = os.path.join(train_dog, image2)
    img2 = cv2.imread(path, cv2.IMREAD_GRAYSCALE) 
    try:
        wpercent = (basewidth / float(img2.size[0]))
        hsize = int((float(img2.size[1]) * float(wpercent)))
        img2 = img2.resize((basewidth, hsize), PIL.Image.ANTIALIAS)

    except:
        pass
    np_img2=np.asarray(img2) 

plt.figure(figsize=(10,10))
plt.subplot(1, 2, 1)
plt.imshow(np_img.reshape(img_size, img_size))
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(np_img2.reshape(img_size, img_size))
plt.axis('off')


# ---------------------------------------------------------------------
# Another way:

image_size = 128
for image in tqdm(os.listdir(train_dog)): 
    path = os.path.join(train_dog, image)
    img = cv2.imread(path, cv2.IMREAD_GRAYSCALE) 
    img = cv2.resize(img, (image_size, image_size)).flatten()   
    np_img=np.asarray(img)

for image2 in tqdm(os.listdir(train_cat)): 
    path = os.path.join(train_cat, image2)
    img2 = cv2.imread(path, cv2.IMREAD_GRAYSCALE) 
    img2 = cv2.resize(img2, (image_size, image_size)).flatten() 
    np_img2=np.asarray(img2)

plt.figure(figsize=(10,10))
plt.subplot(1, 2, 1)
plt.imshow(np_img.reshape(image_size, image_size))
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(np_img2.reshape(image_size, image_size))
plt.axis('off')
plt.title("Cat and Dogs in GrayScale")

Output for first one

ValueError         Traceback (most recent call last)
<ipython-input-47-0984b7467972> in <module>
     28 plt.figure(figsize=(10,10))
     29 plt.subplot(1, 2, 1)
---> 30 plt.imshow(np_img.reshape(img_size, img_size))
     31 plt.axis('off')
     32 plt.subplot(1, 2, 2)


ValueError: cannot reshape array of size 76964 into shape (300,300)

Output for second one

error    Traceback (most recent call last)
<ipython-input-67-99d190c6fd41> in <module>
      4     path = os.path.join(train_dog, image)
      5     img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
----> 6     img = cv2.resize(img, (image_size, image_size)).flatten()
      7     np_img=np.asarray(img)
      8 

error: OpenCV(4.1.0) /io/opencv/modules/imgproc/src/resize.cpp:3718:     error: (-215:Assertion failed) !ssize.empty() in function 'resize'

As I mentioned above, I tried to make a convertion from RGB to Grayscale, and make the images' size 1 type(128x128 optionally). I believed there is no mistake to convertion but resize is a problem that I can't solve for 2-3 days, eventhough I made tons of researches. I'm not sure but it could be a simple problem, like a said, I'm a beginner for now.

Note: I added 2 way of codes because there are the codes I understood best. I hope, you can help me, or teach me a new way, thank you in advance :)

#Appendixes:

Cat and Dog Dataset: https://www.kaggle.com/tongpython/cat-and-dog

The resizes and grayscale codes I've tried to implement are taken from: (In[5])

Base kaggle I'm saw in order to data that has images (In2)

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • Welcome to SO; this is a standard image processing question, and it has nothing to do with `deep-learning`, `classification`, or `logistic-regression` - kindly do not spam irrelevant tags (removed & replaced with `opencv` and `python-imaging-library`). – desertnaut Sep 14 '19 at 12:43

2 Answers2

2

You should avoid mixing OpenCV and PIL when performing image processing. One reason is because OpenCV uses BGR format while PIL uses RGB format. Choose one library and stick with it. To convert an image to grayscale with OpenCV, you can use cv2.cvtColor() with the cv2.COLOR_BGR2GRAY or cv2.COLOR_RGB2GRAY flag

image = cv2.imread('image.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

or just pass in a flag when reading in the image

image = cv2.imread('image.png', 0) # OR
# image = cv2.imread('image.png', cv2.IMREAD_GRAYSCALE)

To resize, you can simply use cv2.resize()

resized = cv2.resize(image, (64, 64)) # OR
# resized = cv2.resize(image, (128, 128)) 

Note, cv2.resize() does not maintain aspect ratio. If you want to maintain aspect ratio, look at imutils.resize(). You may not get the exact shape dimensions though

resized = imutils.resize(image, width=64) # OR
# resized = imutils.resize(image, width=128)
nathancy
  • 42,661
  • 14
  • 115
  • 137
1

You could use skimage for image resizing and gray/rgb conversion as well.

Image Resizing

skimage.transform.resize Docs

from skimage.transform import resize
# resize your grayscale image to 128x128
resized_image = resize(gray_image, (128,128))

Grayscale Conversion

skimage.color.rgb2gray Docs

from skimage.color import rgb2gray
gray_image = rgb2gray(color_image)

Additional Information

  1. Skimage - Weird results of resize function
  2. scipy.misc.imresize
CypherX
  • 7,019
  • 3
  • 25
  • 37