简体   繁体   中英

Plotting a histogram to figure out maximum intensity of gradients on an image

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM