Skip to content

Commit

Permalink
downsample function
Browse files Browse the repository at this point in the history
  • Loading branch information
noh.56 committed Mar 18, 2020
1 parent 1b563d1 commit 22bc64a
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 27 deletions.
35 changes: 12 additions & 23 deletions Makefile
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,20 @@

# If libtiff is installed in a nonstandard location you must edit
# TIFFPATH and uncomment the following three lines.

TIFFPATH=/projects/sciteam/bazu/setsm/lib/tiff-4.0.3-cray
TIFFINC=-I$(TIFFPATH)/include
TIFFLIB=-L$(TIFFPATH)/lib
TIFFPATH=/home/noh.56/software/tiff-4.0.3/libtiff
TIFFINC=-I/home/noh.56/software/tiff-4.0.3/include
TIFFLIB=-L/home/noh.56/software/tiff-4.0.3/lib

# If libgeotiff is installed in a nonstandard location you must edit
# GEOTIFFPATH and uncomment the following three lines.
GEOTIFFPATH=/projects/sciteam/bazu/setsm/lib/geotiff
GEOTIFFINC=-I$(GEOTIFFPATH)/include
GEOTIFFLIB=-L$(GEOTIFFPATH)/lib

PROJLIB=-L/projects/sciteam/bazu/setsm/lib/proj/lib
GEOTIFFPATH=/home/noh.56/software/libgeotiff-1.4.2/libxtiff
GEOTIFFINC=-I/home/noh.56/software/libgeotiff-1.4.2/include
GEOTIFFLIB=-L/home/noh.56/software/libgeotiff-1.4.2/lib

MPIFLAGS = -DBUILDMPI

INCS = $(TIFFINC) $(GEOTIFFINC)
LDFLAGS = $(TIFFLIB) $(GEOTIFFLIB) $(PROJLIB)
LDFLAGS = $(TIFFLIB) $(GEOTIFFLIB)

OBJS = setsmgeo.o grid.o grid_triangulation.o edge_list.o
HDRS = Typedefine.hpp setsm_code.hpp setsmgeo.hpp grid_triangulation.hpp grid_types.hpp grid_iterators.hpp basic_topology_types.hpp git_description.h
Expand All @@ -39,31 +36,24 @@ else ifeq ($(COMPILER), pgi)
MPICXX=mpicxx
CFLAGS=-c99 -O3 -mp=allcores -fast
CXXFLAGS=-std=c++11 -O3 -mp=allcores -fast
else ifeq ($(COMPILER), cray)
CC=cc
CXX=CC
MPICC=mpicc
MPICXX=mpicxx
CFLAGS=
CXXFLAGS=-hstd=c++11
else
CC=gcc
CXX=g++
MPICC=mpicc
MPICXX=mpicxx
CFLAGS=-std=c99 -O3 -fopenmp
CXXFLAGS=-O3 -fopenmp
CFLAGS=-std=c99 -g -O3 -fopenmp
CXXFLAGS=-g -O3 -fopenmp
endif

$(shell git describe --always --tags --dirty > git_description)
GIT_DESCRIPTION:=$(shell cat git_description)
export GIT_DESCRIPTION

setsm : setsm_code.o $(OBJS)
$(CXX) $(CXXFLAGS) -o setsm setsm_code.o $(OBJS) $(LDFLAGS) -lm -lgeotiff -ltiff -lz -ljpeg -lproj
$(CXX) $(CXXFLAGS) -o setsm setsm_code.o $(OBJS) $(LDFLAGS) -lm -lgeotiff -ltiff

setsm_mpi : setsm_code_mpi.o $(OBJS)
$(MPICXX) $(CXXFLAGS) $(MPIFLAGS) -o setsm_mpi setsm_code_mpi.o $(OBJS) $(LDFLAGS) -lm -lgeotiff -ltiff -lz -ljpeg -lproj
$(MPICXX) $(CXXFLAGS) $(MPIFLAGS) -o setsm_mpi setsm_code_mpi.o $(OBJS) $(LDFLAGS) -lm -lgeotiff -ltiff

setsm_code.o : setsm_code.cpp $(HDRS)
$(CXX) -c $(CXXFLAGS) $(INCS) setsm_code.cpp -o setsm_code.o
Expand All @@ -83,8 +73,7 @@ $(OBJS) : $(HDRS)

clean :
rm -f setsm setsm_mpi
rm -f *.o git_description.h
rm -f *.o git_description git_description.h

git_description.h: git_description
echo "#define GIT_DESCRIPTION \"$(GIT_DESCRIPTION)\"" > $@

