會用到的函數:
1、Mat getStructuringElement(int shape, Size esize, Point anchor = Point(-1, -1));
這個函數的第一個參數表示核心的形狀,有三種形狀可以選擇。
矩形:MORPH_RECT;
交叉形:MORPH_CROSS;
橢圓形:MORPH_ELLIPSE;
第二和第三個參數分别是核心的尺寸以及錨點的位置。一般在調用erode以及dilate函數之前,先定義一個Mat類型的變量來獲得getStructuringElement函數的傳回值。對于錨點的位置,有預設值Point(-1,-1),表示錨點位于中心點。element形狀唯一依賴錨點位置,其他情況下,錨點隻是影響了形态學運算結果的偏移。
核心:
錨點:
2、morphologyEx(形态學操作)
void morphologyEx( InputArray src, OutputArray dst,int op, InputArray kernel,Point anchor = Point(-1,-1), int iterations = 1,int borderType = BORDER_CONSTANT,const Scalar& borderValue = morphologyDefaultBorderValue() )
參數解釋
src:源圖像Mat對象
dst:目标圖像Mat對象
op:操作的類型,通過源碼我們得知總共有以下幾種類型:
enum MorphTypes{
MORPH_ERODE = 0, //腐蝕
MORPH_DILATE = 1, //膨脹
其實内部就是進行了先腐蝕後膨脹的操作)
MORPH_OPEN = 2, //開操作(
MORPH_CLOSE = 3, //閉操作
MORPH_GRADIENT = 4, //梯度操作
MORPH_TOPHAT = 5, //頂帽操作
MORPH_BLACKHAT = 6, //黑帽操作
MORPH_HITMISS = 7.
}
kernel:用于膨脹操作的結構元素,如果取值為Mat(),那麼預設使用一個3 x 3 的方形結構元素,可以使用getStructuringElement()來建立結構元素
anchor:參考點,其預設值為(-1,-1)說明位于kernel的中心位置。
borderType :邊緣類型,預設為BORDER_CONSTANT。
borderValue :邊緣值,用它的預設值即可。
3、boundingRect(InputArray points)
points:輸入資訊,可以為包含點的容器(vector)或是Mat。
傳回包覆輸入資訊的最小正矩形。
4、HoughLinesP(InputArray image, OutputArray lines, double rho, double theta, int threshold, double minLineLength=0, double maxLineGap=0 )
函數作用:将輸入圖像按照給出參數要求提取線段,放在lines中。
第一個參數,InputArray類型的image,輸入圖像。
第二個參數,InputArray類型的lines,經過調用HoughLinesP函數後後存儲了檢測到的線條的輸出矢量,每一條線由具有四個元素的矢量(x_1,y_1, x_2, y_2) 表示,其中,(x_1, y_1)和(x_2, y_2) 是是每個檢測到的線段的結束點。
第三個參數,rho,簡單的說就是半徑的分辨率。
第四個參數,theta,以弧度為機關的角度精度。另一種形容方式是直線搜尋時的進步尺寸的機關角度。
第五個參數,threshold,累加平面的門檻值參數,即識别某部分為圖中的一條直線時它在累加平面中必須達到的值。大于門檻值threshold的線段才可以被檢測通過并傳回到結果中。
第六個參數,minLineLength,有預設值0,表示最低線段的長度,比這個設定參數短的線段就不能被顯現出來。
第七個參數,maxLineGap,有預設值0,允許将同一行點與點之間連接配接起來的最大的距離
解決問題的思路:就是找四個點的坐标完成透視變換。
兩個主要的函數:
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()
{
Mat src = imread("D://RM//OpenCv的學習//透視變換//11.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, 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];
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;
}