2

after a deep search i can not understand why my result image is not what I am expecting compared to the one from wikipedia - sobel operator using the same kernel for Sobel operator.

http://s29.postimg.org/kjex7dx6f/300px_Valve_original_1.png

http://s14.postimg.org/vxhvffm29/Untitled.png

So, I have a button listener that load a bmp image, apply Sobel and display an ImageIcon There is the code :

javax.swing.JFileChooser choose = new javax.swing.JFileChooser();

choose.setFileFilter(new DoFileFilter(".bmp"));
int returnVal = choose.showOpenDialog(this);

if (returnVal == javax.swing.JFileChooser.APPROVE_OPTION) {
    try {
        java.io.FileInputStream imgis = null;
        // System.out.println("Ai ales fisierul : " +
        // choose.getSelectedFile());
        String path = choose.getSelectedFile().toString();
        Path.setText(path);

        imgis = new java.io.FileInputStream(path);

        java.awt.image.BufferedImage img = javax.imageio.ImageIO.read(imgis);

        DirectImgToSobel ds = new DirectImgToSobel(img);
        javax.swing.ImageIcon image;
        image = new javax.swing.ImageIcon(ds.getBuffImg());
        ImgPrev.setIcon(image);

        javax.swing.JFrame frame = (javax.swing.JFrame) javax.swing.SwingUtilities.getWindowAncestor(jPanel1);

        frame.pack();
        frame.repaint();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Display.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Display.class.getName()).log(Level.SEVERE, null, ex);
    }
}

And Sobel class :

public class DirectImgToSobel {
    private final java.awt.image.BufferedImage img;
    private java.awt.image.BufferedImage buffimg;
    private int[][]
        sobel_x = { { -1,  0,  1 }, { -2, 0, 2 }, { -1, 0, 1 } },
        sobel_y = { { -1, -2, -1 }, {  0, 0, 0 }, {  1, 2, 1 } };

    public DirectImgToSobel() {
        this.img = null;
    }

    public DirectImgToSobel(java.awt.image.BufferedImage img) {

        this.img = img;
        aplicaFiltru();
    }

    private void aplicaFiltru() {

        this.buffimg = new java.awt.image.BufferedImage(this.img.getWidth(), this.img.getHeight(),
                java.awt.image.BufferedImage.TYPE_BYTE_GRAY);
        for (int x = 1; x < this.img.getWidth() - 1; x++) {
            for (int y = 1; y < this.img.getHeight() - 1; y++) {
                int pixel_x = 
              (sobel_x[0][0] * img.getRGB(x-1,y-1)) + (sobel_x[0][1] * img.getRGB(x,y-1)) + (sobel_x[0][2] * img.getRGB(x+1,y-1)) +
              (sobel_x[1][0] * img.getRGB(x-1,y))   + (sobel_x[1][1] * img.getRGB(x,y))   + (sobel_x[1][2] * img.getRGB(x+1,y)) +
              (sobel_x[2][0] * img.getRGB(x-1,y+1)) + (sobel_x[2][1] * img.getRGB(x,y+1)) + (sobel_x[2][2] * img.getRGB(x+1,y+1));
                int pixel_y = 
              (sobel_y[0][0] * img.getRGB(x-1,y-1)) + (sobel_y[0][1] * img.getRGB(x,y-1)) + (sobel_y[0][2] * img.getRGB(x+1,y-1)) +
              (sobel_y[1][0] * img.getRGB(x-1,y))   + (sobel_y[1][1] * img.getRGB(x,y))   + (sobel_y[1][2] * img.getRGB(x+1,y)) +
              (sobel_y[2][0] * img.getRGB(x-1,y+1)) + (sobel_y[2][1] * img.getRGB(x,y+1)) + (sobel_y[2][2] * img.getRGB(x+1,y+1));
                this.buffimg.setRGB(x, y, (int) Math.sqrt(pixel_x * pixel_x + pixel_y * pixel_y));
            }
        }

        buffimg = thresholdImage(buffimg, 28);

        java.awt.Graphics g = buffimg.getGraphics();
        g.drawImage(buffimg, 0, 0, null);
        g.dispose();
    }

    public java.awt.image.BufferedImage getBuffImg() {

        return this.buffimg;
    }

    public static java.awt.image.BufferedImage thresholdImage(java.awt.image.BufferedImage image, int threshold) {

        java.awt.image.BufferedImage result = new java.awt.image.BufferedImage(image.getWidth(), image.getHeight(),
                java.awt.image.BufferedImage.TYPE_BYTE_GRAY);

        result.getGraphics().drawImage(image, 0, 0, null);
        java.awt.image.WritableRaster raster = result.getRaster();

        int[] pixels = new int[image.getWidth()];

        for (int y = 0; y < image.getHeight(); y++) {
            raster.getPixels(0, y, image.getWidth(), 1, pixels);
            for (int i = 0; i < pixels.length; i++) {
                if (pixels[i] < threshold)
                    pixels[i] = 0;
                else
                    pixels[i] = 255;
            }
            raster.setPixels(0, y, image.getWidth(), 1, pixels);
        }
        return result;
    }
}
cybersam
  • 63,203
  • 6
  • 53
  • 76

2 Answers2

1

