1

I want to implement Sobel Filter by myself (actual no beautiful implementation). But after doing the convolution I have no idea how to calculate the rgb values.

  • Assumption: grey scaled image

    double [][] sobel_x = 
    {
        { -1, 0, 1},
        { -2, 0, 2},
        { -1, 0, 1}
    };
    
    double [][]    sobel_y = 
    {
        { 1, 2, 1},
        { 0, 0, 0},
        {-1, -2, 1}
    };
    
    for(int y=1; y<image.getHeight()-1; y++)
    {
        for(int x=1; x<image.getWidth()-1; x++)
        {
           Color a = new Color(image.getRGB(x-1, y-1));
           Color b = new Color(image.getRGB(x, y-1));
           Color c = new Color(image.getRGB(x+1, y-1));
           Color d = new Color(image.getRGB(x-1, y));
           Color e = new Color(image.getRGB(x, y));
           Color f = new Color(image.getRGB(x+1, y));
           Color g = new Color(image.getRGB(x-1, y+1));
           Color h = new Color(image.getRGB(x, y+1));
           Color i = new Color(image.getRGB(x+1, y+1));
    
            double pixel_x =    (sobel_x[0][0] * a.getRed()) + (sobel_x[0][1] * b.getRed()) + (sobel_x[0][2] * c.getRed()) +
                                (sobel_x[1][0] * d.getRed())   + (sobel_x[1][1] * e.getRed())   + (sobel_x[1][2] * f.getRed()) +
                                (sobel_x[2][0] * g.getRed()) + (sobel_x[2][1] * h.getRed()) + (sobel_x[2][2] * i.getRed());
            double pixel_y = 
                                (sobel_y[0][0] * a.getRed()) + (sobel_x[0][1] * b.getRed()) + (sobel_x[0][2] * c.getRed()) +
                                (sobel_y[1][0] * d.getRed())   + (sobel_x[1][1] * e.getRed())   + (sobel_x[1][2] * f.getRed()) +
                                (sobel_y[2][0] * g.getRed()) + (sobel_x[2][1] * h.getRed()) + (sobel_x[2][2] * i.getRed());  
    
            //Here it is possible to get values between [-1020, 1020]       
    
            //How to going on
    
            //int rgb = (int) Math.sqrt(pixel_x*pixel_x+pixel_y*pixel_y);
    
            //int rgbAsInt = (int)(65536 * rgb + 256 * rgb + rgb);      
        }
    }   
    
4

2 回答 2

0

我的想法之一是进行线性变换。比如你得到的图像中像素值最小的是-998,最大的是1000,那么你可以将-998对应0,1000对应255,然后得到(-998,1000)的比例与(0,255) 的比例并将 [-998,1000] 到 [0,255] 之间的所有值归一化。

于 2015-07-22T17:49:29.570 回答
0

以下图像区域的 x 轴梯度为 1:

1 2 3
1 2 3
1 2 3

应用这个过滤器——

-1 0 1
-2 0 2
-1 0 1

-- 结果为 8。因此 X 和 Y 梯度按该因子缩放。

您需要确定要在输出图像中表示的最大梯度是多少;称之为“gr_max”。X 和 Y 梯度应限制在该值:

float gr_x, gr_y, gr_max = 16;

gr_x /= (gr_max * 8);
gr_y /= (gr_max * 8);

if (gr_x > 1)
    gr_x = 1;
if (gr_x < -1)
    gr_x = -1;

if (gr_y > 1)
    gr_y = 1;
if (gr_y < -1)
    gr_y = -1;

然后,假设您希望输出 RGB 值在 [0, 255] 范围内——

int pixel_x = lround((gr_x + 1) * 255/2),
    pixel_y = lround((gr_y + 1) * 255/2);
于 2017-09-17T13:35:54.857 回答