过滤后如何处理负像素值?

Thu*_*che 2 image-processing convolution edge-detection imagefilter

我有一个8位图像,我想用一个矩阵对它进行过滤以进行边缘检测。我的内核矩阵是

0  1  0
1 -4  1
0  1  0
Run Code Online (Sandbox Code Playgroud)

对于某些指数,它给了我负值。我应该和他们一起做什么?

Cri*_*ngo 5

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

另一方面,如果要在屏幕上显示拉普拉斯滤镜的结果,则必须对像素值做一些有意义的事情。在这种情况下,常见的做法是为每个像素添加128。这会将零值移至中间灰色值,将负值显示为较暗,将正值显示为较亮。加128后,可以裁剪大于255且小于0的值。例如,如果要避免剪切,也可以进一步拉伸值laplace / 2 + 128