OpenCV通過透視變換實現(xiàn)矯正圖像詳解
1、概述
案例:使用OpenCV將一張折射的圖片給矯正過來
實現(xiàn)步驟:
1.載入圖像
2.圖像灰度化
3.二值分割
4.形態(tài)學(xué)操作去除噪點
5.輪廓發(fā)現(xiàn)
6.使用霍夫直線檢測,檢測上下左右四條直線(有可能是多條,但是無所謂)
7.繪制出直線
8.尋找與定位上下左右是條直線
9.擬合四條直線方程
10.計算四條直線的交點,ps:這四個交點其實就是我們最終要尋找的,用于透視變換使用的
11.進行透視變換
12.輸出透視變換的結(jié)果
說明:
解釋一下為啥是上面那些步驟。
1.其實我們的最終目的是通過透視矩陣getPerspectiveTransform+透視變換warpPerspective來完成圖像的矯正
2.但是getPerspectiveTransform需要兩個參數(shù),輸入矩陣參數(shù)和目標(biāo)矩陣參數(shù)。
3.由于輸入矩陣參數(shù)就是原圖像是個角的頂點,由于我們沒有所以要求出來
4.所以我們以上的所有步驟都是為11、12步打基礎(chǔ)的
ps:核心就是利用透視矩陣做透視變換
重點:
1.直線方程y=kx+c
2.如果兩條直線有交點,則必有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é)開操作去除圖像中的造點
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]);
//過濾目標(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】使用霍夫直線檢測
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】四條直線交點,其實最終的目的就是找這是條直線的交點
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);
// 顯示四個點坐標(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通過透視變換實現(xiàn)矯正圖像詳解的詳細(xì)內(nèi)容,更多關(guān)于OpenCV矯正圖像的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語言 詳細(xì)講解接續(xù)符和轉(zhuǎn)義符的使用
接續(xù)符是用來告訴編譯器行為的符號,那編譯器遇到接續(xù)符是什么行為呢,就是去掉接續(xù)符,然后把下一行連接到現(xiàn)在這行上面,轉(zhuǎn)義符是主要用于表示無回顯字符,也用于表示常規(guī)字符,轉(zhuǎn)義符必須放在單引號或者雙引號里面2022-04-04

