How do you handle negative pixel values after filtering?

爱⌒轻易说出口 提交于 2019-12-12 17:02:48

问题


I have a 8-bit image and I want to filter it with a matrix for edge detection. My kernel matrix is

0  1  0
1 -4  1
0  1  0

For some indices it gives me a negative value. What am I supposed to with them?


回答1:


Your kernel is a Laplace filter. Applying it to an image yields a finite difference approximation to the Laplacian operator. The Laplace operator is not an edge detector by itself.

But you can use it as a building block for an edge detector: you need to detect the zero crossings to find edges (this is the Marr-Hildreth edge detector). To find zero crossings, you need to have negative values.

You can also use the Laplace filtered image to sharpen your image. If you subtract it from the original image, the result will be an image with sharper edges and a much crisper feel. For this, negative values are important too.

For both these applications, clamping the result of the operation, as suggested in the accepted answer, is wrong. That clamping sets all negative values to 0. This means there are no more zero crossings to find, so you can't find edges, and for the sharpening it means that one side of each edge will not be sharpened.

So, the best thing to do with the result of the Laplace filter is preserve the values as they are. Use a signed 16-bit integer type to store your results (I actually prefer using floating-point types, it simplifies a lot of things).

On the other hand, if you want to display the result of the Laplace filter to a screen, you will have to do something sensical with the pixel values. Common in this case is to add 128 to each pixel. This shifts the zero to a mid-grey value, shows negative values as darker, and positive values as lighter. After adding 128, values above 255 and below 0 can be clipped. You can also further stretch the values if you want to avoid clipping, for example laplace / 2 + 128.




回答2:


Out of range values are extremely common in JPEG. One handles them by clamping.

If X < 0 then X := 0 ;   
If X > 255 then X := 255 ;


来源:https://stackoverflow.com/questions/53027923/how-do-you-handle-negative-pixel-values-after-filtering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!