问题描述
拍摄或者扫描图像不是规则的矩形,会对后期处理产生不 好影响,需要通过透视变换校正得到正确形状。
解决思路
通过二值分割 + 形态学方法 + Hough直线 +透视变换

opencv实践::透视变换-LMLPHP

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h> using namespace cv;
using namespace std; int main(int argc, char** argv) {
Mat src = imread("D:/case6.png");
if (src.empty()) {
printf("could not load image...\n");
return ;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image", src); // 二值处理 取反
Mat gray_src, binary, dst;
cvtColor(src, gray_src, COLOR_BGR2GRAY);
threshold(gray_src, binary, , , THRESH_BINARY_INV | THRESH_OTSU);
//imshow("binary image", binary); // 形态学操作
Mat kernel = getStructuringElement(MORPH_RECT, Size(, ), Point(-, -));
morphologyEx(binary, dst, MORPH_CLOSE, kernel, Point(-, -), );
//imshow("morphology", dst); // 轮廓发现
bitwise_not(dst, dst, Mat());
vector<vector<Point>> contours;
vector<Vec4i> hireachy;
findContours(dst, contours, hireachy, CV_RETR_TREE, CHAIN_APPROX_SIMPLE, Point()); // 轮廓绘t制
int width = src.cols;
int height = src.rows;
Mat drawImage = Mat::zeros(src.size(), CV_8UC3);
for (size_t t = ; t < contours.size(); t++) {
Rect rect = boundingRect(contours[t]);
if (rect.width > width / && rect.width < width - ) {
drawContours(drawImage, contours, static_cast<int>(t), Scalar(, , ), , , hireachy, , Point());
}
}
//imshow("contours", drawImage); vector<Vec4i> lines;
Mat contoursImg;
int accu = min(width*0.5, height*0.5);
cvtColor(drawImage, contoursImg, COLOR_BGR2GRAY);
HoughLinesP(contoursImg, lines, CV_HOUGH_PROBABILISTIC, CV_PI / 200.0, accu, accu, );
Mat linesImage = Mat::zeros(src.size(), CV_8UC3);
for (size_t t = ; t < lines.size(); t++) {
Vec4i ln = lines[t];
line(linesImage, Point(ln[], ln[]), Point(ln[], ln[]), Scalar(, , ), , , );
}
printf("number of lines : %d\n", lines.size());
//imshow("lines image", linesImage); // 寻找与定位上下左右四条直线
int deltah = ;
Vec4i topLine, bottomLine;
Vec4i leftLine, rightLine;
for (int i = ; i < lines.size(); i++) {
Vec4i ln = lines[i];
deltah = abs(ln[] - ln[]);
if (ln[] < height / 2.0 && ln[] < height / 2.0 && deltah < accu - ) {
if (topLine[] > ln[] && topLine[] > ) {
topLine = lines[i];
}
else {
topLine = lines[i];
}
}
if (ln[] > height / 2.0 && ln[] > height / 2.0 && deltah < accu - ) {
bottomLine = lines[i];
}
if (ln[] < width / 2.0 && ln[] < width / 2.0) {
leftLine = lines[i];
}
if (ln[] > width / 2.0 && ln[] > width / 2.0) {
rightLine = lines[i];
}
}
cout << "top line : p1(x, y) = " << topLine[] << "," << topLine[] << " p2(x, y) = " << topLine[] << "," << topLine[] << endl;
cout << "bottom line : p1(x, y) = " << bottomLine[] << "," << bottomLine[] << " p2(x, y) = " << bottomLine[] << "," << bottomLine[] << endl;
cout << "left line : p1(x, y) = " << leftLine[] << "," << leftLine[] << " p2(x, y) = " << leftLine[] << "," << leftLine[] << endl;
cout << "right line : p1(x, y) = " << rightLine[] << "," << rightLine[] << " p2(x, y) = " << rightLine[] << "," << rightLine[] << endl; // 拟合四条直线方程,求直线相交的点
float k1, c1;
k1 = float(topLine[] - topLine[]) / float(topLine[] - topLine[]);
c1 = topLine[] - k1 * topLine[];
float k2, c2;
k2 = float(bottomLine[] - bottomLine[]) / float(bottomLine[] - bottomLine[]);
c2 = bottomLine[] - k2 * bottomLine[];
float k3, c3;
k3 = float(leftLine[] - leftLine[]) / float(leftLine[] - leftLine[]);
c3 = leftLine[] - k3 * leftLine[];
float k4, c4;
k4 = float(rightLine[] - rightLine[]) / float(rightLine[] - rightLine[]);
c4 = rightLine[] - k4 * rightLine[]; // 四条直线交点
Point p1; // 左上角
p1.x = static_cast<int>((c1 - c3) / (k3 - k1));
p1.y = static_cast<int>(k1*p1.x + c1);
Point p2; // 右上角
p2.x = static_cast<int>((c1 - c4) / (k4 - k1));
p2.y = static_cast<int>(k1*p2.x + c1);
Point p3; // 左下角
p3.x = static_cast<int>((c2 - c3) / (k3 - k2));
p3.y = static_cast<int>(k2*p3.x + c2);
Point p4; // 右下角
p4.x = static_cast<int>((c2 - c4) / (k4 - k2));
p4.y = static_cast<int>(k2*p4.x + c2);
cout << "p1(x, y)=" << p1.x << "," << p1.y << endl;
cout << "p2(x, y)=" << p2.x << "," << p2.y << endl;
cout << "p3(x, y)=" << p3.x << "," << p3.y << endl;
cout << "p4(x, y)=" << p4.x << "," << p4.y << endl; // 显示四个点坐标
circle(linesImage, p1, , Scalar(, , ), , , );
circle(linesImage, p2, , Scalar(, , ), , , );
circle(linesImage, p3, , Scalar(, , ), , , );
circle(linesImage, p4, , Scalar(, , ), , , );
line(linesImage, Point(topLine[], topLine[]), Point(topLine[], topLine[]), Scalar(, , ), , , );
//imshow("four corners", linesImage); // 透视变换
vector<Point2f> src_corners();
src_corners[] = p1;
src_corners[] = p2;
src_corners[] = p3;
src_corners[] = p4; vector<Point2f> dst_corners();
dst_corners[] = Point(, );
dst_corners[] = Point(width, );
dst_corners[] = Point(, height);
dst_corners[] = Point(width, height); // 获取透视变换矩阵
Mat resultImage;
Mat warpmatrix = getPerspectiveTransform(src_corners, dst_corners);
warpPerspective(src, resultImage, warpmatrix, resultImage.size(), INTER_LINEAR);
namedWindow("Final Result", CV_WINDOW_AUTOSIZE);
imshow("Final Result", resultImage); waitKey();
return ;
}
05-28 23:06