Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I've checked again if calcHist() is working fine for 16-bit images and opened the issue https://github.com/Itseez/opencv_contrib/issues/585

Sample image: out1.tiff

#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.tiff", 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;
}