马上要毕业了,比较忙,有半年多没有更新博客了,在毕业之际,对以前写的代码进行整理封装,以备后来者改进,希望长江后浪推前浪,一代更比一点强!
整理原有代码,封装为c++类的形式。
分为三个类:即配置类、点映射类和图像映射类。一个基础函数库。一个demo。一个配置文件。
主函数接口:main_virtual_viewpoint_rendering.cpp
基于函数库:base_function.h base_function.cpp
配置类:configure.h configure.cpp
点映射类:point_mapping.h point_mapping.cpp
图像映射类:image_mapping.h image_mapping.cpp
配置文件:conf_file.txt
main_virtual_viewpoint_rendering.cpp
#include "configure.h"
#include "image_mapping.h"
#include "base_function.h"
#include <fstream>
#include <iostream>
using namespace std;
int main(){
Configure cfg("D:\\virtual_viewpoint_rendering\\conf\\conf_file.txt");
char filename[100];
strcpy(filename, cfg.m_psnr_ssim_file);
char time_buf[15];
strcat_s(filename, getNewFileName(time_buf));
strcat_s(filename, ".txt");
ofstream fout(filename);
ImageMapping imgmp(&cfg);
char str_left[200][200];
char str_right[200][200];
char str_virtual[200][200];
double value_psnr = 0;
double value_ssim = 0;
getArrayOfImageFile(&cfg, str_left, str_right, str_virtual);
for (int i = 0; i < 100; i++){
imgmp.process(str_left[i], str_left[i + 100], str_right[i], str_right[i+100]);
value_psnr += psnr(imgmp.save_dirr, str_virtual[i]);
printf("psnr=%lf\n", value_psnr / (i + 1));
fout << value_psnr / (i + 1) << "\t";
value_ssim += ssim(imgmp.save_dirr, str_virtual[i]);
printf("ssim=%lf\n", value_ssim / (i + 1));
fout << value_ssim / (i + 1) << "\t";
fout << endl;
}
cout << "process finished!!!!!!!!!!!!!!!!!!!!!!!!"<<endl;
cout << "******************************************************************"<<endl;
fout << "process finished!!!!!!!!!!!!!!!!!!!!!!!!"<<endl;
fout << "*****************************************************************" << endl;
return 0;
}
base_function.h
#ifndef __base_function__
#define __base_function__
#include "configure.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <time.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
using namespace std;
char *getNewFileName(char time_buf[15]/*YYYYMMDDhhmmss*/);
double round_off(double x);
double max(double x, double y);
double ssim(char *ref_image, char *obj_image);
double psnr(char *ref_image, char *obj_image);
void getArrayOfImageFile(Configure *cfg, char str_left[200][200], char str_right[200][200], char str_virtual[200][200]);
#endif //__base_function__
base_function.cpp
#include "base_function.h"
char *getNewFileName(char time_buf[15]/*YYYYMMDDhhmmss*/){
time_t aclock;
time(&aclock);
strftime(time_buf, 15, "%Y%m%d%H%M%S", localtime(&aclock));
//printf("%s\n", tmpbuf);
return time_buf;
}
double round_off(double x)
{
double temp = floor(x + 0.5);
return temp;
}
double max(double x, double y) {
return ((x > y) ? x : y);
}
double ssim(char *ref_image, char *obj_image)
{
// default settings
double C1 = 6.5025, C2 = 58.5225;
IplImage
*img1 = NULL, *img2 = NULL, *img1_img2 = NULL,
*img1_temp = NULL, *img2_temp = NULL,
*img1_sq = NULL, *img2_sq = NULL,
*mu1 = NULL, *mu2 = NULL,
*mu1_sq = NULL, *mu2_sq = NULL, *mu1_mu2 = NULL,
*sigma1_sq = NULL, *sigma2_sq = NULL, *sigma12 = NULL,
*ssim_map = NULL, *temp1 = NULL, *temp2 = NULL, *temp3 = NULL;
/***************************** INITS **********************************/
img1_temp = cvLoadImage(ref_image);
img2_temp = cvLoadImage(obj_image);
if (img1_temp == NULL || img2_temp == NULL)
return -1;
int x = img1_temp->width, y = img1_temp->height;
int nChan = img1_temp->nChannels, d = IPL_DEPTH_32F;
CvSize size = cvSize(x, y);
img1 = cvCreateImage(size, d, nChan);
img2 = cvCreateImage(size, d, nChan);
cvConvert(img1_temp, img1);
cvConvert(img2_temp, img2);
cvReleaseImage(&img1_temp);
cvReleaseImage(&img2_temp);
img1_sq = cvCreateImage(size, d, nChan);
img2_sq = cvCreateImage(size, d, nChan);
img1_img2 = cvCreateImage(size, d, nChan);
cvPow(img1, img1_sq, 2);
cvPow(img2, img2_sq, 2);
cvMul(img1, img2, img1_img2, 1);
mu1 = cvCreateImage(size, d, nChan);
mu2 = cvCreateImage(size, d, nChan);
mu1_sq = cvCreateImage(size, d, nChan);
mu2_sq = cvCreateImage(size, d, nChan);
mu1_mu2 = cvCreateImage(size, d, nChan);
sigma1_sq = cvCreateImage(size, d, nChan);
sigma2_sq = cvCreateImage(size, d, nChan);
sigma12 = cvCreateImage(size, d, nChan);
temp1 = cvCreateImage(size, d, nChan);
temp2 = cvCreateImage(size, d, nChan);
temp3 = cvCreateImage(size, d, nChan);
ssim_map = cvCreateImage(size, d, nChan);
/*************************** END INITS **********************************/
//
// PRELIMINARY COMPUTING
cvSmooth(img1, mu1, CV_GAUSSIAN, 11, 11, 1.5);
cvSmooth(img2, mu2, CV_GAUSSIAN, 11, 11, 1.5);
cvPow(mu1, mu1_sq, 2);
cvPow(mu2, mu2_sq, 2);
cvMul(mu1, mu2, mu1_mu2, 1);
cvSmooth(img1_sq, sigma1_sq, CV_GAUSSIAN, 11, 11, 1.5);
cvAddWeighted(sigma1_sq, 1, mu1_sq, -1, 0, sigma1_sq);
cvSmooth(img2_sq, sigma2_sq, CV_GAUSSIAN, 11, 11, 1.5);
cvAddWeighted(sigma2_sq, 1, mu2_sq, -1, 0, sigma2_sq);
cvSmooth(img1_img2, sigma12, CV_GAUSSIAN, 11, 11, 1.5);
cvAddWeighted(sigma12, 1, mu1_mu2, -1, 0, sigma12);
//
// FORMULA
// (2*mu1_mu2 + C1)
cvScale(mu1_mu2, temp1, 2);
cvAddS(temp1, cvScalarAll(C1), temp1);
// (2*sigma12 + C2)
cvScale(sigma12, temp2, 2);
cvAddS(temp2, cvScalarAll(C2), temp2);
// ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
cvMul(temp1, temp2, temp3, 1);
// (mu1_sq + mu2_sq + C1)
cvAdd(mu1_sq, mu2_sq, temp1);
cvAddS(temp1, cvScalarAll(C1), temp1);
// (sigma1_sq + sigma2_sq + C2)
cvAdd(sigma1_sq, sigma2_sq, temp2);
cvAddS(temp2, cvScalarAll(C2), temp2);
// ((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2))
cvMul(temp1, temp2, temp1, 1);
// ((2*mu1_mu2 + C1).*(2*sigma12 + C2))./((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2))
cvDiv(temp3, temp1, ssim_map, 1);
CvScalar index_scalar = cvAvg(ssim_map);
// through observation, there is approximately
// 1% error max with the original matlab program
cout << "(R, G & B SSIM index)" << std::endl;
cout << index_scalar.val[2] << endl;
cout << index_scalar.val[1] << endl;
cout << index_scalar.val[0] << endl;
cvReleaseImage(&img1_sq);
cvReleaseImage(&img2_sq);
cvReleaseImage(&img1_img2);
cvReleaseImage(&mu1);
cvReleaseImage(&mu2);
cvReleaseImage(&mu1_sq);
cvReleaseImage(&mu2_sq);
cvReleaseImage(&mu1_mu2);
cvReleaseImage(&sigma1_sq);
cvReleaseImage(&sigma2_sq);
cvReleaseImage(&sigma12);
cvReleaseImage(&temp1);
cvReleaseImage(&temp2);
cvReleaseImage(&temp3);
cvReleaseImage(&ssim_map);
//double ssim=max(max(index_scalar.val[0], index_scalar.val[1]), index_scalar.val[2]);
double ssim = (index_scalar.val[0] + index_scalar.val[1] + index_scalar.val[2]) / 3;
return ssim;
}
double psnr(char *ref_image, char *obj_image)
{
cv::Mat image_ref = cv::imread(ref_image);
cv::Mat image_obj = cv::imread(obj_image);
double mse = 0;
double div_r = 0;
double div_g = 0;
double div_b = 0;
int width = image_ref.cols;
int height = image_ref.rows;
double psnr = 0;
for (int v = 0; v < height; v++)
{
for (int u = 0; u < width; u++)
{
div_r = image_ref.at<cv::Vec3b>(v, u)[0] - image_obj.at<cv::Vec3b>(v, u)[0];
div_g = image_ref.at<cv::Vec3b>(v, u)[1] - image_obj.at<cv::Vec3b>(v, u)[1];
div_b = image_ref.at<cv::Vec3b>(v, u)[2] - image_obj.at<cv::Vec3b>(v, u)[2];
mse += ((div_r*div_r + div_b*div_b + div_g*div_g) / 3);
}
}
mse = mse / (width*height);
psnr = 10 * log10(255 * 255 / mse);
printf("%lf\n", mse);
printf("%lf\n", psnr);
return psnr;
}
void getArrayOfImageFile(Configure *cfg, char str_left[200][200], char str_right[200][200], char str_virtual[200][200]){
FILE *fp_left; FILE *fp_right; FILE *fp_virtual;
int n = 0;
char tmp_str[200];
sprintf(tmp_str, "dir /a-d /b %s >%s\\cam%d.txt", cfg->m_ref_left_path, cfg->m_store_path, cfg->m_left_viewpoint);
system(tmp_str);
sprintf(tmp_str, "dir /a-d /b %s >%s\\cam%d.txt", cfg->m_ref_right_path, cfg->m_store_path, cfg->m_right_viewpoint);
system(tmp_str);
sprintf(tmp_str, "dir /a-d /b %s >%s\\cam%d.txt", cfg->m_acture_path, cfg->m_store_path, cfg->m_virtual_viewpoint);
system(tmp_str);
sprintf(tmp_str, "%s\\cam%d.txt", cfg->m_store_path, cfg->m_left_viewpoint);
fp_left = fopen(tmp_str, "r");
sprintf(tmp_str, "%s\\cam%d.txt", cfg->m_store_path, cfg->m_right_viewpoint);
fp_right = fopen(tmp_str, "r");
sprintf(tmp_str, "%s\\cam%d.txt", cfg->m_store_path, cfg->m_virtual_viewpoint);
fp_virtual = fopen(tmp_str, "r");
while (1){
if (fgets(str_left[n], 100, fp_left) == NULL) break;
str_left[n][strlen(str_left[n]) - 1] = '\0';
memset(tmp_str,0,200);
strcat_s(tmp_str, cfg->m_ref_left_path);
strcat_s(tmp_str, str_left[n]);
strcpy(str_left[n],tmp_str);
if (fgets(str_right[n], 100, fp_right) == NULL) break;
str_right[n][strlen(str_right[n]) - 1] = '\0';
memset(tmp_str, 0, 200);
strcat_s(tmp_str, cfg->m_ref_right_path);
strcat_s(tmp_str, str_right[n]);
strcpy(str_right[n], tmp_str);
if (fgets(str_virtual[n], 50, fp_virtual) == NULL) break;
str_virtual[n][strlen(str_virtual[n]) - 1] = '\0';
memset(tmp_str, 0, 200);
strcat_s(tmp_str, cfg->m_acture_path);
strcat_s(tmp_str, str_virtual[n]);
strcpy(str_virtual[n], tmp_str);
n++;
}
fclose(fp_left); fclose(fp_right); fclose(fp_virtual);
}
configure.h
#ifndef __configure__
#define __configure__
#include "type.h"
class Configure{
public:
Configure(char *conf_file);
~Configure();
private:
void InitializeFromFile(char *fileName);
void allocMemory(char *fileName);
void readCalibrationFile(char *fileName);
void computeProjectionMatrices();
void releaseMemory();
public:
CalibStruct *m_CalibParams;//store parameter of viewpoint .
int m_NumCameras; //the number of viewpoint
int m_Width; //image width
int m_Height; // image height
double m_Z_Min; //minimum distance of actual z
double m_Z_Max; //maximum distance of actual z
int m_left_viewpoint;// left reference viewpoint
int m_right_viewpoint;//right reference viewpoint
int m_virtual_viewpoint;//synthesis viewpoint
char m_store_path[200];//image store path
char m_ref_left_path[200];//left reference image path
char m_ref_right_path[200];//right reference image path
char m_acture_path[200];//acture image path
char m_parameter_cam_file[200];//camera parameter file
char m_psnr_ssim_file[200];//store psnr and ssim about according image
};
#endif //__configure__
configure.cpp
#include "configure.h"
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
Configure::Configure(char *conf_file){
allocMemory(conf_file);
}
Configure::~Configure(){
releaseMemory();
}
void Configure::allocMemory(char *fileName){
char buffer[256];
ifstream myfile(fileName);
if (!myfile){
cout << "Unable to open myfile";
exit(1); // terminate with error
}
while (!myfile.eof()){
myfile.getline(buffer, 100);
if (strcmp(buffer, "#Z_Min_Max") == 0){
myfile.getline(buffer, 30);
sscanf(buffer, "%lf\t%lf", &m_Z_Min, &m_Z_Max);
}
else if (strcmp(buffer, "#Image_Width_Height") == 0){
myfile.getline(buffer, 30);
sscanf(buffer, "%d\t%d", &m_Width, &m_Height);
}
else if (strcmp(buffer, "#Number_camera") == 0){
myfile.getline(buffer, 30);
sscanf(buffer, "%d", &m_NumCameras);
}
else if (strcmp(buffer, "#Left_right_synthesis") == 0){
myfile.getline(buffer, 30);
sscanf(buffer, "%d\t%d\t%d", &m_left_viewpoint, &m_right_viewpoint, &m_virtual_viewpoint);
}
else if (strcmp(buffer, "#Store_path") == 0){
myfile.getline(buffer, 100);
sscanf(buffer, "%s", m_store_path);
}
else if (strcmp(buffer, "#Left_image_path") == 0){
myfile.getline(buffer, 200);
sscanf(buffer, "%s", m_ref_left_path);
}
else if (strcmp(buffer, "#Right_image_path") == 0){
myfile.getline(buffer, 200);
sscanf(buffer, "%s", m_ref_right_path);
}
else if (strcmp(buffer, "#Acture_image_path") == 0){
myfile.getline(buffer, 200);
sscanf(buffer, "%s", m_acture_path);
}
else if (strcmp(buffer, "#Parameter_cam_file") == 0){
myfile.getline(buffer, 200);
sscanf(buffer, "%s", m_parameter_cam_file);
}
else if (strcmp(buffer, "#Store_psnr_ssim_file") == 0){
myfile.getline(buffer, 200);
sscanf(buffer, "%s", m_psnr_ssim_file);
}
}
myfile.close();
m_CalibParams = (CalibStruct *)malloc(sizeof(CalibStruct) * m_NumCameras);
InitializeFromFile(m_parameter_cam_file);
}
void Configure::releaseMemory(){
free(m_CalibParams);
m_CalibParams = NULL;/*add this sentence*/
}
void Configure::InitializeFromFile(char *fileName){
readCalibrationFile(fileName);
computeProjectionMatrices();
}
void Configure::readCalibrationFile(char *fileName){
int i, j, k;
FILE *pIn;
double dIn; // dummy variable
int camIdx;
if (pIn = fopen(fileName, "r"))
{
for (k = 0; k<m_NumCameras; k++)
{
// camera index
fscanf(pIn, "%d", &camIdx);
//std::cout << camIdx << std::endl;
// camera intrinsics
for (i = 0; i < 3; i++){
fscanf(pIn, "%lf\t%lf\t%lf", &(m_CalibParams[camIdx].m_K[i][0]), &(m_CalibParams[camIdx].m_K[i][1]), &(m_CalibParams[camIdx].m_K[i][2]));
//std::cout << (m_CalibParams[camIdx].m_K[i][0])<<"\t"<<(m_CalibParams[camIdx].m_K[i][1]) <<"\t"<< (m_CalibParams[camIdx].m_K[i][2]) << std::endl;
}
// read barrel distortion params (assume 0)
fscanf(pIn, "%lf", &dIn);
fscanf(pIn, "%lf", &dIn);
// read extrinsics
for (i = 0; i<3; i++)
{
for (j = 0; j<3; j++)
{
fscanf(pIn, "%lf", &dIn);
m_CalibParams[camIdx].m_RotMatrix[i][j] = dIn;
//std::cout << m_CalibParams[camIdx].m_RotMatrix[i][j] << std::endl;
}
fscanf(pIn, "%lf", &dIn);
m_CalibParams[camIdx].m_Trans[i] = dIn;
}
}
fclose(pIn);
}
}
void Configure::computeProjectionMatrices(){
int i, j, k, camIdx;
double(*inMat)[3];
double exMat[3][4];
for (camIdx = 0; camIdx<m_NumCameras; camIdx++)
{
// The intrinsic matrix
inMat = m_CalibParams[camIdx].m_K;
// The extrinsic matrix
for (i = 0; i<3; i++)
{
for (j = 0; j<3; j++)
{
exMat[i][j] = m_CalibParams[camIdx].m_RotMatrix[i][j];
}
}
for (i = 0; i<3; i++)
{
exMat[i][3] = m_CalibParams[camIdx].m_Trans[i];
}
// Multiply the intrinsic matrix by the extrinsic matrix to find our projection matrix
for (i = 0; i<3; i++)
{
for (j = 0; j<4; j++)
{
m_CalibParams[camIdx].m_ProjMatrix[i][j] = 0.0;
for (k = 0; k<3; k++)
{
m_CalibParams[camIdx].m_ProjMatrix[i][j] += inMat[i][k] * exMat[k][j];
}
}
}
m_CalibParams[camIdx].m_ProjMatrix[3][0] = 0.0;
m_CalibParams[camIdx].m_ProjMatrix[3][1] = 0.0;
m_CalibParams[camIdx].m_ProjMatrix[3][2] = 0.0;
m_CalibParams[camIdx].m_ProjMatrix[3][3] = 1.0;
}
}
point_mapping.h
#ifndef __point_mapping__
#define __point_mapping__
#include "configure.h"
class PointMapping{
public:
PointMapping(Configure *cfg);
~PointMapping();
void setValue(int u, int v, int d, int ref, int tgt);
void projectionReftoTgt();
private:
double DepthLevelToZ(unsigned char d);
unsigned char ZToDepthLever(double z);
void projectionUVZtoXY(double projMatrix[4][4], double u, double v, double z, double *x, double *y);
double projectionXYZtoUV(double projMatrix[4][4], double x, double y, double z, double *u, double *v);
public:
int m_reference_viewpoint;
int m_u;
int m_v;
int m_d;
int m_target_viewpoint;
double m_u_t;
double m_v_t;
double m_d_t;
double m_z_t;
private:
Configure *m_cfg_pm;
double m_Z_min_pm;
double m_Z_max_pm;
int m_Height_pm;
};
#endif
point_mapping.cpp
#include "point_mapping.h"
PointMapping::PointMapping(Configure *cfg){
m_Z_min_pm = cfg->m_Z_Min;
m_Z_max_pm = cfg->m_Z_Max;
m_Height_pm = cfg->m_Height;
m_cfg_pm = cfg;
}
PointMapping::~PointMapping(){
}
void PointMapping::setValue(int u, int v, int d, int ref, int tgt){
m_u = u;
m_v = v;
m_d = d;
m_reference_viewpoint = ref;
m_target_viewpoint = tgt;
}
double PointMapping::DepthLevelToZ(unsigned char d){
double z;
z = 1.0 / ((d / 255.0)*(1.0 / m_Z_min_pm - 1.0 / m_Z_max_pm) + 1.0 / m_Z_max_pm);
return z;
}
unsigned char PointMapping::ZToDepthLever(double z){
unsigned char d;
d = (unsigned char)(255.0*(1 / (double)z - 1 / m_Z_max_pm) / (1 / m_Z_min_pm - 1 / m_Z_max_pm));
return d;
}
void PointMapping::projectionUVZtoXY(double projMatrix[4][4], double u, double v, double z, double *x, double *y){
double c0, c1, c2;
// image (0,0) is bottom lefthand corner
v = (double)m_Height_pm - v - 1.0;
c0 = z*projMatrix[0][2] + projMatrix[0][3];
c1 = z*projMatrix[1][2] + projMatrix[1][3];
c2 = z*projMatrix[2][2] + projMatrix[2][3];
*y = u*(c1*projMatrix[2][0] - projMatrix[1][0] * c2) +
v*(c2*projMatrix[0][0] - projMatrix[2][0] * c0) +
projMatrix[1][0] * c0 - c1*projMatrix[0][0];
*y /= v*(projMatrix[2][0] * projMatrix[0][1] - projMatrix[2][1] * projMatrix[0][0]) +
u*(projMatrix[1][0] * projMatrix[2][1] - projMatrix[1][1] * projMatrix[2][0]) +
projMatrix[0][0] * projMatrix[1][1] - projMatrix[1][0] * projMatrix[0][1];
*x = (*y)*(projMatrix[0][1] - projMatrix[2][1] * u) + c0 - c2*u;
*x /= projMatrix[2][0] * u - projMatrix[0][0];
}
double PointMapping::projectionXYZtoUV(double projMatrix[4][4], double x, double y, double z, double *u, double *v){
double w;
*u = projMatrix[0][0] * x +
projMatrix[0][1] * y +
projMatrix[0][2] * z +
projMatrix[0][3];
*v = projMatrix[1][0] * x +
projMatrix[1][1] * y +
projMatrix[1][2] * z +
projMatrix[1][3];
w = projMatrix[2][0] * x +
projMatrix[2][1] * y +
projMatrix[2][2] * z +
projMatrix[2][3];
*u /= w;
*v /= w;
// image (0,0) is bottom lefthand corner
*v = (double)m_Height_pm - *v - 1.0;
return w;
}
void PointMapping::projectionReftoTgt(){
double x, y, z = DepthLevelToZ(m_d);
projectionUVZtoXY(m_cfg_pm->m_CalibParams[m_reference_viewpoint].m_ProjMatrix, (double)m_u, (double)m_v, z, &x, &y);
m_z_t = projectionXYZtoUV(m_cfg_pm->m_CalibParams[m_target_viewpoint].m_ProjMatrix, x, y, z, &m_u_t, &m_v_t);
m_d_t = ZToDepthLever(m_z_t);
}
image_mapping.h
#ifndef __image_mapping__
#define __image_mapping__
#include "point_mapping.h"
#include<opencv2/opencv.hpp>
class ImageMapping{
public:
ImageMapping(Configure *cfg);
~ImageMapping();
void readAndSetImage(char *ref_c_1,char *ref_d_1,char *ref_c_2,char *ref_d_2);
void setArraytoImage(unsigned char *image_out_c, cv::Mat imageColorOut);
void storeImage(cv::Mat imageColorOut);
virtual void imageFusion(unsigned char *image_c1, unsigned char *image_c2, unsigned char *image_c3);
virtual void map(unsigned char *c_image_in, double *d_image_in, unsigned char *c_image_out, double *d_image_out, int ref, int tgt);
virtual void process(char *ref_c_1, char *ref_d_1, char *ref_c_2, char *ref_d_2);
void resetMemory();
public:
char save_dirr[200];
private:
Configure *m_cfg_im;
PointMapping *m_pm_im;
cv::Mat imageColorOut;
unsigned char *image_in_c1;
double *image_in_d1;
unsigned char *image_in_c2;
double *image_in_d2;
unsigned char *image_out_c1;
double *image_out_d1;
unsigned char *image_out_c2;
double *image_out_d2;
unsigned char *image_out_c;
double *image_out_d;
int memofImage;
};
#endif //__image_mapping__
image_mapping.cpp
#include "image_mapping.h"
ImageMapping::ImageMapping(Configure *cfg){
m_cfg_im = cfg;
int sizeofImage = m_cfg_im->m_Width * m_cfg_im->m_Height;
memofImage = sizeofImage * 3;
m_pm_im = new PointMapping(cfg);
image_in_c1 = (unsigned char *)malloc(sizeof(unsigned char)*memofImage);
image_in_d1 = (double *)malloc(sizeof(double)*memofImage);
image_in_c2 = (unsigned char *)malloc(sizeof(unsigned char)*memofImage);
image_in_d2 = (double *)malloc(sizeof(double)*memofImage);
image_out_c1 = (unsigned char *)malloc(sizeof(unsigned char)*memofImage);
image_out_d1 = (double *)malloc(sizeof(double)*memofImage);
image_out_c2 = (unsigned char *)malloc(sizeof(unsigned char)*memofImage);
image_out_d2 = (double *)malloc(sizeof(double)*memofImage);
image_out_c = (unsigned char *)malloc(sizeof(unsigned char)*memofImage);
image_out_d = (double *)malloc(sizeof(double)*memofImage);
}
ImageMapping::~ImageMapping(){
delete m_pm_im;
free(image_in_c1); free(image_in_d1);
free(image_in_c2); free(image_in_d2);
free(image_out_c1); free(image_out_d1);
free(image_out_c2); free(image_out_d2);
free(image_out_c); free(image_out_d);
}
void ImageMapping::resetMemory(){
memset(image_out_c1, 0, sizeof(unsigned char)*memofImage);
memset(image_out_d1, 0, sizeof(double)*memofImage);
memset(image_out_c2, 0, sizeof(unsigned char)*memofImage);
memset(image_out_d2, 0, sizeof(double)*memofImage);
memset(image_out_c, 0, sizeof(unsigned char)*memofImage);
memset(image_out_d, 0, sizeof(double)*memofImage);
}
void ImageMapping::readAndSetImage(char *ref_c_1, char *ref_d_1, char *ref_c_2, char *ref_d_2){
cv::Mat imageColor = cv::imread(ref_c_1); cv::Mat imageDepth = cv::imread(ref_d_1);
cv::Mat imageColor2 = cv::imread(ref_c_2); cv::Mat imageDepth2 = cv::imread(ref_d_2);
imageColorOut.create(imageColor.rows, imageColor.cols, imageColor.type());
/*cv::imshow("virtruel_Depth_image1", imageColor);
cv::imshow("virtruel_Depth_image2", imageDepth);
cv::imshow("virtruel_Depth_image3", imageColor2);
cv::imshow("virtruel_Depth_image4", imageDepth2);
cv::waitKey();*/
resetMemory();
int i = 0, j = 0, m = 0, n = 0;
for (int v = 0; v < m_cfg_im->m_Height; v++)
{
for (int u = 0; u < m_cfg_im->m_Width; u++)
{
image_in_c1[i++] = imageColor.at<cv::Vec3b>(v, u)[0]; image_in_c1[i++] = imageColor.at<cv::Vec3b>(v, u)[1]; image_in_c1[i++] = imageColor.at<cv::Vec3b>(v, u)[2];
image_in_c2[j++] = imageColor2.at<cv::Vec3b>(v, u)[0]; image_in_c2[j++] = imageColor2.at<cv::Vec3b>(v, u)[1]; image_in_c2[j++] = imageColor2.at<cv::Vec3b>(v, u)[2];
image_in_d1[m++] = imageDepth.at<cv::Vec3b>(v, u)[0]; image_in_d1[m++] = imageDepth.at<cv::Vec3b>(v, u)[1]; image_in_d1[m++] = imageDepth.at<cv::Vec3b>(v, u)[2];
image_in_d2[n++] = imageDepth2.at<cv::Vec3b>(v, u)[0]; image_in_d2[n++] = imageDepth2.at<cv::Vec3b>(v, u)[1]; image_in_d2[n++] = imageDepth2.at<cv::Vec3b>(v, u)[2];
imageColorOut.at<cv::Vec3b>(v, u)[0] = imageColorOut.at<cv::Vec3b>(v, u)[1] = imageColorOut.at<cv::Vec3b>(v, u)[2] = 0;
}
}
}
void ImageMapping::setArraytoImage(unsigned char *image_out_c, cv::Mat imageColorOut){
int i = 0;
for (int v = 0; v < m_cfg_im->m_Height; v++)
{
for (int u = 0; u < m_cfg_im->m_Width; u++)
{
imageColorOut.at<cv::Vec3b>(v, u)[0] = image_out_c[i++];
imageColorOut.at<cv::Vec3b>(v, u)[1] = image_out_c[i++];
imageColorOut.at<cv::Vec3b>(v, u)[2] = image_out_c[i++];
}
}
}
void ImageMapping::storeImage(cv::Mat imageColorOut){
memset(save_dirr, 0, 200);
strcpy(save_dirr, m_cfg_im->m_store_path);
char string[10];
char hou[10] = ".jpg";
static int iter = 0;
_itoa(iter++, string, 10);
save_dirr[strlen(save_dirr)] = '\0';
string[strlen(string)] = '\0';
hou[strlen(hou)] = '\0';
strcat(save_dirr, string);
strcat(save_dirr, hou);
cv::imwrite(save_dirr, imageColorOut);
}
void ImageMapping::map(unsigned char *c_image_in, double *d_image_in, unsigned char *c_image_out, double *d_image_out, int ref, int tgt){
for (int v = 0; v < m_cfg_im->m_Height; v++)
for (int u = 0; u < m_cfg_im->m_Width; u++)
{
int offset = (v*m_cfg_im->m_Width + u)*3;
double d = d_image_in[offset];
//printf("%lf\n", d);
m_pm_im->setValue(u, v, d, ref, tgt);
m_pm_im->projectionReftoTgt();
int u_tgt = m_pm_im->m_u_t;
int v_tgt = m_pm_im->m_v_t;
int d_tgt = m_pm_im->m_d_t;
if (u_tgt < 0 || u_tgt >= m_cfg_im->m_Width - 1 || v_tgt < 0 || v_tgt >= m_cfg_im->m_Height - 1)
continue;
int offset_new = (v_tgt*m_cfg_im->m_Width + u_tgt) * 3;
if (d_tgt < d_image_out[offset_new])
continue;
c_image_out[offset_new] = c_image_in[offset]; c_image_out[offset_new+1] = c_image_in[offset+1]; c_image_out[offset_new+2] = c_image_in[offset+2];
d_image_out[offset_new] = d_image_out[offset_new+1] = d_image_out[offset_new+2] = d_tgt;
}
}
void ImageMapping::imageFusion(unsigned char *image_c1, unsigned char *image_c2, unsigned char *image_c3){
for (int v = 0; v < m_cfg_im->m_Height; v++){
for (int u = 0; u < m_cfg_im->m_Width; u++){
int offset = (v*m_cfg_im->m_Width + u) * 3;
bool flag_c2 = image_c2[offset + 2] != 0 || image_c2[offset + 1] != 0 || image_c2[offset + 0] != 0;
bool flag_c1 = image_c1[offset + 2] != 0 || image_c1[offset + 1] != 0 || image_c1[offset + 0] != 0;
//image_c3[offset + 0] = image_c2[offset + 0]; image_c3[offset + 1] = image_c2[offset + 1]; image_c3[offset + 2] = image_c2[offset + 2];
if (image_c1[offset + 2] == 0 && image_c1[offset + 1] == 0 && image_c1[offset + 0] == 0 && flag_c2){
image_c3[offset + 0] = image_c2[offset + 0]; image_c3[offset + 1] = image_c2[offset + 1]; image_c3[offset + 2] = image_c2[offset + 2];
}
else if (image_c2[offset + 2] == 0 && image_c2[offset + 1] == 0 && image_c2[offset + 0] == 0 && flag_c1){
image_c3[offset + 0] = image_c1[offset + 0]; image_c3[offset + 1] = image_c1[offset + 1]; image_c3[offset + 2] = image_c1[offset + 2];
}
else if(flag_c1 && flag_c2){
image_c3[offset + 0] = (image_c1[offset + 0] + image_c2[offset + 0]) / 2; image_c3[offset + 1] = (image_c1[offset + 1] + image_c2[offset + 1]) / 2; image_c3[offset + 2] = (image_c1[offset + 2] + image_c2[offset + 2]) / 2;
}
}
}
}
void ImageMapping::process(char *ref_c_1, char *ref_d_1, char *ref_c_2, char *ref_d_2){
readAndSetImage(ref_c_1, ref_d_1, ref_c_2, ref_d_2);
map(image_in_c1, image_in_d1, image_out_c1, image_out_d1, m_cfg_im->m_left_viewpoint, m_cfg_im->m_virtual_viewpoint);
map(image_in_c2, image_in_d2, image_out_c2, image_out_d2, m_cfg_im->m_right_viewpoint, m_cfg_im->m_virtual_viewpoint);
imageFusion(image_out_c1, image_out_c2, image_out_c);
setArraytoImage(image_out_c, imageColorOut);
//cv::medianBlur(imageColorOut, imageColorOut, 3);
//cv::medianBlur(imageColorOut, imageColorOut, 3);
storeImage(imageColorOut);
}
conf_file.txt
#Z_Min_Max
42.0 130.0
#Image_Width_Height
1024 768
#Left_right_synthesis
5 7 6
#Number_camera
8
#Store_path
D:\virtual_viewpoint_rendering\pic\
#Left_image_path
C:\Users\jiang\Desktop\王江_2016_5_资料整理\实验数据\3DVideos-distrib\MSR3DVideo-Ballet\cam5\
#Right_image_path
C:\Users\jiang\Desktop\王江_2016_5_资料整理\实验数据\3DVideos-distrib\MSR3DVideo-Ballet\cam7\
#Acture_image_path
C:\Users\jiang\Desktop\王江_2016_5_资料整理\实验数据\3DVideos-distrib\MSR3DVideo-Ballet\cam6\
#Parameter_cam_file
C:\Users\jiang\Desktop\王江_2016_5_资料整理\实验数据\3DVideos-distrib\MSR3DVideo-Ballet\calibParams-ballet.txt
#Store_psnr_ssim_file
D:\virtual_viewpoint_rendering\pic\psnr_ssim