9 changes: 7 additions & 2 deletions Typedefine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ typedef struct ProjectInfo{
double minHeight;
double maxHeight;
double System_memory;
double DS_sigma;

int DS_kernel;
int start_row;
int end_row;
int start_col;
Expand Down Expand Up @@ -296,6 +298,9 @@ typedef struct ArgumentInfo{
double focal_length;
double CCD_size;
double System_memory;
double SDM_days;
double SDM_AS;
double DS_sigma;

int check_arg; // 0 : no input, 1: 3 input
int Threads_num;
Expand All @@ -315,8 +320,7 @@ typedef struct ArgumentInfo{
int number_of_images; // 2 is for stereo (default), n is for multi more than 3
uint8 pyramid_level;
uint8 SDM_SS;
double SDM_days;
double SDM_AS;
int DS_kernel;

char Image[MaxImages][500];
char Outputpath[500];
Expand All @@ -326,6 +330,7 @@ typedef struct ArgumentInfo{
char EO_Path[500];
char DEM_input_file[500];

bool check_downsample;
bool check_DEM_space;
bool check_Threads_num;
bool check_seeddem;
Expand Down
174 changes: 172 additions & 2 deletions setsm_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ int main(int argc,char *argv[])
args.check_sdm_ortho = 0; //no coreg = 1 , with coreg = 2
args.check_DEM_coreg_output = false;
args.check_txt_input = 0; //no txt input = 0, DEM coregistration txt input = 1;
args.check_downsample = false;

args.number_of_images = 2;
args.projection = 3;//PS = 1, UTM = 2
Expand All @@ -118,8 +119,9 @@ int main(int argc,char *argv[])
args.RA_only = 0;
args.focal_length = 120;
args.CCD_size = 12;

args.SGM_py = 1;
args.DS_sigma = 1.6;
args.DS_kernel = 9;

TransParam param;
param.bHemisphere = 1;
Expand Down Expand Up @@ -732,6 +734,7 @@ int main(int argc,char *argv[])

for (i=0; i<argc; i++)
{

if (strcmp("-SGMpy",argv[i]) == 0)
{
if (argc == i+1) {
Expand Down Expand Up @@ -1035,6 +1038,34 @@ int main(int argc,char *argv[])
}
}

if (strcmp("-downsample",argv[i]) == 0)
{
if (argc == i+1) {
printf("Please input the threads value\n");
cal_flag = false;
}
else
{
sprintf(args.seedDEMfilename,"%s",argv[i+1]);
printf("%s\n",args.seedDEMfilename);
sprintf(args.Outputpath_name,"%s",argv[i+2]);
printf("%s\n",args.Outputpath_name);
args.check_downsample = true;
}
}

if (strcmp("-kernel",argv[i]) == 0)
{
args.DS_kernel = atoi(argv[i+1]);
printf("%d\n",args.DS_kernel);
}

if (strcmp("-sigma",argv[i]) == 0)
{
args.DS_sigma = atof(argv[i+1]);
printf("%f\n",args.DS_sigma);
}

if (strcmp("-seed",argv[i]) == 0)
{
if(args.check_sdm_ortho == 0)
Expand Down Expand Up @@ -1503,7 +1534,11 @@ int main(int argc,char *argv[])

if(args.check_txt_input == 0)
{
if(image_count > 1 || args.check_ortho)
if(args.check_downsample)
{
DownSample(args);
}
else if(image_count > 1 || args.check_ortho)
{
args.number_of_images = image_count;

Expand Down Expand Up @@ -1639,6 +1674,62 @@ char* SetOutpathName(char *_path)

}

void DownSample(ARGINFO &args)
{
float *seeddem = NULL;
int cols[2];
int rows[2];
CSize data_size;
double minX, maxY, grid_size;

CSize seeddem_size = ReadGeotiff_info(args.seedDEMfilename, &minX, &maxY, &grid_size);

TransParam param;
bool Hemisphere;
SetTranParam_fromOrtho(&param,args.seedDEMfilename,args,&Hemisphere);


CSize *Imagesize = (CSize*)malloc(sizeof(CSize));
Imagesize->width = seeddem_size.width;
Imagesize->height = seeddem_size.height;

cols[0] = 0;
cols[1] = seeddem_size.width;

rows[0] = 0;
rows[1] = seeddem_size.height;

seeddem = Readtiff_DEM(args.seedDEMfilename,Imagesize,cols,rows,&data_size);

int downsample_step = ceil(log2(args.DEM_space/grid_size));

printf("downsample_step %d\t%d\t%f\n",downsample_step,args.DS_kernel,args.DS_sigma);


float **pyimg = (float**)malloc(sizeof(float*)*downsample_step);

CSize out_size;

for(int level = 0 ; level < downsample_step ; level ++)
{
if(level == 0)
{
pyimg[0] = CreateImagePyramid_float(seeddem,seeddem_size,args.DS_kernel,args.DS_sigma);
out_size.width = seeddem_size.width/2;
out_size.height = seeddem_size.height/2;
}
else
{
pyimg[level] = CreateImagePyramid_float(pyimg[level-1],out_size,args.DS_kernel,args.DS_sigma);
out_size.width = out_size.width/2;
out_size.height = out_size.height/2;
free(pyimg[level-1]);
}
}

WriteGeotiff(args.Outputpath_name, pyimg[downsample_step-1], out_size.width, out_size.height, args.DEM_space, minX, maxY, param.projection, param.zone, param.bHemisphere, 4);
}

int SETSMmainfunction(TransParam *return_param, char* _filename, ARGINFO args, char *_save_filepath,double **Imageparams)
{
#ifdef BUILDMPI
Expand Down Expand Up @@ -9990,6 +10081,85 @@ uint16* CreateImagePyramid(uint16* _input, CSize _img_size, int _filter_size, do
return result_img;
}

float* CreateImagePyramid_float(float* _input, CSize _img_size, int _filter_size, double _sigma)
{
//_filter_size = 7, sigma = 1.6
double sigma = _sigma;
double temp,scale;
double sum = 0;
double** GaussianFilter;
CSize result_size;
float* result_img;

GaussianFilter = (double**)malloc(sizeof(double*)*_filter_size);
for(int i=0;i<_filter_size;i++)
GaussianFilter[i] = (double*)malloc(sizeof(double)*_filter_size);


result_size.width = _img_size.width/2;
result_size.height = _img_size.height/2;
scale=sqrt(2*PI)*sigma;

result_img = (float*)malloc(sizeof(float)*result_size.height*result_size.width);

for(int i=-(int)(_filter_size/2);i<(int)(_filter_size/2)+1;i++)
{
for(int j=-(int)(_filter_size/2);j<(int)(_filter_size/2)+1;j++)
{
temp = -1*(i*i+j*j)/(2*sigma*sigma);
GaussianFilter[i+(int)(_filter_size/2)][j+(int)(_filter_size/2)]=exp(temp)/scale;
sum += exp(temp)/scale;
}
}

#pragma omp parallel for schedule(guided)
for(int i=-(int)(_filter_size/2);i<(int)(_filter_size/2)+1;i++)
{
for(int j=-(int)(_filter_size/2);j<(int)(_filter_size/2)+1;j++)
{
GaussianFilter[i+(int)(_filter_size/2)][j+(int)(_filter_size/2)]/=sum;
int GI = (int)((GaussianFilter[i+(int)(_filter_size/2)][j+(int)(_filter_size/2)] + 0.001)*1000);
GaussianFilter[i+(int)(_filter_size/2)][j+(int)(_filter_size/2)] = (double)(GI/1000.0);
}
}

#pragma omp parallel for private(temp) schedule(guided)
//for(long int ori_index=0 ; ori_index<(long)result_size.height*(long)result_size.width ; ori_index++)
for(long int r=0;r<result_size.height;r++)
{
for(long int c=0;c<result_size.width;c++)
{
temp = 0;

for(int l=0;l<_filter_size;l++)
{
for(int k=0;k<_filter_size;k++)
{
//r'->2r+m, c'->2c+n
if( (2*r + l-(int)(_filter_size/2)) >= 0 && (2*c + k-(int)(_filter_size/2)) >= 0 &&
(2*r + l-(int)(_filter_size/2)) < _img_size.height && (2*c + k-(int)(_filter_size/2)) < _img_size.width)
{
temp += GaussianFilter[l][k]*_input[(2*r + l-(int)(_filter_size/2))*_img_size.width +(2*c + k-(int)(_filter_size/2))];

}
}
}

result_img[r*result_size.width + c] = float(temp);
}
}

for(int i=0;i<_filter_size;i++)
if(GaussianFilter[i])
free(GaussianFilter[i]);

if(GaussianFilter)
free(GaussianFilter);


return result_img;
}

unsigned char* CreateImagePyramid_BYTE(unsigned char* _input, CSize _img_size, int _filter_size, double _sigma)
{
//_filter_size = 7, sigma = 1.6
Expand Down
2 changes: 2 additions & 0 deletions setsm_code.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ typedef struct nnXY
float Z;
} NNXY;

void DownSample(ARGINFO &args);
float* CreateImagePyramid_float(float* _input, CSize _img_size, int _filter_size, double _sigma);
int SETSMmainfunction(TransParam *return_param, char* _filename, ARGINFO args, char *_save_filepath,double **ImageParam);
char* SetOutpathName(char *_path);

Expand Down

0 comments on commit 22bc64a

Please sign in to comment.