日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

图像去畸变和添加畸变

發(fā)布時(shí)間:2023/12/10 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 图像去畸变和添加畸变 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

背景:最近的項(xiàng)目中用到的圖像去畸變的知識(shí),剛開(kāi)始是直接調(diào)用opencv中提供的函數(shù)cv::initUndistortRectifyMap()和cv::remap()函數(shù),實(shí)現(xiàn)圖像的全局去畸變,但是由于圖像的分辨率很高,再加上,實(shí)際過(guò)程中我們只用到了很小一塊的圖像,所以為了降低電腦的負(fù)擔(dān),則想選用局部圖像去畸變的方法來(lái)代替全局圖像去畸變。

于是我想到了《SLAM十四講》書(shū)中在相機(jī)模型中有相關(guān)的去畸變的代碼,我就直接使用測(cè)試了,效果還不錯(cuò)。

void LocalZoneUndistortion(cv::Mat& localImage, cv::Mat& undistortLocalImage) {undistortLocalImage = cv::Mat(localImage.rows, localImage.cols, CV_8UC1);for (int row = 0; row < localImage.rows; ++row) {for (int col = 0; col < localImage.cols; ++col) {double x = (col - cx_) / fx_;double y = (row - cy_) / fy_;double r = sqrt(x * x + y * y);double xDistorted = x * (1 + k1_ * r * r + k2_ * r * r * r * r) + 2 * p1_* x * y + p2_ * (r * r + 2 * x * x);double yDistorted = y * (1 + k1_ * r * r + k2_ * r * r * r * r) + p1_ * (r * r + 2 * y * y) + 2 * p2_* x * y;double uDistorted = fx_ * xDistorted + cx_;double vDistorted = fy_ * yDistorted + cy_;if (uDistorted >= 0 && vDistorted >= 0 && uDistorted < localImage.cols && vDistorted < localImage.rows) {undistortLocalImage.at<uchar>(row, col) = localImage.at<uchar>((int)vDistorted,(int)uDistorted);}else {undistortLocalImage.at<uchar>(row, col) = 0;} }}}

當(dāng)然在使用的時(shí)候要把相機(jī)內(nèi)參和畸變系數(shù)傳進(jìn)去呦,我這里是把它們定義為成員變量,就沒(méi)有傳進(jìn)去。其實(shí)我之前一直對(duì)這個(gè)去畸變的過(guò)程很困惑的,這明明是一個(gè)添加畸變的過(guò)程,為啥可以達(dá)到去畸變的效果呢?時(shí)隔多日之后再次使用這個(gè)函數(shù),其實(shí)我還是沒(méi)怎么關(guān)心她背后的邏輯,直到我同時(shí)又需要完成對(duì)點(diǎn)添加畸變的任務(wù),這個(gè)時(shí)候我才重新審視這兩個(gè)過(guò)程背后的邏輯。

添加畸變的函數(shù)如下

void Tracking::DistortPoints(cv::Point2f& undistPoint, cv::Point2f& distPoint) {double x = (undistPoint.x - cx_) / fx_;double y = (undistPoint.y - cy_) / fy_;double r2 = x * x + y * y;// Radial distorsiondouble xDistort = x * (1 + k1_ * r2 + k2_ * r2 * r2 + k3_ * r2 * r2 * r2);double yDistort = y * (1 + k1_ * r2 + k2_ * r2 * r2 + k3_ * r2 * r2 * r2);// Tangential distorsionxDistort = xDistort + (2 * p1_ * x * y + p2_ * (r2 + 2 * x * x));yDistort = yDistort + (p1_ * (r2 + 2 * y * y) + 2 * p2_ * x * y);// Back to absolute coordinates.xDistort = xDistort * fx_ + cx_;yDistort = yDistort * fy_ + cy_;distPoint = cv::Point2f((float)xDistort, (float)yDistort);};

我們平時(shí)遇到的都是將帶有畸變的點(diǎn)轉(zhuǎn)換為不帶畸變的點(diǎn),很少會(huì)遇到在不帶畸變的點(diǎn)上添加畸變,因?yàn)槲乙谝粋€(gè)帶有畸變的圖像上標(biāo)注一個(gè)不帶畸變的點(diǎn),那標(biāo)注出的位置和我們真正的目標(biāo)之間就有一定的偏差了,這個(gè)時(shí)候只有在這些不帶畸變的點(diǎn)上添加上畸變,這樣才能適應(yīng)帶有畸變的圖像。

我們可以發(fā)現(xiàn),上面兩個(gè)過(guò)程,去畸變和添加畸變剛開(kāi)始的部分都是在添加畸變的過(guò)程,

首先是將像素位置點(diǎn),從像平面內(nèi)轉(zhuǎn)換到歸一化平面內(nèi),

double x = (col - cx_) / fx_; double y = (row - cy_) / fy_; double x = (undistPoint.x - cx_) / fx_; double y = (undistPoint.y - cy_) / fy_;

然后再分別添加,徑向畸變和切向畸變,

// Radial distorsion double xDistort = x * (1 + k1_ * r2 + k2_ * r2 * r2 + k3_ * r2 * r2 * r2); double yDistort = y * (1 + k1_ * r2 + k2_ * r2 * r2 + k3_ * r2 * r2 * r2);// Tangential distorsion xDistort = xDistort + (2 * p1_ * x * y + p2_ * (r2 + 2 * x * x)); yDistort = yDistort + (p1_ * (r2 + 2 * y * y) + 2 * p2_ * x * y);

然后在把點(diǎn)轉(zhuǎn)換到像平面內(nèi)。

// Back to absolute coordinates. xDistort = xDistort * fx_ + cx_; yDistort = yDistort * fy_ + cy_;

以上這些步驟兩個(gè)過(guò)程是一樣的,最后一步是不一樣的。

在去畸變過(guò)程中,我們相當(dāng)于給不帶畸變的坐標(biāo)PointA添加畸變,得到帶有畸變的點(diǎn)PointB,而這正是我們直接獲取得到的帶有畸變的圖像I上的點(diǎn),這個(gè)時(shí)候我們可以將帶有畸變的圖像上的PointB位置處的灰度值映射賦值給PointA的位置處,這樣操作圖像I上所有有效點(diǎn),就完成了對(duì)帶有畸變的圖像I的去畸變操作。

if (uDistorted >= 0 && vDistorted >= 0 && uDistorted < localImage.cols && vDistorted < localImage.rows) {undistortLocalImage.at<uchar>(row, col) = localImage.at<uchar>((int)vDistorted,(int)uDistorted); } else {undistortLocalImage.at<uchar>(row, col) = 0; }

而添加畸變的過(guò)程就比較好理解了,就是給不帶有畸變的點(diǎn)添加上徑向畸變和切向畸變,直接得到帶畸變的點(diǎn)。

總結(jié)

以上是生活随笔為你收集整理的图像去畸变和添加畸变的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。