积分图像错误代码cv2.imshow(“ imageIntegral”,imageIntegral)

时间:2019-04-19 09:23:39

标签: python-2.7 opencv

在此代码中,我尝试对图像进行积分,每次运行此代码时 一个窗口闪烁并消失,然后在终端

中出现此错误
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()

错误:

  

cv2.imshow(“ imageIntegral”,imageIntegral)cv2.error:OpenCV(4.1.0)C:/projects/opencv-python/opencv/modules/highgui/src/precomp.hpp:131:

     

错误:(-215:断言失败)src_depth!= CV_16F && src_depth!=   函数“ convertToShow”中的CV_32S

2 个答案:

答案 0 :(得分:2)

关于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

一个简单的演示:

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

并且您不应该直接在dst图像上使用imshow,因为它不是np.uint8。将其标准化为np.uint8(0,255)或np.float32(0,1.0)。您可以在以下链接中找到原因:How to use `cv2.imshow` correctly for the float image returned by `cv2.distanceTransform`?

答案 1 :(得分:0)

检查图像是否为 uint8

image = image.astype(np.uint8)