首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用OpenCV和Java实现自动透视校正

使用OpenCV和Java实现自动透视校正
EN

Stack Overflow用户
提问于 2014-10-24 16:01:45
回答 1查看 3.2K关注 0票数 0

我正在考虑对从安卓相机拍摄的图像进行透视校正,类似于之前发布的this question中的定义。但是,我找不到合适的Java代码。

如果有人能在Java上工作,请帮助我。

EN

回答 1

Stack Overflow用户

发布于 2014-10-24 18:39:15

我找到了我的问题here的答案。我在下面放了一些精炼的代码片段:

代码语言:javascript
运行
复制
public static void correctPerspective() {

    String fileName = "IMG_20141024_132131.jpg";
    Mat imgSource = Highgui.imread(fileName, Highgui.CV_LOAD_IMAGE_UNCHANGED);
    // convert the image to black and white does (8 bit)
    Imgproc.Canny(imgSource.clone(), imgSource, 50, 50);

    // apply gaussian blur to smoothen lines of dots
    Imgproc.GaussianBlur(imgSource, imgSource, new org.opencv.core.Size(5, 5), 5);

    // find the contours
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

    double maxArea = -1;
    MatOfPoint temp_contour = contours.get(0); // the largest is at the
                                                // index 0 for starting
                                                // point
    MatOfPoint2f approxCurve = new MatOfPoint2f();

    for (int idx = 0; idx < contours.size(); idx++) {
        temp_contour = contours.get(idx);
        double contourarea = Imgproc.contourArea(temp_contour);
        // compare this contour to the previous largest contour found
        if (contourarea > maxArea) {
            // check if this contour is a square
            MatOfPoint2f new_mat = new MatOfPoint2f(temp_contour.toArray());
            int contourSize = (int) temp_contour.total();
            MatOfPoint2f approxCurve_temp = new MatOfPoint2f();
            Imgproc.approxPolyDP(new_mat, approxCurve_temp, contourSize * 0.05, true);
            if (approxCurve_temp.total() == 4) {
                maxArea = contourarea;
                approxCurve = approxCurve_temp;
            }
        }
    }

    Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BayerBG2RGB);
    Mat sourceImage = Highgui.imread(fileName, Highgui.CV_LOAD_IMAGE_UNCHANGED);
    double[] temp_double;
    temp_double = approxCurve.get(0, 0);
    Point p1 = new Point(temp_double[0], temp_double[1]);
    // Core.circle(imgSource,p1,55,new Scalar(0,0,255));
    // Imgproc.warpAffine(sourceImage, dummy, rotImage,sourceImage.size());
    temp_double = approxCurve.get(1, 0);
    Point p2 = new Point(temp_double[0], temp_double[1]);
    // Core.circle(imgSource,p2,150,new Scalar(255,255,255));
    temp_double = approxCurve.get(2, 0);
    Point p3 = new Point(temp_double[0], temp_double[1]);
    // Core.circle(imgSource,p3,200,new Scalar(255,0,0));
    temp_double = approxCurve.get(3, 0);
    Point p4 = new Point(temp_double[0], temp_double[1]);
    // Core.circle(imgSource,p4,100,new Scalar(0,0,255));
    List<Point> source = new ArrayList<Point>();
    source.add(p1);
    source.add(p2);
    source.add(p3);
    source.add(p4);
    Mat startM = Converters.vector_Point2f_to_Mat(source);
    Mat result = warp(sourceImage, startM);

    Highgui.imwrite("corrected.jpg", result);
}

public static Mat warp(Mat inputMat, Mat startM) {

    int resultWidth = 1200;
    int resultHeight = 680;

    Point ocvPOut4 = new Point(0, 0);
    Point ocvPOut1 = new Point(0, resultHeight);
    Point ocvPOut2 = new Point(resultWidth, resultHeight);
    Point ocvPOut3 = new Point(resultWidth, 0);

    if (inputMat.height() > inputMat.width()) {
        // int temp = resultWidth;
        // resultWidth = resultHeight;
        // resultHeight = temp;

        ocvPOut3 = new Point(0, 0);
        ocvPOut4 = new Point(0, resultHeight);
        ocvPOut1 = new Point(resultWidth, resultHeight);
        ocvPOut2 = new Point(resultWidth, 0);
    }

    Mat outputMat = new Mat(resultWidth, resultHeight, CvType.CV_8UC4);

    List<Point> dest = new ArrayList<Point>();
    dest.add(ocvPOut1);
    dest.add(ocvPOut2);
    dest.add(ocvPOut3);
    dest.add(ocvPOut4);

    Mat endM = Converters.vector_Point2f_to_Mat(dest);

    Mat perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM);

    Imgproc.warpPerspective(inputMat, outputMat, perspectiveTransform, new Size(resultWidth, resultHeight), Imgproc.INTER_CUBIC);

    return outputMat;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26543764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档