我想自己实现Sobel筛选器(实际上没有漂亮的实现)。但是在进行卷积之后,我不知道如何计算rgb值。


假设:灰度图像

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);
    }
}

最佳答案

我的想法之一是进行线性变换。例如,您获得的图像的最小像素值为-998,最大为1000。因此,您可以将-998对应于0,将1000对应于255,然后获得(-998,1000)比例与缩放(0,255),并将[-998,1000]至[0,255]之间的所有值标准化。

关于java - 在Java中实现Sobel过滤器-标准化值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25770019/

10-10 17:58