Perspective correction of UIImage from Points

前端 未结 4 2031
孤独总比滥情好
孤独总比滥情好 2020-12-23 10:30

I\'m working on a app where I\'ll let the user take a picture e.g of a business card or photograph.

The user will then mark the four corners of the object (which the

4条回答
  •  余生分开走
    2020-12-23 11:28

    I'm not sure if you've tried the Opencv library yet, but it has a very nice way to deskew an image. I've got here a small snippet that takes an array of corners, your four corners for example, and a final size to map it into.

    You can read the man page for warpPerspective on the OpenCV site.

    cv::Mat deskew(cv::Mat& capturedFrame, cv::Point2f source_points[], cv::Size finalSize)
    {
        cv::Point2f dest_points[4];
    
        // Output of deskew operation has same color space as source frame, but
        // is proportional to the area the document occupied; this is to reduce
        // blur effects from a scaling component.
        cv::Mat deskewedMat = cv::Mat(finalSize, capturedFrame.type());
    
        cv::Size s = capturedFrame.size();
    
        // Deskew to full output image corners
        dest_points[0] = cv::Point2f(0,s.height); // lower left
        dest_points[1] = cv::Point2f(0,0);        // upper left
        dest_points[2] = cv::Point2f(s.width,0);  // upper right
        dest_points[3] = cv::Point2f(s.width,s.height);  // lower right
    
        // Build quandrangle "de-skew" transform matrix values
        cv::Mat transform = cv::getPerspectiveTransform( source_points, dest_points  );
        // Apply the deskew transform
        cv::warpPerspective( capturedFrame, deskewedMat, transform, s, cv::INTER_CUBIC );
    
        return deskewedMat;
    }
    

提交回复
热议问题