Skip to content

calcHist() works for ushort but this is not documented #585

@drtimes

Description

@drtimes

Documentation for OpenCV 3.0 states that calcHist() function takes as input arrays of depths CV_8U or CV_32F. But from source code in OpenCV 3.1.0 it seems the function can be used for 16-bit arrays. I tried and it works just fine. It would be great if you documented the feature.

16-bit image sample_image.zip

#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <opencv/cv.h>

using namespace cv;

int main(int argc, char *argv[])
{
        Mat img = imread("out1.tif", IMREAD_UNCHANGED);

        if (img.depth() == CV_16U)
        {
            int histSize = 2048;
            float range[] = {0, histSize};
            const float* histRange = { range };
            bool uniform = true; bool accumulate = false;

            Mat hist;
            calcHist( &img, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate );

            double hmin, hmax;
            minMaxLoc(img, &hmin, &hmax);

            //plot the histogram
            Mat canvas = Mat::zeros(200, histSize, CV_8UC1);
            for (int j = 0, rows = canvas.rows; j < histSize-1; j++)
                line(canvas, Point(j, rows), Point(j, rows - (hist.at<float>(j) * rows/hmax)), Scalar(255,0,0));

            imshow("hist", canvas);
            waitKey(0);
        }
        else
            return 1;

        return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions