Opencv透視變換綜合實(shí)例詳解
本文實(shí)例為大家分享了Opencv透視變換綜合實(shí)例的具體代碼,供大家參考,具體內(nèi)容如下
案例背景:對下面發(fā)生畸變的圖像進(jìn)行校正
方案思路:灰度二值化分割,閉操作,尋找輪廓,霍夫直線檢測,直線排序,直線方程,直線交點(diǎn),透視矩陣,透視變換。
#include<opencv2\opencv.hpp>
using namespace cv;
using namespace std;
int main(int arc, char** argv) {
Mat src = imread("1.jpg");
namedWindow("input", CV_WINDOW_AUTOSIZE);
imshow("input", src);
//灰度化
Mat grayImg;
cvtColor(src, grayImg, CV_BGR2GRAY);
//二值化
Mat binaryImg;
threshold(grayImg, binaryImg, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
//閉操作
Mat kernel = getStructuringElement(MORPH_RECT,Size(3,3));
morphologyEx(binaryImg, binaryImg, MORPH_CLOSE,kernel,Point(-1,-1) ,3);
imshow("output", binaryImg);
//尋找輪廓
Mat draw = Mat::zeros(src.size(), CV_8UC3);
vector<vector<Point>>contours;
findContours(binaryImg, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point());
for (int i = 0; i < contours.size(); i++) {
Rect rect = boundingRect(contours[i]);
if (rect.width < src.cols / 2 && rect.height < src.rows / 2)continue;
drawContours(draw, contours, i, Scalar(0, 0, 255), 2);
}
imshow("output2", draw);
//霍夫直線檢測
vector<Vec4i> lines;
cvtColor(draw, draw, CV_BGR2GRAY);
HoughLinesP(draw, lines, 1, CV_PI / 180, src.rows/2,src.rows/2,0);
Mat draw2 = Mat::zeros(src.size(), CV_8UC3);
for (int j = 0; j < lines.size(); j++) {
Vec4i ln = lines[j];
line(draw2, Point(ln[0], ln[1]), Point(ln[2], ln[3]), Scalar(0, 255, 0), 2);
}
printf("number of line:%d\n", lines.size());
imshow("output3", draw2);
//尋找與定位直線
Vec4i topLine,bottomLine,leftLine,rightLine;
for (int j = 0; j < lines.size(); j++) {
Vec4i ln = lines[j];
if (ln[1] < src.rows / 2 && ln[3] < src.rows / 2) {
topLine = ln;
}
if (ln[1] > src.rows / 2 && ln[3] > src.rows / 2) {
bottomLine = ln;
}
if (ln[0] < src.cols / 2 && ln[2] < src.cols / 2) {
leftLine = ln;
}
if (ln[0] > src.cols / 2 && ln[2] > src.cols / 2) {
rightLine = ln;
}
}
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, 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];
//求解直線交點(diǎn)
Point p1;
p1.x = (int)((c1 - c3) / (k3 - k1));
p1.y = (int)(k1*p1.x + c1);
Point p2;
p2.x = (int)((c1 - c4) / (k4 - k1));
p2.y = (int)(k1*p2.x + c1);
Point p3;
p3.x = (int)((c2 - c4) / (k4 - k2));
p3.y = (int)(k2*p3.x + c2);
Point p4;
p4.x = (int)((c2 - c3) / (k3 - k2));
p4.y = (int)(k2*p4.x + c2);
cout << "左上角:" << p1.x << "," << p1.y << endl;
cout << "右上角:" << p2.x << "," << p2.y << endl;
cout << "右下角:" << p3.x << "," << p3.y << endl;
cout << "左下角:" << p4.x << "," << p4.y << endl;
//畫出交點(diǎn)
circle(draw2, p1, 2, Scalar(0, 0, 255));
circle(draw2, p2, 2, Scalar(0, 0, 255));
circle(draw2, p3, 2, Scalar(0, 0, 255));
circle(draw2, p4, 2, Scalar(0, 0, 255));
imshow("output4", draw2);
//透視變換
vector<Point2f> srcCorners(4);
srcCorners[0] = p1;
srcCorners[1] = p2;
srcCorners[2] = p3;
srcCorners[3] = p4;
vector<Point2f> dstCorners(4);
dstCorners[0] = Point(0, 0);
dstCorners[1] = Point(src.cols, 0);
dstCorners[2] = Point(src.cols, src.rows);
dstCorners[3] = Point(0, src.rows);
Mat warpMartrix = getPerspectiveTransform(srcCorners, dstCorners);//Mat warpMartrix = findHomography(srcCorners, dstCorners);
Mat result = Mat::zeros(src.size(), -1);
warpPerspective(src, result, warpMartrix, result.size(),INTER_LINEAR);
imshow("output5", result);
waitKey(0);
return 0;
}
原圖像
二值化閉操作
尋找輪廓
霍夫直線
直線及其交點(diǎn)
效果圖
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C語言控制臺繪制曲線的實(shí)現(xiàn)代碼
欄 目:C語言
下一篇:Opencv實(shí)現(xiàn)對象提取與測量
本文標(biāo)題:Opencv透視變換綜合實(shí)例詳解
本文地址:http://m.jygsgssxh.com/a1/Cyuyan/277.html
您可能感興趣的文章
- 01-10實(shí)現(xiàn)opencv圖像裁剪分屏顯示示例
- 01-10使用opencv拉伸圖像擴(kuò)大分辨率示例
- 01-10C++實(shí)現(xiàn)二維圖形的傅里葉變換
- 01-10基于C++實(shí)現(xiàn)kinect+opencv 獲取深度及彩色數(shù)據(jù)
- 01-10c++圖像處理:24位真彩圖顏色變換實(shí)例
- 01-10淺談CMake配置OpenCV 時(shí)靜態(tài)鏈接與動(dòng)態(tài)鏈接的選擇
- 01-10OpenCV中C++函數(shù)imread讀取圖片的問題及解決方法
- 01-10visual studio 2013中配置opencv圖文教程 Opencv2.4.9安裝配置教程
- 01-10OPENCV批量讀取圖片實(shí)現(xiàn)方法
- 01-10Opencv學(xué)習(xí)教程之漫水填充算法實(shí)例詳解


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子


