Monday 15 September 2014

optimization - writing ITK label data to file in the fastest possible way -



optimization - writing ITK label data to file in the fastest possible way -

i stuck in nasty optimization problem in ct analysis software. utilize itk perform heavy filtering on data, , using itk binaryimagetoshapelabelmapfilter isolate separate regions , save coordinates of each point belonging them text file (that going input of separate software made)

the problem is, phase of writing file, takes, in case of big regions, much longer filtering (i happened have datasets filtered in 30 minutes , wrote file in additional hour).

in next code, tried avoid flushing @ end of each line ("\n" instead endl) nil seems change. have thought on how massively improve write file of data?

regards, emiliano

ofstream outfile; string labelsfile = subfolderpath+"/regionlabels_"+volumecompletename+".lri"; outfile.open(labelsfile.c_str()); outfile << "labelindexes - v1.0" << "\n"; outfile << "dataset origin : "<< originx << "," << originy << "," << originz << "\n"; int cloud = 0; for(unsigned int = 0; < binaryimagetoshapelabelmapfilter->getoutput()->getnumberoflabelobjects(); i++){ binaryimagetoshapelabelmapfiltertype::outputimagetype::labelobjecttype* labelobject = binaryimagetoshapelabelmapfilter->getoutput()->getnthlabelobject(i); if(labelobject->getnumberofpixels()>4500){ outfile << "region " << cloud << "\n"; outfile << "centroid " << labelobject->getcentroid() << "\n"; outfile << labelobject->getnumberofpixels() << "\n"; for(int j=0;j<labelobject->size();j++){ //only save labels voxels @ to the lowest degree 2 voxels distant border of dataset if(labelobject->getindex(j)[0]>2 && labelobject->getindex(j)[1]>2 && labelobject->getindex(j)[2]>2){ if(labelobject->getindex(j)[0]<(maxx-2) && labelobject->getindex(j)[1]<(maxy-2) && labelobject->getindex(j)[2]<(maxz-2)){ outfile << labelobject->getindex(j) << "\n"; } } } cloud++; } } outfile << "endoffile" << "\n"; outfile.flush(); outfile.close(); cout << "all labels indices saved file : " << labelsfile << endl;

one lastly additional question. didn't have time seek dataset filtered older method, lastly 2 filtered new 1 seems half of each part beingness lost. misusing indices using :

file* fout = fopen(labelsfile.c_str(), "w"); fprintf(fout,"labelindexes - v1.0\n"); fprintf(fout,"dataset origin : %d,%d,%d\n",originx,originy,originz); for(unsigned int = 0; < binaryimagetoshapelabelmapfilter->getoutput()->getnumberoflabelobjects(); i++){ binaryimagetoshapelabelmapfiltertype::outputimagetype::labelobjecttype* labelobject = binaryimagetoshapelabelmapfilter->getoutput()->getnthlabelobject(i); if(labelobject->getnumberofpixels()>4500){ fprintf(fout,"region %d \n",cloud); double c1 = labelobject->getcentroid()[0]; double c2 = labelobject->getcentroid()[0]; double c3 = labelobject->getcentroid()[0]; fprintf(fout,"centroid [%f, %f, %f]\n",c1,c2,c3); fprintf(fout,"%u\n",(long)labelobject->getnumberofpixels()); (int l = 0; l < labelobject->getnumberoflines(); ++l){ labelobjectline<3> line = labelobject->getline(l); outputimagetype::indextype startindex = line.getindex(); int i1 = startindex[0]; int i2 = startindex[1]; int i3 = startindex[2]; if(i1>2 && i2>2 && i3>2 && i1<(maxx-2) && i2<(maxy-2) && i3<(maxz-2))fprintf(fout,"[%d, %d, %d]\n",i1,i2,i3); } cloud++; } }

did seek profiling code? tell computation time going.

however, think it's going numerous calls labelobject::getindex. if @ implementation of labelobject::getindex can see it's on order of number of lines in label object. calling 7 time per index. save results of function phone call variable give 7x speedup.

however lets computational cost of loop! loop on number of indexes in label object, , multiplication factor of getindex call. results in order of computation beingness grater number of indexes in label object.

a improve approach utilize labelobject::getline method , line based algorithm:

(l = 0; l < labelobject.getnumberoflines(); ++l) line = labelobject.getline(l) startindex = line.getindex(); endindex = startindex; endindex[0] = endindex[0] + line.getlength() - 1 // determine indexes print.

the cut down computational complexity downwards the number of lines. not surprise me if took less min apporach.

file optimization ofstream itk

No comments:

Post a Comment