Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OpenCV getPerspectiveTransform和Java透视图_Java_Opencv_Transformation_Perspective - Fatal编程技术网

OpenCV getPerspectiveTransform和Java透视图

OpenCV getPerspectiveTransform和Java透视图,java,opencv,transformation,perspective,Java,Opencv,Transformation,Perspective,我当前有一个ArrayList,它存储图像的轮廓,并检索到最大的轮廓,这是我的透视目标 List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Imgproc.findContours(matOut, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); sort(contours); MatOfPoint2f m2f = M

我当前有一个
ArrayList
,它存储图像的轮廓,并检索到最大的轮廓,这是我的透视目标

List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(matOut, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

sort(contours);
MatOfPoint2f  m2f = MatOfPoint2f(contours.get(0).toArray());
double arc = Imgproc.arcLength(m2f, true);
MatOfPoint2f approx = new MatOfPoint2f();
Imgproc.approxPolyDP(m2f, approx, arc*0.02, true);

这里是源链接:

简介:要使用wrapPerspective,我们需要使用getPerspectiveTransform计算包装垫:mat src和mat dst。这些是带有轮廓src(左上、右上、左下、右下)和相应的mat dst(0,0;0449;449,0;449449)的MatOfPoints2f(因此图像将为450x450-如果这是错误的,请纠正我)

opencv 3的Java解决方案是:

  • 整理积分 矩形轮廓为MatOfPoints2f约我们需要按照上面所述对点进行排序:

    //calculate the center of mass of our contour image using moments
    Moments moment = Imgproc.moments(approx.get(0));
    int x = (int) (moment.get_m10() / moment.get_m00());
    int y = (int) (moment.get_m01() / moment.get_m00());
    
    //SORT POINTS RELATIVE TO CENTER OF MASS 
    Point[] sortedPoints = new Point[4];
    
    double[] data;
    int count = 0;
    for(int i=0; i<approx.get(0).rows(); i++){
        data = approx.get(0).get(i, 0);
        double datax = data[0];
        double datay = data[1];
        if(datax < x && datay < y){
            sortedPoints[0]=new Point(datax,datay);
            count++;
        }else if(datax > x && datay < y){
            sortedPoints[1]=new Point(datax,datay);
            count++;
        }else if (datax < x && datay > y){
            sortedPoints[2]=new Point(datax,datay);
            count++;
        }else if (datax > x && datay > y){
            sortedPoints[3]=new Point(datax,datay);
            count++;
        }
    }
    
  • 使用getPerspectiveTransform和WrapSpective

        Mat warpMat = Imgproc.getPerspectiveTransform(src,dst);
        //This is you new image as Mat
        Mat destImage = new Mat();
        Imgproc.warpPerspective(originalImg, destImage, warpMat, originalImg.size());
    
  • 我刚刚完成了类似的项目,所以我相信这可以做得更好,但这是一个工作的解决方案,如果任何人需要一个

            MatOfPoint2f src = new MatOfPoint2f(
                    sortedPoints[0],
                    sortedPoints[1],
                    sortedPoints[2],
                    sortedPoints[3]);
    
            MatOfPoint2f dst = new MatOfPoint2f(
                    new Point(0, 0),
                    new Point(450-1,0),
                    new Point(0,450-1),
                    new Point(450-1,450-1)      
                    );
    
        Mat warpMat = Imgproc.getPerspectiveTransform(src,dst);
        //This is you new image as Mat
        Mat destImage = new Mat();
        Imgproc.warpPerspective(originalImg, destImage, warpMat, originalImg.size());