Sobel滤波器 – 如何正确显示卷积结果?

huangapple go评论81阅读模式
英文:

Sobel filter - How to correctly display convolution result?

问题

I'm currently trying to implement my own version of the Sobel-Filter/Operator. I tried to pull as much information as possible from Wikipedia(https://en.wikipedia.org/wiki/Sobel_operator) and numerous explanations on YouTube, but I can't find a solution to my problem.

So after gray-scaling my image:

public BufferedImage grayscale(String path) {
    BufferedImage img = readImage(path);
    for (int x = 0; x < img.getWidth(); x++) {
        for (int y = 0; y < img.getHeight(); y++) {
            Color oldColor = new Color(img.getRGB(x, y));
            int red = oldColor.getRed();
            int green = oldColor.getGreen();
            int blue = oldColor.getBlue();
            int grayValue = (int) (0.244 * red + 0.587 * green + 0.114 * blue);
            Color newColor = new Color(grayValue, grayValue, grayValue);
            img.setRGB(x, y, newColor.getRGB());
        }
    }
    return img;
}

I tried to extract the part of the image that should be convolved with the Sobel-Operator Sx. Since I gray-scaled the entire image, I figured it should be fine to use the red-portion since all values should be the same:

public void sobel(BufferedImage grayScaledImg) {
    for (int x = 1; x < grayScaledImg.getWidth() - 1; x++) {
        for (int y = 1; y < grayScaledImg.getHeight() - 1; y++) {
            int field1 = new Color(grayScaledImg.getRGB(x - 1, y - 1)).getRed();
            int field2 = new Color(grayScaledImg.getRGB(x, y - 1)).getRed();
            int field3 = new Color(grayScaledImg.getRGB(x + 1, y - 1)).getRed();
            int field4 = new Color(grayScaledImg.getRGB(x - 1, y)).getRed();
            int field5 = new Color(grayScaledImg.getRGB(x, y)).getRed();
            int field6 = new Color(grayScaledImg.getRGB(x + 1, y)).getRed();
            int field7 = new Color(grayScaledImg.getRGB(x - 1, y + 1)).getRed();
            int field8 = new Color(grayScaledImg.getRGB(x, y + 1)).getRed();
            int field9 = new Color(grayScaledImg.getRGB(x + 1, y + 1)).getRed();
            int[] currentPartOfImage = {field1, field2, field3, field4, field5, field6, field7, field8, field9};

And I'm pretty sure that in the following piece of code, I did something wrong but I can't figure out what:

            int newGrayScale = sobelX(currentPartOfImage);
            grayScaledImg.setRGB(x, y, new Color(newGrayScale, newGrayScale, newGrayScale).getRGB());
        }
    }
}

The sobelX-Function looks like this:

public int sobelX(int[] image) {
    return Math.abs(image[2] + (2 * image[5]) + image[8] - image[0] - (2 * image[3]) - image[6]);
}

Problem: Some of the values I get while doing the convolution are larger than 255, which means they aren't valid RGB-values. So I'm currently not sure how to properly use those values?

Thanks in advance for the help! Sobel滤波器 – 如何正确显示卷积结果?

英文:

I'm currently trying to implement my own version of the Sobel-Filter/Operator. I tried to pull as much information as possible from Wikipedia(<https://en.wikipedia.org/wiki/Sobel_operator>) and numerous explanations on YouTube, but i can't find a solution to my problem.

So after gray-scaling my image:

    public BufferedImage grayscale(String path) {
        BufferedImage img = readImage(path);
        for (int x = 0; x &lt; img.getWidth(); x++) {
            for (int y = 0; y &lt; img.getHeight(); y++) {
                Color oldColor = new Color(img.getRGB(x, y));
                int red = oldColor.getRed();
                int green = oldColor.getGreen();
                int blue = oldColor.getBlue();
                int grayValue = (int) (0.244 * red + 0.587 * green + 0.114 * blue);
                Color newColor = new Color(grayValue, grayValue, grayValue);
                img.setRGB(x, y, newColor.getRGB());
            }
        }
        return img;
    }

I tried to extract the part of the image that should be convolved with the Sobel-Operator Sx. Since i gray-scaled the entire image, i figured it should be fine to use the red-portion since all values should be the same:

    public void sobel(BufferedImage grayScaledImg) {
        for (int x = 1; x &lt; grayScaledImg.getWidth() - 1; x++) {
            for (int y = 1; y &lt; grayScaledImg.getHeight() - 1; y++) {
                int field1 = new Color(grayScaledImg.getRGB(x - 1, y - 1)).getRed();
                int field2 = new Color(grayScaledImg.getRGB(x, y - 1)).getRed();
                int field3 = new Color(grayScaledImg.getRGB(x + 1, y - 1)).getRed();
                int field4 = new Color(grayScaledImg.getRGB(x - 1, y)).getRed();
                int field5 = new Color(grayScaledImg.getRGB(x, y)).getRed();
                int field6 = new Color(grayScaledImg.getRGB(x + 1, y)).getRed();
                int field7 = new Color(grayScaledImg.getRGB(x - 1, y + 1)).getRed();
                int field8 = new Color(grayScaledImg.getRGB(x, y + 1)).getRed();
                int field9 = new Color(grayScaledImg.getRGB(x + 1, y + 1)).getRed();
                int[] currentPartOfImage = {field1, field2, field3, field4, field5, field6, field7, field8, field9};

And i'm pretty sure that in the following piece of code i did something wrong but i cant figure out what..

                int newGrayScale = sobelX(currentPartOfImage);
                grayScaledImg.setRGB(x, y, new Color(newGrayScale, newGrayScale, newGrayScale).getRGB());
            }
        }
    }

The sobelX-Function looks like this:

    public int sobelX(int[] image) {
        return Math.abs(image[2] + (2 * image[5]) + image[8] - image[0] - (2 * image[3]) - image[6]);
    }

Problem <br />
Some of the values i get while doing the convolution are larger than 255, which means they aren't valid RGB-values. So i'm currently not sure how to properly use those values?

Thanks in advance for the help! Sobel滤波器 – 如何正确显示卷积结果?

答案1

得分: 1

在执行卷积时,我得到的一些值大于255

通常可以通过使用"clamp"函数来解决这个问题。它是一个函数,用于检查参数是否在允许的范围内,如果不在范围内,则返回最接近的允许值。

int clamp(int value) {
    if (value > 255) return 255;
    if (value < 0) return 0;
    return value;
}

这是如何使用它的方法:

int newGrayScale = clamp(sobelX(currentPartOfImage));
英文:

> Some of the values i get while doing the convolution are larger than 255

This is often solved by using a "clamp" function. It is a function that checks if the argument is in the allowed range, and if not, returns the closest allowed value.

int clamp(int value) {
    if (value &gt; 255) return 255;
    if (value &lt; 0) return 0;
    return value;
}

And this is how you use it:

int newGrayScale = clamp(sobelX(currentPartOfImage));

huangapple
  • 本文由 发表于 2020年8月3日 19:38:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63228738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定