原文地址:https://blog.csdn.net/plSong_CSDN/article/details/93743821
透視變換
先看一下圖,在牌照的過程中,由於角度問題,難免會出現一些傾斜的問題,如下圖,
我們要解決的就是通過一系列的操作,將上圖變爲
解決的思路
用opencv中的透視變換的API,輸入四個角點的座標,完成透視變換。
看下一主要的兩個opecnv的API
getPerspectiveTransform( InputArray src, InputArray dst );//獲取透視變換矩陣
warpPerspective( InputArray src, OutputArray dst,
InputArray M, Size dsize,
int flags = INTER_LINEAR,
int borderMode = BORDER_CONSTANT,
const Scalar& borderValue = Scalar());//透視變換
從函數的需求上可以看出,透視變換的主要任務就是:找到原圖像的角點。主要流程如下
(1)灰度處理、二值化、形態學操作形成連通區域
(2)輪廓發現、將目標的輪廓繪製出來
(3)在繪製的輪廓中進行直線檢測
(4)找出四條邊,求出四個交點
(5)使用透視變換函數,得到結果
下面是主要的幾個中間效果圖
完全示例代碼
#include<iostream>
#include<opencv2\opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
//input image
Mat src = imread("D:/images/shebaoka.png");
imshow("input image", src);
//bgr 2 gray 轉爲灰度圖像
Mat src_gray;
cvtColor(src, src_gray, COLOR_BGR2GRAY);
//binary 二值化
Mat binary;
threshold(src_gray, binary, 0, 255, THRESH_BINARY_INV|THRESH_OTSU); //THRESH_BINARY_INV二值化後取反
//imshow("binary", binary);//因爲有一些斑點存在
//形態學 閉操作:可以填充小的區域
Mat morhp_img;
Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1));
morphologyEx(binary, morhp_img, MORPH_CLOSE, kernel, Point(-1, -1), 3);
//imshow("morphology", morhp_img);
Mat dst;
bitwise_not(morhp_img, dst);//在取反
imshow("dst", dst);//
//輪廓發現
vector<vector<Point>> contous;
vector<Vec4i> hireachy;
findContours(dst, contous, hireachy, CV_RETR_TREE, CHAIN_APPROX_SIMPLE, Point());
cout << "contous.size:" << contous.size() << endl;
//輪廓繪製
int width = src.cols;
int height = src.rows;
Mat drawImage = Mat::zeros(src.size(), CV_8UC3);
cout << contous.size() << endl;
for (size_t t = 0; t < contous.size(); t++)
{
Rect rect = boundingRect(contous[t]);
if (rect.width > width / 2 && rect.height > height / 2 && rect.width<width-5 && rect.height<height-5)
{
drawContours(drawImage, contous, static_cast<int>(t), Scalar(0, 0, 255), 2, 8, hireachy, 0, Point(0, 0));
}
}
imshow("contours", drawImage);//顯示找到的輪廓
//直線檢測
vector<Vec4i> lines;
Mat contoursImg;
int accu = min(width*0.5, height*0.5);
cvtColor(drawImage, contoursImg, COLOR_BGR2GRAY);
imshow("contours", contoursImg);
Mat linesImage = Mat::zeros(src.size(), CV_8UC3);
HoughLinesP(contoursImg, lines, 1, CV_PI / 180.0, accu,accu,0);
for (size_t t = 0; t < lines.size(); t++)
{
Vec4i ln = lines[t];
line(linesImage, Point(ln[0], ln[1]), Point(ln[2], ln[3]), Scalar(0, 0, 255), 2, 8, 0);//繪製直線
}
cout << "number of lines:"<<lines.size() << endl;
imshow("linesImages", linesImage);
//尋找與定位上下 左右 四條直線
int deltah = 0; //高度差
int deltaw = 0; //寬度差
Vec4i topLine, bottomLine; //直線定義
Vec4i rightLine, leftLine;
for (int i = 0; i < lines.size(); i++)
{
Vec4i ln = lines[i];//?????
/*
Opencv中的累計霍夫變換HoughLinesP(),輸出的是一個Vector of Vec4i,
Vector每個元素代表一條直線,是由一個4元浮點數構成,
前兩個一組x_1,y_1,後兩個一組x_2,y_2,代表了圖像中直線的起始點和結束點。
*/
deltah = abs(ln[3] - ln[1]); //計算高度差(y2-y1)
//topLine
if (ln[3] < height / 2.0 && ln[1] < height / 2.0 && deltah < accu - 1)
{
topLine = lines[i];
}
//bottomLine
if (ln[3] > height / 2.0 && ln[1] >height / 2.0 && deltah < accu - 1)
{
bottomLine = lines[i];
}
deltaw = abs(ln[2] - ln[0]); //計算寬度差(x2-x1)
//leftLine
if (ln[0] < height / 2.0 && ln[2] < height / 2.0 && deltaw < accu - 1)
{
leftLine = lines[i];
}
//rightLine
if (ln[0] > width / 2.0 && ln[2] >width / 2.0 && deltaw < accu - 1)
{
rightLine = lines[i];
}
}
// 打印四條線的座標
cout << "topLine : p1(x,y)= " << topLine[0] << "," << topLine[1] << "; p2(x,y)= " << topLine[2] << "," << topLine[3] << endl;
cout << "bottomLine : p1(x,y)= " << bottomLine[0] << "," << bottomLine[1] << "; p2(x,y)= " << bottomLine[2] << "," << bottomLine[3] << endl;
cout << "leftLine : p1(x,y)= " << leftLine[0] << "," << leftLine[1] << "; p2(x,y)= " << leftLine[2] << "," << leftLine[3] << endl;
cout << "rightLine : p1(x,y)= " << rightLine[0] << "," << rightLine[1] << "; p2(x,y)= " << rightLine[2] << "," << rightLine[3] << endl;
//擬合四條直線
float k1, k2, k3, k4, c1, c2, c3, c4;
k1 = float(topLine[3] - topLine[1]) / float(topLine[2] - topLine[0]);
c1 = topLine[1] - k1*topLine[0];
k2 = float(bottomLine[3] - bottomLine[1]) / float(bottomLine[2] - bottomLine[0]);
c2 = bottomLine[1] - k2*bottomLine[0];
k3 = float(leftLine[3] - leftLine[1]) / float(leftLine[2] - leftLine[0]);
c3 = leftLine[1] - k3*leftLine[0];
k4 = float(rightLine[3] - rightLine[1]) / float(rightLine[2] - rightLine[0]);
c4 = rightLine[1] - k4*rightLine[0];
//求四個角點,
Point p1;//topLine leftLine 左上角
p1.x = static_cast<int>(c1 - c3) / k3 - k1;
p1.y = k1*p1.x + c1;
Point p2;//topLine rightLine 右上角
p2.x = static_cast<int>(c1 - c4) / k4 - k1;
p2.y = k1*p2.x + c1;
Point p3;//bottomLine leftLine 左下角
p3.x = static_cast<int>(c2 - c3) / k3 - k2;
p3.y = k2*p3.x + c2;
Point p4;//bottomLine rightLine 右下角
p4.x = static_cast<int>(c2 - c4) / k4 - k2;
p4.y = k2*p4.x + c2;
cout << "Point p1: (" << p1.x << "," << p1.y << ")" << endl;
cout << "Point p2: (" << p2.x << "," << p2.y << ")" << endl;
cout << "Point p3: (" << p3.x << "," << p3.y << ")" << endl;
cout << "Point p4: (" << p4.x << "," << p4.y << ")" << endl;
//顯示四個點
circle(linesImage, p1, 2, Scalar(0,255, 0), 2);
circle(linesImage, p2, 2, Scalar(0,255, 0), 2);
circle(linesImage, p3, 2, Scalar(0, 255, 0), 2);
circle(linesImage, p4, 2, Scalar(0, 255, 0), 2);
imshow("find four points", linesImage);
//透視變換
vector<Point2f> src_corners(4);
src_corners[0] = p1;
src_corners[1] = p2;
src_corners[2] = p3;
src_corners[3] = p4;
Mat result_images = Mat::zeros(height*0.7, width*0.9, CV_8UC3);
vector<Point2f> dst_corners(4);
dst_corners[0] = Point(0, 0);
dst_corners[1] = Point(result_images.cols, 0);
dst_corners[2] = Point(0, result_images.rows);
dst_corners[3] = Point(result_images.cols, result_images.rows);
Mat warpmatrix = getPerspectiveTransform(src_corners, dst_corners); //獲取透視變換矩陣
warpPerspective(src, result_images, warpmatrix, result_images.size()); //透視變換
imshow("final result", result_images);
imwrite("D:/images/warpPerspective.png", result_images);
waitKey(0);
return 0;
}
控制檯的輸出
***** VIDEOINPUT LIBRARY - 0.1995 - TFW07 *****
contous.size:6
6
number of lines:5
topLine : p1(x,y)= 20,64; p2(x,y)= 487,56
bottomLine : p1(x,y)= 145,329; p2(x,y)= 361,332
leftLine : p1(x,y)= 13,75; p2(x,y)= 47,320
rightLine : p1(x,y)= 479,328; p2(x,y)= 497,66
Point p1: (11,64)
Point p2: (497,55)
Point p3: (47,327)
Point p4: (479,333)
————————————————