Ask Your Question

# calcHist() float values in range param? Is the range param in calcHist() supposed to accept and correctly return counts for bins defined using negative float values? As an example, is the following a valid range definition for three dimensions?

range = [
0, 255,
-1 * np.pi, np.pi,
0.0, 255 * np.sqrt(2)
]

edit retag close merge delete

## Comments

1

I believe it is, at least in C++ API calcHist() works correctly with negative float values in range

## 1 answer

Sort by » oldest newest most voted strann was right. calcHist() does appear to work with negative float32 values, so something else must've been the problem in my code. The simple script below demonstrates calcHist() applied to floating point values in an array, which I tested using Python 2.7.6 and OpenCV 3.1.0 (cv2.__version__).

import cv2
import numpy as np

def print_arr(arr, name='arr'):
print '{}({}):\n{}'.format(name, arr.dtype, arr)

if __name__ == '__main__':

# Define an arbitrary 3-D array
im = np.array(
[[[1.0, -50.3, 22.3], [-88.2, 30.1, 59.8]],
[[9.0, 50.3, -22.3], [ -8.2, 90.1, -9.8]]], dtype=np.float32)
print_arr(im, 'im')

# Define expected ranges
images = [im]
channels = [0, 1, 2]
mask = None
histSize = [3, 6, 12]
ranges = [
-180.0, 180.0,
-90.0, 270.0,
-45.0, 315.0
]

# Calculate the histogram
hist = cv2.calcHist(images, channels, mask, histSize, ranges)
print_arr(hist, 'hist')

more

Official site

GitHub

Wiki

Documentation

## Stats

Asked: 2016-03-21 19:13:22 -0500

Seen: 674 times

Last updated: Mar 23 '16