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")
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)
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)
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)
You could use skimage for image resizing and gray/rgb conversion as well.
skimage.transform.resize Docs
from skimage.transform import resize
# resize your grayscale image to 128x128
resized_image = resize(gray_image, (128,128))
skimage.color.rgb2gray Docs
from skimage.color import rgb2gray
gray_image = rgb2gray(color_image)
scipy.misc.imresizeIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With