C語(yǔ)言 OpenCV實(shí)現(xiàn)柱面投影
前言
在做全景拼接的時(shí)候,為了保持圖片中的空間約束與視覺(jué)的一致性,需要進(jìn)行柱面投影,否則離中心圖像距離越遠(yuǎn)的圖像拼接后變形越大。
柱面投影公式為

實(shí)現(xiàn)代碼
針對(duì)彩色圖像
int main()
{
cv::Mat image1 = cv::imread("images/1.jpg", 1);
if (!image1.data)
return 0;
imshow("image1", image1);
Mat imgOut = Mat(image1.rows, image1.cols, CV_8UC3);
float w = image1.cols;
float h = image1.rows;
float f = (w / 2) / atan(PI / 8);
for (int i = 0; i < image1.rows; i++)
{
for (int j = 0; j < image1.cols; j++)
{
float x = j;
float y = i;
float x1 = f * atan((x - w / 2) / f) + f * atan(w / (2.0f * f));
float y1 = f * (y - h / 2.0f) / sqrt((x - w / 2.0f) * (x - w / 2.0f) + f * f) + h / 2.0f;
int col = (int)(x1 + 0.5f);//加0.5是為了四舍五入
int row = (int)(y1 + 0.5f);//加0.5是為了四舍五入
if (col < image1.cols && row < image1.rows)
{
imgOut.at<Vec3b>(row, col)[0] = image1.at<Vec3b>(i, j)[0];
imgOut.at<Vec3b>(row, col)[1] = image1.at<Vec3b>(i, j)[1];
imgOut.at<Vec3b>(row, col)[2] = image1.at<Vec3b>(i, j)[2];
}
}
}
imshow("imgOut", imgOut);
waitKey(0);
return 0;
}
實(shí)現(xiàn)效果


針對(duì)灰度圖像
cv::Mat image1 = cv::imread("E:\\zcb_work\\2113\\pic2\\k.jpg", 0);
if (!image1.data)
return 0;
imshow("image1", image1);
cv::Mat image2 = cv::imread("E:\\zcb_work\\2113\\pic2\\j.jpg", 0);
if (!image2.data)
return 0;
imshow("image2", image2);
Mat imgOut1 = Mat(image1.rows, image1.cols, CV_8UC1);
imgOut1.setTo(0);
Mat imgOut2 = Mat(image2.rows, image2.cols, CV_8UC1);
imgOut2.setTo(0);
float w = image1.cols;
float h = image1.rows;
float f = (w / 2) / atan(PI / 8);
for (int i = 0; i < image1.rows; i++)
{
for (int j = 0; j < image1.cols; j++)
{
float x = j;
float y = i;
float x1 = f * atan((x - w / 2) / f) + f * atan(w / (2.0f * f));
float y1 = f * (y - h / 2.0f) / sqrt((x - w / 2.0f) * (x - w / 2.0f) + f * f) + h / 2.0f;
int col = (int)(x1 + 0.5f);//加0.5是為了四舍五入
int row = (int)(y1 + 0.5f);//加0.5是為了四舍五入
if (col < image1.cols && row < image1.rows)
{
imgOut1.at<uchar>(row, col) = image1.at<uchar>(i, j);
imgOut2.at<uchar>(row, col) = image2.at<uchar>(i, j);
//imgOut.at<Vec3b>(row, col)[1] = image1.at<Vec3b>(i, j)[1];
//imgOut.at<Vec3b>(row, col)[2] = image1.at<Vec3b>(i, j)[2];
}
}
}
imshow("imgOut1", imgOut1);
imshow("imgOut2", imgOut2);
實(shí)現(xiàn)效果
原圖

柱面投影

用surf算法,特征檢測(cè),

合成這樣,呵呵呵,
?
以上就是C語(yǔ)言 OpenCV實(shí)現(xiàn)柱面投影的詳細(xì)內(nèi)容,更多關(guān)于C語(yǔ)言 OpenCV柱面投影的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
用C語(yǔ)言實(shí)現(xiàn)圣誕樹(shù)(簡(jiǎn)易版+進(jìn)階版)
大家好,本篇文章主要講的是用C語(yǔ)言實(shí)現(xiàn)圣誕樹(shù)(簡(jiǎn)易版+進(jìn)階版),感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
C語(yǔ)言超詳細(xì)講解函數(shù)指針的運(yùn)用
函數(shù)指針是一個(gè)指針變量,它可以存儲(chǔ)函數(shù)的地址,然后使用函數(shù)指針,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言進(jìn)階教程之函數(shù)指針的相關(guān)資料,需要的朋友可以參考下2022-06-06
C++begin和end運(yùn)算符的返回迭代器的類(lèi)型如何判斷?
今天小編就為大家分享一篇關(guān)于C++begin和end運(yùn)算符的返回迭代器的類(lèi)型如何判斷?,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04
C語(yǔ)言數(shù)據(jù)輸入與輸出實(shí)例詳解
這篇文章主要介紹了C語(yǔ)言數(shù)據(jù)輸入與輸出實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06

