Laplacian of Gaussian: how does it work? (OpenCV)

maximus picture maximus · Mar 26, 2010 · Viewed 16.2k times · Source

Does anybody know how does it work and how to do it using OpenCV? Laplacian can be calculated using OpenCV, but the result is not what I expected. I mean I expect the image to be approximately constant contrast at background regions, but it is black, and edges are white. There are a lot of noise also, even after gauss filter. I filter image using gaussian filter and then apply laplace. I think what I want is done by a different way.

Answer

tzaman picture tzaman · Mar 26, 2010

Laplacian of Gaussian is an edge-detection filter; the output is 0 in constant ('background') regions, and positive or negative where there is contrast. The reason why you're seeing black in the background regions is because OpenCV is just giving you the raw output; the kind of image you're describing (gray on background, with positive / negative edges in black or white) is produced after scaling the output into an appropriate range.

The output range varies depending on the actual kernel used, but it's always going to fit in a (-max, +max) range around zero where max is the maximum output magnitude of the filter kernel; to get the "typical" output image you need to scale that into a (0, 1) range (or (0, 255) if you're using 8-bit images).

You can perform the necessary scaling using the cvScale function, with 1/(2*max) as the scale factor and 0.5 shift. (Or for 8-bit images use 255/(2*max) scale and 128 shift.)