简体   繁体   中英

Difficulty blurring python image

I'm having trouble with blurring an image. I am trying to use the Gaussian blur filter from skimage. The image I am working with was downloaded to the environment from a url. Below is the code I have.

import numpy as np
import matplotlib.pyplot as plt
import scipy.ndimage as ndi
import skimage
import skimage.transform
from skimage import io
from skimage import color
from skimage import data
from skimage import filters
from skimage import img_as_float


url = 'https://github.com/jagar2/Summer_2020_MAT-395-495_Scientific-Data-Analysis-and-Computing/blob/master/homeworks/HW2/Zebra.jpg?raw=true'

zebra = io.imread(url)

print(type(zebra))
print(zebra.dtype)
print(zebra.shape)
print(zebra.min(), zebra.max())

plt.imshow(zebra)

# make a copy of the original 
zebra_copy = np.copy(zebra)

# convert image to grayscale
from skimage.color import rgb2gray
     
zebra_gray = rgb2gray(zebra_copy)    
grayscale = plt.imshow(zebra_gray, cmap=plt.get_cmap('gray'), vmin=0, vmax=1)
plt.show(grayscale)

All of the above code runs as expected. Below is the code I am running to blur the image and I also get the error below.

blur = filters.gaussian(grayscale, sigma=1)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-27-7533f0f238e2> in <module>()
----> 1 blur = filters.gaussian(grayscale, sigma=1)

1 frames
/usr/local/lib/python3.6/dist-packages/skimage/filters/_gaussian.py in _guess_spatial_dimensions(image)
    137         If the image array has less than two or more than four dimensions.
    138     """
--> 139     if image.ndim == 2:
    140         return 2
    141     if image.ndim == 3 and image.shape[-1] != 3:

AttributeError: 'AxesImage' object has no attribute 'ndim'

Any tips on how to fix this to get the blurred image is greatly appreciated.

The error is probably due to the conversion to grayscale. Normally, all sRGB images have 3 color channels that have intensity values for each color of a pixel. The number of color channels is what ndim is referring to. When the image is converted to grayscale, the color channel dimension is removed as there is need for only 1 color channel. Try not converting the image to grayscale and see what happens.

If you really need the image to be in grayscale, then convert the grayscale image back to RGB(the color will not come back as the grayscale image does not contain that information).

According to documentation , filters.gaussian takes array-like (eg numpy.ndarray ) as input.

However, in

blur = filters.gaussian(grayscale, sigma=1)

grayscale is matplotlib.image.AxesImage .

If you pass numpy.ndarray , it should work.

eg

blur = filters.gaussian(zebra_gray, sigma=1)

Try:

In[1]: type(zebra_gray)
Out[1]: numpy.ndarray

In[2]: type(grayscale)
Out[2]: matplotlib.image.AxesImage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM