Histogram Equalization

asked 2019-01-16 03:08:05 -0600

snehal gravatar image

updated 2019-01-16 07:47:06 -0600

berak gravatar image

I want to do histogram equilization(for 16 bit image) manually without using opencv API , i am using the function :

   Mat image = Mat(h, w, CV_16UC1, image_data);

   Mat new_image = Mat::zeros(image.size(), image.type());

   for (int x = 0; x < image.rows; ++x) 
    {
        for (int y = 0; y < image.cols; ++y) 
        {
            new_image.at<ushort>(x, y) = saturate_cast<ushort>(((pow(2, bpp) - 1) / (max - min))*(image.at<ushort>(x, y) - min));
        }
    }

Here ,

image : 16 bit image.

min : 0

max :65535

bpp :16

But i am not getting equalized image. I got half image is same as input image and half image black. If i give min =0 and max = 255 then i got half image white and half image black. why it is happening is there need to modified the code ? Please give me any suggestion.

edit retag flag offensive close merge delete

Comments

2

don't use loop to iterate over pixels if there is no specific function in opencv : use normalize

Take care of conversion here ((pow(2, bpp) - 1) / (max - min))*(image.at<ushort>(x, y) - min)

x is usually for horizontal and y for verticals. x < image.rows and y < image.cols I don't like that.

Where is histogram?

LBerger gravatar imageLBerger ( 2019-01-16 03:22:47 -0600 )edit

I am not calculating histogram separately. i use this function for 8bit image and its give me the expected image. but not working when i pass 16bit image . Is there need to calculate histogram separately ?

snehal gravatar imagesnehal ( 2019-01-16 03:40:33 -0600 )edit

you also probably need to measure the min max values, not assume anything, using minMaxLoc()

berak gravatar imageberak ( 2019-01-16 07:49:42 -0600 )edit

What is image_data?

" I got half image is same as input image and half image black." Do you use imshow with CV_16U image ?

LBerger gravatar imageLBerger ( 2019-01-16 07:53:12 -0600 )edit

lol, you're calculating pow(2, bpp) (where bpp is also a constant) in the inner loop ? oh my ...

also, you are doing some kind of normalization there, NOT histogram equalization AT ALL !

berak gravatar imageberak ( 2019-01-16 08:02:49 -0600 )edit

Image_data is in unsigned short * type i converted it in Mat . also i am not using imshow , i used imwrite to check the output.

snehal gravatar imagesnehal ( 2019-01-16 22:21:01 -0600 )edit