Search code examples
javaimage-processingedge-detectionsobel

Sobel edge detection with BufferedImage.TYPE_BYTE_BINARY as output


I´m doing a school assignment where we are supposed to do a sobel edge detection on an image. We should do a convolution with the sobel cores och then calculate the gradientmagnitude for each pixel. After that, we should use the threshold method to give a pixel the value 255 (white) or 0 (black), depending on the threshold value. The output image from the edge detection must be of the type BufferedImage.TYPE_BYTE_BINARY. I use a grayscale image as input but the endresult ends up looking very weird.. it definitely does not detect the edges.

I googled around and managed to find working code (here, see the marked correct answer), however, the output image here is of the type BufferedImage.TYPE_INT_RGB, which is not allowed... In this question, the also use a BufferedImage.TYPE.INT.RGB as input to the edge detection.

Help on resolving this matter is much appreciated!

Result when I execute the program. The edge detection result is on the far right. enter image description here

What the edge detection result should look like. enter image description here

My code:

/**
 * turns an image to a grayscale version of the image
 */
public void alterImageGrayScale() throws IOException {
    imageGrayScale = new BufferedImage(imageOriginal.getWidth(), imageOriginal.getHeight(), BufferedImage.TYPE_BYTE_GRAY);

    for(int i = 0; i < imageOriginal.getWidth(); i++) {
        for(int j = 0; j < imageOriginal.getHeight(); j++) {
            Color c = new Color(imageOriginal.getRGB(i, j));
            int red = c.getRed();
            int green = c.getGreen();
            int blue = c.getBlue();
            int gray = (int) (0.2126*red + 0.7152*green + 0.0722*blue);
            imageGrayScale.setRGB(i, j, new Color(gray, gray, gray).getRGB());
        }
    }
}

/**
 * edge detection
 * @throws IOException
 */
public void alterEdgeDetection() throws IOException {
    imageBlackAndWhite = new BufferedImage(imageGrayScale.getWidth(), imageGrayScale.getHeight(), BufferedImage.TYPE_INT_RGB);
    int x = imageGrayScale.getWidth();
    int y = imageGrayScale.getHeight();
    int threshold = 250;

    for (int i = 1; i < x - 1; i++) {
        for (int j = 1; j < y - 1; j++) {

            int val00 = imageGrayScale.getRGB(i - 1, j - 1);
            int val01 = imageGrayScale.getRGB(i - 1, j);
            int val02 = imageGrayScale.getRGB(i - 1, j + 1);
            int val10 = imageGrayScale.getRGB(i, j - 1);
            int val11 = imageGrayScale.getRGB(i, j);
            int val12 = imageGrayScale.getRGB(i, j + 1);
            int val20 = imageGrayScale.getRGB(i + 1, j - 1);
            int val21 = imageGrayScale.getRGB(i + 1, j);
            int val22 = imageGrayScale.getRGB(i + 1, j + 1);

            int gradientX = ((-1 * val00) + (0 * val01) + (1 * val02)) + ((-2 * val10) + (0 * val11) + (2 * val12))
                    + ((-1 * val20) + (0 * val21) + (1 * val22));
            int gradientY = ((-1 * val00) + (-2 * val01) + (-1 * val02)) + ((0 * val10) + (0 * val11) + (0 * val12))
                    + ((1 * val20) + (2 * val21) + (1 * val22));
            int gradientValue = (int) Math.sqrt(Math.pow(gradientX, 2) + Math.pow(gradientY, 2));

            //???? feel like something should be done here, but dont know what

            if(threshold > gradientValue) {
                imageBlackAndWhite.setRGB(i, j, new Color(0, 0, 0).getRGB());
            } else {
                imageBlackAndWhite.setRGB(i, j, new Color(255, 255, 255).getRGB());
            }

        }
    }
}

Solution

  • According to the response in the comment, it should be sufficient to convert the grayscale image that is computed by the edge detection into an image of type TYPE_BYTE_BINARY.

    The following is a MCVE that loads the grayscale image that was linked to in the question (containing the edge detection result), and converts it into a binary image.

    For the conversion to the binary image, there is a threshold that can be modified with the slider at the bottom of the screen: It determines which grayscale value will be converted into a BLACK or WHITE pixel, respectively.

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.image.BufferedImage;
    import java.net.URL;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSlider;
    import javax.swing.SwingUtilities;
    
    public class ImageToBinary
    {
        public static void main(String[] args) throws Exception
        {
            BufferedImage input = 
                ImageIO.read(new URL("https://i.sstatic.net/jvOan.png"));        
    
            BufferedImage output = convertToBinary(input, 10);
    
            SwingUtilities.invokeLater(() -> createAndShowGui(input, output));
        }
    
        private static void createAndShowGui(
            BufferedImage input, BufferedImage output)
        {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel p = new JPanel(new GridLayout(1,2));
            JLabel outputLabel = new JLabel(new ImageIcon(output));
            p.add(new JLabel(new ImageIcon(input)));    
            p.add(outputLabel);
            f.getContentPane().setLayout(new BorderLayout());
            f.getContentPane().add(p, BorderLayout.NORTH);   
    
            JSlider slider = new JSlider(0, 256, 10);
            slider.addChangeListener(e -> 
            {
                int threshold = slider.getValue();
                BufferedImage newOutput = convertToBinary(input, threshold);
                outputLabel.setIcon(new ImageIcon(newOutput));
            });
    
            f.getContentPane().add(slider, BorderLayout.SOUTH);
            f.pack();
    
            f.setLocationRelativeTo(null);
            f.setVisible(true);
    
        }
    
        private static BufferedImage convertToBinary(
            BufferedImage input, int threshold)
        {
            int w = input.getWidth();
            int h = input.getHeight();
            BufferedImage output = new BufferedImage(
                w, h, BufferedImage.TYPE_BYTE_BINARY);
    
            int blackRgb = Color.BLACK.getRGB();
            int whiteRgb = Color.WHITE.getRGB();
    
            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
    
                    int rgb = input.getRGB(x, y);
                    int r = (rgb >> 16) & 0xFF;
                    int g = (rgb >> 8) & 0xFF;
                    int b = (rgb) & 0xFF;
                    int gray = (int) (0.2126 * r + 0.7152 * g + 0.0722 * b);
                    if (gray >= threshold)
                    {
                        output.setRGB(x, y, whiteRgb);
                    }
                    else
                    {
                        output.setRGB(x, y, blackRgb);
                    }
                }
            }
            return output;
        }        
    
    }