2

I'm trying to use perspective to warp the image, but when I try it, the dst image is full black. What can I doing wrong? In my code, the pt0, pt1, pt2 and pt3 are the corner of the polygon what I want do warp My code:

private void perspective(IplImage src, CvPoint pt0, CvPoint pt1, CvPoint pt2, CvPoint pt3) {
    CvPoint2D32f srcQuad = new CvPoint2D32f(4), dstQuad = new CvPoint2D32f(4);
    CvMat warp_matrix = cvCreateMat(3, 3, CV_32FC1);
    IplImage dst = cvCloneImage(src);

    srcQuad.position(0).x(pt0.x()); //src Top left
    srcQuad.position(0).y(pt0.y());
    srcQuad.position(1).x(pt1.x()); //src Top right
    srcQuad.position(1).y(pt1.y());
    srcQuad.position(2).x(pt2.x()); //src Bottom left
    srcQuad.position(2).y(pt2.y());
    srcQuad.position(3).x(pt3.x()); //src Bot right
    srcQuad.position(3).y(pt3.y());

    dstQuad.position(0).x(0); //dst Top left
    dstQuad.position(0).y(0);
    dstQuad.position(1).x(src.width() - 1); //dst Top right
    dstQuad.position(1).y(0);
    dstQuad.position(2).x(0); //dst Bottom left
    dstQuad.position(2).y(src.height() - 1);
    dstQuad.position(3).x(src.width() - 1); //dst Bot right
    dstQuad.position(3).y(src.height() - 1);

    cvGetPerspectiveTransform(srcQuad, dstQuad, warp_matrix);

    cvWarpPerspective(src, dst, warp_matrix);

    cvShowImage("Original", src);
}

Can anyone know what i'm doing wrong? thanks!

well, I found a code in python and edited for JavaCV, the method works well:

private void perspective(IplImage src, CvPoint pt0, CvPoint pt1, CvPoint pt2, CvPoint pt3) {
    CvMat p = cvCreateMat(2, 4, CV_64FC1);
    CvMat h = cvCreateMat(2, 4, CV_64FC1);
    CvMat p2h = cvCreateMat(3, 3, CV_64FC1);
    cvZero(p);
    cvZero(h);
    cvZero(p2h);

    p.put(0, 0, pt0.x());
    p.put(1, 0, pt0.y());
    p.put(0, 1, pt1.x());
    p.put(1, 1, pt1.y());
    p.put(0, 2, pt2.x());
    p.put(1, 2, pt2.y());
    p.put(0, 3, pt3.x());
    p.put(1, 3, pt3.y());

    h.put(0, 0, 0);
    h.put(1, 0, 0);
    h.put(0, 1, src.width());
    h.put(1, 1, 0);
    h.put(0, 2, 0);
    h.put(1, 2, src.height());
    h.put(0, 3, src.width());
    h.put(1, 3, src.height());

    cvFindHomography(p, h, p2h);
    cvWarpPerspective(src, src, p2h);
}

So, I'll accept the only answer for granted, TY!

4

1 回答 1

1

我记得我遇到过类似的问题。您可以尝试增加 dst 图像的大小。例如,使它成为 src 图像大小的两倍。

为了让您更清楚地了解,C++ 语法将是:

Mat dst = Mat::zeros(Size(src.cols*2, src.rows*2), CV_8UC1);

祝你好运 !

于 2013-06-10T09:07:03.600 回答