2013-10-12 27 views
0

您好我试图通过创建我自己的插件在imagej中实现Sobel过滤器。我有代码可以工作,但它没有正确突出白色的边缘。我一直在尝试一段时间,我只是不知道我做错了什么,或者我不完全理解sobel滤波器的植入和/或不理解数学。任何帮助将不胜感激。这里是我的代码:在ImageJ中实现一个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/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's Sobel Filter Plugin作为参考?

也许你应该让你的输出一个32位浮点图像,而不是在0和255

+0

截断值啊谢谢你了!对此,我真的非常感激!我很害怕看看! –