全景图六张图片的点
float M_PI = 3.14159265358979323846f;
float faceTransform[6][2] =
{
{0, 0},
{M_PI / 2,0},
{M_PI,0},
{-M_PI / 2,0},
{0,-M_PI / 2},
{0,M_PI / 2}
};
c++实现源码
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<math.h>
#include <iostream>using namespace cv;
using namespace std;float M_PI = 3.14159265358979323846f;
float faceTransform[6][2] =
{
{0, 0},
{M_PI / 2,0},
{M_PI,0},
{-M_PI / 2,0},
{0,-M_PI / 2},
{0,M_PI / 2}
};
inline void createCubeMapFace(const Mat& in, Mat& face, int faceId = 0, const int width = -1, const int height = -1)
{float inWidth = in.cols;
float inHeight = in.rows; // 获取图片的行列数量// cout << in.cols;
// cout << in.rows;
// system("pause");
// Allocate map
Mat mapx(height, width, CV_32F);
Mat mapy(height, width, CV_32F); //分配图的x,y轴
const float an = sin(M_PI / 4);
const float ak = cos(M_PI / 4); //计算相邻ak和相反an的三角形张成球体中心const float ftu = faceTransform[faceId][0];
const float ftv = faceTransform[faceId][1];//
// 对于每个图像计算相应的源坐标
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {// 将坐标映射在平面上
float nx = (float)y / (float)height - 0.5f;
float ny = (float)x / (float)width - 0.5f;nx *= 2;
ny *= 2;
nx *= an;
ny *= an;float u, v;
if (ftv == 0) {
u = atan2(nx, ak);
v = atan2(ny * cos(u), ak);
u += ftu;
}
else if (ftv > 0) {
float d = sqrt(nx * nx + ny * ny);
v = M_PI / 2 - atan2(d, ak);
u = atan2(ny, nx);
}
else {
float d = sqrt(nx * nx + ny * ny);
v = -M_PI / 2 + atan2(d, ak);
u = atan2(-ny, nx);
}u = u / (M_PI);
v = v / (M_PI / 2);while (v < -1) {
v += 2;
u += 1;
}
while (v > 1) {
v -= 2;
u += 1;
}while (u < -1) {
u += 2;
}
while (u > 1) {
u -= 2;
}u = u / 2.0f + 0.5f;
v = v / 2.0f + 0.5f;u = u * (inWidth - 1);
v = v * (inHeight - 1);mapx.at<float>(x, y) = u;
mapy.at<float>(x, y) = v;
}
}if (face.cols != width || face.rows != height ||
face.type() != in.type()) {
face = Mat(width, height, in.type());
}remap(in, face, mapx, mapy, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));
if (faceId == 0)
{
imwrite("F:\\bean\\cube0000.jpg", face);
}
else if (faceId == 1)
{
imwrite("F:\\bean\\cube0001.jpg", face);
}
else if (faceId == 2)
{
imwrite("F:\\bean\\cube0002.jpg", face);
}
else if (faceId == 3)
{
imwrite("F:\\bean\\cube0003.jpg", face);
}
else if (faceId == 4)
{
imwrite("F:\\bean\\cube0004.jpg", face);
}
else if (faceId == 5)
{
imwrite("F:\\bean\\cube0005.jpg", face);
}}
int main(int count, char* inputstr[])
{
for (int i = 0; i < count; i++)
cout << inputstr[i] << endl;
int faceId = 0;
int width = 1296;
int height = 1296;
string path = "C:\\Users\\Admin\\Desktop\\testimage\\3.png";
if (count > 1) {
path = inputstr[2];
}
cv::Mat srcimage = cv::imread(path);
cv::Mat resultImage;
for (int i = 0; i < 6; i++) {
createCubeMapFace(srcimage, resultImage, i, width, height);
}}