将图像转换为二进制后无法使用 matplotlib 在笔记本中显示
After converting image to binary cannot display it in notebook with matplotlib
我正在尝试显示图像 在 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()
returns两个值。第一个是阈值(float),第二个是图像。
因此,您一直在尝试绘制 float
值,因此出现 值错误。
将该行重写为以下内容:
ret, resized_img = cv2.threshold(resized_img, 100, 200, cv2.THRESH_BINARY)
你也可以看看at the documentation
我正在尝试显示图像 在 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()
returns两个值。第一个是阈值(float),第二个是图像。
因此,您一直在尝试绘制 float
值,因此出现 值错误。
将该行重写为以下内容:
ret, resized_img = cv2.threshold(resized_img, 100, 200, cv2.THRESH_BINARY)
你也可以看看at the documentation