main.cpp 5.32 KB
#include <iostream>
#include <fstream>
#include <cuda_runtime.h>
#include <stim/math/vector.h>
#include <stim/parser/arguments.h>
#include <stim/parser/filename.h>
#include <stim/grids/image_stack.h>
#include <stim/grids/grid.h>
#include <stim/visualization/colormap.h>
#include <stim/image/image.h>
#define pi	3.14159


void ivote3(float* center, float* img, float std[], float anisotropy, float phi, float d_phi, unsigned int r[], int iter, float t, unsigned int conn[], 
			unsigned int x, unsigned int y, unsigned int z);

void invert_data(float* cpuI, unsigned int x, unsigned int y, unsigned int z){
		for(int ix = 0; ix < x; ix++){
			for (int iy = 0; iy < y; iy++){
				for (int iz = 0; iz < z; iz++){
					int idx = iz * x * y + iy * x + ix;
					cpuI[idx] = 255 - cpuI[idx];
				}
			}
		}
	}
	
int main(int argc, char** argv){


	cudaDeviceProp prop;
	int count;
	cudaGetDeviceCount(&count);
	//printf("cudadevicecount: %i\n", count);
	for (int i=0; i<count; i++){
		cudaGetDeviceProperties(&prop, i);
		printf("current device ID: %d\n", i);
		printf("device name: %s\n", prop.name);
		printf("total global mem: %lu\n", prop.totalGlobalMem);
	}
	
	
	
	//output advertisement
	std::cout<<std::endl<<std::endl;
	std::cout<<"========================================================================="<<std::endl;
	std::cout<<"Thank you for using the ivote3 segmentation tool!"<<std::endl;
	std::cout<<"Scalable Tissue Imaging and Modeling (STIM) Lab, University of Houston"<<std::endl;
	std::cout<<"Developers: Laila Saadatifard and David Mayerich"<<std::endl;
	std::cout<<"Source: https://git.stim.ee.uh.edu/segmentation/ivote3"<<std::endl;
	std::cout<<"========================================================================="<<std::endl<<std::endl;

	stim::arglist args;

#ifdef _WIN32
	args.set_ansi(false);
#endif

	//add arduments
	args.add("help", "prints this help");
	args.add("x", "size of the dataset along X axis", "positive value");
	args.add("y", "size of the dataset along Y axis", "positive value");
	args.add("z", "size of the dataset along Z axis", "positive value");
	args.add("t", "threshold value for the final result", "positive valu");
	args.add("invert", "to invert the input data set", "string");
	args.add("anisotropy", "anisotropy value of the imaging", "positive value");
	//parse the command line arguments.
	args.parse(argc, argv);

	//display the help text if requested
	if(args["help"].is_set()){				
		std::cout<<std::endl<<"usage: ivote input_image output_list --option [A B C ...]"<<std::endl;
		std::cout<<std::endl<<std::endl
				  << "examples: ivote blue.bmp list.txt "<<std::endl;
		
		std::cout<<std::endl<<std::endl;
		std::cout<<args.str()<<std::endl;
		exit(1);
	}

	//if the input and output files aren't specified, throw an error and exit
	if(args.nargs() < 2){
		std::cout<<"ERROR: two files must be specified for segmentation, enter ivote --help for options."<<std::endl<<std::endl;
		exit(1);
	}

	//get the input image file
	stim::filename Ifilename(args.arg(0));

	//get the output file name
	stim::filename OutName(args.arg(1));

	//set the x, y, z.
	int x = args["x"].as_int();
	int y = args["y"].as_int();
	int z = args["z"].as_int();

	//set the threshold.
	float t = args["t"].as_float();
	//set the anisotropy
	float anisotropy =  args["anisotropy"].as_float();
	unsigned int rmax = 10 ;
	unsigned int r[3] = { rmax, rmax, rmax};
	float std = 5;
	float sigma[3] = { std, std, std};
	unsigned int nlmax = 5;
	unsigned int conn[3] = { nlmax, nlmax, nlmax};
	float phi_deg = 25.0;
	float phi = phi_deg * pi /180;
	int iter = 8;
	float d_phi = phi/(iter+2);
	
	std::string filename = Ifilename.str();
	unsigned int bytes = x*y*z*sizeof(float);

	//allocate space on the cpu for the input data
	float* cpuI = (float*) malloc(bytes);

	//load the input file into the cpuI
	std::ifstream nissl(filename, std::ios::in | std::ios::binary);
	nissl.read((char*)cpuI, bytes);
	nissl.close();
	if(args["invert"].is_set())
		invert_data(cpuI, x, y, z);
	
	//write a new file from the cpuI.
	std::ofstream original("std5.5-r10.10-v8/inv-128.vol", std::ofstream::out | std::ofstream::binary);
	original.write((char*)cpuI, bytes);
	original.close();
	
	//allocate space on the cpu for the output result
	float* cpu_out = (float*) malloc(bytes*3);
	
	// call the ivote function
	ivote3(cpu_out, cpuI, sigma, anisotropy, phi, d_phi, r, iter, t, conn, x, y, z);
	
	//write the blurred file from the cpuI.
	std::ofstream fblur("vote-check/vote8.vol", std::ofstream::out | std::ofstream::binary);
	fblur.write((char*)cpuI, bytes);
	fblur.close();
	/*
	stim::image<float>imgrad3;
	imgrad3.set_interleaved3(cpu_out, 128,128,128,3);
	std::ofstream fgx("syn/gx-128.vol", std::ofstream::out | std::ofstream::binary);
	fgx.write((char*)imgrad3.channel(0).data(), bytes);
	fgx.close();
	*/
	//write the output file.
	std::ofstream fo("std5.5-r10.10-v8/" + OutName.str(), std::ofstream::out | std::ofstream::binary);
	fo.write((char*)cpu_out, bytes);
	fo.close();
	
	// creat a file for saving the list centers
 
		std::ofstream list("std5.5-r10.10-v8/center.txt");
		if (list.is_open()){

				for (int ix=0; ix<x; ix++){
					for (int iy=0; iy<y; iy++){
						for (int iz=0; iz<z; iz++){

							int idx = iz * x * y + iy * x + ix;
							if (cpu_out[idx]==1){
						list << ix << "\t" << iy << "\t"<< iz << '\n' ;
					
							}
						}
					}
				}

		list.close();
		}
	
		cudaDeviceReset();       
	
}