Image Processing - Implementing Sobel Filter

后端 未结 8 1734
梦毁少年i
梦毁少年i 2020-12-13 02:46

I\'ve got a task to implement Sobel filter which is, as you know, an image processing filter for edge detection. But unfortunately, I\'ve got no experience in image processi

8条回答
  •  时光说笑
    2020-12-13 03:06

    Sobel Operator Wikipedia page is well descriptive about how to perform it. There other operators such as Roberts cross and Prewitt

    Using convolution operation, you can switch the approach by changing the kernel matrix. Below, the implementation of Sobel and Convolution using Marvin Framework may help you.

    Sobel:

    public class Sobel extends MarvinAbstractImagePlugin{
    
        // Definitions
        double[][] matrixSobelX = new double[][]{
                {1,     0,  -1},
                {2,     0,  -2},
                {1,     0,  -1}
        };
        double[][] matrixSobelY = new double[][]{
                {-1,    -2,     -1},
                {0,     0,      0},
                {1,     2,      1}
        };
    
        private MarvinImagePlugin   convolution;
    
        public void load(){
            convolution = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.convolution.jar");
        }
    
        public MarvinAttributesPanel getAttributesPanel(){
            return null;
        }
        public void process
        (
            MarvinImage imageIn, 
            MarvinImage imageOut,
            MarvinAttributes attrOut,
            MarvinImageMask mask, 
            boolean previewMode
        )
        {
            convolution.setAttribute("matrix", matrixSobelX);
            convolution.process(imageIn, imageOut, null, mask, previewMode);
            convolution.setAttribute("matrix", matrixSobelY);
            convolution.process(imageIn, imageOut, null, mask, previewMode);
        }
    }
    

    Convolution:

    public class Convolution extends MarvinAbstractImagePlugin{
    
        private MarvinAttributesPanel   attributesPanel;
        private MarvinAttributes        attributes;
    
        public void process
        (
            MarvinImage imageIn, 
            MarvinImage imageOut,
            MarvinAttributes attributesOut,
            MarvinImageMask mask, 
            boolean previewMode
        )
        {
            double[][] matrix = (double[][])attributes.get("matrix");
    
            if(matrix != null && matrix.length > 0){
                for(int y=0; y= 0 && nx < imageOut.getWidth() && ny >= 0 && ny < imageOut.getHeight()){
    
                            resultRed   +=  (matrix[i][j]*(imageIn.getIntComponent0(nx, ny)));
                            resultGreen +=  (matrix[i][j]*(imageIn.getIntComponent1(nx, ny)));
                            resultBlue  +=  (matrix[i][j]*(imageIn.getIntComponent2(nx, ny)));
                        }
    
    
                    }
    
    
    
                }
            }
    
            resultRed   = Math.abs(resultRed);
            resultGreen = Math.abs(resultGreen);
            resultBlue = Math.abs(resultBlue);
    
            // allow the combination of multiple appications
            resultRed   += imageOut.getIntComponent0(x,y);
            resultGreen += imageOut.getIntComponent1(x,y);
            resultBlue  += imageOut.getIntComponent2(x,y);
    
            resultRed   = Math.min(resultRed, 255);
            resultGreen = Math.min(resultGreen, 255);
            resultBlue  = Math.min(resultBlue, 255);
    
            resultRed   = Math.max(resultRed, 0);
            resultGreen = Math.max(resultGreen, 0);
            resultBlue  = Math.max(resultBlue, 0);
    
            imageOut.setIntColor(x, y, imageIn.getAlphaComponent(x, y), (int)resultRed, (int)resultGreen, (int)resultBlue);
        }
    
        public void load(){
            attributes = getAttributes();
            attributes.set("matrix", null);
        }
    
        public MarvinAttributesPanel getAttributesPanel(){
            if(attributesPanel == null){
                attributesPanel = new MarvinAttributesPanel();
                attributesPanel.addMatrixPanel("matrixPanel", "matrix", attributes, 3, 3);
            }
            return attributesPanel;
        }
    
    }
    

提交回复
热议问题