从位图中裁剪梯形部分并创建一个矩形结果位图。

3
我想使用我已经有的四个坐标点从位图中裁剪出梯形部分,然后将此裁剪后的位图作为矩形图像获取为结果位图。
由于我不熟悉C++本地开发或适用于Android的openCV,因此我想在Android JAVA上实现此操作。
图示 - figure represents problem statement

选择梯形的顶点作为源点,选择最终矩形的顶点作为目标点。使用findHomography找到变换,并使用warpPerspective进行变换。注意顶点的顺序。 - Miki
1个回答

2
    // Set up a source polygon.  
    // X and Y values are "flattened" into the array.
    float[] src = new float[8];
    src[0] = x1;   // from your diagram
    src[1] = y1;
    src[2] = x2;
    src[3] = y2;
    src[4] = x3;
    src[5] = y3;
    src[6] = x4;
    src[7] = y4;

    // set up a dest polygon which is just a rectangle
    float[] dst = new float[8];
    dst[0] = 0;
    dst[1] = 0;
    dst[2] = width;
    dst[3] = 0;
    dst[4] = width;
    dst[5] = height;
    dst[6] = 0;
    dst[7] = height;

    // create a matrix for transformation.
    Matrix matrix = new Matrix();

    // set the matrix to map the source values to the dest values.
    boolean mapped = matrix.setPolyToPoly (src, 0, dst, 0, 4);

    // check to make sure your mapping succeeded
    // if your source polygon is a distorted rectangle, you should be okay
    if (mapped) {

        // create a new bitmap from the original bitmap using the matrix for transform
        Bitmap imageOut = Bitmap.createBitmap(imageIn, 0, 0, width, height, matrix, true);
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接