Histogram Equalization

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

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?

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 ?

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

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 ?

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 !

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.