【问题标题】:Sobel Edge Detection, image convolutionSobel边缘检测,图像卷积
【发布时间】:2020-04-06 13:04:22
【问题描述】:

我正在对图像使用 sobel 边缘检测进行分配。我目前正在努力为渐变进行操作。编译时收到“二进制运算符'*'的错误操作数类型”错误。我认为这可能是因为我将所有像素都定义为字母,我不确定下一步应该是什么。任何帮助将不胜感激!提前谢谢!

public static BufferedImage sobelEdgeDetect(BufferedImage input) {
     int img_width = input.getWidth();
    int img_height = input.getHeight();

    BufferedImage output_img = new BufferedImage(
        img_width, img_height, BufferedImage.TYPE_INT_RGB);

    for (int x = 0; x < img_width; x++) {
      for (int y = 0; y < img_height; y++) {

    Color color_at_pos = new Color(input.getRGB(x, y));

    int red = color_at_pos.getRed();
    int green = color_at_pos.getGreen();
    int blue = color_at_pos.getBlue();

    int average = (red + green + blue) / 3;

    Color A,B,C,D,F,G,H,I;

    if(x-1 > 0 && y+1 < img_height){
          A = new Color (input.getRGB(x-1,y+1));
        } else {
          A = Color.BLACK;
        }

        if(y+1 < img_height){
          B = new Color (input.getRGB(x,y+1));
        } else {
          B = Color.BLACK;
        }

        if(x+1 < img_width && y+1 < img_height){
          C = new Color (input.getRGB(x+1,y+1));
        } else {
          C = Color.BLACK;
        }

        if(x-1 > 0){
          D = new Color (input.getRGB(x-1,y));
        } else {
          D = Color.BLACK;
        }

        if(x+1 < img_width){
          F = new Color (input.getRGB(x+1,y));
        } else {
          F = Color.BLACK;
        }

        if(x-1 > 0 && y-1 > 0){
          G = new Color (input.getRGB(x-1,y-1));
        } else {
          G = Color.BLACK;
        }

        if(y-1 > 0){
          H = new Color (input.getRGB(x,y-1));
        } else {
          H = Color.BLACK;
        }

        if(x+1 > img_width && y-1 > 0){
          I = new Color (input.getRGB(x+1,y-1));
        } else {
          I = Color.BLACK;
        }

       int gx = (-A + (-2*D) + -G + C + (2*F)+ I);
       int gy = (A + (2*B) + C + (-G) + (-2*H) + (-I));

      int result = (int)math.sqrt((gx*gx) + (gy*gy));

        if (average < 0) {
          average = 0;
        } else if (average > 255) {
          average = 255;
        }

        Color average_color = new Color(average, average, average);

        output_img.setRGB(x, y, average_color.getRGB());
      }
    } 
     return output_img;
  }

【问题讨论】:

  • 这些操作无效:int gx = (-A + (-2*D) + -G + C + (2*F)+ I); 和 nextline int gy = (A + (2*B) + C + (-G) + (-2*H) + (-I)); 也不会编译
  • 很抱歉,如果这很明显,但我怎样才能使它们有效?
  • 您必须分别处理每种颜色 int green = Color.getGreen()int red = Color.getRed()int blue = Color.getBlue() 将为您提供 int 值,这些值可以添加、减去以及所有您想做的事情 得到你的渐变

标签: java convolution edge-detection sobel


【解决方案1】:

问题在于颜色的处理,这里:

int gx = (-A + (-2*D) + -G + C + (2*F)+ I);
int gy = (A + (2*B) + C + (-G) + (-2*H) + (-I));

这行不通。

要获得渐变,您必须要么

  • 单独处理每种颜色
  • 处理灰度图像

我不能告诉你哪一个适合你。

分别处理每种颜色:

使用这种方法,您可以单独处理每种颜色以检测该颜色的边缘

//red:
int redGx = (-A.getRed() + (-2*D.getRed()) + -G.getRed() + C.getRed() + (2*F.getRed())+ I.getRed());
int redGy = ...

//green:
int greenGx = (-A.getGreen()...

灰色处理

int redGx = (toGray(A) + (-2*toGray(D)) + -toGray(G) + toGray(C) + (2*toGray(F))+ toGray(I));
int redGy = ...

你必须提供方法toGray/平均颜色

static int toGray(Color col){
    return (color.getGreen()+color.getRed()+col.getBlue()) / 3;
}

【讨论】:

  • 非常感谢!!我分别处理每种颜色!
  • 我返回的图像似乎只是变成了灰度而不是强调高光。
  • 它是灰色的,因为您使用Color average_color = new Color(average, average, average); 来创建渐变的灰度颜色
猜你喜欢
  • 2015-08-09
  • 1970-01-01
  • 2016-10-03
  • 1970-01-01
  • 2011-02-25
  • 2015-01-02
  • 2017-05-01
  • 2021-06-02
  • 1970-01-01
相关资源
最近更新 更多