Ask Your Question

Normalize image 0 - 255 for display

asked 2016-02-06 02:04:04 -0500

Nbb gravatar image

updated 2016-02-06 02:42:17 -0500

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);

this is what i got in matlab with code

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

How can I get the same result in opencv ?

image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-02-06 02:27:26 -0500

berak gravatar image

updated 2016-02-06 03:12:36 -0500

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.


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

image description

edit flag offensive delete link more



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

Nbb gravatar imageNbb ( 2016-02-06 02:39:35 -0500 )edit

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

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

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

berak gravatar imageberak ( 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?

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

Question Tools

1 follower


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

Seen: 50,841 times

Last updated: Feb 06 '16