经过几天的学习测试,并行程序初见成效,GPU高性能变成CUDA实战确实是初学者的福音,下面两个程序是初次编写的cuda程序,并不完整,还需要完善的。
/*
提出的算法
*/
#ifndef _wrapingOf3D1
#define _wrapingOf3D1
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
//#include "cuda.h"
#include<iostream>
#include<opencv2/opencv.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <math.h>
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include<io.h>
using std::cout;
using std::endl;
using namespace std;
#include <cmath>
#include<string.h>
#include <iostream>
#include <fstream>
using namespace std;
#include <stdio.h>
#include <time.h>
char tmpbuf[15];//YYYYMMDDhhmmss
int ttt = 1;
char *getNewFileName(){
time_t aclock;
time(&aclock);
strftime(tmpbuf, 15, "%Y%m%d%H%M%S", localtime(&aclock));
printf("%s\n", tmpbuf);
return tmpbuf;
}
double round_off(double x)
{
double temp = floor(x + 0.5);
return temp;
}
__device__ int pround_off(double x)
{
int temp = (int)(x + 0.5);
return temp;
}
double max1(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 = max1(max1(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;
}
/*
**define a struct included intrinsic and extrinsic args
*/
typedef struct {
double m_K[3][3]; // 3x3 intrinsic matrix
double m_RotMatrix[3][3]; // rotation matrix
double m_Trans[3]; // translation vector
double m_ProjMatrix[4][4]; // projection matrix
} CalibStruct;
typedef struct {
double m_K[9]; // 3x3 intrinsic matrix
double m_RotMatrix[9]; // rotation matrix
double m_Trans[3]; // translation vector
double m_ProjMatrix[16]; // projection matrix
} pCalibStruct;
/*
**define globle variables
*/
CalibStruct m_CalibParams[8];
pCalibStruct *devm_CalibParams;
int m_NumCameras = 8;
int m_Width = 1024, m_Height = 768; // camera resolution is 1024x768
double pts[8][3];
double ppts[24];
double *devpts;
/*
**declare function
*/
//void InitializeFromFile(char *fileName);
//double DepthLevelToZ(unsigned char d);
//unsigned char ZToDepthLever(double z);
//double projXYZtoUV(double projMatrix[4][4], double x, double y, double z, double *u, double *v);
//void projUVZtoXY(double projMatrix[4][4], double u, double v, double z, double *x, double *y, double pt[8][2]);
//void wrapingImage(int ref, int proj, cv::Mat &imageColor, cv::Mat &imageDepth, cv::Mat &imageColorOut);
//void pointProject_from_ref_to_otherView(double pts[8][2], int ref, int u, int v, unsigned char d);
/*
**define function
*/
/*
** read text file and write args to struct of globle variable
*/
void 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);
}
}// readCalibrationFile
/*
**calcular all projectionMatrices depended on struct variables
*/
void 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;
}
}
/**
**init projection matrix
*/
void InitializeFromFile(char *fileName)
{
readCalibrationFile(fileName);
computeProjectionMatrices();
}
/**
**calcular z depended on d of depth image
*/
double DepthLevelToZ(unsigned char d)
{
double z;
double MinZ = 44.0, MaxZ = 120.0;
z = 1.0 / ((d / 255.0)*(1.0 / MinZ - 1.0 / MaxZ) + 1.0 / MaxZ);
return z;
}
__device__ double pDepthLevelToZ(unsigned char d)
{
double z;
double MinZ = 44.0, MaxZ = 120.0;
z = 1.0 / ((d / 255.0)*(1.0 / MinZ - 1.0 / MaxZ) + 1.0 / MaxZ);
return z;
}
/**
**calcular d of depth image depended on z
*/
unsigned char ZToDepthLever(double z)
{
double MinZ = 44.0, MaxZ = 120.0;
unsigned char d;
d = (unsigned char)(255.0*(1 / (double)z - 1 / MaxZ) / (1 / MinZ - 1 / MaxZ));
return d;
}
__device__ unsigned char pZToDepthLever(double z)
{
double MinZ = 44.0, MaxZ = 120.0;
unsigned char d;
d = (unsigned char)(255.0*(1 / (double)z - 1 / MaxZ) / (1 / MinZ - 1 / MaxZ));
return d;
}
/**
**calcular x,y depended on u,v,z and projection Matrix
**for example,projection Matrix is m_CalibParams[i].m_ProjMatrix which is index of camera
*/
void projUVZtoXY(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 - 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];
} // projUVZtoXY
__device__ void pprojUVZtoXY(double *projMatrix, double u, double v, double z, double *x, double *y)
{
double c0, c1, c2;
// image (0,0) is bottom lefthand corner
int mheight = 768;
v = (double)mheight - v - 1.0;
c0 = z*projMatrix[2] + projMatrix[3];
c1 = z*projMatrix[6] + projMatrix[7];
c2 = z*projMatrix[10] + projMatrix[11];
*y = u*(c1*projMatrix[8] - projMatrix[4] * c2) +
v*(c2*projMatrix[0] - projMatrix[8] * c0) +
projMatrix[4] * c0 - c1*projMatrix[0];
*y /= v*(projMatrix[8] * projMatrix[1] - projMatrix[9] * projMatrix[0]) +
u*(projMatrix[4] * projMatrix[9] - projMatrix[5] * projMatrix[8]) +
projMatrix[0] * projMatrix[5] - projMatrix[4] * projMatrix[1];
*x = (*y)*(projMatrix[1] - projMatrix[9] * u) + c0 - c2*u;
*x /= projMatrix[8] * u - projMatrix[0];
} // projUVZtoXY
/**
**calcular u,v,z1 depended on x,y,z
**z1 is after projection and z is before projection.z1 is return value
**/
double projXYZtoUV(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 - *v - 1.0;
return w;
} // projXYZtoUV
__device__ double pprojXYZtoUV(double projMatrix[16], double x, double y, double z, double *u, double *v, int devm_Height)
{
double w;
*u = projMatrix[0] * x +
projMatrix[1] * y +
projMatrix[2] * z +
projMatrix[3];
*v = projMatrix[4] * x +
projMatrix[5] * y +
projMatrix[6] * z +
projMatrix[7];
w = projMatrix[8] * x +
projMatrix[9] * y +
projMatrix[10] * z +
projMatrix[11];
*u /= w;
*v /= w;
// image (0,0) is bottom lefthand corner
*v = (double)devm_Height - *v - 1.0;
return w;
} // projXYZtoUV
/**
**/
void pointProject_from_ref_to_otherView(double pts[8][3], int ref, int u, int v, unsigned char d)
{
double x, y, z = DepthLevelToZ(d);
//printf("Testing projection of pt (%d,%d) in camera 0 with d = %d (z = %f) to other cameras\n", u, v, d, z);
projUVZtoXY(m_CalibParams[ref].m_ProjMatrix, (double)u, (double)v, z, &x, &y);
//printf("3D pt = (%f, %f, %f) [coordinates wrt reference camera]\n", x, y, z);
for (int cam = 0; cam<8; cam++)
{
double *pt = pts[cam];
pt[0] = 0;
pt[1] = 0;
pt[2] = 0;
}
for (int cam = 0; cam<8; cam++)
{
double *pt = pts[cam];
pt[2] = projXYZtoUV(m_CalibParams[cam].m_ProjMatrix, x, y, z, &pt[0], &pt[1]);
//printf("Camera %d: (%f, %f)\n", cam, pt[0], pt[1]);
pt[2] = ZToDepthLever(pt[2]);
}
}
__device__ void ppointProject_from_ref_to_otherView(pCalibStruct *devm_CalibParams, double *devpts, int ref, int u, int v, unsigned char d)
{
double x, y, z = pDepthLevelToZ(d);
//printf("Testing projection of pt (%d,%d) in camera 0 with d = %d (z = %f) to other cameras\n", u, v, d, z);
pprojUVZtoXY(devm_CalibParams[ref].m_ProjMatrix, (double)u, (double)v, z, &x, &y);
//printf("3D pt = (%f, %f, %f) [coordinates wrt reference camera]\n", x, y, z);
for (int cam = 0; cam<24; cam++)
{
devpts[cam] = 0;
}
for (int cam = 0; cam<8; cam++)
{
//pt[2] = projXYZtoUV(devm_CalibParams[cam].m_ProjMatrix, x, y, z, &pt[0], &pt[1]);
devpts[cam * 3 + 2] = pprojXYZtoUV(devm_CalibParams[cam].m_ProjMatrix, x, y, z, &devpts[cam * 3], &devpts[cam * 3 + 1], 768);
//printf("Camera %d: (%f, %f)\n", cam, pt[0], pt[1]);
//pt[2] = ZToDepthLever(pt[2]);
devpts[cam * 3 + 2] = pZToDepthLever(devpts[cam * 3 + 2]);
}
}
/**
**wraping image,ref represent reference cam,proj represent projection cam
**the kernal code
**/
void wrapingImage(int ref, int proj, cv::Mat &imageColor, cv::Mat &imageDepth, cv::Mat &imageColorOut, cv::Mat &imageDepthOut)
{
for (int v = 0; v < imageColor.rows; v++)
for (int u = 0; u < imageColor.cols; u++)
{
double d = imageDepth.at<cv::Vec3b>(v, u)[0];
pointProject_from_ref_to_otherView(pts, ref, u, v, d);
int u1 = (int)pts[proj][0];
int v1 = (int)pts[proj][1];
int k1 = (int)pts[proj][2];
if (u1 < 0 || u1 >= imageColor.cols - 1 || v1 < 0 || v1 >= imageColor.rows - 1)
continue;
if (k1 < imageDepthOut.at<cv::Vec3b>(v1, u1)[0])
continue;
imageColorOut.at<cv::Vec3b>(v1, u1) = imageColor.at<cv::Vec3b>(v, u);
imageDepthOut.at<cv::Vec3b>(v1, u1)[0] = k1;
imageDepthOut.at<cv::Vec3b>(v1, u1)[1] = k1;
imageDepthOut.at<cv::Vec3b>(v1, u1)[2] = k1;
}
}
__global__ void pwrapingImage(double * pmatBj, double *devpts, pCalibStruct *devm_CalibParams, int ref, int proj, char *devimageColor, char *devimageDepth, char *devimageColorOut, char *devimageDepthOut, int x, int y, int cols, int rows)
{
int offset = threadIdx.x + blockIdx.x*1024;
int offset3 = offset * 3;
double d = devimageDepth[offset3 + 0];
double devpts1[24];
ppointProject_from_ref_to_otherView(devm_CalibParams, devpts1, ref, threadIdx.x, blockIdx.x, d);
int u1 = (int)devpts1[proj * 3 + 0];
int v1 = (int)devpts1[proj * 3 + 1];
int k1 = (int)devpts1[proj * 3 + 2];
if (u1 < 0 || u1 >= cols - 1 || v1 < 0 || v1 >= rows - 1)
return;
if (k1 < devimageDepthOut[offset3])
return;
devimageColorOut[(v1*cols + u1) * 3 + 0] = devimageColor[offset3 + 0];
devimageColorOut[(v1*cols + u1) * 3 + 1] = devimageColor[offset3 + 1];
devimageColorOut[(v1*cols + u1) * 3 + 2] = devimageColor[offset3 + 2];
devimageDepthOut[(v1*cols + u1) * 3 + 0] = k1;
devimageDepthOut[(v1*cols + u1) * 3 + 1] = k1;
devimageDepthOut[(v1*cols + u1) * 3 + 2] = k1;
}
void wrapingImageGai1(int ref, int proj, cv::Mat &imageColor, cv::Mat &imageDepth, cv::Mat &imageColorOut, cv::Mat &imageDepthOut)
{
cv::Mat matBj;
matBj.create(imageColor.rows, imageColor.cols, CV_64FC3);
for (int v = 0; v < imageColor.rows; v++)
{
for (int u = 0; u < imageColor.cols; u++)
{
matBj.at<cv::Vec3d>(v, u)[0] = 0;
matBj.at<cv::Vec3d>(v, u)[1] = 0;
matBj.at<cv::Vec3d>(v, u)[2] = 0;
}
}
for (int v = 0; v < imageColor.rows; v++)
for (int u = 0; u < imageColor.cols; u++)
{
double d = imageDepth.at<cv::Vec3b>(v, u)[0];
pointProject_from_ref_to_otherView(pts, ref, u, v, d);
int u1 = (int)round_off(pts[proj][0]);
int v1 = (int)round_off(pts[proj][1]);
double xy = (u1 - pts[proj][0])*(u1 - pts[proj][0]) + (v1 - pts[proj][1])*(v1 - pts[proj][1]);
double z = pts[proj][2];
int k1 = (int)round_off(pts[proj][2]);
if (u1 < 0 || u1 >= imageColor.cols - 1 || v1 < 0 || v1 >= imageColor.rows - 1)
continue;
if (int(matBj.at<cv::Vec3d>(v1, u1)[2]) != 0 && z < matBj.at<cv::Vec3d>(v1, u1)[1])
continue;
if (z == matBj.at<cv::Vec3d>(v1, u1)[1] && int(matBj.at<cv::Vec3d>(v1, u1)[2]) != 0 && xy > matBj.at<cv::Vec3d>(v1, u1)[0])
continue;
imageColorOut.at<cv::Vec3b>(v1, u1) = imageColor.at<cv::Vec3b>(v, u);
imageDepthOut.at<cv::Vec3b>(v1, u1)[0] = k1;
imageDepthOut.at<cv::Vec3b>(v1, u1)[1] = k1;
imageDepthOut.at<cv::Vec3b>(v1, u1)[2] = k1;
matBj.at<cv::Vec3d>(v1, u1)[0] = xy;
matBj.at<cv::Vec3d>(v1, u1)[1] = z;
matBj.at<cv::Vec3d>(v1, u1)[2] = 1;
}
}
cv::Mat matBj;
cv::Mat matBj2;
double * pmatBj;
double *pmatBj2;
void initialmatBj(cv::Mat imageColor){
matBj.create(imageColor.rows, imageColor.cols, CV_64FC3);
matBj2.create(imageColor.rows, imageColor.cols, CV_64FC3);
int size = imageColor.rows*imageColor.cols*imageColor.channels();
cudaMalloc((void**)&pmatBj, sizeof(double)*size);
cudaMalloc((void**)&pmatBj2, sizeof(double)*size);
cudaMemset(pmatBj, 0, sizeof(double)* size);
cudaMemset(pmatBj2, 0, sizeof(double)* size);
for (int v = 0; v < imageColor.rows; v++)
{
for (int u = 0; u < imageColor.cols; u++)
{
matBj.at<cv::Vec3d>(v, u)[0] = 0;
matBj.at<cv::Vec3d>(v, u)[1] = 0;
matBj.at<cv::Vec3d>(v, u)[2] = 0;
matBj2.at<cv::Vec3d>(v, u)[0] = 0;
matBj2.at<cv::Vec3d>(v, u)[1] = 0;
matBj2.at<cv::Vec3d>(v, u)[2] = 0;
}
}
}
void wrapingImageGai3(int ref, int proj, cv::Mat &imageColor, cv::Mat &imageDepth, cv::Mat &imageColorOut, cv::Mat &imageDepthOut, int x, int y)
{
for (int v = 0; v < imageColor.rows; v++)
for (int u = 0; u < imageColor.cols; u++)
{
if (imageDepth.at<cv::Vec3b>(v, u)[0] >= x&&imageDepth.at<cv::Vec3b>(v, u)[0] <= y)
{
double d = imageDepth.at<cv::Vec3b>(v, u)[0];
pointProject_from_ref_to_otherView(pts, ref, u, v, d);
int u1 = (int)round_off(pts[proj][0]);
int v1 = (int)round_off(pts[proj][1]);
double xy = (u1 - pts[proj][0])*(u1 - pts[proj][0]) + (v1 - pts[proj][1])*(v1 - pts[proj][1]);
double z = pts[proj][2];
int k1 = (int)round_off(pts[proj][2]);
if (u1 < 0 || u1 >= imageColor.cols - 1 || v1 < 0 || v1 >= imageColor.rows - 1)
continue;
if (int(matBj.at<cv::Vec3d>(v1, u1)[2]) != 0 && z < matBj.at<cv::Vec3d>(v1, u1)[1])
continue;
if (z == matBj.at<cv::Vec3d>(v1, u1)[1] && int(matBj.at<cv::Vec3d>(v1, u1)[2]) != 0 && xy > matBj.at<cv::Vec3d>(v1, u1)[0])
continue;
imageColorOut.at<cv::Vec3b>(v1, u1) = imageColor.at<cv::Vec3b>(v, u);
imageDepthOut.at<cv::Vec3b>(v1, u1)[0] = k1;
imageDepthOut.at<cv::Vec3b>(v1, u1)[1] = k1;
imageDepthOut.at<cv::Vec3b>(v1, u1)[2] = k1;
matBj.at<cv::Vec3d>(v1, u1)[0] = xy;
matBj.at<cv::Vec3d>(v1, u1)[1] = z;
matBj.at<cv::Vec3d>(v1, u1)[2] = 1;
}
else {
continue;
}
}
}
__global__ void pwrapingImageGai3(double * pmatBj, double *devpts, pCalibStruct *devm_CalibParams, int ref, int proj, char *devimageColor, char *devimageDepth, char *devimageColorOut, char *devimageDepthOut, int x, int y, int cols, int rows)
{
int u = threadIdx.x + blockIdx.x*blockDim.x;
int v = threadIdx.y + blockIdx.y*blockDim.y;
int offset = u + v*blockDim.x*gridDim.x;
int offset3 = offset * 3;
double devpts1[24];
if (devimageDepth[offset3 + 0] >= x&&devimageDepth[offset3 + 0] <= y)
{
double d = devimageDepth[offset3 + 0];
ppointProject_from_ref_to_otherView(devm_CalibParams, devpts1, ref, u, v,d);
int u1 = (int)pround_off(devpts1[proj * 3]);
int v1 = (int)pround_off(devpts1[proj * 3 + 1]);
double xy = (u1 - devpts1[proj * 3])*(u1 - devpts1[proj * 3]) + (v1 - devpts1[proj * 3 + 1])*(v1 - devpts1[proj * 3 + 1]);
double z = devpts1[proj * 3 + 2];
int k1 = (int)pround_off(devpts1[proj * 3 + 2]);
if (u1 < 0 || u1 >= cols - 1 || v1 < 0 || v1 >= rows - 1)
return;
if (int(pmatBj[(v1*cols + u1) * 3 + 2]) != 0 && z < pmatBj[(v1*cols + u1) * 3 + 1])
return;
if (z == pmatBj[(v1*cols + u1) * 3 + 1] && int(pmatBj[(v1*cols + u1) * 3 + 2]) != 0 && xy > pmatBj[(v1*cols + u1) * 3 + 0])
return;
devimageColorOut[(v1*cols + u1) * 3 + 0] = devimageColor[offset3 + 0];
devimageColorOut[(v1*cols + u1) * 3 + 1] = devimageColor[offset3 + 1];
devimageColorOut[(v1*cols + u1) * 3 + 2] = devimageColor[offset3 + 2];
devimageDepthOut[(v1*cols + u1) * 3 + 0] = k1;
devimageDepthOut[(v1*cols + u1) * 3 + 1] = k1;
devimageDepthOut[(v1*cols + u1) * 3 + 2] = k1;
pmatBj[(v1*cols + u1) * 3 + 0] = xy;
pmatBj[(v1*cols + u1) * 3 + 1] = z;
pmatBj[(v1*cols + u1) * 3 + 2] = 1;
}
else {
return;
}
}
void wrapingImageGai4(int ref, int proj, cv::Mat &imageColor, cv::Mat &imageDepth, cv::Mat &imageColorOut, cv::Mat &imageDepthOut, int x, int y)
{
for (int v = 0; v < imageColor.rows; v++)
for (int u = 0; u < imageColor.cols; u++)
{
if (imageDepth.at<cv::Vec3b>(v, u)[0] >= x&&imageDepth.at<cv::Vec3b>(v, u)[0] <= y)
{
double d = imageDepth.at<cv::Vec3b>(v, u)[0];
pointProject_from_ref_to_otherView(pts, ref, u, v, d);
int u1 = (int)round_off(pts[proj][0]);
int v1 = (int)round_off(pts[proj][1]);
double xy = (u1 - pts[proj][0])*(u1 - pts[proj][0]) + (v1 - pts[proj][1])*(v1 - pts[proj][1]);
double z = pts[proj][2];
int k1 = (int)round_off(pts[proj][2]);
if (u1 < 0 || u1 >= imageColor.cols - 1 || v1 < 0 || v1 >= imageColor.rows - 1)
continue;
if (int(matBj2.at<cv::Vec3d>(v1, u1)[2]) != 0 && z < matBj2.at<cv::Vec3d>(v1, u1)[1])
continue;
if (z == matBj2.at<cv::Vec3d>(v1, u1)[1] && int(matBj2.at<cv::Vec3d>(v1, u1)[2]) != 0 && xy > matBj2.at<cv::Vec3d>(v1, u1)[0])
continue;
imageColorOut.at<cv::Vec3b>(v1, u1) = imageColor.at<cv::Vec3b>(v, u);
imageDepthOut.at<cv::Vec3b>(v1, u1)[0] = k1;
imageDepthOut.at<cv::Vec3b>(v1, u1)[1] = k1;
imageDepthOut.at<cv::Vec3b>(v1, u1)[2] = k1;
matBj2.at<cv::Vec3d>(v1, u1)[0] = xy;
matBj2.at<cv::Vec3d>(v1, u1)[1] = z;
matBj2.at<cv::Vec3d>(v1, u1)[2] = 1;
}
else {
continue;
}
}
}
void pwrapingImageGai4(int ref, int proj, cv::Mat &imageColor, cv::Mat &imageDepth, cv::Mat &imageColorOut, cv::Mat &imageDepthOut, int x, int y)
{
for (int v = 0; v < imageColor.rows; v++)
for (int u = 0; u < imageColor.cols; u++)
{
if (imageDepth.at<cv::Vec3b>(v, u)[0] >= x&&imageDepth.at<cv::Vec3b>(v, u)[0] <= y)
{
double d = imageDepth.at<cv::Vec3b>(v, u)[0];
pointProject_from_ref_to_otherView(pts, ref, u, v, d);
int u1 = (int)round_off(pts[proj][0]);
int v1 = (int)round_off(pts[proj][1]);
double xy = (u1 - pts[proj][0])*(u1 - pts[proj][0]) + (v1 - pts[proj][1])*(v1 - pts[proj][1]);
double z = pts[proj][2];
int k1 = (int)round_off(pts[proj][2]);
if (u1 < 0 || u1 >= imageColor.cols - 1 || v1 < 0 || v1 >= imageColor.rows - 1)
continue;
if (int(matBj2.at<cv::Vec3d>(v1, u1)[2]) != 0 && z < matBj2.at<cv::Vec3d>(v1, u1)[1])
continue;
if (z == matBj2.at<cv::Vec3d>(v1, u1)[1] && int(matBj2.at<cv::Vec3d>(v1, u1)[2]) != 0 && xy > matBj2.at<cv::Vec3d>(v1, u1)[0])
continue;
imageColorOut.at<cv::Vec3b>(v1, u1) = imageColor.at<cv::Vec3b>(v, u);
imageDepthOut.at<cv::Vec3b>(v1, u1)[0] = k1;
imageDepthOut.at<cv::Vec3b>(v1, u1)[1] = k1;
imageDepthOut.at<cv::Vec3b>(v1, u1)[2] = k1;
matBj2.at<cv::Vec3d>(v1, u1)[0] = xy;
matBj2.at<cv::Vec3d>(v1, u1)[1] = z;
matBj2.at<cv::Vec3d>(v1, u1)[2] = 1;
}
else {
continue;
}
}
}
void wrapingImage_inverse(int ref, int proj, cv::Mat &imageColor, cv::Mat &imageDepth, cv::Mat &imageColorOut, cv::Mat &imageDepthOut)
{
for (int v = 0; v < imageColor.rows; v++)
for (int u = 0; u < imageColor.cols; u++)
{
double d = imageDepthOut.at<cv::Vec3b>(v, u)[0];
if (d == 0)
continue;
if (imageColorOut.at<cv::Vec3b>(v, u)[0] != 0 && imageColorOut.at<cv::Vec3b>(v, u)[1] != 0 && imageColorOut.at<cv::Vec3b>(v, u)[2] != 0)
continue;
pointProject_from_ref_to_otherView(pts, proj, u, v, d);
int u1 = (int)pts[ref][0];
int v1 = (int)pts[ref][1];
int k1 = (int)pts[ref][2];
if (u1 < 0 || u1 >= imageColor.cols - 1 || v1 < 0 || v1 >= imageColor.rows - 1)
continue;
imageColorOut.at<cv::Vec3b>(v, u) = imageColor.at<cv::Vec3b>(v1, u1);
}
}
int MedianValue(cv::Mat imageColor, int tw, int x, int y, int cn){
int i, j, k;
int px, py, c;
int *value;
value = new int[tw*tw];
k = 0;
for (i = 0; i < tw; i++)
{
for (j = 0; j < tw; j++)
{
py = y - tw / 2 + i;
px = x - tw / 2 + j;
value[k++] = imageColor.at<cv::Vec3b>(px, py)[cn];
}
}
int count;
count = k;
for (i = 0; i < count - 1; i++)
{
k = i;
for (j = i + 1; j < count; j++)
{
if (value[j] < value[k])
{
k = j;
}
}
c = value[i];
value[i] = value[k];
value[k] = c;
}
c = value[count / 2];
delete[] value;
return c;
}
void fillMedian(cv::Mat &imageColor, int tw)
{
cv::Mat imageColor2;
imageColor2.create(imageColor.rows, imageColor.cols, imageColor.type());
for (int v = 0; v < imageColor.rows; v++)
{
for (int u = 0; u < imageColor.cols; u++)
{
imageColor2.at<cv::Vec3b>(v, u) = imageColor.at<cv::Vec3b>(v, u);
}
}
for (int v = 0 + tw / 2; v < imageColor.rows - tw / 2; v++)
{
for (int u = 0 + tw / 2; u < imageColor.cols - tw / 2; u++)
{
int k = 0;
if (imageColor2.at<cv::Vec3b>(v, u)[0] != 0 || imageColor2.at<cv::Vec3b>(v, u)[1] != 0 || imageColor2.at<cv::Vec3b>(v, u)[2] != 0)
{
continue;
}
for (int i = 0; i < tw; i++)
{
for (int j = 0; j < tw; j++)
{
int py = u - tw / 2 + i;
int px = v - tw / 2 + j;
if (imageColor2.at<cv::Vec3b>(px, py)[0] != 0 && imageColor2.at<cv::Vec3b>(px, py)[1] != 0 && imageColor2.at<cv::Vec3b>(px, py)[2] != 0)
{
k++;
}
}
}
if (k > tw*tw / 2)
{
for (int cn = 0; cn < 3; cn++)
{
imageColor.at<cv::Vec3b>(v, u)[cn] = MedianValue(imageColor2, 3, v, u, cn);
}
}
}
}
}
void dealXjlk(cv::Mat &imageColor)
{
cv::Mat imageColor2;
imageColor2.create(imageColor.rows, imageColor.cols, imageColor.type());
for (int v = 0; v < imageColor.rows; v++)
{
for (int u = 0; u < imageColor.cols; u++)
{
imageColor2.at<cv::Vec3b>(v, u) = imageColor.at<cv::Vec3b>(v, u);
}
}
for (int v = 8; v < imageColor2.rows - 8; v++)
{
for (int u = 8; u < imageColor2.cols - 8; u++)
{
if (imageColor2.at<cv::Vec3b>(v, u)[2] != 0 && imageColor2.at<cv::Vec3b>(v, u)[1] != 0 && imageColor2.at<cv::Vec3b>(v, u)[0] != 0)
{
/*if (imageColor.at<cv::Vec3b>(v, u + 1)[2] != 0 || imageColor.at<cv::Vec3b>(v, u + 1)[1] != 0 || imageColor.at<cv::Vec3b>(v, u + 1)[0] != 0 || imageColor.at<cv::Vec3b>(v, u + 2)[2] != 0 || imageColor.at<cv::Vec3b>(v, u + 2)[1] != 0 || imageColor.at<cv::Vec3b>(v, u + 2)[0] != 0 || imageColor.at<cv::Vec3b>(v, u + 3)[2] != 0 || imageColor.at<cv::Vec3b>(v, u + 3)[1] != 0 || imageColor.at<cv::Vec3b>(v, u + 3)[0] != 0 || imageColor.at<cv::Vec3b>(v, u + 4)[2] != 0 || imageColor.at<cv::Vec3b>(v, u + 4)[1] != 0 || imageColor.at<cv::Vec3b>(v, u + 4)[0] != 0 || imageColor.at<cv::Vec3b>(v, u + 5)[2] != 0 || imageColor.at<cv::Vec3b>(v, u + 5)[1] != 0 || imageColor.at<cv::Vec3b>(v, u + 5)[0] != 0 || imageColor.at<cv::Vec3b>(v, u + 6)[2] != 0 || imageColor.at<cv::Vec3b>(v, u + 6)[1] != 0 || imageColor.at<cv::Vec3b>(v, u + 6)[0] != 0 || imageColor.at<cv::Vec3b>(v, u + 7)[2] != 0 || imageColor.at<cv::Vec3b>(v, u + 7)[1] != 0 || imageColor.at<cv::Vec3b>(v, u + 7)[0] != 0 || imageColor.at<cv::Vec3b>(v, u + 8)[2] != 0 || imageColor.at<cv::Vec3b>(v, u + 8)[1] != 0 || imageColor.at<cv::Vec3b>(v, u + 8)[0] != 0)
{
continue;
}
if (imageColor.at<cv::Vec3b>(v, u - 1)[2] != 0 || imageColor.at<cv::Vec3b>(v, u - 1)[1] != 0 || imageColor.at<cv::Vec3b>(v, u - 1)[0] != 0 || imageColor.at<cv::Vec3b>(v, u - 2)[2] != 0 || imageColor.at<cv::Vec3b>(v, u - 2)[1] != 0 || imageColor.at<cv::Vec3b>(v, u - 2)[0] != 0 || imageColor.at<cv::Vec3b>(v, u - 3)[2] != 0 || imageColor.at<cv::Vec3b>(v, u - 3)[1] != 0 || imageColor.at<cv::Vec3b>(v, u - 3)[0] != 0 || imageColor.at<cv::Vec3b>(v, u - 4)[2] != 0 || imageColor.at<cv::Vec3b>(v, u - 4)[1] != 0 || imageColor.at<cv::Vec3b>(v, u - 4)[0] != 0 || imageColor.at<cv::Vec3b>(v, u - 5)[2] != 0 || imageColor.at<cv::Vec3b>(v, u - 5)[1] != 0 || imageColor.at<cv::Vec3b>(v, u - 5)[0] != 0 || imageColor.at<cv::Vec3b>(v, u - 6)[2] != 0 || imageColor.at<cv::Vec3b>(v, u - 6)[1] != 0 || imageColor.at<cv::Vec3b>(v, u - 6)[0] != 0 || imageColor.at<cv::Vec3b>(v, u - 7)[2] != 0 || imageColor.at<cv::Vec3b>(v, u - 7)[1] != 0 || imageColor.at<cv::Vec3b>(v, u - 7)[0] != 0 || imageColor.at<cv::Vec3b>(v, u - 8)[2] != 0 || imageColor.at<cv::Vec3b>(v, u - 8)[1] != 0 || imageColor.at<cv::Vec3b>(v, u - 8)[0] != 0)
{
continue;
}
if (imageColor.at<cv::Vec3b>(v + 1, u)[2] != 0 || imageColor.at<cv::Vec3b>(v + 1, u)[1] != 0 || imageColor.at<cv::Vec3b>(v + 1, u)[0] != 0 || imageColor.at<cv::Vec3b>(v + 2, u)[2] != 0 || imageColor.at<cv::Vec3b>(v + 2, u)[1] != 0 || imageColor.at<cv::Vec3b>(v + 2, u)[0] != 0 || imageColor.at<cv::Vec3b>(v + 3, u)[2] != 0 || imageColor.at<cv::Vec3b>(v + 3, u)[1] != 0 || imageColor.at<cv::Vec3b>(v + 3, u)[0] != 0 || imageColor.at<cv::Vec3b>(v + 4, u)[2] != 0 || imageColor.at<cv::Vec3b>(v + 4, u)[1] != 0 || imageColor.at<cv::Vec3b>(v + 4, u)[0] != 0 || imageColor.at<cv::Vec3b>(v + 5, u)[2] != 0 || imageColor.at<cv::Vec3b>(v + 5, u)[1] != 0 || imageColor.at<cv::Vec3b>(v + 5, u)[0] != 0 || imageColor.at<cv::Vec3b>(v + 6, u)[2] != 0 || imageColor.at<cv::Vec3b>(v + 6, u)[1] != 0 || imageColor.at<cv::Vec3b>(v + 6, u)[0] != 0 || imageColor.at<cv::Vec3b>(v + 7, u)[2] != 0 || imageColor.at<cv::Vec3b>(v + 7, u)[1] != 0 || imageColor.at<cv::Vec3b>(v + 7, u)[0] != 0 || imageColor.at<cv::Vec3b>(v + 8, u)[2] != 0 || imageColor.at<cv::Vec3b>(v + 8, u)[1] != 0 || imageColor.at<cv::Vec3b>(v + 8, u)[0] != 0)
{
continue;
}
if (imageColor.at<cv::Vec3b>(v - 1, u)[2] != 0 || imageColor.at<cv::Vec3b>(v - 1, u)[1] != 0 || imageColor.at<cv::Vec3b>(v - 1, u)[0] != 0 || imageColor.at<cv::Vec3b>(v - 2, u)[2] != 0 || imageColor.at<cv::Vec3b>(v - 2, u)[1] != 0 || imageColor.at<cv::Vec3b>(v - 2, u)[0] != 0 || imageColor.at<cv::Vec3b>(v - 3, u)[2] != 0 || imageColor.at<cv::Vec3b>(v - 3, u)[1] != 0 || imageColor.at<cv::Vec3b>(v - 3, u)[0] != 0 || imageColor.at<cv::Vec3b>(v - 4, u)[2] != 0 || imageColor.at<cv::Vec3b>(v - 4, u)[1] != 0 || imageColor.at<cv::Vec3b>(v - 4, u)[0] != 0 || imageColor.at<cv::Vec3b>(v - 5, u)[2] != 0 || imageColor.at<cv::Vec3b>(v - 5, u)[1] != 0 || imageColor.at<cv::Vec3b>(v - 5, u)[0] != 0 || imageColor.at<cv::Vec3b>(v - 6, u)[2] != 0 || imageColor.at<cv::Vec3b>(v - 6, u)[1] != 0 || imageColor.at<cv::Vec3b>(v - 6, u)[0] != 0 || imageColor.at<cv::Vec3b>(v - 7, u)[2] != 0 || imageColor.at<cv::Vec3b>(v - 7, u)[1] != 0 || imageColor.at<cv::Vec3b>(v - 7, u)[0] != 0 || imageColor.at<cv::Vec3b>(v - 8, u)[2] != 0 || imageColor.at<cv::Vec3b>(v - 8, u)[1] != 0 || imageColor.at<cv::Vec3b>(v - 8, u)[0] != 0)
{
continue;
}*/
//第一种情况,左上,左
if (imageColor2.at<cv::Vec3b>(v, u + 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u + 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u + 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u + 2)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u + 2)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u + 2)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u + 3)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u + 3)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u + 3)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u + 4)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u + 4)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u + 4)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u + 5)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u + 5)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u + 5)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u + 6)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u + 6)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u + 6)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u + 7)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u + 7)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u + 7)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u + 8)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u + 8)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u + 8)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 2)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 2)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 2)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 3)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 3)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 3)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 4)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 4)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 4)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 5)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 5)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 5)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 6)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 6)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 6)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 7)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 7)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 7)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 8)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 8)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 8)[0] == 0)
{
imageColor.at<cv::Vec3b>(v, u)[0] = 0;
imageColor.at<cv::Vec3b>(v, u)[1] = 0;
imageColor.at<cv::Vec3b>(v, u)[2] = 0;
continue;
}
//第二种情况,左下
if (imageColor2.at<cv::Vec3b>(v, u + 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u + 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u + 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u + 2)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u + 2)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u + 2)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u + 3)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u + 3)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u + 3)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u + 4)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u + 4)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u + 4)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u + 5)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u + 5)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u + 5)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u + 6)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u + 6)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u + 6)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u + 7)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u + 7)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u + 7)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u + 8)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u + 8)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u + 8)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 2)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 2)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 2)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 3)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 3)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 3)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 4)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 4)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 4)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 5)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 5)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 5)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 6)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 6)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 6)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 7)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 7)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 7)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 8)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 8)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 8)[0] == 0)
{
imageColor.at<cv::Vec3b>(v, u)[0] = 0;
imageColor.at<cv::Vec3b>(v, u)[1] = 0;
imageColor.at<cv::Vec3b>(v, u)[2] = 0;
continue;
}
//第三种情况,右上,右
if (imageColor2.at<cv::Vec3b>(v, u - 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u - 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u - 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u - 2)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u - 2)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u - 2)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u - 3)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u - 3)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u - 3)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u - 4)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u - 4)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u - 4)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u - 5)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u - 5)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u - 5)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u - 6)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u - 6)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u - 6)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u - 7)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u - 7)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u - 7)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u - 8)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u - 8)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u - 8)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 2)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 2)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 2)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 3)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 3)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 3)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 4)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 4)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 4)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 5)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 5)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 5)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 6)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 6)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 6)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 7)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 7)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 7)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 8)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 8)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 8)[0] == 0)
{
imageColor.at<cv::Vec3b>(v, u)[0] = 0;
imageColor.at<cv::Vec3b>(v, u)[1] = 0;
imageColor.at<cv::Vec3b>(v, u)[2] = 0;
continue;
}
//第四种情况,右下
if (imageColor2.at<cv::Vec3b>(v, u - 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u - 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u - 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u - 2)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u - 2)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u - 2)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u - 3)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u - 3)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u - 3)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u - 4)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u - 4)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u - 4)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u - 5)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u - 5)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u - 5)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u - 6)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u - 6)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u - 6)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u - 7)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u - 7)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u - 7)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u - 8)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u - 8)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u - 8)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 2)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 2)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 2)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 3)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 3)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 3)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 4)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 4)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 4)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 5)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 5)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 5)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 6)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 6)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 6)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 7)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 7)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 7)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 8)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 8)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 8)[0] == 0)
{
imageColor.at<cv::Vec3b>(v, u)[0] = 0;
imageColor.at<cv::Vec3b>(v, u)[1] = 0;
imageColor.at<cv::Vec3b>(v, u)[2] = 0;
continue;
}
//第五种情况,上
if (imageColor2.at<cv::Vec3b>(v + 1, u)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 2, u)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 2, u)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 2, u)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 3, u)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 3, u)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 3, u)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 4, u)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 4, u)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 4, u)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 5, u)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 5, u)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 5, u)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 6, u)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 6, u)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 6, u)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 7, u)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 7, u)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 7, u)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 8, u)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 8, u)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 8, u)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 2, u + 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 2, u + 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 2, u + 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 3, u + 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 3, u + 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 3, u + 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 4, u + 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 4, u + 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 4, u + 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 5, u + 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 5, u + 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 5, u + 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 6, u + 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 6, u + 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 6, u + 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 7, u + 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 7, u + 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 7, u + 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 8, u + 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v + 8, u + 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 8, u + 1)[0] == 0)
{
imageColor.at<cv::Vec3b>(v, u)[0] = 0;
imageColor.at<cv::Vec3b>(v, u)[1] = 0;
imageColor.at<cv::Vec3b>(v, u)[2] = 0;
continue;
}
//第六种情况,下
if (imageColor2.at<cv::Vec3b>(v - 1, u)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 2, u)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 2, u)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 2, u)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 3, u)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 3, u)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 3, u)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 4, u)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 4, u)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 4, u)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 5, u)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 5, u)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 5, u)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 6, u)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 6, u)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 6, u)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 7, u)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 7, u)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 7, u)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 8, u)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 8, u)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 8, u)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 2, u + 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 2, u + 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 2, u + 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 3, u + 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 3, u + 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 3, u + 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 4, u + 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 4, u + 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 4, u + 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 5, u + 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 5, u + 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 5, u + 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 6, u + 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 6, u + 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 6, u + 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 7, u + 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 7, u + 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 7, u + 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v - 8, u + 1)[2] == 0 && imageColor2.at<cv::Vec3b>(v - 8, u + 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v - 8, u + 1)[0] == 0)
{
imageColor.at<cv::Vec3b>(v, u)[0] = 0;
imageColor.at<cv::Vec3b>(v, u)[1] = 0;
imageColor.at<cv::Vec3b>(v, u)[2] = 0;
}
}
}
}
}
int tt = 1;
void dilate(cv::Mat &imageColor)
{
cv::Mat imageColor2;
imageColor2.create(imageColor.rows, imageColor.cols, imageColor.type());
for (int v = 0 + tt; v < imageColor.rows - tt; v++)
{
for (int u = 0 + tt; u < imageColor.cols - tt; u++)
{
imageColor2.at<cv::Vec3b>(v, u) = imageColor.at<cv::Vec3b>(v, u);
}
}
for (int v = 0 + tt; v < imageColor2.rows - tt; v++)
{
for (int u = 0 + tt; u < imageColor2.cols - tt; u++)
{
if (imageColor2.at<cv::Vec3b>(v, u)[2] != 0 && imageColor2.at<cv::Vec3b>(v, u)[1] != 0 && imageColor2.at<cv::Vec3b>(v, u)[0] != 0)
{
if (imageColor2.at<cv::Vec3b>(v + 1, u)[0] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u)[1] == 0 && imageColor2.at<cv::Vec3b>(v + 1, u)[2] == 0)
{
imageColor.at<cv::Vec3b>(v + 1, u)[0] = imageColor2.at<cv::Vec3b>(v, u)[0];
imageColor.at<cv::Vec3b>(v + 1, u)[1] = imageColor2.at<cv::Vec3b>(v, u)[1];
imageColor.at<cv::Vec3b>(v + 1, u)[2] = imageColor2.at<cv::Vec3b>(v, u)[2];
}
if (imageColor2.at<cv::Vec3b>(v, u + 1)[0] == 0 && imageColor2.at<cv::Vec3b>(v, u + 1)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u + 1)[2] == 0)
{
imageColor.at<cv::Vec3b>(v, u + 1)[0] = imageColor2.at<cv::Vec3b>(v, u)[0];
imageColor.at<cv::Vec3b>(v, u + 1)[1] = imageColor2.at<cv::Vec3b>(v, u)[1];
imageColor.at<cv::Vec3b>(v, u + 1)[2] = imageColor2.at<cv::Vec3b>(v, u)[2];
}
if (imageColor2.at<cv::Vec3b>(v - 1, u)[0] != 0 && imageColor2.at<cv::Vec3b>(v - 1, u)[1] != 0 && imageColor2.at<cv::Vec3b>(v - 1, u)[2] != 0)
{
imageColor.at<cv::Vec3b>(v - 1, u)[0] = imageColor2.at<cv::Vec3b>(v, u)[0];
imageColor.at<cv::Vec3b>(v - 1, u)[1] = imageColor2.at<cv::Vec3b>(v, u)[1];
imageColor.at<cv::Vec3b>(v - 1, u)[2] = imageColor2.at<cv::Vec3b>(v, u)[2];
}
if (imageColor2.at<cv::Vec3b>(v, u - 1)[0] != 0 && imageColor2.at<cv::Vec3b>(v, u - 1)[1] != 0 && imageColor2.at<cv::Vec3b>(v, u - 1)[2] != 0)
{
imageColor.at<cv::Vec3b>(v, u - 1)[0] = imageColor2.at<cv::Vec3b>(v, u)[0];
imageColor.at<cv::Vec3b>(v, u - 1)[1] = imageColor2.at<cv::Vec3b>(v, u)[1];
imageColor.at<cv::Vec3b>(v, u - 1)[2] = imageColor2.at<cv::Vec3b>(v, u)[2];
}
if (imageColor2.at<cv::Vec3b>(v + 1, u + 1)[0] != 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 1)[1] != 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 1)[2] != 0)
{
imageColor.at<cv::Vec3b>(v + 1, u + 1)[0] = imageColor2.at<cv::Vec3b>(v, u)[0];
imageColor.at<cv::Vec3b>(v + 1, u + 1)[1] = imageColor2.at<cv::Vec3b>(v, u)[1];
imageColor.at<cv::Vec3b>(v + 1, u + 1)[2] = imageColor2.at<cv::Vec3b>(v, u)[2];
}
if (imageColor2.at<cv::Vec3b>(v - 1, u + 1)[0] != 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 1)[1] != 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 1)[2] != 0)
{
imageColor.at<cv::Vec3b>(v - 1, u + 1)[0] = imageColor2.at<cv::Vec3b>(v, u)[0];
imageColor.at<cv::Vec3b>(v - 1, u + 1)[1] = imageColor2.at<cv::Vec3b>(v, u)[1];
imageColor.at<cv::Vec3b>(v - 1, u + 1)[2] = imageColor2.at<cv::Vec3b>(v, u)[2];
}
if (imageColor2.at<cv::Vec3b>(v - 1, u - 1)[0] != 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 1)[1] != 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 1)[2] != 0)
{
imageColor.at<cv::Vec3b>(v - 1, u - 1)[0] = imageColor2.at<cv::Vec3b>(v, u)[0];
imageColor.at<cv::Vec3b>(v - 1, u - 1)[1] = imageColor2.at<cv::Vec3b>(v, u)[1];
imageColor.at<cv::Vec3b>(v - 1, u - 1)[2] = imageColor2.at<cv::Vec3b>(v, u)[2];
}
if (imageColor2.at<cv::Vec3b>(v + 1, u - 1)[0] != 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 1)[1] != 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 1)[2] != 0)
{
imageColor.at<cv::Vec3b>(v + 1, u - 1)[0] = imageColor2.at<cv::Vec3b>(v, u)[0];
imageColor.at<cv::Vec3b>(v + 1, u - 1)[1] = imageColor2.at<cv::Vec3b>(v, u)[1];
imageColor.at<cv::Vec3b>(v + 1, u - 1)[2] = imageColor2.at<cv::Vec3b>(v, u)[2];
}
}
}
}
}
void erode(cv::Mat &imageColor)
{
cv::Mat imageColor2;
imageColor2.create(imageColor.rows, imageColor.cols, imageColor.type());
for (int v = 0 + tt; v < imageColor.rows - tt; v++)
{
for (int u = 0 + tt; u < imageColor.cols - tt; u++)
{
imageColor2.at<cv::Vec3b>(v, u) = imageColor.at<cv::Vec3b>(v, u);
}
}
for (int v = 0 + tt; v < imageColor2.rows - tt; v++)
{
for (int u = 0 + tt; u < imageColor2.cols - tt; u++)
{
if (imageColor2.at<cv::Vec3b>(v, u)[2] == 0 && imageColor2.at<cv::Vec3b>(v, u)[1] == 0 && imageColor2.at<cv::Vec3b>(v, u)[0] == 0)
{
if (imageColor2.at<cv::Vec3b>(v + 1, u)[0] != 0 && imageColor2.at<cv::Vec3b>(v + 1, u)[1] != 0 && imageColor2.at<cv::Vec3b>(v + 1, u)[2] != 0)
{
imageColor.at<cv::Vec3b>(v + 1, u)[0] = 0;
imageColor.at<cv::Vec3b>(v + 1, u)[1] = 0;
imageColor.at<cv::Vec3b>(v + 1, u)[2] = 0;
}
if (imageColor2.at<cv::Vec3b>(v, u + 1)[0] != 0 && imageColor2.at<cv::Vec3b>(v, u + 1)[1] != 0 && imageColor2.at<cv::Vec3b>(v, u + 1)[2] != 0)
{
imageColor.at<cv::Vec3b>(v, u + 1)[0] = 0;
imageColor.at<cv::Vec3b>(v, u + 1)[1] = 0;
imageColor.at<cv::Vec3b>(v, u + 1)[2] = 0;
}
if (imageColor2.at<cv::Vec3b>(v - 1, u)[0] != 0 && imageColor2.at<cv::Vec3b>(v - 1, u)[1] != 0 && imageColor2.at<cv::Vec3b>(v - 1, u)[2] != 0)
{
imageColor.at<cv::Vec3b>(v - 1, u)[0] = 0;
imageColor.at<cv::Vec3b>(v - 1, u)[1] = 0;
imageColor.at<cv::Vec3b>(v - 1, u)[2] = 0;
}
if (imageColor2.at<cv::Vec3b>(v, u - 1)[0] != 0 && imageColor2.at<cv::Vec3b>(v, u - 1)[1] != 0 && imageColor2.at<cv::Vec3b>(v, u - 1)[2] != 0)
{
imageColor.at<cv::Vec3b>(v, u - 1)[0] = 0;
imageColor.at<cv::Vec3b>(v, u - 1)[1] = 0;
imageColor.at<cv::Vec3b>(v, u - 1)[2] = 0;
}
if (imageColor2.at<cv::Vec3b>(v + 1, u + 1)[0] != 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 1)[1] != 0 && imageColor2.at<cv::Vec3b>(v + 1, u + 1)[2] != 0)
{
imageColor.at<cv::Vec3b>(v + 1, u + 1)[0] = 0;
imageColor.at<cv::Vec3b>(v + 1, u + 1)[1] = 0;
imageColor.at<cv::Vec3b>(v + 1, u + 1)[2] = 0;
}
if (imageColor2.at<cv::Vec3b>(v - 1, u + 1)[0] != 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 1)[1] != 0 && imageColor2.at<cv::Vec3b>(v - 1, u + 1)[2] != 0)
{
imageColor.at<cv::Vec3b>(v - 1, u + 1)[0] = 0;
imageColor.at<cv::Vec3b>(v - 1, u + 1)[1] = 0;
imageColor.at<cv::Vec3b>(v - 1, u + 1)[2] = 0;
}
if (imageColor2.at<cv::Vec3b>(v - 1, u - 1)[0] != 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 1)[1] != 0 && imageColor2.at<cv::Vec3b>(v - 1, u - 1)[2] != 0)
{
imageColor.at<cv::Vec3b>(v - 1, u - 1)[0] = 0;
imageColor.at<cv::Vec3b>(v - 1, u - 1)[1] = 0;
imageColor.at<cv::Vec3b>(v - 1, u - 1)[2] = 0;
}
if (imageColor2.at<cv::Vec3b>(v + 1, u - 1)[0] != 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 1)[1] != 0 && imageColor2.at<cv::Vec3b>(v + 1, u - 1)[2] != 0)
{
imageColor.at<cv::Vec3b>(v + 1, u - 1)[0] = 0;
imageColor.at<cv::Vec3b>(v + 1, u - 1)[1] = 0;
imageColor.at<cv::Vec3b>(v + 1, u - 1)[2] = 0;
}
}
}
}
}
void dipslay(char *calibParam, char *refColor, char *refDepth, char *refColor2, char *refDepth2, char *actColor, int ref = 5, int ref2 = 7, int proj = 6)
{
//initialize projection_Matrix
InitializeFromFile(calibParam);
//display projection_Matrix
//for (int i = 0; i < m_NumCameras; i++){
// for (int j = 0; j < 3; j++){
// for (int k = 0; k < 3; k++)
// std::cout << m_CalibParams[i].m_K[j][k] << "\t";
// std::cout << std::endl;
// }
// for (int j = 0; j < 3; j++){
// for (int k = 0; k < 3; k++)
// std::cout << m_CalibParams[i].m_RotMatrix[j][k] << "\t";
// std::cout << std::endl;
// }
// for (int k = 0; k < 3; k++)
// std::cout << m_CalibParams[i].m_Trans[k] << "\t";
// std::cout << std::endl;
// std::cout << std::endl;
// std::cout << std::endl;
// std::cout << std::endl;
//}
suspend and users enter a digit
//int aa;
//std::cin >> aa;
//read color image and depth image of refrence
cudaMalloc((void**)&devm_CalibParams, 8 * sizeof(pCalibStruct));
cudaMemcpy(devm_CalibParams, m_CalibParams, 8 * sizeof(pCalibStruct), cudaMemcpyHostToDevice);
cv::Mat imageColor = cv::imread(refColor);
cv::Mat imageDepth = cv::imread(refDepth);
cv::Mat imageColor2 = cv::imread(refColor2);
cv::Mat imageDepth2 = cv::imread(refDepth2);
//read true image used to compare
cv::Mat imageColor_actual = cv::imread(actColor);
//set reference cam
int size = imageColor.rows*imageColor.cols*imageColor.channels();
char *pimageColor = (char*)malloc(sizeof(char)*size);
char *pimageDepth = (char*)malloc(sizeof(char)*size);
char *pimageColor2 = (char*)malloc(sizeof(char)*size);
char *pimageDepth2 = (char*)malloc(sizeof(char)*size);
char *pimageColor_actual = (char*)malloc(sizeof(char)*size);
char *devimageColor;
char *devimageDepth;
char *devimageColor2;
char *devimageDepth2;
char *devimageColor_actual;
cudaMalloc((void**)&devimageColor, sizeof(char)*size);
cudaMalloc((void**)&devimageColor2, sizeof(char)*size);
cudaMalloc((void**)&devimageDepth, sizeof(char)*size);
cudaMalloc((void**)&devimageDepth2, sizeof(char)*size);
cudaMalloc((void**)&devimageColor_actual, sizeof(char)*size);
for (int v = 0; v < imageColor.rows; v++)
{
for (int u = 0; u < imageColor.cols; u++)
{
*pimageColor = imageColor.at<cv::Vec3b>(v, u)[0];
pimageColor++;
*pimageColor = imageColor.at<cv::Vec3b>(v, u)[1];
pimageColor++;
*pimageColor = imageColor.at<cv::Vec3b>(v, u)[2];
pimageColor++;
*pimageColor2 = imageColor2.at<cv::Vec3b>(v, u)[0];
pimageColor2++;
*pimageColor2 = imageColor2.at<cv::Vec3b>(v, u)[1];
pimageColor2++;
*pimageColor2 = imageColor2.at<cv::Vec3b>(v, u)[2];
pimageColor2++;
*pimageDepth = imageDepth.at<cv::Vec3b>(v, u)[0];
pimageDepth++;
*pimageDepth = imageDepth.at<cv::Vec3b>(v, u)[1];
pimageDepth++;
*pimageDepth = imageDepth.at<cv::Vec3b>(v, u)[2];
pimageDepth++;
*pimageDepth2 = imageDepth2.at<cv::Vec3b>(v, u)[0];
pimageDepth2++;
*pimageDepth2 = imageDepth2.at<cv::Vec3b>(v, u)[1];
pimageDepth2++;
*pimageDepth2 = imageDepth2.at<cv::Vec3b>(v, u)[2];
pimageDepth2++;
*pimageColor_actual = imageColor_actual.at<cv::Vec3b>(v, u)[0];
pimageColor_actual++;
*pimageColor_actual = imageColor_actual.at<cv::Vec3b>(v, u)[1];
pimageColor_actual++;
*pimageColor_actual = imageColor_actual.at<cv::Vec3b>(v, u)[2];
pimageColor_actual++;
}
}
pimageColor = pimageColor - sizeof(char)*size;
pimageColor2 = pimageColor2 - sizeof(char)*size;
pimageDepth = pimageDepth - sizeof(char)*size;
pimageDepth2 = pimageDepth2 - sizeof(char)*size;
pimageColor_actual = pimageColor_actual - sizeof(char)*size;
cudaMemcpy(devimageColor, pimageColor, sizeof(char)*size, cudaMemcpyHostToDevice);
cudaMemcpy(devimageColor2, pimageColor2, sizeof(char)*size, cudaMemcpyHostToDevice);
cudaMemcpy(devimageDepth, pimageDepth, sizeof(char)*size, cudaMemcpyHostToDevice);
cudaMemcpy(devimageDepth2, pimageDepth2, sizeof(char)*size, cudaMemcpyHostToDevice);
cudaMemcpy(devimageColor_actual, pimageColor_actual, sizeof(char)*size, cudaMemcpyHostToDevice);
//set projection cam
//test code
/*pointProject_from_ref_to_otherView(pts, ref, 700, 700, imageDepth.at<cv::Vec3b>(700, 700)[0]);
for (int i = 0; i < 8; i++)
{
std::cout << pts[i][0] << "\t" << pts[i][1] << std::endl;
}
std::cin >> aa;*/
//define two variable of output
cv::Mat imageColorOut;
cv::Mat imageColorOut2;
cv::Mat imageColorOut3;
cv::Mat imageDepthOut;
cv::Mat imageDepthOut2;
cv::Mat imageDepthOut3;//存储暂时深度图
cv::Mat imageColorOut4;//存储暂时彩色图
char *pimageColorOut;
char *pimageColorOut2;
char *pimageColorOut3;
char *pimageDepthOut;
char *pimageDepthOut2;
char *pimageDepthOut3;
char *pimageColorOut4;
char *devimageColorOut;
char *devimageColorOut2;
char *devimageColorOut3;
char *devimageDepthOut;
char *devimageDepthOut2;
char *devimageDepthOut3;
char *devimageColorOut4;
imageColorOut.create(imageColor.rows, imageColor.cols, imageColor.type());
imageColorOut2.create(imageColor.rows, imageColor.cols, imageColor.type());
imageColorOut3.create(imageColor.rows, imageColor.cols, imageColor.type());
imageDepthOut.create(imageColor.rows, imageColor.cols, imageColor.type());
imageDepthOut2.create(imageColor.rows, imageColor.cols, imageColor.type());
imageDepthOut3.create(imageColor.rows, imageColor.cols, imageColor.type());
imageColorOut4.create(imageColor.rows, imageColor.cols, imageColor.type());
pimageColorOut = (char*)malloc(sizeof(char)*size);
pimageColorOut2 = (char*)malloc(sizeof(char)*size);
pimageColorOut3 = (char*)malloc(sizeof(char)*size);
pimageDepthOut = (char*)malloc(sizeof(char)*size);
pimageDepthOut2 = (char*)malloc(sizeof(char)*size);
pimageDepthOut3 = (char*)malloc(sizeof(char)*size);
pimageColorOut4 = (char*)malloc(sizeof(char)*size);
cudaMalloc((void**)&devimageColorOut, sizeof(char)*size);
cudaMalloc((void**)&devimageColorOut2, sizeof(char)*size);
cudaMalloc((void**)&devimageColorOut3, sizeof(char)*size);
cudaMalloc((void**)&devimageDepthOut, sizeof(char)*size);
cudaMalloc((void**)&devimageDepthOut2, sizeof(char)*size);
cudaMalloc((void**)&devimageDepthOut3, sizeof(char)*size);
cudaMalloc((void**)&devimageColorOut4, sizeof(char)*size);
cudaMemset(devimageColorOut, 0, size*sizeof(char));
cudaMemset(devimageColorOut2, 0, size*sizeof(char));
cudaMemset(devimageColorOut3, 0, size*sizeof(char));
cudaMemset(devimageDepthOut, 0, size*sizeof(char));
cudaMemset(devimageDepthOut2, 0, size*sizeof(char));
cudaMemset(devimageDepthOut3, 0, size*sizeof(char));
cudaMemset(devimageColorOut4, 0, size*sizeof(char));
memset(pimageColorOut, 0, sizeof(char)*size);
memset(pimageColorOut2, 0, sizeof(char)*size);
memset(pimageColorOut3, 0, sizeof(char)*size);
memset(pimageDepthOut, 0, sizeof(char)*size);
memset(pimageDepthOut2, 0, sizeof(char)*size);
memset(pimageDepthOut3, 0, sizeof(char)*size);
memset(pimageColorOut4, 0, sizeof(char)*size);
for (int v = 0; v < imageColor.rows; v++)
{
for (int u = 0; u < imageColor.cols; u++)
{
imageColorOut.at<cv::Vec3b>(v, u)[0] = 0;
imageColorOut.at<cv::Vec3b>(v, u)[1] = 0;
imageColorOut.at<cv::Vec3b>(v, u)[2] = 0;
imageColorOut2.at<cv::Vec3b>(v, u)[0] = 0;
imageColorOut2.at<cv::Vec3b>(v, u)[1] = 0;
imageColorOut2.at<cv::Vec3b>(v, u)[2] = 0;
imageColorOut3.at<cv::Vec3b>(v, u)[0] = 0;
imageColorOut3.at<cv::Vec3b>(v, u)[1] = 0;
imageColorOut3.at<cv::Vec3b>(v, u)[2] = 0;
imageDepthOut.at<cv::Vec3b>(v, u)[0] = 0;
imageDepthOut.at<cv::Vec3b>(v, u)[1] = 0;
imageDepthOut.at<cv::Vec3b>(v, u)[2] = 0;
imageDepthOut2.at<cv::Vec3b>(v, u)[0] = 0;
imageDepthOut2.at<cv::Vec3b>(v, u)[1] = 0;
imageDepthOut2.at<cv::Vec3b>(v, u)[2] = 0;
imageDepthOut3.at<cv::Vec3b>(v, u)[0] = 0;
imageDepthOut3.at<cv::Vec3b>(v, u)[1] = 0;
imageDepthOut3.at<cv::Vec3b>(v, u)[2] = 0;
imageColorOut4.at<cv::Vec3b>(v, u)[0] = 0;
imageColorOut4.at<cv::Vec3b>(v, u)[1] = 0;
imageColorOut4.at<cv::Vec3b>(v, u)[2] = 0;
}
}
//save_dir
char *save_dir = "C:\\Users\\jiang\\Desktop\\experientdata\\experiencePictrue\\";
//wrapingImage(ref, proj, imageColor, imageDepth, imageColorOut, imageDepthOut);
//wrapingImage(ref2, proj, imageColor2, imageDepth2, imageColorOut2, imageDepthOut2);
/*wrapingImageGai1(ref, proj, imageColor, imageDepth, imageColorOut, imageDepthOut);
wrapingImageGai1(ref2, proj, imageColor2, imageDepth2, imageColorOut2, imageDepthOut2);*/
initialmatBj(imageColor);
for (int i = 0; i<8; i++)
for (int j = 0; j<3; j++)
{
ppts[3 * i + j] = pts[i][j];
}
cudaMalloc((void**)&devpts, 24 * sizeof(double));
cudaMemcpy(devpts, ppts, 24 * sizeof(double), cudaMemcpyHostToDevice);
for (int m = 0; m < 8; m++){
int t1 = 32 * m;
int t2 = 32 * (m + 1);
dim3 grids(imageColor.cols / 16, imageColor.rows / 16);
dim3 threads(16, 16);
pwrapingImageGai3 << <grids, threads >> >(pmatBj, devpts, devm_CalibParams, ref, proj, devimageColor, devimageDepth, devimageColorOut4, devimageDepthOut3, t1, t2, imageColor.cols, imageColor.rows);
//pwrapingImage(pmatBj, devpts, devm_CalibParams, ref, proj, devimageColor, devimageDepth, devimageColorOut4, devimageDepthOut3, t1, t2, imageColor.cols, imageColor.rows);
cudaMemcpy(pimageColorOut4, devimageColorOut4, sizeof(char)*size, cudaMemcpyDeviceToHost);
cudaMemcpy(pimageDepthOut3, devimageDepthOut3, sizeof(char)*size, cudaMemcpyDeviceToHost);
int wang = 0;
for (int v = 0; v < imageColor.rows; v++)
{
for (int u = 0; u < imageColor.cols; u++)
{
imageColorOut4.at<cv::Vec3b>(v, u)[0] = pimageColorOut4[wang];
imageDepthOut3.at<cv::Vec3b>(v, u)[0] = pimageDepthOut3[wang];
wang++;
imageColorOut4.at<cv::Vec3b>(v, u)[1] = pimageColorOut4[wang];
imageDepthOut3.at<cv::Vec3b>(v, u)[1] = pimageDepthOut3[wang];
wang++;
imageColorOut4.at<cv::Vec3b>(v, u)[2] = pimageColorOut4[wang];
imageDepthOut3.at<cv::Vec3b>(v, u)[2] = pimageDepthOut3[wang];
wang++;
}
}
fillMedian(imageColorOut4, 3);
fillMedian(imageDepthOut3, 3);
fillMedian(imageColorOut4, 3);
fillMedian(imageDepthOut3, 3);
for (int v = 0; v < imageColor.rows; v++)
{
for (int u = 0; u < imageColor.cols; u++)
{
if (imageDepthOut3.at<cv::Vec3b>(v, u)[0] == 0 && imageDepthOut3.at<cv::Vec3b>(v, u)[1] == 0 && imageDepthOut3.at<cv::Vec3b>(v, u)[2] == 0)
continue;
imageColorOut.at<cv::Vec3b>(v, u) = imageColorOut4.at<cv::Vec3b>(v, u);
imageDepthOut.at<cv::Vec3b>(v, u) = imageDepthOut3.at<cv::Vec3b>(v, u);
imageDepthOut3.at<cv::Vec3b>(v, u)[0] = 0;
imageDepthOut3.at<cv::Vec3b>(v, u)[1] = 0;
imageDepthOut3.at<cv::Vec3b>(v, u)[2] = 0;
imageColorOut4.at<cv::Vec3b>(v, u)[0] = 0;
imageColorOut4.at<cv::Vec3b>(v, u)[1] = 0;
imageColorOut4.at<cv::Vec3b>(v, u)[2] = 0;
}
}
wrapingImageGai4(ref2, proj, imageColor2, imageDepth2, imageColorOut4, imageDepthOut3, t1, t2);
fillMedian(imageColorOut4, 3);
fillMedian(imageDepthOut3, 3);
fillMedian(imageColorOut4, 3);
fillMedian(imageDepthOut3, 3);
for (int v = 0; v < imageColor.rows; v++)
{
for (int u = 0; u < imageColor.cols; u++)
{
if (imageDepthOut3.at<cv::Vec3b>(v, u)[0] == 0 && imageDepthOut3.at<cv::Vec3b>(v, u)[1] == 0 && imageDepthOut3.at<cv::Vec3b>(v, u)[2] == 0)
continue;
imageColorOut2.at<cv::Vec3b>(v, u) = imageColorOut4.at<cv::Vec3b>(v, u);
imageDepthOut2.at<cv::Vec3b>(v, u) = imageDepthOut3.at<cv::Vec3b>(v, u);
imageDepthOut3.at<cv::Vec3b>(v, u)[0] = 0;
imageDepthOut3.at<cv::Vec3b>(v, u)[1] = 0;
imageDepthOut3.at<cv::Vec3b>(v, u)[2] = 0;
imageColorOut4.at<cv::Vec3b>(v, u)[0] = 0;
imageColorOut4.at<cv::Vec3b>(v, u)[1] = 0;
imageColorOut4.at<cv::Vec3b>(v, u)[2] = 0;
}
}
}
dealXjlk(imageColorOut);
dealXjlk(imageColorOut2);
dealXjlk(imageColorOut);
dealXjlk(imageColorOut2);
for (int v = 0; v < imageColor.rows; v++)
{
for (int u = 0; u < imageColor.cols; u++)
{
if (imageColorOut.at<cv::Vec3b>(v, u)[2] == 0 && imageColorOut.at<cv::Vec3b>(v, u)[1] == 0 && imageColorOut.at<cv::Vec3b>(v, u)[0] == 0 && imageColorOut2.at<cv::Vec3b>(v, u)[2] != 0 && imageColorOut2.at<cv::Vec3b>(v, u)[1] != 0 && imageColorOut2.at<cv::Vec3b>(v, u)[0] != 0)
{
imageColorOut3.at<cv::Vec3b>(v, u)[0] = imageColorOut2.at<cv::Vec3b>(v, u)[0];
imageColorOut3.at<cv::Vec3b>(v, u)[1] = imageColorOut2.at<cv::Vec3b>(v, u)[1];
imageColorOut3.at<cv::Vec3b>(v, u)[2] = imageColorOut2.at<cv::Vec3b>(v, u)[2];
}
else if (imageColorOut2.at<cv::Vec3b>(v, u)[2] == 0 && imageColorOut2.at<cv::Vec3b>(v, u)[1] == 0 && imageColorOut2.at<cv::Vec3b>(v, u)[0] == 0 && imageColorOut.at<cv::Vec3b>(v, u)[2] != 0 && imageColorOut.at<cv::Vec3b>(v, u)[1] != 0 && imageColorOut.at<cv::Vec3b>(v, u)[0] != 0)
{
imageColorOut3.at<cv::Vec3b>(v, u)[0] = imageColorOut.at<cv::Vec3b>(v, u)[0];
imageColorOut3.at<cv::Vec3b>(v, u)[1] = imageColorOut.at<cv::Vec3b>(v, u)[1];
imageColorOut3.at<cv::Vec3b>(v, u)[2] = imageColorOut.at<cv::Vec3b>(v, u)[2];
}
else if (imageColorOut2.at<cv::Vec3b>(v, u)[2] != 0 && imageColorOut2.at<cv::Vec3b>(v, u)[1] != 0 && imageColorOut2.at<cv::Vec3b>(v, u)[0] != 0 && imageColorOut.at<cv::Vec3b>(v, u)[2] != 0 && imageColorOut.at<cv::Vec3b>(v, u)[1] != 0 && imageColorOut.at<cv::Vec3b>(v, u)[0] != 0)
{
imageColorOut3.at<cv::Vec3b>(v, u)[0] = (imageColorOut.at<cv::Vec3b>(v, u)[0] + imageColorOut2.at<cv::Vec3b>(v, u)[0]) / 2;
imageColorOut3.at<cv::Vec3b>(v, u)[1] = (imageColorOut.at<cv::Vec3b>(v, u)[1] + imageColorOut2.at<cv::Vec3b>(v, u)[1]) / 2;
imageColorOut3.at<cv::Vec3b>(v, u)[2] = (imageColorOut.at<cv::Vec3b>(v, u)[2] + imageColorOut2.at<cv::Vec3b>(v, u)[2]) / 2;
}
}
}
fillMedian(imageColorOut3, 3);
fillMedian(imageColorOut3, 3);
cv::Mat mask(imageColor.rows, imageColor.cols, CV_8UC1, cv::Scalar(0));
//cv::Mat mask = imageColorOut3.create(imageColorOut3.rows, imageColorOut3.cols, CV_8UC1);
for (int v = 0; v < imageColor.rows; v++)
{
for (int u = 0; u < imageColor.cols; u++)
{
if (imageColorOut3.at<cv::Vec3b>(v, u)[0] != 0 || imageColorOut3.at<cv::Vec3b>(v, u)[1] != 0
|| imageColorOut3.at<cv::Vec3b>(v, u)[2] != 0)
mask.at<uchar>(v, u) = 0;
else
mask.at<uchar>(v, u) = 1;
}
}
cv::inpaint(imageColorOut3, mask, imageColorOut3, 3, cv::INPAINT_TELEA);
cv::imwrite("D:\\jiang\\result\\jiang\\Desktop\\experientdata\\experiencePictrue\\virtruel_Color_image01.jpg", imageColorOut);
//cv::imshow("virtruel_Color_image21", imageColorOut2);
cv::imwrite("D:\\jiang\\result\\jiang\\Desktop\\experientdata\\experiencePictrue\\virtruel_Color_image21.jpg", imageColorOut2);
//cv::imshow("virtruel_Color_image11", imageColorOut3);
cv::imwrite("D:\\jiang\\result\\jiang\\Desktop\\experientdata\\experiencePictrue\\virtruel_Color_image11.jpg", imageColorOut3);
char save_dirr[100] = "D:\\jiang\\result\\jiang\\Desktop\\experientdata\\experiencePictrue\\";
char string[10];
char hou[10] = ".jpg";
_itoa(ttt++, string, 10);
string[strlen(string)] = '\0';
hou[strlen(hou)] = '\0';
strcat(save_dirr, string);
strcat(save_dirr, hou);
cv::imwrite(save_dirr, imageColorOut3);
cv::imshow("virtruel_Depth_image", imageDepthOut);
cv::imwrite("D:\\jiang\\result\\jiang\\Desktop\\experientdata\\experiencePictrue\\virtruel_Depth_image01.jpg", imageDepthOut);
cv::imshow("virtruel_Depth_image2", imageDepthOut2);
cv::imwrite("D:\\jiang\\result\\jiang\\Desktop\\experientdata\\experiencePictrue\\virtruel_Depth_image21.jpg", imageDepthOut2);
display actruel_image
//cv::imshow("actruel_image", imageColor_actual);
cv::imwrite("D:\\jiang\\result\\jiang\\Desktop\\experientdata\\experiencePictrue\\actruel_image.jpg", imageColor_actual);
}
void main()
{
//char refColor[200];
//char refDepth[200];
FILE *fp;
FILE *fp2;
FILE *fp3;
char filename[50] = "D:\\jiang\\result\\psnr_ssim"; // 此处写入文件名
strcat_s(filename, getNewFileName());
strcat_s(filename, ".txt");
ofstream fout(filename);
char str0[200][100];
char str2[200][100];
char str3[200][100];
int i, n = 0;
system("dir /a-d /b D:\\jiang\\3DVideos-distrib\\MSR3DVideo-Breakdancers\\cam3 >D:\\jiang\\result\\cam3.txt");
system("dir /a-d /b D:\\jiang\\3DVideos-distrib\\MSR3DVideo-Breakdancers\\cam5 >D:\\jiang\\result\\cam5.txt");
system("dir /a-d /b D:\\jiang\\3DVideos-distrib\\MSR3DVideo-Breakdancers\\cam4 >D:\\jiang\\result\\cam4.txt");
fp = fopen("D:\\jiang\\result\\cam3.txt", "r");
fp2 = fopen("D:\\jiang\\result\\cam5.txt", "r");
fp3 = fopen("D:\\jiang\\result\\cam4.txt", "r");
while (1){
if (fgets(str0[n], 50, fp) == NULL) break;
str0[n][strlen(str0[n]) - 1] = '\0'; // 加一个字符串结束符
if (fgets(str2[n], 50, fp2) == NULL) break;
str2[n][strlen(str2[n]) - 1] = '\0'; // 加一个字符串结束符
if (fgets(str3[n], 50, fp3) == NULL) break;
str3[n][strlen(str3[n]) - 1] = '\0'; // 加一个字符串结束符
n++;
}
n--;
fclose(fp);
fclose(fp2);
fclose(fp3);
double value_psnr01 = 0;
double value_ssim01 = 0;
double value_psnr21 = 0;
double value_ssim21 = 0;
double value_psnr11 = 0;
double value_ssim11 = 0;
for (i = 0; i < 100; i++){
char refColor[200] = "D:\\jiang\\3DVideos-distrib\\MSR3DVideo-Breakdancers\\cam3\\";
char refDepth[200] = "D:\\jiang\\3DVideos-distrib\\MSR3DVideo-Breakdancers\\cam3\\";
char refColor2[200] = "D:\\jiang\\3DVideos-distrib\\MSR3DVideo-Breakdancers\\cam5\\";
char refDepth2[200] = "D:\\jiang\\3DVideos-distrib\\MSR3DVideo-Breakdancers\\cam5\\";
char calibParam[200] = "D:\\jiang\\3DVideos-distrib\\MSR3DVideo-Breakdancers\\calibParams-breakdancers.txt";
char actColor[200] = "D:\\jiang\\3DVideos-distrib\\MSR3DVideo-Breakdancers\\cam4\\";
strcat_s(refColor, str0[i]);
strcat_s(refDepth, str0[i + 100]);
strcat_s(refColor2, str2[i]);
strcat_s(refDepth2, str2[i + 100]);
strcat_s(actColor, str3[i]);
/*printf("%s\n",refColor);
printf("%s\n",refDepth);
printf("%s\n", refColor2);
printf("%s\n", refDepth2);
printf("%s\n", actColor);*/
dipslay(calibParam, refColor, refDepth, refColor2, refDepth2, actColor, 3, 5, 4);
char *ref_img01 = "D:\\jiang\\result\\jiang\\Desktop\\experientdata\\experiencePictrue\\virtruel_Color_image01.jpg";
char *ref_img21 = "D:\\jiang\\result\\jiang\\Desktop\\experientdata\\experiencePictrue\\virtruel_Color_image21.jpg";
char *ref_img11 = "D:\\jiang\\result\\jiang\\Desktop\\experientdata\\experiencePictrue\\virtruel_Color_image11.jpg";
char *act_img = "D:\\jiang\\result\\jiang\\Desktop\\experientdata\\experiencePictrue\\actruel_image.jpg";
value_psnr01 += psnr(ref_img01, act_img);
printf("psnr01=%lf\n", value_psnr01 / (i + 1));
fout << value_psnr01 / (i + 1) << "\t"; // fout用法和cout一致, 不过是写到文件里面去
value_ssim01 += ssim(ref_img01, act_img);
printf("ssim01=%lf\n", value_ssim01 / (i + 1));
fout << value_ssim01 / (i + 1) << "\t"; // fout用法和cout一致, 不过是写到文件里面去
value_psnr21 += psnr(ref_img21, act_img);
printf("psnr21=%lf\n", value_psnr21 / (i + 1));
fout << value_psnr21 / (i + 1) << "\t"; // fout用法和cout一致, 不过是写到文件里面去
value_ssim21 += ssim(ref_img21, act_img);
printf("ssim21=%lf\n", value_ssim21 / (i + 1));
fout << value_ssim21 / (i + 1) << "\t"; // fout用法和cout一致, 不过是写到文件里面去
value_psnr11 += psnr(ref_img11, act_img);
printf("psnr11=%lf\n", value_psnr11 / (i + 1));
fout << value_psnr11 / (i + 1) << "\t"; // fout用法和cout一致, 不过是写到文件里面去
value_ssim11 += ssim(ref_img11, act_img);
printf("ssim11=%lf\n", value_ssim11 / (i + 1));
fout << value_ssim11 / (i + 1) << "\t"; // fout用法和cout一致, 不过是写到文件里面去
fout << endl;
}
cout << "完啦!!!!!!!!!!!!!";
}
#endif
#ifndef _wrapingOf3D1
#define _wrapingOf3D1
#include<iostream>
#include<opencv2\opencv.hpp>
/*
**define a struct included intrinsic and extrinsic args
*/
typedef struct {
double m_K[3][3]; // 3x3 intrinsic matrix
double m_RotMatrix[3][3]; // rotation matrix
double m_Trans[3]; // translation vector
double m_ProjMatrix[4][4]; // projection matrix
} CalibStruct;
typedef struct {
double m_K[9]; // 3x3 intrinsic matrix
double m_RotMatrix[9]; // rotation matrix
double m_Trans[3]; // translation vector
double m_ProjMatrix[16]; // projection matrix
} devCalibStruct;
/*
**define globle variables
*/
CalibStruct m_CalibParams[8];
devCalibStruct hostm_CalibParams[8];
int m_NumCameras = 8;
__device__ int devm_NumCameras = 8;
int m_Width = 1024, m_Height = 768; // camera resolution is 1024x768
__device__ int devm_Width = 1024, devm_Height = 768;
/*
**declare function
*/
void InitializeFromFile(char *fileName);
double DepthLevelToZ(unsigned char d);
unsigned char ZToDepthLever(double z);
double projXYZtoUV(double projMatrix[4][4], double x, double y, double z, double *u, double *v);
void projUVZtoXY(double projMatrix[4][4], double u, double v, double z, double *x, double *y, double pt[8][2]);
void wrapingImage(int ref, int proj, cv::Mat &imageColor, cv::Mat &imageDepth, cv::Mat &imageColorOut);
void pointProject_from_ref_to_otherView(double pts[8][2], int ref, int u, int v, unsigned char d);
/*
**define function
*/
/*
** read text file and write args to struct of globle variable
*/
void 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);
}
}// readCalibrationFile
/*
**calcular all projectionMatrices depended on struct variables
*/
void 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;
}
}
/**
**init projection matrix
*/
void InitializeFromFile(char *fileName)
{
readCalibrationFile(fileName);
computeProjectionMatrices();
}
/**
**calcular z depended on d of depth image
*/
__device__ double devDepthLevelToZ(unsigned char d)
{
double z;
double MinZ = 44.0, MaxZ = 120.0;
z = 1.0 / ((d / 255.0)*(1.0 / MinZ - 1.0 / MaxZ) + 1.0 / MaxZ);
return z;
}
/**
**calcular d of depth image depended on z
*/
__device__ unsigned char devZToDepthLever(double z)
{
double MinZ = 44.0, MaxZ = 120.0;
unsigned char d;
d = (unsigned char)(255.0*(1 / (double)z - 1 / MaxZ) / (1 / MinZ - 1 / MaxZ));
return d;
}
/**
**calcular x,y depended on u,v,z and projection Matrix
**for example,projection Matrix is m_CalibParams[i].m_ProjMatrix which is index of camera
*/
__device__ void devprojUVZtoXY(double projMatrix[16], double u, double v, double z, double &x, double &y)
{
double c0, c1, c2;
// image (0,0) is bottom lefthand corner
v = (double)devm_Height - v - 1.0;
c0 = z*projMatrix[2] + projMatrix[3];
c1 = z*projMatrix[6] + projMatrix[7];
c2 = z*projMatrix[10] + projMatrix[11];
y = u*(c1*projMatrix[8] - projMatrix[4] * c2) +
v*(c2*projMatrix[0] - projMatrix[8] * c0) +
projMatrix[4] * c0 - c1*projMatrix[0];
y /= v*(projMatrix[8] * projMatrix[1] - projMatrix[9] * projMatrix[0]) +
u*(projMatrix[4] * projMatrix[9] - projMatrix[5] * projMatrix[8]) +
projMatrix[0] * projMatrix[5] - projMatrix[4] * projMatrix[1];
x = (y)*(projMatrix[1] - projMatrix[9] * u) + c0 - c2*u;
x /= projMatrix[8] * u - projMatrix[0];
} // projUVZtoXY
/**
**calcular u,v,z1 depended on x,y,z
**z1 is after projection and z is before projection.z1 is return value
**/
__device__ double devprojXYZtoUV(double projMatrix[16], double x, double y, double z, double *u, double *v)
{
double w;
*u = projMatrix[0] * x +
projMatrix[1] * y +
projMatrix[2] * z +
projMatrix[3];
*v = projMatrix[4] * x +
projMatrix[5] * y +
projMatrix[6] * z +
projMatrix[7];
w = projMatrix[8] * x +
projMatrix[9] * y +
projMatrix[10] * z +
projMatrix[11];
*u /= w;
*v /= w;
// image (0,0) is bottom lefthand corner
*v = (double)devm_Height - *v - 1.0;
return w;
} // projXYZtoUV
/**
**/
__device__ void devpointProject_from_ref_to_otherView(devCalibStruct *devm_CalibParams,double *devpts, int ref, int u, int v, unsigned char d)
{
double x=0, y=0;
double z = devDepthLevelToZ(d);
//printf("Testing projection of pt (%d,%d) in camera 0 with d = %d (z = %f) to other cameras\n", u, v, d, z);
devprojUVZtoXY(devm_CalibParams[ref].m_ProjMatrix, (double)u, (double)v, z, x, y);
//printf("3D pt = (%f, %f, %f) [coordinates wrt reference camera]\n", x, y, z);
for (int cam = 0; cam<8; cam++)
{
double *pt = devpts + cam * 3;
pt[2] = devprojXYZtoUV(devm_CalibParams[cam].m_ProjMatrix, x, y, z, &pt[0], &pt[1]);
//printf("Camera %d: (%f, %f)\n", cam, pt[0], pt[1]);
pt[2] = devZToDepthLever(pt[2]);
}
}
/**
**wraping image,ref represent reference cam,proj represent projection cam
**the kernal code
**/
__global__ void devwrapingImage(devCalibStruct *devm_CalibParams, double *devpts, int ref, int proj, unsigned char *imageColor, unsigned char *imageDepth, unsigned char *imageColorOut, unsigned char *imageDepthOut)
{
// int u = 300;
// int v = 250;
//int offset = u + 1024*v;
int offset = u + 1024 * 250;
//double d = imageDepth[offset*3+0];
//double devpts1[24];
//devpointProject_from_ref_to_otherView(devm_CalibParams, devpts1, ref, u, v, d);
///*int u1 = (int)devpts[proj*3+0];
//int v1 = (int)devpts[proj*3+1];
//int k1 = (int)devpts[proj*3+2];
//int newoffset = u1 + blockDim.x*gridDim.x*v1;
//if (!(u1 < 0 || u1 >= devm_Width - 1 || v1 < 0 || v1 >= devm_Height - 1) && !(k1 < imageDepthOut[newoffset * 3 + 0]))
//{
// imageColorOut[newoffset * 3 + 0] = imageColor[offset * 3 + 0];
// imageColorOut[newoffset * 3 + 1] = imageColor[offset * 3 + 1];
// imageColorOut[newoffset * 3 + 2] = imageColor[offset * 3 + 2];
// imageDepthOut[newoffset * 3 + 0] = k1;
// imageDepthOut[newoffset * 3 + 1] = k1;
// imageDepthOut[newoffset * 3 + 2] = k1;
//}*/
/*{
imageColorOut[offset * 3 + 0] = imageColor[offset * 3 + 0];
imageColorOut[offset * 3 + 1] = imageColor[offset * 3 + 1];
imageColorOut[offset * 3 + 2] = imageColor[offset * 3 + 2];
imageDepthOut[offset * 3 + 0] = d;
imageDepthOut[offset * 3 + 1] = d;
imageDepthOut[offset * 3 + 2] = d;
}*/
int u = threadIdx.x + blockIdx.x*blockDim.x;
int v = threadIdx.y + blockIdx.y*blockDim.y;
int offset = u + blockDim.x*gridDim.x*v;
double devpts1[24];
double d = imageDepth[offset * 3 + 0];
devpointProject_from_ref_to_otherView(devm_CalibParams, devpts1, ref, u, v, d);
int u1 = (int)devpts1[proj * 3 + 0];
int v1 = (int)devpts1[proj * 3 + 1];
int k1 = (int)devpts1[proj * 3 + 2];
int newoffset = u1 + blockDim.x*gridDim.x*v1;
if (!(u1 < 0 || u1 >= devm_Width - 1 || v1 < 0 || v1 >= devm_Height - 1) && !(k1 < imageDepthOut[newoffset * 3 + 0]))
{
imageColorOut[newoffset * 3 + 0] = imageColor[offset * 3 + 0];
imageColorOut[newoffset * 3 + 1] = imageColor[offset * 3 + 1];
imageColorOut[newoffset * 3 + 2] = imageColor[offset * 3 + 2];
imageDepthOut[newoffset * 3 + 0] = k1;
imageDepthOut[newoffset * 3 + 1] = k1;
imageDepthOut[newoffset * 3 + 2] = k1;
}
}
void dipslay(char *calibParam, char *refColor, char *refDepth, char *refColor2, char *refDepth2, char *actColor)
{
//initialize projection_Matrix
InitializeFromFile(calibParam);
//display projection_Matrix
for (int i = 0; i < m_NumCameras; i++){
for (int j = 0; j < 3; j++){
for (int k = 0; k < 3; k++)
std::cout << m_CalibParams[i].m_K[j][k] << "\t";
std::cout << std::endl;
}
for (int j = 0; j < 3; j++){
for (int k = 0; k < 3; k++)
std::cout << m_CalibParams[i].m_RotMatrix[j][k] << "\t";
std::cout << std::endl;
}
for (int k = 0; k < 3; k++)
std::cout << m_CalibParams[i].m_Trans[k] << "\t";
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
}
for (int i = 0; i < m_NumCameras; i++){
for (int j = 0; j < 3; j++){
for (int k = 0; k < 3; k++){
hostm_CalibParams[i].m_K[j * 3 + k] = m_CalibParams[i].m_K[j][k];
}
}
for (int j = 0; j < 3; j++){
for (int k = 0; k < 3; k++){
hostm_CalibParams[i].m_RotMatrix[j * 3 + k] = m_CalibParams[i].m_RotMatrix[j][k];
}
}
for (int k = 0; k < 3; k++)
hostm_CalibParams[i].m_Trans[k] = m_CalibParams[i].m_Trans[k];
for (int j = 0; j < 4; j++){
for (int k = 0; k < 4; k++){
hostm_CalibParams[i].m_ProjMatrix[j * 4 + k] = m_CalibParams[i].m_ProjMatrix[j][k];
}
}
}
for (int i = 0; i < m_NumCameras; i++){
for (int j = 0; j < 3; j++){
for (int k = 0; k < 3; k++)
std::cout << hostm_CalibParams[i].m_K[j * 3 + k] << "\t";
std::cout << std::endl;
}
for (int j = 0; j < 3; j++){
for (int k = 0; k < 3; k++)
std::cout << hostm_CalibParams[i].m_RotMatrix[j * 3 + k] << "\t";
std::cout << std::endl;
}
for (int k = 0; k < 3; k++)
std::cout << hostm_CalibParams[i].m_Trans[k] << "\t";
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
}
devCalibStruct *devm_CalibParams;
double *devpts;
cudaMalloc((void**)&devm_CalibParams, m_NumCameras*sizeof(devCalibStruct));
cudaMalloc((void**)&devpts, 24 * sizeof(double));
cudaMemcpy(devm_CalibParams, hostm_CalibParams, m_NumCameras*sizeof(devCalibStruct), cudaMemcpyHostToDevice);
//set reference cam
int ref = 0;
int ref2 = 2;
//set projection cam
int proj = 1;
//suspend and users enter a digit
//read color image and depth image of refrence
cv::Mat imageColor = cv::imread(refColor);
cv::Mat imageDepth = cv::imread(refDepth);
cv::Mat imageColor2 = cv::imread(refColor2);
cv::Mat imageDepth2 = cv::imread(refDepth2);
int size = imageColor.cols*imageColor.rows*imageColor.channels();
unsigned char *hostimageColor = (unsigned char*)malloc(size*sizeof(unsigned char));
unsigned char *hostimageColor2 = (unsigned char*)malloc(size*sizeof(unsigned char));
unsigned char *hostimageDepth = (unsigned char*)malloc(size*sizeof(unsigned char));
unsigned char *hostimageDepth2 = (unsigned char*)malloc(size*sizeof(unsigned char));
for (int v = 0; v < imageColor.rows; v++)
{
for (int u = 0; u < imageColor.cols; u++)
{
int offset = (v*imageColor.cols + u) * 3;
hostimageColor[offset + 0] = imageColor.at<cv::Vec3b>(v, u)[0];
hostimageColor[offset + 1] = imageColor.at<cv::Vec3b>(v, u)[1];
hostimageColor[offset + 2] = imageColor.at<cv::Vec3b>(v, u)[2];
hostimageColor2[offset + 0] = imageColor2.at<cv::Vec3b>(v, u)[0];
hostimageColor2[offset + 1] = imageColor2.at<cv::Vec3b>(v, u)[1];
hostimageColor2[offset + 2] = imageColor2.at<cv::Vec3b>(v, u)[2];
hostimageDepth[offset + 0] = imageDepth.at<cv::Vec3b>(v, u)[0];
hostimageDepth[offset + 1] = imageDepth.at<cv::Vec3b>(v, u)[1];
hostimageDepth[offset + 2] = imageDepth.at<cv::Vec3b>(v, u)[2];
hostimageDepth2[offset + 0] = imageDepth2.at<cv::Vec3b>(v, u)[0];
hostimageDepth2[offset + 1] = imageDepth2.at<cv::Vec3b>(v, u)[1];
hostimageDepth2[offset + 2] = imageDepth2.at<cv::Vec3b>(v, u)[2];
}
}
unsigned char *devimageColor;
unsigned char *devimageColor2;
unsigned char *devimageDepth;
unsigned char *devimageDepth2;
cudaMalloc((void**)&devimageColor, size*sizeof(unsigned char));
cudaMalloc((void**)&devimageDepth, size*sizeof(unsigned char));
cudaMalloc((void**)&devimageColor2, size*sizeof(unsigned char));
cudaMalloc((void**)&devimageDepth2, size*sizeof(unsigned char));
cudaMemcpy(devimageColor, hostimageColor, size*sizeof(unsigned char), cudaMemcpyHostToDevice);
cudaMemcpy(devimageDepth, hostimageDepth, size*sizeof(unsigned char), cudaMemcpyHostToDevice);
cudaMemcpy(devimageColor2, hostimageColor, size*sizeof(unsigned char), cudaMemcpyHostToDevice);
cudaMemcpy(devimageDepth2, hostimageDepth, size*sizeof(unsigned char), cudaMemcpyHostToDevice);
//read true image used to compare
cv::Mat imageColor_actual = cv::imread(actColor);
//test code
//pointProject_from_ref_to_otherView(pts, ref, 700, 700, imageDepth.at<cv::Vec3b>(700, 700)[0]);
unsigned char d = imageDepth.at<cv::Vec3b>(300, 250)[0];
//devpointProject_from_ref_to_otherView<<<1,1>>>(devm_CalibParams,devpts, ref, 300, 250, d);
/*double hostpts[24];
cudaMemcpy(hostpts, devpts, sizeof(double)* 24, cudaMemcpyDeviceToHost);
for (int i = 0; i < 8; i++)
{
std::cout << hostpts[i*3+0] << "\t" << hostpts[i*3+1] << std::endl;
}*/
//define two variable of output
cv::Mat imageColorOut;
cv::Mat imageColorOut2;
cv::Mat imageDepthOut;
unsigned char *hostimageColorOut = (unsigned char*)malloc(size*sizeof(unsigned char));
unsigned char *hostimageColorOut2 = (unsigned char*)malloc(size*sizeof(unsigned char));
unsigned char *hostimageDepthOut = (unsigned char*)malloc(size*sizeof(unsigned char));
memset(hostimageColorOut, 0, size*sizeof(unsigned char));
memset(hostimageColorOut2, 0, size*sizeof(unsigned char));
memset(hostimageDepthOut, 0, size*sizeof(unsigned char));
unsigned char *devimageColorOut;
unsigned char *devimageColorOut2;
unsigned char *devimageDepthOut;
cudaMalloc((void**)&devimageColorOut, size*sizeof(unsigned char));
cudaMalloc((void**)&devimageDepthOut, size*sizeof(unsigned char));
cudaMalloc((void**)&devimageColorOut2, size*sizeof(unsigned char));
cudaMemset(devimageColorOut, 0, size*sizeof(unsigned char));
cudaMemset(devimageColorOut2, 0, size*sizeof(unsigned char));
cudaMemset(devimageDepthOut, 0, size*sizeof(unsigned char));
imageColorOut.create(imageColor.rows, imageColor.cols, imageColor.type());
imageColorOut2.create(imageColor.rows, imageColor.cols, imageColor.type());
imageDepthOut.create(imageColor.rows, imageColor.cols, imageColor.type());
for (int v = 0; v < imageColor.rows; v++)
{
for (int u = 0; u < imageColor.cols; u++)
{
imageColorOut.at<cv::Vec3b>(v, u)[0] = 0;
imageColorOut.at<cv::Vec3b>(v, u)[1] = 0;
imageColorOut2.at<cv::Vec3b>(v, u)[2] = 0;
imageColorOut2.at<cv::Vec3b>(v, u)[0] = 0;
imageColorOut2.at<cv::Vec3b>(v, u)[1] = 0;
imageColorOut.at<cv::Vec3b>(v, u)[2] = 0;
imageDepthOut.at<cv::Vec3b>(v, u)[0] = 0;
imageDepthOut.at<cv::Vec3b>(v, u)[1] = 0;
imageDepthOut.at<cv::Vec3b>(v, u)[2] = 0;
}
}
//save_dir
char *save_dir = "D:\\jiang\\result\\jiang\\Desktop\\experientdata\\experiencePictrue";
//wraping from reference view to virtruel view
/*wrapingImage(ref, proj, imageColor, imageDepth, imageColorOut, imageDepthOut);
wrapingImage(ref2, proj, imageColor2, imageDepth2, imageColorOut, imageDepthOut);*/
dim3 grids(1024/16,768/16);
dim3 threads(16,16);
devwrapingImage<<<grids,threads>>>(devm_CalibParams, devpts, ref, proj, devimageColor, devimageDepth, devimageColorOut, devimageDepthOut);
/*double hostpts[24];
cudaMemcpy(hostpts, devpts, sizeof(double)* 24, cudaMemcpyDeviceToHost);
for (int i = 0; i < 8; i++)
{
std::cout << hostpts[i * 3 + 0] << "\t" << hostpts[i * 3 + 1] << std::endl;
}*/
//devwrapingImage << <grids,threads>> >(devm_CalibParams, devpts, ref2, proj, devimageColor2, devimageDepth2, devimageColorOut, devimageDepthOut);
cudaMemcpy(hostimageColorOut, devimageColorOut, size*sizeof(unsigned char), cudaMemcpyDeviceToHost);
cudaMemcpy(hostimageDepthOut, devimageDepthOut, size*sizeof(unsigned char), cudaMemcpyDeviceToHost);
cudaMemcpy(hostimageColorOut2, devimageColorOut2, size*sizeof(unsigned char), cudaMemcpyDeviceToHost);
for (int v = 0; v < imageColor.rows; v++)
{
for (int u = 0; u < imageColor.cols; u++)
{
int offset = (v*imageColor.cols + u) * 3;
imageColorOut.at<cv::Vec3b>(v, u)[0] = hostimageColorOut[offset + 0];
imageColorOut.at<cv::Vec3b>(v, u)[1] = hostimageColorOut[offset + 1];
imageColorOut.at<cv::Vec3b>(v, u)[2] = hostimageColorOut[offset + 2];
/*imageColorOut2.at<cv::Vec3b>(v, u)[0] = hostimageColorOut2[offset + 0];
imageColorOut2.at<cv::Vec3b>(v, u)[1] = hostimageColorOut2[offset + 1];
imageColorOut2.at<cv::Vec3b>(v, u)[2] = hostimageColorOut2[offset + 2];*/
imageDepthOut.at<cv::Vec3b>(v, u)[0] = hostimageDepthOut[offset + 0];
imageDepthOut.at<cv::Vec3b>(v, u)[1] = hostimageDepthOut[offset + 1];
imageDepthOut.at<cv::Vec3b>(v, u)[2] = hostimageDepthOut[offset + 2];
//std::cout << (int)imageColorOut.at<cv::Vec3b>(v, u)[0]<<" ";
}
}
//display reference_image
/*cv::imshow("reference_image", imageColor);
cv::imwrite("C:\\Users\\jiang\\Desktop\\experientdata\\experiencePictrue\\reference_image.jpg", imageColor);*/
//display virtruel_image
cv::medianBlur(imageColorOut, imageColorOut, 3);
cv::imshow("virtruel_Color_image01", imageColorOut);
cv::imwrite("D:\\jiang\\result\\jiang\\Desktop\\experientdata\\experiencePictrue\\virtruel_Color_image01.jpg", imageColorOut);
display virtruel_image
//cv::imshow("virtruel_Color_image21", imageColorOut2);
//cv::imwrite("C:\\Users\\jiang\\Desktop\\experientdata\\experiencePictrue\\virtruel_Color_image21.jpg", imageColorOut2);
//display virtruel_image
cv::medianBlur(imageDepthOut, imageDepthOut, 3);
cv::imshow("virtruel_Depth_image", imageDepthOut);
cv::imwrite("D:\\jiang\\result\\jiang\\Desktop\\experientdata\\experiencePictrue\\virtruel_Depth_image.jpg", imageDepthOut);
//display actruel_image
cv::imshow("actruel_image", imageColor_actual);
cv::imwrite("D:\\jiang\\result\\jiang\\Desktop\\experientdata\\experiencePictrue\\actruel_image.jpg", imageColor_actual);
cv::waitKey();
}
void main()
{
char *refColor = "D:\\jiang\\3DVideos-distrib\\MSR3DVideo-Breakdancers\\cam0\\color-cam0-f000.jpg";
char *refDepth = "D:\\jiang\\3DVideos-distrib\\MSR3DVideo-Breakdancers\\cam0\\depth-cam0-f000.png";
char *refColor2 = "D:\\jiang\\3DVideos-distrib\\MSR3DVideo-Breakdancers\\cam2\\color-cam2-f000.jpg";
char *refDepth2 = "D:\\jiang\\3DVideos-distrib\\MSR3DVideo-Breakdancers\\cam2\\depth-cam2-f000.png";
char *calibParam = "D:\\jiang\\3DVideos-distrib\\MSR3DVideo-Breakdancers\\calibParams-breakdancers.txt";
char *actColor = "D:\\jiang\\3DVideos-distrib\\MSR3DVideo-Breakdancers\\cam1\\color-cam1-f000.jpg";
dipslay(calibParam, refColor, refDepth, refColor2, refDepth2, actColor);
}
#endif