OpenCV通過(guò)透視變換實(shí)現(xiàn)矯正圖像詳解
1、概述
案例:使用OpenCV將一張折射的圖片給矯正過(guò)來(lái)
實(shí)現(xiàn)步驟:
1.載入圖像
2.圖像灰度化
3.二值分割
4.形態(tài)學(xué)操作去除噪點(diǎn)
5.輪廓發(fā)現(xiàn)
6.使用霍夫直線檢測(cè),檢測(cè)上下左右四條直線(有可能是多條,但是無(wú)所謂)
7.繪制出直線
8.尋找與定位上下左右是條直線
9.擬合四條直線方程
10.計(jì)算四條直線的交點(diǎn),ps:這四個(gè)交點(diǎn)其實(shí)就是我們最終要尋找的,用于透視變換使用的
11.進(jìn)行透視變換
12.輸出透視變換的結(jié)果
說(shuō)明:
解釋一下為啥是上面那些步驟。
1.其實(shí)我們的最終目的是通過(guò)透視矩陣getPerspectiveTransform+透視變換warpPerspective來(lái)完成圖像的矯正
2.但是getPerspectiveTransform需要兩個(gè)參數(shù),輸入矩陣參數(shù)和目標(biāo)矩陣參數(shù)。
3.由于輸入矩陣參數(shù)就是原圖像是個(gè)角的頂點(diǎn),由于我們沒(méi)有所以要求出來(lái)
4.所以我們以上的所有步驟都是為11、12步打基礎(chǔ)的
ps:核心就是利用透視矩陣做透視變換
重點(diǎn):
1.直線方程y=kx+c
2.如果兩條直線有交點(diǎn),則必有k1x1+c1=k2x2+c2
2、代碼演示
//【1】載入圖像
Mat src = imread(filePath);
if(src.empty()){
qDebug()<<"圖片為空";
return;
}
imshow("src",src);
//【2】圖像灰度化
Mat gray;
cvtColor(src,gray,COLOR_BGR2GRAY);
//【3】執(zhí)行二值分割
threshold(gray,gray,0,255,THRESH_BINARY_INV|THRESH_OTSU);
imshow("threshold",gray);
//【4】執(zhí)行形態(tài)學(xué)開(kāi)操作去除圖像中的造點(diǎn)
Mat kernel = getStructuringElement(MORPH_RECT,Size(5,5),Point(-1,-1));
morphologyEx(gray,gray,MORPH_CLOSE,kernel,Point(-1,-1),3);
imshow("morphologyEx",gray);
//【5】輪廓發(fā)現(xiàn)
bitwise_not(gray,gray);
imshow("bitwise_not",gray);
vector<vector<Point>> contours;
vector<Vec4i> hier;
RNG rng(12345);
findContours(gray,contours,hier,RETR_TREE,CHAIN_APPROX_SIMPLE);
Mat colorImage = Mat::zeros(gray.size(),CV_8UC3);
for(size_t i = 0;i<contours.size();i++){
Rect rect = boundingRect(contours[i]);
//過(guò)濾目標(biāo)輪廓
if(rect.width<src.cols-5&&rect.height<src.rows-5&&rect.width>src.cols/2){
drawContours(colorImage,contours,i,Scalar(rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255)),1);
}
}
imshow("findContours",colorImage);
//【6】使用霍夫直線檢測(cè)
vector<Vec4i> lines;
cvtColor(colorImage,colorImage,COLOR_BGR2GRAY);
kernel = getStructuringElement(MORPH_RECT,Size(3,3),Point(-1,-1));
dilate(colorImage,colorImage,kernel,Point(-1,-1),1);
imshow("colorImage_gray",colorImage);
int accu = min(src.cols*0.5, src.rows*0.5);
HoughLinesP(colorImage,lines,1,CV_PI/180,accu,accu,0);
//【7】繪制出直線
Mat lineColorImage = Mat::zeros(gray.size(),CV_8UC3);
qDebug()<<"line count:"<<lines.size();
for(size_t i = 0;i<lines.size();i++){
Vec4i ll = lines[i];
line(lineColorImage,Point(ll[0],ll[1]),Point(ll[2],ll[3]),Scalar(rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255)),2,LINE_8);
}
imshow("lines",lineColorImage);
//【8】尋找與定位上下左右四條直線
int deltah = 0;
int width = src.cols;
int height = src.rows;
Vec4i topLine, bottomLine;
Vec4i leftLine, rightLine;
for(size_t i=0;i<lines.size();i++){
Vec4i ln = lines[i];
deltah = abs(ln[3]-ln[1]);//直線高度
if (ln[3] < height / 2.0 && ln[1] < height / 2.0 && deltah < accu - 1) {
if (topLine[3] > ln[3] && topLine[3]>0) {
topLine = lines[i];
} else {
topLine = lines[i];
}
}
if (ln[3] > height / 2.0 && ln[1] > height / 2.0 && deltah < accu - 1) {
bottomLine = lines[i];
}
if (ln[0] < width / 2.0 && ln[2] < width/2.0) {
leftLine = lines[i];
}
if (ln[0] > width / 2.0 && ln[2] > width / 2.0) {
rightLine = lines[i];
}
}
//直線方程y=kx+c
// 【9】擬合四條直線方程
float k1, c1;
k1 = float(topLine[3] - topLine[1]) / float(topLine[2] - topLine[0]);
c1 = topLine[1] - k1*topLine[0];
float k2, c2;
k2 = float(bottomLine[3] - bottomLine[1]) / float(bottomLine[2] - bottomLine[0]);
c2 = bottomLine[1] - k2*bottomLine[0];
float k3, c3;
k3 = float(leftLine[3] - leftLine[1]) / float(leftLine[2] - leftLine[0]);
c3 = leftLine[1] - k3*leftLine[0];
float k4, c4;
k4 = float(rightLine[3] - rightLine[1]) / float(rightLine[2] - rightLine[0]);
c4 = rightLine[1] - k4*rightLine[0];
// 【10】四條直線交點(diǎn),其實(shí)最終的目的就是找這是條直線的交點(diǎn)
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);
// 顯示四個(gè)點(diǎn)坐標(biāo)
circle(lineColorImage, p1, 2, Scalar(255, 0, 0), 2, 8, 0);
circle(lineColorImage, p2, 2, Scalar(255, 0, 0), 2, 8, 0);
circle(lineColorImage, p3, 2, Scalar(255, 0, 0), 2, 8, 0);
circle(lineColorImage, p4, 2, Scalar(255, 0, 0), 2, 8, 0);
line(lineColorImage, Point(topLine[0], topLine[1]), Point(topLine[2], topLine[3]), Scalar(0, 255, 0), 2, 8, 0);
imshow("four corners", lineColorImage);
// 【11】透視變換
vector<Point2f> src_corners(4);
src_corners[0] = p1;
src_corners[1] = p2;
src_corners[2] = p3;
src_corners[3] = p4;
vector<Point2f> dst_corners(4);
dst_corners[0] = Point(0, 0);
dst_corners[1] = Point(width, 0);
dst_corners[2] = Point(0, height);
dst_corners[3] = Point(width, height);
// 【12】獲取透視變換矩陣,并最終顯示變換后的結(jié)果
Mat resultImage;
Mat warpmatrix = getPerspectiveTransform(src_corners, dst_corners);
warpPerspective(src, resultImage, warpmatrix, resultImage.size(), INTER_LINEAR);
imshow("Final Result", resultImage);3、示例圖片

