java - Sobel operator doesn't work with rectangle images -


i try implement sobel operator in java result mix of pixels.

    int i, j;     fileinputstream infile = new fileinputstream(args[0]);     bufferedimage inimg = imageio.read(infile);     int width = inimg.getwidth();     int height = inimg.getheight();     int[] output = new int[width * height];     int[] pixels = inimg.getraster().getpixels(0, 0, width, height, (int[])null);      double gx;     double gy;     double g;      for(i = 0 ; < width ; i++ )     {         for(j = 0 ; j < height ; j++ )         {             if (i==0 || i==width-1 || j==0 || j==height-1)                 g = 0;             else{                 gx = pixels[(i+1)*height + j-1] + 2*pixels[(i+1)*height +j] + pixels[(i+1)*height +j+1] -                         pixels[(i-1)*height +j-1] - 2*pixels[(i-1)*height+j] - pixels[(i-1)*height+j+1];                 gy = pixels[(i-1)*height+j+1] + 2*pixels[i*height +j+1] + pixels[(i+1)*height+j+1] -                         pixels[(i-1)*height+j-1] - 2*pixels[i*height+j-1] - pixels[(i+1)*height+j-1];                 g  = math.hypot(gx, gy);             }              output[i*height+j] = (int)g;         }     }       bufferedimage outimg = new bufferedimage(width,height,bufferedimage.type_byte_gray);     outimg.getraster().setpixels(0,0,width,height,output);     fileoutputstream outfile = new fileoutputstream("result.jpg");     imageio.write(outimg,"jpg",outfile);      jframe theframe = new jframe("result");      jlabel thelabel = new jlabel(new imageicon(outimg));     theframe.getcontentpane().add(thelabel);      theframe.setsize(width, height);      theframe.addwindowlistener(new windowadapter() {         public void windowclosing(windowevent e) {             system.exit(0);         }     });     theframe.setvisible(true); 

it works great square images when width != height result image broken , there diagonal black lines. :\

example:

enter image description here

result:

enter image description here

your code appears expect raster.getpixels produce result in columns, this:

0  3  6 1  4  7 2  5  8 

but believe in rows, this:

0  1  2 3  4  5 6  7  8 

so basically, have like:

pxy = pixels[x * height + y]; 

you should have

pxy = pixels[y * width + x]; 

so example, have:

pixels[(i+1)*height + j-1] 

you want

pixels[(j-1)*width + i-1] 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -