0

您好我试图通过创建自己的插件在 imagej 中实现 Sobel 过滤器。我有有效的代码,但它没有正确突出边缘白色。我已经尝试了一段时间,但我无法弄清楚我做错了什么,或者我可能不完全理解 sobel 滤波器的植入,或者不理解数学。任何帮助将不胜感激。这是我的代码:

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.util.*;
import java.awt.*;
import ij.plugin.filter.*;
import ij.process.*;
import java.lang.Math.*;

public class Filter_Plugin implements PlugInFilter {
String title = null; 

int sobel_x[][] = {{-1,0,1},
                        {-2,0,2},
                            {-1,0,1}};

        int sobel_y[][] = {{-1,-2,-1},
                            {0,0,0},
                            {1,2,1}};

int pixel_x;
int pixel_y;

public int setup(String arg, ImagePlus im) { 
    title = im.getTitle(); 
    return DOES_8G; 
} 

public void run(ImageProcessor ip) { 

    int w = ip.getWidth();
    int h = ip.getHeight();
    ImageProcessor copy = ip.duplicate(); 

     for (int x=1; x < w-2; x++) 
     {
            for (int y=1; y < h-2; y++) 
        {
            pixel_x = 1/6 * (sobel_x[0][0] * copy.getPixel(x-1,y-1)) + (sobel_x[0][1] * copy.getPixel(x,y-1)) + (sobel_x[0][2] * copy.getPixel(x+1,y-1)) +
                    (sobel_x[1][0] * copy.getPixel(x-1,y))   + (sobel_x[1][1] * copy.getPixel(x,y))   + (sobel_x[1][2] * copy.getPixel(x+1,y)) +
                            (sobel_x[2][0] * copy.getPixel(x-1,y+1)) + (sobel_x[2][1] * copy.getPixel(x,y+1)) + (sobel_x[2][2] * copy.getPixel(x+1,y+1));

            pixel_y = 1/6 * (sobel_y[0][0] * copy.getPixel(x-1,y-1)) + (sobel_y[0][1] * copy.getPixel(x,y-1)) + (sobel_y[0][2] * copy.getPixel(x+1,y-1)) +
                    (sobel_y[1][0] * copy.getPixel(x-1,y))   + (sobel_y[1][1] * copy.getPixel(x,y))   + (sobel_y[1][2] * copy.getPixel(x+1,y)) +
                            (sobel_y[2][0] * copy.getPixel(x-1,y+1)) + (sobel_y[2][1] * copy.getPixel(x,y+1)) + (sobel_x[2][2] * copy.getPixel(x+1,y+1));

            int val = (int)Math.sqrt((pixel_x * pixel_x) + (pixel_y * pixel_y));

            if(val < 0)
            {
               val = 0;
            }

            if(val > 255)
            {
                  val = 255;
            }

            ip.putPixel(x,y,val);

             }
        }
 }

}

4

1 回答 1

5

您的计算中有一个错误:您仅将第一个和(即矩阵的左上角)乘以 1/6。通过删除这个因素,我得到以下适用于我的代码:

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.util.*;
import java.awt.*;
import ij.plugin.filter.*;
import ij.process.*;
import java.lang.Math.*;

public class Filter_Plugin implements PlugInFilter {
    String title = null; 
    int sobel_x[][] = {{-1,0,1},
                       {-2,0,2},
                       {-1,0,1}};
    int sobel_y[][] = {{-1,-2,-1},
                       {0,0,0},
                       {1,2,1}};
    int pixel_x;
    int pixel_y;

    public int setup(String arg, ImagePlus im) { 
        title = im.getTitle(); 
        return DOES_8G; 
    } 

    public void run(ImageProcessor ip) { 
        int w = ip.getWidth();
        int h = ip.getHeight();
        ImageProcessor copy = ip.duplicate(); 
        for (int x=1; x < w-2; x++) {
            for (int y=1; y < h-2; y++) {
                pixel_x = (sobel_x[0][0] * copy.getPixel(x-1,y-1)) + (sobel_x[0][1] * copy.getPixel(x,y-1)) + (sobel_x[0][2] * copy.getPixel(x+1,y-1)) +
                    (sobel_x[1][0] * copy.getPixel(x-1,y))   + (sobel_x[1][1] * copy.getPixel(x,y))   + (sobel_x[1][2] * copy.getPixel(x+1,y)) +
                    (sobel_x[2][0] * copy.getPixel(x-1,y+1)) + (sobel_x[2][1] * copy.getPixel(x,y+1)) + (sobel_x[2][2] * copy.getPixel(x+1,y+1));
                pixel_y = (sobel_y[0][0] * copy.getPixel(x-1,y-1)) + (sobel_y[0][1] * copy.getPixel(x,y-1)) + (sobel_y[0][2] * copy.getPixel(x+1,y-1)) +
                    (sobel_y[1][0] * copy.getPixel(x-1,y))   + (sobel_y[1][1] * copy.getPixel(x,y))   + (sobel_y[1][2] * copy.getPixel(x+1,y)) +
                    (sobel_y[2][0] * copy.getPixel(x-1,y+1)) + (sobel_y[2][1] * copy.getPixel(x,y+1)) + (sobel_x[2][2] * copy.getPixel(x+1,y+1));

                int val = (int)Math.sqrt((pixel_x * pixel_x) + (pixel_y * pixel_y));

                if(val < 0)
                {
                   val = 0;
                }

                if(val > 255)
                {
                   val = 255;
                }

                ip.putPixel(x,y,val);
            }
        }
    }
}

您是否参考过Stephan Preibisch 的 Sobel 过滤器插件

也许您应该将输出设为 32 位浮点图像,而不是截断 0 和 255 处的值。

于 2013-10-17T13:09:32.230 回答