Ask Your Question

Histogram for ushort

asked 2016-03-14 10:58:46 -0500

strann gravatar image


Documentation for OpenCV 3.0 states that calcHist() function takes as input images of depths CV_8U or CV_32F. But from source code it seems the function can be used for 16-bit images in OpenCV 3.1. Is it undocumented yet or I shouldn't use it?

else if( depth == CV_16U )
    calcHist_<ushort>(ptrs, deltas, imsize, ihist, dims, ranges, _uniranges, uniform )
edit retag flag offensive close merge delete



Have you give it a try? If it is working, please make a pull request to update the doc.

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2016-03-14 11:33:46 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-03-16 06:34:36 -0500

strann gravatar image

I've checked again if calcHist() is working fine for 16-bit images and opened the issue

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 - (<float>(j) * rows/hmax)), Scalar(255,0,0));

            imshow("hist", canvas);
            return 1;

        return 0;
edit flag offensive delete link more


hi, imho, opencv_contrib is the wrong place for this issue, it should go to instead

(good, that you're doing that, nonetheless !)

berak gravatar imageberak ( 2016-03-16 08:07:52 -0500 )edit

Question Tools

1 follower


Asked: 2016-03-14 10:58:46 -0500

Seen: 1,011 times

Last updated: Mar 16 '16