gpt4 book ai didi

python - 将图像转换为二进制文件后,无法在带有matplotlib的笔记本中显示图像

转载 作者:行者123 更新时间:2023-12-02 17:34:25 25 4
gpt4 key购买 nike

我在python笔记本中将图像覆盖为二进制后试图显示图像:

resized_img = cv2.cvtColor(char_mask, cv2.COLOR_BGR2GRAY)
resized_img = cv2.threshold(resized_img, 100, 200, cv2.THRESH_BINARY)
#cv2.imwrite('licence_plate_mask3.png', char_mask)
plt.imshow(resized_img)

plt.show()

我无法显示图像。我收到此错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-32-0a3eb57cc497> in <module>()
8 resized_img = cv2.threshold(resized_img, 100, 200, cv2.THRESH_BINARY)
9 #cv2.imwrite('licence_plate_mask3.png', char_mask)
---> 10 plt.imshow(resized_img)
11
12 plt.show()

C:\Anaconda3\lib\site-packages\matplotlib\pyplot.py in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, hold, data, **kwargs)
3155 filternorm=filternorm, filterrad=filterrad,
3156 imlim=imlim, resample=resample, url=url, data=data,
-> 3157 **kwargs)
3158 finally:
3159 ax._hold = washold

C:\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, *args, **kwargs)
1895 warnings.warn(msg % (label_namer, func.__name__),
1896 RuntimeWarning, stacklevel=2)
-> 1897 return func(ax, *args, **kwargs)
1898 pre_doc = inner.__doc__
1899 if pre_doc is None:

C:\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
5122 resample=resample, **kwargs)
5123
-> 5124 im.set_data(X)
5125 im.set_alpha(alpha)
5126 if im.get_clip_path() is None:

C:\Anaconda3\lib\site-packages\matplotlib\image.py in set_data(self, A)
590 self._A = pil_to_array(A)
591 else:
--> 592 self._A = cbook.safe_masked_invalid(A, copy=True)
593
594 if (self._A.dtype != np.uint8 and

C:\Anaconda3\lib\site-packages\matplotlib\cbook.py in safe_masked_invalid(x, copy)
1504
1505 def safe_masked_invalid(x, copy=False):
-> 1506 x = np.array(x, subok=True, copy=copy)
1507 if not x.dtype.isnative:
1508 # Note that the argument to `byteswap` is 'inplace',

ValueError: setting an array element with a sequence.

在使用阈值之前,我可以看到笔记本中的图像没有问题。有什么办法解决这个问题?谢谢。

最佳答案

错误在于resized_img = cv2.threshold(resized_img, 100, 200, cv2.THRESH_BINARY)行。
cv2.threshold()返回两个值。第一个是阈值(浮点),第二个是图像。

因此,一直以来,您一直在尝试绘制float值,因此会出现Value错误。

将该行重写为以下内容:

ret, resized_img = cv2.threshold(resized_img, 100, 200, cv2.THRESH_BINARY)

您也可以看看 at the documentation

关于python - 将图像转换为二进制文件后,无法在带有matplotlib的笔记本中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52776723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com