0

I am trying to display the image After coverting the image to binary in python notebook:

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()

I cannot show image. I get this error:

---------------------------------------------------------------------------
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.

Before using threshold I can see the image inside the notebook without problem. Any way to resolve this problem? Thanks.

1 Answer 1

1

The error lies in line resized_img = cv2.threshold(resized_img, 100, 200, cv2.THRESH_BINARY).

cv2.threshold() returns two values. The first is the threshold value (float) and the second is the image.

So all the while you have been trying to plot a float value, hence the Value error.

Rewrite the line to the following:

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

You can have a look at the documentation as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.