# Normalize image 0 - 255 for display

Hi, I am trying to create a gaussian kernel and then normalize it so I can display it because the values are all too small like to the power of negative something. i used the normalize function but im still getting a black screen. the minimum is supposed to be 0 and the max 255 and everything else is scale in between. my code is below. help thanks.

 int main() {

Mat kernelX  = getGaussianKernel(49, 13);
Mat kernelY  = getGaussianKernel(49, 13);
Mat kernelXY = kernelX * kernelY.t();

normalize(kernelXY, kernelXY, 0, 255);

namedWindow("l", CV_WINDOW_NORMAL);
imshow("l", kernelXY);
waitKey(0);
}


this is what i got in matlab with code

gaussian = fspecial('gaussian', [49 49], 13);
colormap gray
imagesc(gaussian);


How can I get the same result in opencv ?

edit retag close merge delete

Sort by ยป oldest newest most voted

have a look at the docs , you're setting alpha to 0, no wonder, it's all black.

(i guess, you only swapped alpha and beta params here) . instead try:

normalize(kernelXY, kernelXY, 255,0);


it probably even needs a much smaller alpha factor, 16 looks perfect to me.

### edit:

it looks like you want NORM_MINMAX instead of NORM_L2 (the default) here:

    normalize(kernelXY, kernelXY, 1, 0,NORM_MINMAX);


also, you do not need any alpha then, imshow() can handle [0..1] ranges pretty well

more

Hello,

Why should alpha be 16 ? Shouldn't the maximum value of an image be 255 ? So it will be kernelXY(x,y) = ( kernelXY(x,y) - min ) / ( max - min ) * 255 ? so everything is now between 0 to 255

where the min and max is the min and max of the input image

( 2016-02-06 02:39:35 -0500 )edit

Thank you so much lol. This is so tedious in opencv

( 2016-02-06 03:15:14 -0500 )edit

^^ lol, yea, the "fine print" ....

( 2016-02-06 03:18:26 -0500 )edit

Are you sure about the alpha-beta order berak? Because although the default values are 0-1, that is for norm normalization and the docs say that alpha is the lower range boundary and beta is the upper range boundary in case of range normalization?

( 2016-11-29 07:50:28 -0500 )edit

Official site

GitHub

Wiki

Documentation