OpenCV getPerspectiveTransform和warpPerspective Java [英] OpenCV getPerspectiveTransform and warpPerspective Java

查看:483
本文介绍了OpenCV getPerspectiveTransform和warpPerspective Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个 ArrayList< MatOfPoint> ,它存储图像的轮廓并检索出最大的轮廓,这是我对warpPerspective的目标。

I current have a ArrayList<MatOfPoint> which stores the contours of the image and have retrieved the largest contour which is my target for the warpPerspective.

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);

我在如何使用getPerspectiveTransform然后应用warpPerspective更改图像方面遇到了麻烦它将适合450x450的图像。

I am having trouble on how to use the getPerspectiveTransform and then applying the warpPerspective to change the image so that it will fit within a 450x450 image.

我在python中找到了一个非常好的例子,不过我对它不是很熟悉有人可以解释我会如何纠正在java?

I found an pretty good example of this in python, however I am not very familiar with it can someone explain how I would do rectify in java?

def rectify(h):
    ''' this function put vertices of square we got, in clockwise order '''
    h = h.reshape((4,2))
    hnew = np.zeros((4,2),dtype = np.float32)

    add = h.sum(1)
    hnew[0] = h[np.argmin(add)]
    hnew[2] = h[np.argmax(add)]

    diff = np.diff(h,axis = 1)
    hnew[1] = h[np.argmin(diff)]
    hnew[3] = h[np.argmax(diff)]

    return hnew

这里是源链接: https://github.com/abidrahmank/OpenCV2-Python/blob/master/ OpenCV_Python_Blog / sudoku_v_0.0.6 / sudoku.py

推荐答案

简介:使用wrapPerspective我们需要使用getPerspectiveTransform来计算包裹垫:Mat src和Mat dst。那些是你的轮廓的src(左下角,右下角,左上角,右上角)的MarOfPoints2f和相应的mat dst(0,0; ​​0,449; 449,0; 449,449)(所以图像将是450x450 - 如果这是错误的,请更正我。)

Intro: to make use of wrapPerspective we need to calculate wrapping mat using getPerspectiveTransform takes: Mat src and Mat dst. Those are MarOfPoints2f with src(bottom-left, bottom-right, top-left, top-right) of your contour and corresponding mat dst( 0,0 ; 0,449 ; 449,0 ; 449,449) (So the image will be 450x450 - If this is wrong please correct me).

解决方案在Java中用于opencv 3将是:

Solution in Java for opencv 3 would be:


  1. 获得点数
    矩形轮廓为 MatOfPoints2f ,我们需要按上面所述对点进行排序:

  1. Get points sorted Having rectangular contour as MatOfPoints2f approx we need to sort the points as written above:

//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++;
    }
}


  • 准备Mat src和det

  • Prepare Mat src and det

            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)      
                    );
    


  • 使用getPerspectiveTransform和wrapPrespective

  • Use getPerspectiveTransform and wrapPrespective

        Mat warpMat = Imgproc.getPerspectiveTransform(src,dst);
        //This is you new image as Mat
        Mat destImage = new Mat();
        Imgproc.warpPerspective(originalImg, destImage, warpMat, originalImg.size());
    


  • 我刚刚完成了类似的项目,所以我我相信这可以做得更好,但如果有人需要,这是一个有效的解决方案。

    I have just finished doing similar project so I am sure this can be done better but it is a working solution if anyone needs one.

    这篇关于OpenCV getPerspectiveTransform和warpPerspective Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