1. 程式人生 > 其它 >OpenCV利用透視變換矯正影象

OpenCV利用透視變換矯正影象

一、概述

  案例:使用OpenCV將一張折射的圖片給矯正過來

  實現步驟:

    1.載入影象

    2.影象灰度化

    3.二值分割

    4.形態學操作去除噪點

    5.輪廓發現

    6.使用霍夫直線檢測,檢測上下左右四條直線(有可能是多條,但是無所謂)

    7.繪製出直線

    8.尋找與定位上下左右是條直線

    9.擬合四條直線方程

    10.計算四條直線的交點,ps:這四個交點其實就是我們最終要尋找的,用於透視變換使用的

    11.進行透視變換

    12.輸出透視變換的結果

  說明:

    解釋一下為啥是上面那些步驟。

    1.其實我們的最終目的是通過透視矩陣getPerspectiveTransform+透視變換warpPerspective來完成影象的矯正

    2.但是getPerspectiveTransform需要兩個引數,輸入矩陣引數和目標矩陣引數。

    3.由於輸入矩陣引數就是原影象是個角的頂點,由於我們沒有所以要求出來

    4.所以我們以上的所有步驟都是為11、12步打基礎的

  ps:核心就是利用透視矩陣做透視變換

  重點:

    1.直線方程y=kx+c

    2.如果兩條直線有交點,則必有k1x1+c1=k2x2+c2

二、程式碼演示

//【1】載入影象
    Mat src = imread(filePath);
    if(src.empty()){
        qDebug()<<"圖片為空
"; return; } imshow("src",src); //【2】影象灰度化 Mat gray; cvtColor(src,gray,COLOR_BGR2GRAY); //【3】執行二值分割 threshold(gray,gray,0,255,THRESH_BINARY_INV|THRESH_OTSU); imshow("threshold",gray); //【4】執行形態學開操作去除影象中的造點 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】輪廓發現 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]); //過濾目標輪廓 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); // 顯示四個點座標 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】獲取透視變換矩陣,並最終顯示變換後的結果 Mat resultImage; Mat warpmatrix = getPerspectiveTransform(src_corners, dst_corners); warpPerspective(src, resultImage, warpmatrix, resultImage.size(), INTER_LINEAR); imshow("Final Result", resultImage);

 

三、示例圖片