Sobel

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Sobel

magla
Hi Everybody,

I'm just starting with ImageJ and I've tried to write a simple plugin that generates a picture containing the magnitudes of the gradients using Sobel-Masks.
But unfortunatly I get some weird Output and I can't find my Errors.
Below is the code I used for it.

Thanks in Advance for helping me
 
import ij.ImagePlus;
import ij.plugin.filter.PlugInFilter;
import ij.process.ImageProcessor;


public class Sobel_ implements PlugInFilter
{
    public int setup(String arg, ImagePlus imp)
    {
        return DOES_8G; // this plugin accepts 8-bit grayscale images
    }
       
    public void run(ImageProcessor ip)
    {
        int w = ip.getWidth();
        int h = ip.getHeight();
        ImageProcessor gradIp = ip.duplicate();

        for (int v = 1; v <= h-2; v++)
        {
            for (int u = 1; u <= w-2; u++)
            {
                int grad_x = (gradIp.getPixel(u+1,v-1) + 2 * gradIp.getPixel(u+1,v) + gradIp.getPixel(u+1,v+1))
                           - (gradIp.getPixel(u-1,v-1) + gradIp.getPixel(u-1,v) + gradIp.getPixel(u-1,v+1));
                int grad_y = (gradIp.getPixel(u-1,v+1) + 2 * gradIp.getPixel(u,v+1) + gradIp.getPixel(u+1, v+1))
                           - (gradIp.getPixel(u-1,v-1) + 2 * gradIp.getPixel(u,v-1) + gradIp.getPixel(u+1, v-1));
                int value = (int)Math.sqrt(grad_x * grad_x + grad_y * grad_y);
               
                ip.putPixel(u,v,(byte)value);
            }
        }
    }
}