Ask Your Question

Revision history [back]

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')