OpenCV 透視變換執行個體

來源:互聯網
上載者:User

標籤:blog   http   io   ar   color   os   使用   sp   for   

 

參考文獻:

http://www.cnblogs.com/self-control/archive/2013/01/18/2867022.html

http://opencv-code.com/tutorials/automatic-perspective-correction-for-quadrilateral-objects/ 

 

透視變換:

http://blog.csdn.net/xiaowei_cqu/article/details/26478135

 

 

具體流程為:

a)載入映像→灰階化→邊緣處理得到邊緣映像(edge map)

cv::Mat im = cv::imread(filename);

cv::Mat gray;

cvtColor(im,gray,CV_BGR2GRAY);

Canny(gray,gray,100,150,3);

 

b)霍夫變換進行直線檢測,此處使用的是probabilistic Hough transform(cv::HoughLinesP)而不是standard Hough transform(cv::HoughLines)

std::vector<Vec4i> lines;

cv::HoughLinesP(gray,lines,1,CV_PI/180,70,30,10);

for(int i = 0; i < lines.size(); i++)

    line(im,cv::Point(lines[i][0],lines[i][1]),cv::Point(lines[i][2],lines[i][3]),Scalar(255,0,0),2,8,0);

 

c)通過上面的圖我們可以看出,通過霍夫變換檢測到的直線並沒有將整個邊緣包含,但是我們要求的是四個頂點所以並不一定要直線真正的相交,下面就要求四個頂點的座標,公式為:

 

?
12345678910111213141516 cv::Point2f computeIntersect(cv::Vec4i a, cv::Vec4i b){    intx1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3];    intx3 = b[0], y3 = b[1], x4 = b[2], y4 = b[3];     if(floatd = ((float)(x1-x2) * (y3-y4)) - ((y1-y2) * (x3-x4)))    {        cv::Point2f pt;        pt.x = ((x1*y2 - y1*x2) * (x3-x4) - (x1-x2) * (x3*y4 - y3*x4)) / d;        pt.y = ((x1*y2 - y1*x2) * (y3-y4) - (y1-y2) * (x3*y4 - y3*x4)) / d;        returnpt;    }    else        returncv::Point2f(-1, -1);}  

  

?
12345678910 std::vector<cv::Point2f> corners;for (int i = 0; i < lines.size(); i++){    for(intj = i+1; j < lines.size(); j++)    {        cv::Point2f pt = computeIntersect(lines[i], lines[j]);        if(pt.x >= 0 && pt.y >= 0)            corners.push_back(pt);    }}

 d)檢查是不是四邊形?
123456789 std::vector<cv::Point2f> approx;cv::approxPolyDP(cv::Mat(corners), approx,                 cv::arcLength(cv::Mat(corners),true) * 0.02,true); if (approx.size() != 4){    std::cout <<"The object is not quadrilateral!"<< std::endl;    return-1;}

  

 e)確定四個頂點的具體位置(top-left, bottom-left, top-right, and bottom-right corner)→通過四個頂點求出映射矩陣來.?
void sortCorners(std::vector<cv::Point2f>& corners, cv::Point2f center){    std::vector<cv::Point2f> top, bot;     for(inti = 0; i < corners.size(); i++)    {        if(corners[i].y < center.y)            top.push_back(corners[i]);        else            bot.push_back(corners[i]);    }     cv::Point2f tl = top[0].x > top[1].x ? top[1] : top[0];    cv::Point2f tr = top[0].x > top[1].x ? top[0] : top[1];    cv::Point2f bl = bot[0].x > bot[1].x ? bot[1] : bot[0];    cv::Point2f br = bot[0].x > bot[1].x ? bot[0] : bot[1];     corners.clear();    corners.push_back(tl);    corners.push_back(tr);    corners.push_back(br);    corners.push_back(bl);}

 下面是獲得中心點座標然後利用上面的函數確定四個頂點的座標

?
for (int i = 0; i < corners.size(); i++)    center += corners[i]; center *= (1. / corners.size());sortCorners(corners, center);

 定義目的映像並初始化為0

?
cv::Mat quad = cv::Mat::zeros(300, 220, CV_8UC3);

 擷取目的映像的四個頂點

?
std::vector<cv::Point2f> dst_pt;dst.push_back(cv::Point2f(0,0));dst.push_back(cv::Point2f(quad.cols,0));dst.push_back(cv::Point2f(quad.cols,quad.rows));dst.push_back(cv::Point2f(0,quad.rows));

 計算映射矩陣

?
cv::Mat transmtx = cv::getPerspectiveTransform(corners, quad_pts);

進行透視變換並顯示結果

?
cv::warpPerspective(im, quad, transmtx, quad.size());cv::imshow("quadrilateral", quad);

  

 

 

 

 

 

 

 

 

 

 

 

 

 