以上就是OpenCV通過(guò)透視變換實(shí)現(xiàn)矯正圖像詳解的詳細(xì)內(nèi)容,更多關(guān)于OpenCV矯正圖像的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Java OpenCV圖像處理之仿射變換,透視變換,旋轉(zhuǎn)詳解
- OpenCV透視變換應(yīng)用之書(shū)本視圖矯正+廣告屏幕切換
- Python?OpenCV超詳細(xì)講解透視變換的實(shí)現(xiàn)
- OpenCV實(shí)現(xiàn)透視變換矯正
- Python?Opencv基于透視變換的圖像矯正
- 詳解如何在pyqt中通過(guò)OpenCV實(shí)現(xiàn)對(duì)窗口的透視變換
- opencv3/C++ 平面對(duì)象識(shí)別&透視變換方式
- OpenCV圖像幾何變換之透視變換
- Opencv透視變換綜合實(shí)例詳解
- OpenCV實(shí)現(xiàn)透視變換的示例代碼
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(114.將二叉樹(shù)展開(kāi)成鏈表)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(114.將二叉樹(shù)展開(kāi)成鏈表),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++標(biāo)準(zhǔn)模版庫(kù)(STL)之vector容器詳解
vector的功能和水桶一樣,就是用來(lái)裝東西的,并且vector還提供了迭代器來(lái)很方便的訪問(wèn)這些數(shù)據(jù),下面就讓我們一起看下如何使用C++的vector吧2023-03-03
C++實(shí)現(xiàn)線性表有序表的合并方式(順序表實(shí)現(xiàn)and鏈表實(shí)現(xiàn))
這篇文章主要介紹了C++實(shí)現(xiàn)線性表有序表的合并方式(順序表實(shí)現(xiàn)and鏈表實(shí)現(xiàn)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
C語(yǔ)言實(shí)現(xiàn)單位車(chē)輛調(diào)度管理
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)單位車(chē)輛調(diào)度管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
C++ qt 使用jsoncpp json 讀寫(xiě)操作
JsonCpp是一個(gè)基于C++語(yǔ)言的開(kāi)源庫(kù),用于C++程序的Json數(shù)據(jù)的讀寫(xiě)操作,本文重點(diǎn)給大家介紹C++ qt 使用jsoncpp json 讀寫(xiě)操作,感興趣的朋友跟隨小編一起看看吧2021-11-11

