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

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

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:

  1. public BufferedImage grayscale(String path) {
  2. BufferedImage img = readImage(path);
  3. for (int x = 0; x < img.getWidth(); x++) {
  4. for (int y = 0; y < img.getHeight(); y++) {
  5. Color oldColor = new Color(img.getRGB(x, y));
  6. int red = oldColor.getRed();
  7. int green = oldColor.getGreen();
  8. int blue = oldColor.getBlue();
  9. int grayValue = (int) (0.244 * red + 0.587 * green + 0.114 * blue);
  10. Color newColor = new Color(grayValue, grayValue, grayValue);
  11. img.setRGB(x, y, newColor.getRGB());
  12. }
  13. }
  14. return img;
  15. }

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:

  1. public void sobel(BufferedImage grayScaledImg) {
  2. for (int x = 1; x < grayScaledImg.getWidth() - 1; x++) {
  3. for (int y = 1; y < grayScaledImg.getHeight() - 1; y++) {
  4. int field1 = new Color(grayScaledImg.getRGB(x - 1, y - 1)).getRed();
  5. int field2 = new Color(grayScaledImg.getRGB(x, y - 1)).getRed();
  6. int field3 = new Color(grayScaledImg.getRGB(x + 1, y - 1)).getRed();
  7. int field4 = new Color(grayScaledImg.getRGB(x - 1, y)).getRed();
  8. int field5 = new Color(grayScaledImg.getRGB(x, y)).getRed();
  9. int field6 = new Color(grayScaledImg.getRGB(x + 1, y)).getRed();
  10. int field7 = new Color(grayScaledImg.getRGB(x - 1, y + 1)).getRed();
  11. int field8 = new Color(grayScaledImg.getRGB(x, y + 1)).getRed();
  12. int field9 = new Color(grayScaledImg.getRGB(x + 1, y + 1)).getRed();
  13. 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:

  1. int newGrayScale = sobelX(currentPartOfImage);
  2. grayScaledImg.setRGB(x, y, new Color(newGrayScale, newGrayScale, newGrayScale).getRGB());
  3. }
  4. }
  5. }

The sobelX-Function looks like this:

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

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:

  1. public BufferedImage grayscale(String path) {
  2. BufferedImage img = readImage(path);
  3. for (int x = 0; x &lt; img.getWidth(); x++) {
  4. for (int y = 0; y &lt; img.getHeight(); y++) {
  5. Color oldColor = new Color(img.getRGB(x, y));
  6. int red = oldColor.getRed();
  7. int green = oldColor.getGreen();
  8. int blue = oldColor.getBlue();
  9. int grayValue = (int) (0.244 * red + 0.587 * green + 0.114 * blue);
  10. Color newColor = new Color(grayValue, grayValue, grayValue);
  11. img.setRGB(x, y, newColor.getRGB());
  12. }
  13. }
  14. return img;
  15. }

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:

  1. public void sobel(BufferedImage grayScaledImg) {
  2. for (int x = 1; x &lt; grayScaledImg.getWidth() - 1; x++) {
  3. for (int y = 1; y &lt; grayScaledImg.getHeight() - 1; y++) {
  4. int field1 = new Color(grayScaledImg.getRGB(x - 1, y - 1)).getRed();
  5. int field2 = new Color(grayScaledImg.getRGB(x, y - 1)).getRed();
  6. int field3 = new Color(grayScaledImg.getRGB(x + 1, y - 1)).getRed();
  7. int field4 = new Color(grayScaledImg.getRGB(x - 1, y)).getRed();
  8. int field5 = new Color(grayScaledImg.getRGB(x, y)).getRed();
  9. int field6 = new Color(grayScaledImg.getRGB(x + 1, y)).getRed();
  10. int field7 = new Color(grayScaledImg.getRGB(x - 1, y + 1)).getRed();
  11. int field8 = new Color(grayScaledImg.getRGB(x, y + 1)).getRed();
  12. int field9 = new Color(grayScaledImg.getRGB(x + 1, y + 1)).getRed();
  13. 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..

  1. int newGrayScale = sobelX(currentPartOfImage);
  2. grayScaledImg.setRGB(x, y, new Color(newGrayScale, newGrayScale, newGrayScale).getRGB());
  3. }
  4. }
  5. }

The sobelX-Function looks like this:

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

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"函数来解决这个问题。它是一个函数,用于检查参数是否在允许的范围内,如果不在范围内,则返回最接近的允许值。

  1. int clamp(int value) {
  2. if (value > 255) return 255;
  3. if (value < 0) return 0;
  4. return value;
  5. }

这是如何使用它的方法:

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

  1. int clamp(int value) {
  2. if (value &gt; 255) return 255;
  3. if (value &lt; 0) return 0;
  4. return value;
  5. }

And this is how you use it:

  1. 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:

确定