0
votes

I am working on an assignment using sobel edge detection on an image. I am currently struggling to do the operation for the gradient. I am receiving a "bad operand types for binary operator '*'" error when compiling. I think it may be because I defined all of my pixels as letters and I'm not sure what my next step should be. Any help would be greatly appreciated! Thank you in advance!

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;
  }
1
these operations are not valid: int gx = (-A + (-2*D) + -G + C + (2*F)+ I); and nextline int gy = (A + (2*B) + C + (-G) + (-2*H) + (-I)); also won't compile - Martin Frank
sorry if this is obvious, but how would I make them valid? - Rachael
you have to handle each color seperate int green = Color.getGreen(), int red = Color.getRed() and int blue = Color.getBlue() will give you int values which can be added, subtracted and all the stuff you want to do to get your gradient - Martin Frank

1 Answers

0
votes

the problem lies within the handling of Colors, here:

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

this won't work.

to get the gradient you have to either

  • handle each color separate
  • handle the image in grayscale

i can't tell you which one would work for you.

handle each color separate:

using this approach you handle each color separate to detect edges for that color

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

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

handle as gray

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

you have to provide the methode toGray / average the colors

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