首页 > 解决方案 > 在 Canny 边缘检测器中绘制梯度,在 python 中显示不支持的数据类型错误

问题描述

我的梯度计算代码如下:

from  skimage.feature import canny
import numpy as np


def Canny_detector(img):
     import cv2
     from scipy.ndimage.filters import convolve
     import math
     blur = cv2.GaussianBlur(img, (5, 5), 1.4)
     Kx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], np.float32)
     Ky = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]], np.float32)

     Ix = convolve(blur, Kx)
     Iy = convolve(blur, Ky)

     G = np.hypot(Ix, Iy)
     theta = np.arctan2(Iy, Ix)
     plt.imshow(G, cmap=plt.cm.gray)
     return

 canny_imgs = []
 for img in plates:
     Canny_detector(img)

这里的图板包含有关 6 个二维阵列(灰度)形式的图像的信息

执行上述代码时的错误是:

ValueError                                Traceback (most recent call last)
E:\Programs\anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    339                 pass
    340             else:
--> 341                 return printer(obj)
    342             # Finally look for special method names
    343             method = get_real_method(obj, self.print_method)

E:\Programs\anaconda3\lib\site-packages\IPython\core\pylabtools.py in <lambda>(fig)
    246 
    247     if 'png' in formats:
--> 248         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    249     if 'retina' in formats or 'png2x' in formats:
    250         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

E:\Programs\anaconda3\lib\site-packages\IPython\core\pylabtools.py in print_figure(fig, fmt, 
               bbox_inches, **kwargs)
    130         FigureCanvasBase(fig)
    131 
--> 132     fig.canvas.print_figure(bytes_io, **kw)
    133     data = bytes_io.getvalue()
    134     if fmt == 'svg':

E:\Programs\anaconda3\lib\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, 
    dpi, facecolor, edgecolor, orientation, format, bbox_inches, **kwargs)
      2098                            else suppress())
   2099                     with ctx:
-> 2100                         self.figure.draw(renderer)
  2101                     bbox_artists = kwargs.pop("bbox_extra_artists", None)
 2102                     bbox_inches = self.figure.get_tightbbox(renderer,

  E:\Programs\anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, 
  *args, **kwargs)
 36                 renderer.start_filter()
 37 
 ---> 38             return draw(artist, renderer, *args, **kwargs)
 39         finally:
 40             if artist.get_agg_filter() is not None:

 E:\Programs\anaconda3\lib\site-packages\matplotlib\figure.py in draw(self, renderer)
 1733 
 1734             self.patch.draw(renderer)
  -> 1735             mimage._draw_list_compositing_images(
 1736                 renderer, self, artists, self.suppressComposite)
 1737 

 E:\Programs\anaconda3\lib\site-packages\matplotlib\image.py in 
   _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
135     if not_composite or not has_images:
136         for a in artists:
 --> 137             a.draw(renderer)
138     else:
139         # Composite any adjacent images together

E:\Programs\anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, 
    **kwargs)
 36                 renderer.start_filter()
 37 
 ---> 38             return draw(artist, renderer, *args, **kwargs)
 39         finally:
 40             if artist.get_agg_filter() is not None:

 E:\Programs\anaconda3\lib\site-packages\matplotlib\axes\_base.py in draw(self, renderer, inframe)
   2628             renderer.stop_rasterizing()
     2629 
  -> 2630         mimage._draw_list_compositing_images(renderer, self, artists)
  2631 
 2632         renderer.close_group('axes')

  E:\Programs\anaconda3\lib\site-packages\matplotlib\image.py in 
  _draw_list_compositing_images(renderer, 
     parent, artists, suppress_composite)
135     if not_composite or not has_images:
136         for a in artists:
--> 137             a.draw(renderer)
138     else:
139         # Composite any adjacent images together

E:\Programs\anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, 
   **kwargs)
 36                 renderer.start_filter()
 37 
 ---> 38             return draw(artist, renderer, *args, **kwargs)
 39         finally:
 40             if artist.get_agg_filter() is not None:

E:\Programs\anaconda3\lib\site-packages\matplotlib\image.py in draw(self, renderer, *args, **kwargs)
623             self._draw_unsampled_image(renderer, gc)
624         else:
--> 625             im, l, b, trans = self.make_image(
626                 renderer, renderer.get_image_magnification())
627             if im is not None:

E:\Programs\anaconda3\lib\site-packages\matplotlib\image.py in make_image(self, renderer, 
  magnification, unsampled)
912         bbox = Bbox(np.array([[x1, y1], [x2, y2]]))
913         transformed_bbox = TransformedBbox(bbox, trans)
 --> 914         return self._make_image(
915             self._A, bbox, transformed_bbox,
916             self.get_clip_box() or self.axes.bbox,

E:\Programs\anaconda3\lib\site-packages\matplotlib\image.py in _make_image(self, A, in_bbox, 
      out_bbox, clip_bbox, magnification, unsampled, round_to_pixel_border)
476                 A_scaled += 0.1
477                 # resample the input data to the correct resolution and shape
 --> 478                 A_resampled = _resample(self, A_scaled, out_shape, t)
479                 # done with A_scaled now, remove from namespace to be sure!
480                 del A_scaled

E:\Programs\anaconda3\lib\site-packages\matplotlib\image.py in _resample(image_obj, data, out_shape, 
        transform, resample, alpha)
195     if resample is None:
196         resample = image_obj.get_resample()
 --> 197     _image.resample(data, out, transform,
198                     _interpd_[interpolation],
199                     resample,

ValueError: Unsupported dtype

<Figure size 432x288 with 1 Axes>

最后一个错误是不支持的数据类型。matplotlib imshow() 方法有什么问题吗?我花了很多时间检查我无法弄清楚的错误。谁能告诉我如何纠正这个问题?

谢谢!

标签: python-3.xopencvcomputer-vision

解决方案


推荐阅读