Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

integral image error code cv2.imshow("imageIntegral",imageIntegral)

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'

like image 529
N.Elsayed Avatar asked Apr 19 '19 09:04

N.Elsayed


People also ask

How do I find the integral of an image in OpenCV?

OpenCV also provides a function that returns the integral image of both the input image and its square. This can be done by the following function. Here, sqdepth is the depth of the integral of the squared image (must be of type CV_32F, or CV_64F).

How do you find the integral of an image?

Suppose an image is w pixels wide and h pixels high. Then the integral of this will be w+1 pixels wide and h+1 pixels high. The first row and column of the integral image are all zeros. All other pixels have a value equal to the sum of all pixels before it. See the integral in the above image?

How to implement CV2 imshow () in Python?

Steps to Implement cv2.imshow() in python Step 1: Import all necessary libraries. In this entire tutorial, I am using two main python modules. One is NumPy for black and white image creation. And the other is the OpenCV(cv2) module. Let’s import them. import numpy as np import cv2 Step 2: Examples on cv2.imshow() method

What is the use of integral image in image processing?

An integral image lets you calculate summations over image subregions. Rapidly.These summations are useful in many applications, like calculating HAAR wavelets. These are used in face recognition and other similar algorithms. Suppose an image is w pixels wide and h pixels high. Then the integral of this will be w+1 pixels wide and h+1 pixels high.


2 Answers

Check whether your image is uint8 or not

image = image.astype(np.uint8)
like image 74
vishal Avatar answered Oct 11 '22 19:10

vishal


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`?

like image 10
Kinght 金 Avatar answered Oct 11 '22 20:10

Kinght 金