Ask Your Question
0

calcHist() float values in range param?

asked 2016-03-21 19:13:22 -0600

martyychang gravatar image

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 flag offensive close merge delete

Comments

1

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

strann gravatar imagestrann ( 2016-03-22 04:33:40 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-03-23 19:13:28 -0600

martyychang gravatar image

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')
edit flag offensive delete link more

Question Tools

1 follower

Stats

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

Seen: 1,161 times

Last updated: Mar 23 '16