For obtain same result as in Wikipedia you have to do:

  1. Use brightness of image point instead of colors packed to single int that returns getRGB.
  2. Normalize result (map low values to black and high values to white).

EDIT: I accidentally found good article about Sobel filters in Java: http://asserttrue.blogspot.ru/2010/08/smart-sobel-image-filter.html

EDIT2: Check this How to convert get.rgb(x,y) integer pixel to Color(r,g,b,a) in Java? question described how to extract colors from image.

But my suggestion is to do float brightness = (new Color(img.getRGB(x, y))).RGBtoHSB()[2]; and apply Sobel to brightness.

About your threshold function: you should get grayscaled image, not black-and-white.

like:

if (pixels[i] < threshold) pixels[i] = 0;
else pixels[i] = (int)((pixels[i] - threshold)/(255.0 - threshold)*255.0);

But, again, rgba color representation isn't suitable for math.

Normalizing will be improved by finding minimum and maximum pixel values and stretch (min-max) range to (0-255)

Community
  • 1
  • 1
Nikolay
  • 1,949
  • 18
  • 26
  • I don't understand... you say that I should have the red,green,blue instead of `getRGB` , why ? – Ionut Ionut Nov 30 '13 at 08:15
  • Because `getRGB` returns bit-packed int, that not suitable for arithmetic operations. Check this question: http://stackoverflow.com/questions/2615522/ – Nikolay Nov 30 '13 at 08:46
  • First of all, thank you for answer :). I work on this only after work, sorry that I do not respond lately. I don't know precisely how to work with separate color from your answer. I have updated my code but it shows a bunch of points on a black screen. Also I have implemented the code from your answer but display only a band on the right and down of the image and I can't understand the logic. Can you help me with a pseudocode or show how to work with independent colors ? Thank you. – Ionut Ionut Dec 03 '13 at 18:55
  • I guess you didn't receive notifications about answer edit (I'm new at SO). It's my fail, I had supposed to write comment here right after edit. Hope that help to get over your problem. – Nikolay Dec 14 '13 at 15:00
  • Also, I revised my answer and make micro-edit due to ensure float-point division here. – Nikolay Dec 14 '13 at 15:06
0

change the image type from

TYPE_BYTE_GRAY to TYPE_INT_RGB

use the correct color channel do convolve

sobel_x[0][0] * new Color(img.getRGB(x-1,y-1)).getBlue()

pack the convolutioned color into bit packed RGB, and set the color

int packedRGB=(int)Math.sqrt(pixel_x*pixel_x+pixel_y*pixel_y); packedRGB=(packedRGB << 16 | packedRGB << 8 | RGB); this.buffimg.setRGB(x, y, packedRGB);

Convolution accepts only 1 color channel, it can be r,g,b or gray [(r+g+b)/3], and returns one color channel, thats why you have to pack it back to bit packed RGB, because BufferedImage.setColor() takes only bit-packed RGB.

My code

`

static BufferedImage inputImg,outputImg;
static int[][] pixelMatrix=new int[3][3];

public static void main(String[] args) {
    try {

        inputImg=ImageIO.read(new File("your input image"));
        outputImg=new BufferedImage(inputImg.getWidth(),inputImg.getHeight(),TYPE_INT_RGB);

        for(int i=1;i<inputImg.getWidth()-1;i++){
            for(int j=1;j<inputImg.getHeight()-1;j++){
                pixelMatrix[0][0]=new Color(inputImg.getRGB(i-1,j-1)).getRed();
                pixelMatrix[0][1]=new Color(inputImg.getRGB(i-1,j)).getRed();
                pixelMatrix[0][2]=new Color(inputImg.getRGB(i-1,j+1)).getRed();
                pixelMatrix[1][0]=new Color(inputImg.getRGB(i,j-1)).getRed();
                pixelMatrix[1][2]=new Color(inputImg.getRGB(i,j+1)).getRed();
                pixelMatrix[2][0]=new Color(inputImg.getRGB(i+1,j-1)).getRed();
                pixelMatrix[2][1]=new Color(inputImg.getRGB(i+1,j)).getRed();
                pixelMatrix[2][2]=new Color(inputImg.getRGB(i+1,j+1)).getRed();

                int edge=(int) convolution(pixelMatrix);
                outputImg.setRGB(i,j,(edge<<16 | edge<<8 | edge));
            }
        }

        File outputfile = new File("your output image");
        ImageIO.write(outputImg,"jpg", outputfile);

    } catch (IOException ex) {System.err.println("Image width:height="+inputImg.getWidth()+":"+inputImg.getHeight());}
}
public static double convolution(int[][] pixelMatrix){

    int gy=(pixelMatrix[0][0]*-1)+(pixelMatrix[0][1]*-2)+(pixelMatrix[0][2]*-1)+(pixelMatrix[2][0])+(pixelMatrix[2][1]*2)+(pixelMatrix[2][2]*1);
    int gx=(pixelMatrix[0][0])+(pixelMatrix[0][2]*-1)+(pixelMatrix[1][0]*2)+(pixelMatrix[1][2]*-2)+(pixelMatrix[2][0])+(pixelMatrix[2][2]*-1);
    return Math.sqrt(Math.pow(gy,2)+Math.pow(gx,2));

}

`

junhaotee
  • 483
  • 4
  • 10