Ask Your Question
0

how I correct the perspective?

asked 2014-06-12 09:33:03 -0600

updated 2014-06-12 13:06:40 -0600

how do I correct the perspective and keeping the aspect ratio of a car plate? this is the code:

Mat src = imread(path);
Mat thr;
cvtColor(src, thr, CV_BGR2GRAY);
threshold(thr, thr, 70, 255, CV_THRESH_BINARY);

vector< vector <Point> > contours; // Vector for storing contour
vector< Vec4i > hierarchy;
int largest_contour_index = 0;
int largest_area = 0;

Mat dst(src.rows, src.cols, CV_8UC1, Scalar::all(0)); //create destination image
findContours(thr.clone(), contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image
for (int i = 0; i< contours.size(); i++){
    double a = contourArea(contours[i], false);  //  Find the area of contour
    if (a>largest_area){
        largest_area = a;
        largest_contour_index = i;                //Store the index of largest contour
    }
}

drawContours(dst, contours, largest_contour_index, Scalar(255, 255, 255), CV_FILLED, 8, hierarchy);
vector<vector<Point> > contours_poly(1);
approxPolyDP(Mat(contours[largest_contour_index]), contours_poly[0], 5, true);
Rect boundRect = boundingRect(contours[largest_contour_index]);

if (contours_poly[0].size() == 4){
    std::vector<Point2f> quad_pts;
    std::vector<Point2f> squre_pts;
    quad_pts.push_back(Point2f(contours_poly[0][0].x, contours_poly[0][0].y));
    quad_pts.push_back(Point2f(contours_poly[0][1].x, contours_poly[0][1].y));
    quad_pts.push_back(Point2f(contours_poly[0][3].x, contours_poly[0][3].y));
    quad_pts.push_back(Point2f(contours_poly[0][2].x, contours_poly[0][2].y));

    squre_pts.push_back(Point2f(boundRect.x, boundRect.y));
    squre_pts.push_back(Point2f(boundRect.x, boundRect.y + boundRect.height));
    squre_pts.push_back(Point2f(boundRect.x + boundRect.width, boundRect.y));
    squre_pts.push_back(Point2f(boundRect.x + boundRect.width, boundRect.y + boundRect.height));

    Mat transmtx = getPerspectiveTransform(quad_pts, squre_pts);
    Mat transformed = Mat::zeros(src.rows, src.cols, CV_8UC3);
    warpPerspective(src, transformed, transmtx, src.size());
    Point P1 = contours_poly[0][0];
    Point P2 = contours_poly[0][1];
    Point P3 = contours_poly[0][2];
    Point P4 = contours_poly[0][3];


    line(src, P1, P2, Scalar(0, 0, 255), 1, CV_AA, 0);
    line(src, P2, P3, Scalar(0, 0, 255), 1, CV_AA, 0);
    line(src, P3, P4, Scalar(0, 0, 255), 1, CV_AA, 0);
    line(src, P4, P1, Scalar(0, 0, 255), 1, CV_AA, 0);
    rectangle(src, boundRect, Scalar(0, 255, 0), 1, 8, 0);
    rectangle(transformed, boundRect, Scalar(0, 255, 0), 1, 8, 0);

    imshow("quadrilateral", transformed);
    imshow("thr", thr);
    imshow("dst", dst);
    imshow("src", src);
    waitKey();
}
else
    cout << "Make sure that your are getting 4 corner using approxPolyDP..." << endl;

these are the images original and result image description image description

How I keep the aspect ratio?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-11-04 07:49:16 -0600

yeuche6 gravatar image

updated 2014-11-05 07:47:24 -0600

You add below lines:

int distanceOfPoints(cv::Point point1, cv::Point point2)
{

int result = (int) sqrt(pow(point1.x - point2.x, 2.0) + pow(point1.y - point2.y, 2.0));

return result;

}

int* hw(cv::Point p1,cv::Point p2,cv::Point p3,cv::Point p4)

{
    int a[2];

    if(distanceOfPoints(p1,p2)<distanceOfPoints(p3,p4))
        a[0]=distanceOfPoints(p3,p4);

    else
        a[0]=distanceOfPoints(p1,p2);

    if(distanceOfPoints(p2,p3)<distanceOfPoints(p4,p1))
        a[1]=distanceOfPoints(p4,p1);

    else
        a[1]=distanceOfPoints(p2,p3);

    return a;
}


int main(){
...............

cv::Point P1 = contours_poly[0][0];
cv::Point P2 = contours_poly[0][1];
cv::Point P3 = contours_poly[0][2];
cv::Point P4 = contours_poly[0][3];
cv::Mat transformed = cv::Mat::zeros(hw(P1,P2,P3,P4)[1], hw(P1,P2,P3,P4)[0], CV_8UC3);

cv::Mat transmtx = getPerspectiveTransform(quad_pts, squre_pts);
warpPerspective(src, transformed, transmtx, transformed.size());
................
}
edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-06-12 09:33:03 -0600

Seen: 1,968 times

Last updated: Nov 05 '14