python-3.x - 在 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() 方法有什么问题吗?我花了很多时间检查我无法弄清楚的错误。谁能告诉我如何纠正这个问题?
谢谢!
解决方案
推荐阅读
- javascript - 在 Flask Python 中使用 Bootstarp 模式添加一个完整的日历事件
- jenkins - Jenkins 的存储无法与 Dockerfile 代理一起运行
- java - 如何更改 OpenXava 应用程序的语言?
- kotlin - 为什么我需要泛型中的 in 修饰符以使其接受超类?
- python-3.x - 将 .CSV 数据转换为 NER 的 CoNLL BIO 格式
- python - 如何用列表而不是浮点对象制作字典
- azure - 无法对打开了用户映射部分的文件执行请求的操作
- javascript - 用js改变全局css
- mongodb - 如何在 MongoDB 中保存嵌套对象
- typescript - !. 打字稿操作员理解