Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: mat data type = 0 is not supported

I'd like use cv2.imshow("Otsu img", binary) instead of plt.imshow( binary)

I got the error

Full code:

import matplotlib.pyplot as plt
from skimage import io
from skimage.filters.rank import entropy
from skimage.morphology import disk
import numpy as np
from skimage.filters import threshold_otsu
import cv2

img = io.imread("scratch.jpg")
entropy_img = entropy(img, disk(10))
thresh = threshold_otsu(entropy_img)

binary = entropy_img <= thresh



cv2.imshow("Otsu img", binary)

cv2.waitKey(0)
cv2.destroyAllWindows()

How to fix this error?

 cv2.imshow("Otsu img", binary)
TypeError: mat data type = 0 is not supported
like image 809
Redhwan Avatar asked Oct 21 '25 08:10

Redhwan


1 Answers

The TypeError can be rectified by converting binary into a dtype=uint8 using,

binary = np.asarray(binary, dtype="uint8")

or change the type of binary by using astype(np.uint8)

But upon further discussion between Original Poster @Redhwan, OP identified the problem and following script seemed to solve the issue:

import matplotlib.pyplot as plt
from skimage import io
from skimage.filters.rank import entropy
from skimage.morphology import disk
import numpy as np
from skimage.filters import threshold_otsu
import cv2

img = cv2.imread("scratch.jpg", 0)
entropy_img = entropy(img, disk(10))
# print type(entropy_img), entropy_img
thresh = threshold_otsu(entropy_img)
# print thresh
# binary = entropy_img <= thresh
ret1, th1 = cv2.threshold(entropy_img, thresh, 255, cv2.THRESH_BINARY_INV)
# print type(entropy)


cv2.imshow("Otsu img", img)
cv2.imshow("Otsu th2", th1)
# cv2.imshow("OTSU Gaussian cleaned", th3)
# cv2.imshow("OTSU median cleaned", th4)
cv2.waitKey(0)
cv2.destroyAllWindows()
like image 151
AshlinJP Avatar answered Oct 22 '25 21:10

AshlinJP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!