4
$\begingroup$

I was using PIL library to open an image and then convert it into array later on for DL operations. However I found out that image opened was blurry as opposed to the original image. Why is the image coming out the way it is? and what can be done to fix it? If a possible solution is present in OpenCV that'd be welcome as well.

I tried to use OpenCV as well but I was getting an Assertion Error i.e

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

Code:

from PIL import Image
import numpy as np
import pandas as pd
import os
import cv2
import keras
from keras.models import Sequential
from keras.layers import Conv2D,MaxPooling2D,Dense,Flatten,Dropout
import matplotlib.pyplot as plt

image=Image.open("../content/cell_images/Parasitized/"+i)
size_image = image.resize((50, 50))
print("image")
plot(image,count)
print("resize image")
plot(size_image,count)

Here is the code for plotting:

def plot(img, count):
    w=10
    h=10
    fig=plt.figure(figsize=(8, 8))
    img = np.random.randint(10, size=(h,w))
    fig.add_subplot(rows, columns, count)
    plt.imshow(img)
    plt.show()

enter image description here enter image description here

| improve this question | | | | |
$\endgroup$
  • $\begingroup$ Is PIL's version current ? PIL changed default options for resize. $\endgroup$ – Shamit Verma Apr 4 '19 at 5:37
  • $\begingroup$ Are you redefining the img as a random numpy array before plotting? $\endgroup$ – Pedro Henrique Monforte Apr 4 '19 at 11:59
  • $\begingroup$ Nope, as you can see in code, I just read it from PIL read function and then directly plotted it. I am not converting it into array format $\endgroup$ – Arohan Ajit Apr 5 '19 at 6:09
1
$\begingroup$

Some parts of the code are missing that might contain clues (e.g. you plot method would only plot random noise, as it stands). What might be happening is that you are showing the image in matplotlib in a figure that is much smaller than the image, so matplotlib will automatically scale the image to the pixel space.

The plt.imshow() method can take an argument interpolation, which can produce dramatically different effects. Here are them all compared side-by-side:

interpolation methods

Have a look at the relevant documentation


I personally stick to using OpenCV and cv2.imread() to load images. You just have to be aware (in the context of using Tensorflow/Keras) that the channel are the first dimension. i.e. (Channels, Height, Width) - whereas e.g. plt.imread() will return (Height, Width, Channels).

| improve this answer | | | | |
$\endgroup$
0
$\begingroup$

Try specifying the resampling filter. Default filter (Nearest Neighbor) is fast but results are not as good.

image=Image.open("../content/cell_images/Parasitized/"+i)
size_image = image.resize((50, 50),PIL.Image.LANCZOS)

Documentation : https://pillow.readthedocs.io/en/latest/reference/Image.html https://pillow.readthedocs.io/en/latest/handbook/concepts.html#concept-filters

| improve this answer | | | | |
$\endgroup$
  • 1
    $\begingroup$ Still, Nearest Neighbor does not wield blurred image. It is actually causes a aliasing-like effect which can be solved by blurring $\endgroup$ – Pedro Henrique Monforte Apr 4 '19 at 11:56
  • $\begingroup$ Actually I was looking into various kernels for this dataset, and what they did was read it using cv2.imread and then converting it back into image format using image.from_array. I plotted that as well and still same results. $\endgroup$ – Arohan Ajit Apr 5 '19 at 6:11
  • $\begingroup$ For plotting, you are converting it to 10, 10 thumbnail. Is that intentional ? $\endgroup$ – Shamit Verma Apr 5 '19 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.