// affine transformation.cpp : 定義控制台應用程式的進入點。//#include "stdafx.h"/** * Automatic perspective correction for quadrilateral objects. See the tutorial at * http://opencv-code.com/tutorials/automatic-perspective-correction-for-quadrilateral-objects/ */#include <opencv2/imgproc/imgproc.hpp>#include <opencv2/highgui/highgui.hpp>#include <iostream>#pragma comment(lib,"opencv_core2410d.lib")          #pragma comment(lib,"opencv_highgui2410d.lib")          #pragma comment(lib,"opencv_imgproc2410d.lib")    cv::Point2f center(0,0);cv::Point2f computeIntersect(cv::Vec4i a, cv::Vec4i b){int x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3], x3 = b[0], y3 = b[1], x4 = b[2], y4 = b[3];float denom;if (float d = ((float)(x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4))){cv::Point2f pt;pt.x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / d;pt.y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / d;return pt;}elsereturn cv::Point2f(-1, -1);}void sortCorners(std::vector<cv::Point2f>& corners,                  cv::Point2f center){std::vector<cv::Point2f> top, bot;for (int i = 0; i < corners.size(); i++){if (corners[i].y < center.y)top.push_back(corners[i]);elsebot.push_back(corners[i]);}corners.clear();if (top.size() == 2 && bot.size() == 2){cv::Point2f tl = top[0].x > top[1].x ? top[1] : top[0];cv::Point2f tr = top[0].x > top[1].x ? top[0] : top[1];cv::Point2f bl = bot[0].x > bot[1].x ? bot[1] : bot[0];cv::Point2f br = bot[0].x > bot[1].x ? bot[0] : bot[1];corners.push_back(tl);corners.push_back(tr);corners.push_back(br);corners.push_back(bl);}}int main(){cv::Mat src = cv::imread("image.jpg");if (src.empty())return -1;cv::Mat bw;cv::cvtColor(src, bw, CV_BGR2GRAY);cv::blur(bw, bw, cv::Size(3, 3));cv::Canny(bw, bw, 100, 100, 3);std::vector<cv::Vec4i> lines;cv::HoughLinesP(bw, lines, 1, CV_PI/180, 70, 30, 10);// Expand the linesfor (int i = 0; i < lines.size(); i++){cv::Vec4i v = lines[i];lines[i][0] = 0;lines[i][1] = ((float)v[1] - v[3]) / (v[0] - v[2]) * -v[0] + v[1]; lines[i][2] = src.cols; lines[i][3] = ((float)v[1] - v[3]) / (v[0] - v[2]) * (src.cols - v[2]) + v[3];}std::vector<cv::Point2f> corners;for (int i = 0; i < lines.size(); i++){for (int j = i+1; j < lines.size(); j++){cv::Point2f pt = computeIntersect(lines[i], lines[j]);if (pt.x >= 0 && pt.y >= 0)corners.push_back(pt);}}std::vector<cv::Point2f> approx;cv::approxPolyDP(cv::Mat(corners), approx, cv::arcLength(cv::Mat(corners), true) * 0.02, true);if (approx.size() != 4){std::cout << "The object is not quadrilateral!" << std::endl;return -1;}// Get mass centerfor (int i = 0; i < corners.size(); i++)center += corners[i];center *= (1. / corners.size());sortCorners(corners, center);if (corners.size() == 0){std::cout << "The corners were not sorted correctly!" << std::endl;return -1;}cv::Mat dst = src.clone();// Draw linesfor (int i = 0; i < lines.size(); i++){cv::Vec4i v = lines[i];cv::line(dst, cv::Point(v[0], v[1]), cv::Point(v[2], v[3]), CV_RGB(0,255,0));}// Draw corner pointscv::circle(dst, corners[0], 3, CV_RGB(255,0,0), 2);cv::circle(dst, corners[1], 3, CV_RGB(0,255,0), 2);cv::circle(dst, corners[2], 3, CV_RGB(0,0,255), 2);cv::circle(dst, corners[3], 3, CV_RGB(255,255,255), 2);// Draw mass centercv::circle(dst, center, 3, CV_RGB(255,255,0), 2);cv::Mat quad = cv::Mat::zeros(300, 220, CV_8UC3);std::vector<cv::Point2f> quad_pts;quad_pts.push_back(cv::Point2f(0, 0));quad_pts.push_back(cv::Point2f(quad.cols, 0));quad_pts.push_back(cv::Point2f(quad.cols, quad.rows));quad_pts.push_back(cv::Point2f(0, quad.rows));cv::Mat transmtx = cv::getPerspectiveTransform(corners, quad_pts);cv::warpPerspective(src, quad, transmtx, quad.size());cv::imshow("image", dst);cv::imshow("quadrilateral", quad);cv::waitKey();return 0;}


 

 

實現結果:

 

 

OpenCV 透視變換執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.