Image Processing Edge Detection in Java

后端 未结 4 474
故里飘歌
故里飘歌 2021-02-06 09:33

This is my situation. It involves aligning a scanned image which will account for incorrect scanning. I must align the scanned image with my Java program.

These are more

4条回答
  •  礼貌的吻别
    2021-02-06 10:22

    Try to start from a simple scenario and then improve the approach.

    1. Detect corners.
    2. Find the corners in the boundaries of the form.
    3. Using the form corners coordinates, calculate the rotation angle.
    4. Rotate/scale the image.
    5. Map the position of each field in the form relative to form origin coordinates.
    6. Match the textboxes.

    The program presented at the end of this post does the steps 1 to 3. It was implemented using Marvin Framework. The image below shows the output image with the detected corners.

    enter image description here

    The program also outputs: Rotation angle:1.6365770416167182

    Source code:

    import java.awt.Color;
    import java.awt.Point;
    import marvin.image.MarvinImage;
    import marvin.io.MarvinImageIO;
    import marvin.plugin.MarvinImagePlugin;
    import marvin.util.MarvinAttributes;
    import marvin.util.MarvinPluginLoader;
    
    public class FormCorners {
    
    public FormCorners(){
        // Load plug-in
        MarvinImagePlugin moravec = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.corner.moravec");
        MarvinAttributes attr = new MarvinAttributes();
    
        // Load image
        MarvinImage image = MarvinImageIO.loadImage("./res/printedForm.jpg");
    
        // Process and save output image
        moravec.setAttribute("threshold", 2000);
        moravec.process(image, null, attr);
        Point[] boundaries = boundaries(attr);
        image = showCorners(image, boundaries, 12);
        MarvinImageIO.saveImage(image, "./res/printedForm_output.jpg");
    
        // Print rotation angle
        double angle =  (Math.atan2((boundaries[1].y*-1)-(boundaries[0].y*-1),boundaries[1].x-boundaries[0].x) * 180 / Math.PI);
        angle =  angle >= 0 ? angle : angle + 360;
        System.out.println("Rotation angle:"+angle);
    }
    
    private Point[] boundaries(MarvinAttributes attr){
        Point upLeft = new Point(-1,-1);
        Point upRight = new Point(-1,-1);
        Point bottomLeft = new Point(-1,-1);
        Point bottomRight = new Point(-1,-1);
        double ulDistance=9999,blDistance=9999,urDistance=9999,brDistance=9999;
        double tempDistance=-1;
        int[][] cornernessMap = (int[][]) attr.get("cornernessMap");
    
        for(int x=0; x 0){
                    if((tempDistance = Point.distance(x, y, 0, 0)) < ulDistance){
                        upLeft.x = x; upLeft.y = y;
                        ulDistance = tempDistance;
                    } 
                    if((tempDistance = Point.distance(x, y, cornernessMap.length, 0)) < urDistance){
                        upRight.x = x; upRight.y = y;
                        urDistance = tempDistance;
                    }
                    if((tempDistance = Point.distance(x, y, 0, cornernessMap[0].length)) < blDistance){
                        bottomLeft.x = x; bottomLeft.y = y;
                        blDistance = tempDistance;
                    }
                    if((tempDistance = Point.distance(x, y, cornernessMap.length, cornernessMap[0].length)) < brDistance){
                        bottomRight.x = x; bottomRight.y = y;
                        brDistance = tempDistance;
                    }
                }
            }
        }
        return new Point[]{upLeft, upRight, bottomRight, bottomLeft};
    }
    
    private MarvinImage showCorners(MarvinImage image, Point[] points, int rectSize){
        MarvinImage ret = image.clone();
        for(Point p:points){
            ret.fillRect(p.x-(rectSize/2), p.y-(rectSize/2), rectSize, rectSize, Color.red);
        }
        return ret;
    }
    
    public static void main(String[] args) {
        new FormCorners();
    }
    }
    

提交回复
热议问题