Ask Your Question

# 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

## 1 answer

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

## Comments

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

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?

Official site

GitHub

Wiki

Documentation

## Stats

Asked: 2016-02-06 02:04:04 -0500

Seen: 33,690 times

Last updated: Feb 06 '16