例子。
你可以在这里找到 github 存储库:https://github.com/okanyenigun/cpp-opencv-warping
代码:
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/objdetect.hpp>
#include <iostream>
using namespace cv;
using namespace std;
string PATH = "funk.jpg"; //Image Path
int AREA_FILTER = 1000;
Mat imgOrg, imgProc, imgWarp;
vector<Point> initialPoints, docPoints;
int w = 420, h = 596;
Mat preProcessing(Mat img)
{
cvtColor(img, imgProc, COLOR_BGR2GRAY); // to gray scale
GaussianBlur(imgProc, imgProc, Size(3,3), 3, 0); // blurring for better canny performance
Canny(imgProc, imgProc, 25, 75); // edge detection
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
dilate(imgProc, imgProc, kernel);
return imgProc;
}
vector<Point> getContours(Mat imgDil){
//detects the biggest rectangle in image
vector<vector<Point>> contours; //vectors example: {
{Point(20,30),Point(50,60)},{},{}}
vector<Vec4i> hierarchy;
findContours(imgDil,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE); //finding contours
vector<vector<Point>> conPoly(contours.size());
vector<Rect> boundRect(contours.size());
vector<Point> biggest;
int maxArea=0;
for (int i=0;i<contours.size();i++){
int