【问题标题】:Implement Sobel Filter in Java - Normalize values在 Java 中实现 Sobel 过滤器 - 规范化值
【发布时间】:2014-11-04 08:58:25
【问题描述】:

我想自己实现 Sobel 过滤器(实际上没有漂亮的实现)。但是在进行卷积之后,我不知道如何计算 rgb 值。

  • 假设:灰度图像

    double [][] sobel_x = 
    {
        { -1, 0, 1},
        { -2, 0, 2},
        { -1, 0, 1}
    };
    
    double [][]    sobel_y = 
    {
        { 1, 2, 1},
        { 0, 0, 0},
        {-1, -2, 1}
    };
    
    for(int y=1; y<image.getHeight()-1; y++)
    {
        for(int x=1; x<image.getWidth()-1; x++)
        {
           Color a = new Color(image.getRGB(x-1, y-1));
           Color b = new Color(image.getRGB(x, y-1));
           Color c = new Color(image.getRGB(x+1, y-1));
           Color d = new Color(image.getRGB(x-1, y));
           Color e = new Color(image.getRGB(x, y));
           Color f = new Color(image.getRGB(x+1, y));
           Color g = new Color(image.getRGB(x-1, y+1));
           Color h = new Color(image.getRGB(x, y+1));
           Color i = new Color(image.getRGB(x+1, y+1));
    
            double pixel_x =    (sobel_x[0][0] * a.getRed()) + (sobel_x[0][1] * b.getRed()) + (sobel_x[0][2] * c.getRed()) +
                                (sobel_x[1][0] * d.getRed())   + (sobel_x[1][1] * e.getRed())   + (sobel_x[1][2] * f.getRed()) +
                                (sobel_x[2][0] * g.getRed()) + (sobel_x[2][1] * h.getRed()) + (sobel_x[2][2] * i.getRed());
            double pixel_y = 
                                (sobel_y[0][0] * a.getRed()) + (sobel_x[0][1] * b.getRed()) + (sobel_x[0][2] * c.getRed()) +
                                (sobel_y[1][0] * d.getRed())   + (sobel_x[1][1] * e.getRed())   + (sobel_x[1][2] * f.getRed()) +
                                (sobel_y[2][0] * g.getRed()) + (sobel_x[2][1] * h.getRed()) + (sobel_x[2][2] * i.getRed());  
    
            //Here it is possible to get values between [-1020, 1020]       
    
            //How to going on
    
            //int rgb = (int) Math.sqrt(pixel_x*pixel_x+pixel_y*pixel_y);
    
            //int rgbAsInt = (int)(65536 * rgb + 256 * rgb + rgb);      
        }
    }   
    

【问题讨论】:

    标签: java image-processing edge-detection sobel


    【解决方案1】:

    我的一个想法是进行线性变换。比如你得到的图像中像素值最小的是-998,最大的是1000,那么你可以把-998对应0,1000对应255,然后得到(-998,1000)的比例与(0,255) 的比例并将 [-998,1000] 到 [0,255] 之间的所有值归一化。

    【讨论】:

      【解决方案2】:

      以下图像区域的 x 轴梯度为 1:

      1 2 3
      1 2 3
      1 2 3
      

      应用此过滤器 --

      -1 0 1
      -2 0 2
      -1 0 1
      

      -- 结果为 8。因此 X 和 Y 梯度按该因子缩放。

      您需要决定要在输出图像中表示的最大梯度是多少;称之为“gr_max”。 X 和 Y 梯度应限制在该值:

      float gr_x, gr_y, gr_max = 16;
      
      gr_x /= (gr_max * 8);
      gr_y /= (gr_max * 8);
      
      if (gr_x > 1)
          gr_x = 1;
      if (gr_x < -1)
          gr_x = -1;
      
      if (gr_y > 1)
          gr_y = 1;
      if (gr_y < -1)
          gr_y = -1;
      

      然后,假设您希望输出 RGB 值在 [0, 255] 范围内 --

      int pixel_x = lround((gr_x + 1) * 255/2),
          pixel_y = lround((gr_y + 1) * 255/2);
      

      【讨论】:

        猜你喜欢
        • 2019-03-27
        • 1970-01-01
        • 2017-06-20
        • 2021-03-15
        • 2016-02-24
        • 2018-04-08
        • 1970-01-01
        • 2013-07-22
        • 1970-01-01
        相关资源
        最近更新 更多