Reputation: 31
I have this image,
in which I want to perform gradient calculation using sobel filter:
kx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], np.float32)
ky = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]], np.float32)
ix = ndimage.filters.convolve(img, kx)
iy = ndimage.filters.convolve(img, ky)
g = np.hypot(ix, iy)
g = g / g.max() * 255
theta = np.arctan2(iy, ix)
I want to plot the g
value into a histogram to figure out the range of the intensity of the gradient in the image. When I try histr = cv2.calcHist([g], [0], None, [256], [0, 256])
, it gives me the following error:
TypeError: images data type = 23 is not supported
I wanted to know, how I can plot the intensity of the gradients in a histogram to figure out the range.
Upvotes: 2
Views: 284
Reputation: 18905
As the error message indicates, the type of your g
seems to be unsupported. Let's have a look at the documentation of cv2.calcHist
:
images Source arrays. They all should have the same depth, CV_8U, CV_16U or CV_32F, and the same size. Each of them can have an arbitrary number of channels.
Running your code as is, g
is of type np.float16
. So, all of the following corrections work:
histr = cv2.calcHist([g.astype(np.uint8)], [0], None, [256], [0, 256])
histr = cv2.calcHist([g.astype(np.uint16)], [0], None, [256], [0, 256])
histr = cv2.calcHist([g.astype(np.float32)], [0], None, [256], [0, 256])
Just pick one that best fits your needs.
Hope that helps!
Upvotes: 2