cv.imshow throws (-215:Assertion failed) src_depth != CV_16F && src_depth != CV_32S in function 'convertToShow'
Question:
In this code i am trying to integral image and every time i run this code
a window flash and desapear , then i get this error in terminal
import cv2
import numpy as np
image = cv2.imread("nancy.jpg")
(rows,cols,dims) = image.shape
sum = np.zeros((rows,cols), np.uint8)
imageIntegral = cv2.integral(image, sum, -1)
cv2.imshow("imageIntegral", imageIntegral)
cv2.waitKey()
Error:
cv2.imshow("imageIntegral",imageIntegral)cv2.error: OpenCV(4.1.0) C:/projects/opencv-python/opencv/modules/highgui/src/precomp.hpp:131:
error: (-215:Assertion failed) src_depth != CV_16F && src_depth !=
CV_32S in function ‘convertToShow’
Answers:
Help on cv2.integral:
>>> import cv2
>>> print(cv2.__version__)
4.0.1-dev
>>> help(cv2.integral)
Help on built-in function integral:
integral(...)
integral(src[, sum[, sdepth]]) -> sum
. @overload
A simple demo:
import numpy as np
import cv2
img = np.uint8(np.random.random((2,2,3))*255)
dst = cv2.integral(img)
>>> print(img.shape, img.dtype)
(2, 2, 3) uint8
>>> print(dst.shape, dst.dtype)
(3, 3, 3) int32
And you shouldn’t use imshow directly on the dst image because it’s not np.uint8. Normalize it to np.uint8
(range 0 to 255) or np.float32
(range 0.0 to 1.0). You can find the reason at this link: How to use `cv2.imshow` correctly for the float image returned by `cv2.distanceTransform`?
Check whether your image is uint8 or not
image = image.astype(np.uint8)
cv.imshow
requires the given image to have dtype from the set of np.uint8
, np.uint16
, np.float32
, np.float64
. It does not accept other types.
Your integral image has type np.int32
. The error message explicitly rejected your data because it was np.int32
(or half-float, CV_16F, but that’s not the case here).
So, you need to convert it to a dtype
that is acceptable to imshow
. Before converting with .astype(...)
, you must make sure that your value range is mapped into the range of 8-bit integers (0 to 255), or floats (imshow expects 0.0 to 1.0). Normalize your data using the maximum, like this:
imageIntegral = cv.integral(src=image)
# division results in values ranging from 0.0 to 1.0
# type is floating point array (float64)
presentable = imageIntegral / imageIntegral.max()
cv.imshow("imageIntegral", presentable)
cv.waitKey()
cv.destroyWindow("imageIntegral")
input, from OpenCV documentation:
presentable
:
You obviously don’t see much in an integral image because by its nature it’s mostly a gradient. That is what happens when one integrates a series of values chosen from a range. That is also why imageIntegral.astype(np.uint8)
results in noise:
In this code i am trying to integral image and every time i run this code
a window flash and desapear , then i get this error in terminal
import cv2
import numpy as np
image = cv2.imread("nancy.jpg")
(rows,cols,dims) = image.shape
sum = np.zeros((rows,cols), np.uint8)
imageIntegral = cv2.integral(image, sum, -1)
cv2.imshow("imageIntegral", imageIntegral)
cv2.waitKey()
Error:
cv2.imshow("imageIntegral",imageIntegral)cv2.error: OpenCV(4.1.0) C:/projects/opencv-python/opencv/modules/highgui/src/precomp.hpp:131:
error: (-215:Assertion failed) src_depth != CV_16F && src_depth !=
CV_32S in function ‘convertToShow’
Help on cv2.integral:
>>> import cv2
>>> print(cv2.__version__)
4.0.1-dev
>>> help(cv2.integral)
Help on built-in function integral:
integral(...)
integral(src[, sum[, sdepth]]) -> sum
. @overload
A simple demo:
import numpy as np
import cv2
img = np.uint8(np.random.random((2,2,3))*255)
dst = cv2.integral(img)
>>> print(img.shape, img.dtype)
(2, 2, 3) uint8
>>> print(dst.shape, dst.dtype)
(3, 3, 3) int32
And you shouldn’t use imshow directly on the dst image because it’s not np.uint8. Normalize it to np.uint8
(range 0 to 255) or np.float32
(range 0.0 to 1.0). You can find the reason at this link: How to use `cv2.imshow` correctly for the float image returned by `cv2.distanceTransform`?
Check whether your image is uint8 or not
image = image.astype(np.uint8)
cv.imshow
requires the given image to have dtype from the set of np.uint8
, np.uint16
, np.float32
, np.float64
. It does not accept other types.
Your integral image has type np.int32
. The error message explicitly rejected your data because it was np.int32
(or half-float, CV_16F, but that’s not the case here).
So, you need to convert it to a dtype
that is acceptable to imshow
. Before converting with .astype(...)
, you must make sure that your value range is mapped into the range of 8-bit integers (0 to 255), or floats (imshow expects 0.0 to 1.0). Normalize your data using the maximum, like this:
imageIntegral = cv.integral(src=image)
# division results in values ranging from 0.0 to 1.0
# type is floating point array (float64)
presentable = imageIntegral / imageIntegral.max()
cv.imshow("imageIntegral", presentable)
cv.waitKey()
cv.destroyWindow("imageIntegral")
input, from OpenCV documentation:
presentable
:
You obviously don’t see much in an integral image because by its nature it’s mostly a gradient. That is what happens when one integrates a series of values chosen from a range. That is also why imageIntegral.astype(np.uint8)
results in noise: