实施laplacian 3x3 [英] implement laplacian 3x3

查看:180
本文介绍了实施laplacian 3x3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过Gonzalez和Woods的DIP第二版,并使用wxImage尝试使用拉普拉斯面具(第129和130页)。

Im reading DIP 2nd edition by Gonzalez and Woods and try to my hands dirty with Laplacian mask (page 129&130) using wxImage.

float kernel [3][3]= {{1, 1, 1},{1,-8, 1},{1, 1, 1}};   

这里是处理循环:

unsigned char r,g,b;                    

float rtotal, gtotal, btotal; rtotal = gtotal = btotal = 0.0;   
//ignore the border pixel              

for(int i = 1; i<imgWidth-1; i++)
{

   for(int j = 1; j<imgHeight-1; j++) 
    {

     rtotal = gtotal=btotal =0.0;


       for(int y = -1; y<=1;y++)

       {

            for(int x = -1; x<=1;x++)

            {

            // get each channel pixel value

            r = Image->GetRed(i+y,j+x);

            g = Image->GetGreen(i+y,j+x);

            b = Image->GetBlue(i+y,j+x);

            // calculate each channel surrouding neighbour pixel value base   

            rtotal += r* kernel[y+1][x+1];

            gtotal += g* kernel[y+1][x+1] ;

            btotal += b* kernel[y+1][x+1];

            }

    }
            //edit1: here is how to sharpen the image
            // original pixel - (0.2 * the sum of pixel neighbour)
            rtotal = loadedImage->GetRed(x,y) - 0.2*rtotal;

    gtotal = loadedImage->GetGreen(x,y) - 0.2*gtotal;

    btotal = loadedImage->GetBlue(x,y) - 0.2*btotal;
    // range checking

    if (rtotal >255) rtotal = 255;

       else if (rtotal <0) rtotal = 0;

    if(btotal>255) btotal = 255;

       else if(btotal < 0) btotal = 0;

    if(gtotal > 255) gtotal = 255;

       else if (gtotal < 0 ) gtotal =0;

    // commit new pixel value

    Image->SetRGB(i,j, rtotal, gtotal, btotal);

我把它应用到北极图片(灰色图片),我得到的是一个黑点和白色像素!

I applied that to the North Pole picture (grey image) and all I get is a blob of black and white pixels!

任何想法我可能错过了在for循环中的一些东西?

Any ideas where may I have missed something in the for loops?

Edit1:得到的答案后,在谷歌翻阅。这个dsp东西是绝对棘手!我添加到上面的代码,它将锐化图像。

Finally get the answer after looking around on google. This dsp stuff is definitely tricky! I added to the code above, it will sharpen the image.

干杯

推荐答案

首先,与拉普拉斯卷积的结果可以具有负值。考虑由0包围的值为1的像素。在该像素处的卷积的结果将是-8。

First, the result of convolving with a Laplacian can have negative values. Consider a pixel with a value of 1 surrounded by 0's. The result of the convolution at that pixel will be -8.

其次,结果的范围将在[-8 * 255,8 * 255]之间,这绝对不适合8位。基本上,当您进行范围检查时,您将丢失大部分信息,并且大部分生成的像素将最终为0或255.

Second, the range of the result will be between [-8 * 255, 8 * 255], which definitely does not fit into 8 bits. Essentially, when you do your range checking, you are losing most of the information, and most of your resulting pixels will end up either being 0 or 255.

您有什么要做的是将结果存储在一个类型的数组,该数组是有符号的,足够宽以处理该范围。然后,如果要输出8位图像,则需要重新调整值,以便-8 * 255映射为0,并且8 * 255映射为255.或者,您可以重新缩放,以使最小值映射到0和最大值映射到255.

What you have to do is store the result in an array of a type that is signed and wide enough to handle the range. Then, if you wish to output an 8-bit image, you would need to rescale the values so that -8 * 255 maps to 0, and 8 * 255 maps to 255. Or you can rescale it so that the least value maps to 0 and the greatest value maps to 255.

编辑:在此特定情况下,您可以执行以下操作:

in this specific case, you can do the following:

rtotal = (rtotal + 8 * 255) / (16 * 255) * 255;

这简化为

rtotal = (rtotal + 8 * 255) / 16;

这会将rtotal映射到0到25​​5之间的范围,而不会截断。您应该对 gtotal btotal 执行相同操作。

This would map rtotal into a range between 0 and 255 without truncation. You should do the same for gtotal and btotal.

这篇关于实施laplacian 3x3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