首页 > 鱼眼图像的unwarping过程

鱼眼图像的unwarping过程

请添加图片描述

第一行是提取有效区域后的鱼眼图;第二行就是展开图(第二张和第四张根据需要裁剪了一部分)

#include 
#include "opencv2/highgui/highgui.hpp"    
#include "opencv2/opencv.hpp"    
#include   using namespace std;
using namespace cv;
#define IMAGE_WIDTH 1000
#define IMAGE_HEIGHT 1000
#define mode 0 //mode=0是不需要重新生成映射yml
#define FOV_240 4.188790//FOV是240°void ExtractFish(vector<cv::Mat>& fishes) { for (int i = 1; i < 5; i++) { cv::Mat input = cv::imread("./input/" + to_string(i) + ".png", 1);cv::Mat mask = cv::imread("./input/Mask" + to_string(i) + ".png", 0);//cv::Mat mask1 = cv::Mat::zeros(mask.size(), mask.type());//for (int j = 0; j < mask1.rows; j++) { //	for (int k = 0; k < mask1.cols; k++) { //		if ((float)mask.at(j, k) == 0) { //			mask1.at(j, k) = (uchar)0.0;//		}//		else { //			mask1.at(j, k) = (uchar)255.0;//		}//		//mask1.at(j, k) =(uchar)(1.0 - (float)mask.at(j, k));//	}//}//cv::imwrite("./out/Mask" + to_string(i) + ".png", mask1);cv::Mat out;input.copyTo(out, mask);cv::Mat newbg = cv::Mat::zeros(1000, 1000, CV_8UC3);if (i == 1) { resize(out, out, cv::Size(1000, 1000));cv::Rect rec(0, 0, out.cols, out.rows);out.copyTo(newbg(rec));resize(newbg, newbg, cv::Size(1000, 1000));fishes.emplace_back(newbg);cv::imwrite("./out/out" + to_string(i) + ".png", newbg);}else { resize(out, out, cv::Size(1000, 1000));fishes.emplace_back(out);cv::imwrite("./out/out" + to_string(i) + ".png", out);}}
}void writeMatToFile(cv::Mat& m, int width, int height)
{ std::ofstream fout("./out/step_240.yml");if (!fout){ std::cout << "File Not Opened" << std::endl;return;}int n = 0;int t = 0;fout << "%YAML:1.0" << std::endl;fout << "    " << "Xd: !!opencv-matrix" << std::endl;fout << "        " << "rows: " << width << std::endl;fout << "        " << "cols: " << height << std::endl;fout << "        " << "dt: f" << std::endl;fout << "        " << "data: [ ";for (int i = 0; i<m.cols - 1; i++){ fout << m.at<float>(0, i) << ",";n++;if (n % 10 == 0){ fout << std::endl << "            ";}}fout << m.at<float>(0, m.cols - 1) << "]" << std::endl;fout << "    " << "Yd: !!opencv-matrix" << std::endl;fout << "        " << "rows: " << width << std::endl;fout << "        " << "cols: " << height << std::endl;fout << "        " << "dt: f" << std::endl;fout << "        " << "data: [ ";for (int j = 0; j < m.cols - 1; j++){ fout << m.at<float>(1, j) << ",";t++;if (t % 10 == 0){ fout << std::endl << "            ";}}fout << m.at<float>(1, m.cols - 1) << "]" << std::endl;
}void fish2Eqt(double x_dest, double  y_dest, double* x_src, double* y_src, double Wd)
{ double phi1, phi2, theta1, theta2, dc, zcoor, p, _y, _z;double v[3];int theta = 20;double cos0 = 1.0;//俯仰角cosdouble sin0 = 0.0;//俯仰角sintheta1 = FOV_240 * (x_dest / Wd);//-pi to piphi1 = CV_PI * (y_dest / Wd);//0 to pizcoor = sin(phi1);//zv[0] = sin(theta1) * cos(phi1);//xv[1] = cos(phi1) * cos(theta1);//y_y = v[1] * cos0 - zcoor * sin0;_z = v[1] * sin0 + zcoor * cos0;dc = sqrt(_z * _z + v[0] * v[0]);theta2 = atan2(_z, v[0]);phi2 = atan2(dc, _y);p = Wd * phi2 / FOV_240;*x_src = p * cos(theta2);*y_src = p * sin(theta2);
}void fish_2D_map(cv::Mat &map_x, cv::Mat &map_y, int Hs, int Ws)
{ double x_d, y_d; // destdouble x_s, y_s; // sourcedouble w2 = (double)Ws / 2.0 - 0.5; double h2 = (double)Hs / 2.0 - 0.5; Point2f temp, ttemp;vector<Point2f> changepoints;Mat fv = Mat::zeros(2, IMAGE_WIDTH*IMAGE_HEIGHT, CV_32F);int t = 0;for (int _y = 0; _y < Hs; _y++){ // y-coordinate in dest image relative to centery_d = (double)_y - h2;//y_d代表相对于展开图中心位置的y坐标for (int _x = 0; _x < Ws; _x++){ x_d = (double)_x - w2;//x_d代表相对于展开图中心位置的x坐标// Convert fisheye coordinate to cartesian coordinate (equirectangular)fish2Eqt(x_d, y_d, &x_s, &y_s, Ws);//cout << x_d << endl;x_s += w2;y_s += h2;// Create mapmap_x.at<float>(_y, _x) = float(x_s);map_y.at<float>(_y, _x) = float(y_s);//map_x map_y是鱼眼展开图对应原鱼眼图xy位置		fv.at<float>(0, t) = x_s;fv.at<float>(1, t) = y_s;if (t < IMAGE_WIDTH*IMAGE_HEIGHT) { t++;}}}writeMatToFile(fv, IMAGE_WIDTH, IMAGE_HEIGHT);
}vector<cv::Mat> Unwarping(vector<cv::Mat>& fishes) { cv::Rect rect(0, 0, 1000, 1000);cv::Mat mls_map_x, mls_map_y;cv::Mat map_x, map_y;map_x = cv::Mat(IMAGE_HEIGHT, IMAGE_WIDTH, CV_32FC1);map_y = cv::Mat(IMAGE_HEIGHT, IMAGE_WIDTH, CV_32FC1);cv::Mat remapped[4];vector<cv::Mat> vectorremapped;cv::Mat Crop_remapped[4];if (mode == 0) { cv::FileStorage fs("./out/step_240.yml", cv::FileStorage::READ);fs["Xd"] >> mls_map_x;fs["Yd"] >> mls_map_y;fs.release();}for (int i = 0; i < fishes.size(); i++) { //需要重新生成yml时就取消fish_2D_map,注释remap。if (mode == 1) { fish_2D_map(map_x, map_y, IMAGE_HEIGHT, IMAGE_WIDTH);}else{ remap(fishes[i], remapped[i], mls_map_x, mls_map_y, INTER_AREA, BORDER_CONSTANT, Scalar(0, 0, 0, 0));imwrite("./out/Unwarp/" + to_string(i) + ".jpg", remapped[i])                    

更多相关:

  • 论文阅读模块将分享点云处理,SLAM,三维视觉,高精地图相关的文章。公众号致力于理解三维视觉领域相关内容的干货分享,欢迎各位加入我,我们一起每天一篇文章阅读,开启分享之旅,有兴趣的可联系微信[email protected]。opencv中ArUco模块实践(1)ArUco的生成与检测ArUco与AprilTag简介ChAruco...

  • 在x y的梯度上再求一次导数后相加便是散度。注意不要使用convertScaleAbs int main(int argc, char*argv[]) {cv::Mat IMG = cv::imread("./img.jpg", 1);cv::Mat k = getStructuringElement(cv::MORPH_RECT,...

  • 算法是基于A Fast Parallel Algorithm for Thinning Digital Patterns论文 https://blog.csdn.net/keneyr/article/details/88944563 简单的解释算法: https://blog.csdn.net/xukaiwen_2016...

  • cv::Mat inputIMG = cv::imread("./input/src.jpg", 1);cv::Mat graySrc, grad_x, grad_y;cv::Mat abs_grad_x, abs_grad_y;cv::cvtColor(inputIMG, graySrc, cv::COLOR_BGR2GRAY);...

  • 论文阅读模块将分享点云处理,SLAM,三维视觉,高精地图相关的文章。公众号致力于理解三维视觉领域相关内容的干货分享,欢迎各位加入我,我们一起每天一篇文章阅读,开启分享之旅,有兴趣的可联系微信[email protected]。单应矩阵介绍单应性在计算机视觉领域是一个非常重要的概念,它在图像校正、图像拼接、俯视图生成,相机位姿估计、...

  • 介绍 默认情况下,nginx会加载标准模块ngx_http_map_module(或称ngx_map),除非人为的在configure时添加--without-http_map_module选项。 ngx_http_map_module模块可以创建变量,这些变量的值与另外的变量值相关联。允许分类或者同时映射多个值到多个不同值并储存...

  • 文章目录1. Map的声明2. Map 元素访问带来的问题3. Map 元素的遍历4. 通过Map 实现 函数对象(C++)5. 通过Map实现一个简单Set功能...