简体   繁体   English

python图像难以模糊

[英]Difficulty blurring python image

I'm having trouble with blurring an image.我在模糊图像时遇到问题。 I am trying to use the Gaussian blur filter from skimage.我正在尝试使用 skimage 中的高斯模糊滤镜。 The image I am working with was downloaded to the environment from a url.我正在使用的图像是从 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.通常,所有 sRGB 图像都有 3 个颜色通道,它们具有每个像素颜色的强度值。 The number of color channels is what ndim is referring to.颜色通道的数量是 ndim 所指的。 When the image is converted to grayscale, the color channel dimension is removed as there is need for only 1 color channel.当图像转换为灰度时,颜色通道维度被移除,因为只需要 1 个颜色通道。 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).如果您确实需要图像为灰度,则将灰度图像转换回 RGB(由于灰度图像不包含该信息,因此颜色不会返回)。

According to documentation , filters.gaussian takes array-like (eg numpy.ndarray ) as input.根据文档filters.gaussian将类数组(例如numpy.ndarray )作为输入。

However, in然而,在

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

grayscale is matplotlib.image.AxesImage . grayscalematplotlib.image.AxesImage

If you pass numpy.ndarray , it should work.如果您通过numpy.ndarray ,它应该可以工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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