void run() { std::string extension=fnStack.getExtension(); if (extension=="mrc") fnStack+=":mrcs"; else if (extension=="ali") fnStack+=":mrcs"; std::ifstream fhAngles; fhAngles.open(fnAngles.c_str()); if (!fhAngles) REPORT_ERROR(ERR_IO_NOTOPEN,fnAngles); Image<double> stack; stack.read(fnStack, HEADER); size_t xdim, ydim, zdim, ndim; stack.getDimensions(xdim, ydim, zdim, ndim); MetaData MD; FileName fnSlice; for (unsigned long slice = FIRST_IMAGE; slice <= ndim; slice++) { double angle; fhAngles >> angle; fnSlice.compose((int)slice,fnStack); size_t objId=MD.addObject(); MD.setValue(MDL_IMAGE,fnSlice,objId); MD.setValue(MDL_ANGLE_TILT,angle,objId); MD.setValue(MDL_ENABLED,1,objId); } MD.write(fnOut); }
AnimationController* AnimationControllerFactory::createDefault() { AnimationController *model = 0; const FileName fileName("data/models/cylinder/cylinder.md3xml"); MAP::iterator i = loader.find(fileName.getExtension()); if(i == loader.end() || (i != loader.end() && !(model = i->second->load(fileName, textureFactory)))) { FAIL("Failed to load default model: " + fileName.str()); return 0; } return model; }
AnimationController* AnimationControllerFactory::createDefault() { const FileName fileName("data/models/cylinder/cylinder.md3xml"); MAP::iterator i = loader.find(fileName.getExtension()); shared_ptr<ModelLoader> &modelLoader = i->second; if (i == loader.end()) { FAIL("No loader appropriate for default model: " + fileName.str()); return 0; } // Attempt to load the default model AnimationController *model = modelLoader->load(fileName, textureFactory); if (model) { return model; } else { FAIL("Failed to load default model: " + fileName.str()); return 0; } }
bool checkImageFileSize(const FileName &name, const ImageInfo &imgInfo, bool error) { FileName ext = name.getExtension(); FileName dataFname; if ( ext.contains("hed")) dataFname = name.removeLastExtension().addExtension("img"); else if (ext.contains("inf")) dataFname = name.removeLastExtension(); else if (ext.contains("tif") || ext.contains("jpg") || ext.contains("hdf") || ext.contains("h5")) return true; else dataFname = name; size_t expectedSize = imgInfo.adim.nzyxdim*gettypesize(imgInfo.datatype) + imgInfo.offset; size_t actualSize = dataFname.removeAllPrefixes().removeFileFormat().getFileSize(); bool result = (actualSize >= expectedSize); if (error && !result) REPORT_ERROR(ERR_IO_SIZE, formatString("Image Extension: File %s has wrong size.\n" "Expected size (at least) %u bytes. Actual size %u bytes.", name.c_str(), expectedSize, actualSize)); return result; }
void run() { FileName fn_out; FileName fn_ext = fn_in.getExtension(); Image<double> I, label; I.read(fn_in); int object_no; if (ZSIZE(I())==1) object_no=labelImage2D(I(), label()); else object_no=labelImage3D(I(), label()); for (int o = 0; o <= object_no; o++) { I() = label(); MultidimArray<double> &Im=MULTIDIM_ARRAY(I); FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(Im) { DIRECT_MULTIDIM_ELEM(Im,n) = (DIRECT_MULTIDIM_ELEM(Im,n) == o); if (invert) DIRECT_MULTIDIM_ELEM(Im,n) = 1 - DIRECT_MULTIDIM_ELEM(Im,n); } double number_elements = I().sum(); if (number_elements > min_size) { fn_out.compose(fn_root, o+1, fn_ext); I.write(fn_out); } if (ZSIZE(I())==1) std::cout << "Image number " << o+1 << " contains " << number_elements << " pixels set to 1\n"; else std::cout << "Volume number " << o+1 << " contains " << number_elements << " voxels set to 1\n"; } }
int main(int argc, char *argv[]) { if(argc > 1) { if(!strcmp(argv[1], "-h")) { cout << "Paker - Compressão de ficheiros" << endl; cout << endl << "Para empacotar use:" << endl; cout << endl << argv[0] << " rle|lzss|mix repetition_bits|[position_bits coincidence_bits] <filename>" << endl; cout << endl << "ou para desempacotar:" << endl; cout << endl << argv[0] << " -u <filename>" << endl; cout << endl << "Para comprimir escolher um dos seguintes modos:" << endl; cout << endl << "\trle - Utilizar a te'cnica RLE sobre o ficheiro, "; cout << "e' necessa'rio fornecer o valore de repetition_bits (valores 4 por defeito)." << endl; cout << endl << "\tlzss - Utilizar a te'cnica LZSS sobre o ficheiro, "; cout << "e' necessa'rio fornecer os valores de position_bits e coincidence_bits (valores 10 e 4 por defeito)." << endl; cout << endl << "\tmix - Utilizar a te'cnica LZSS em conjunto com RLE sobre o ficheiro, "; cout << "e' necessa'rio fornecer os valores de repetition_bits, position_bits e coincidence_bits (valores 4, 10 e 4 por defeito)." << endl; cout << endl << endl << "ISEL 2008 - Fa'bio Oliveira [30979] e Renato Verissi'mo [30796]" << endl; return 0; } else if(!strcmp(argv[1], "-u")) { if (argc < 3) { cout << "Use " << argv[0] << " -h para obter ajuda" << endl; return 1; } UnPk upk(argv[argc - 1]); int error = upk.init(); if(error) return error; upk.go(); } else { if (argc < 4) { cout << "Use " << argv[0] << " -h para obter ajuda" << endl; return 1; } FileName iname = argv[argc - 1]; FileName oname = iname.getBaseName(); oname.addExtension(pk_ext); Pk pk(iname, oname); int error = pk.init(); if(error) return error; pk.go(argc - 2, argv + 1, iname.getExtension()); } } else { cout << "Use " << argv[0] << " -h para obter ajuda" << endl; return 1; } }
int main2() { MultidimArray<double> preImg, avgCurr, mappedImg; MultidimArray<double> outputMovie; Matrix1D<double> meanStdev; ImageGeneric movieStack; Image<double> II; MetaData MD; // To save plot information FileName motionInfFile, flowFileName, flowXFileName, flowYFileName; ArrayDim aDim; // For measuring times (both for whole process and for each level of the pyramid) clock_t tStart, tStart2; #ifdef GPU // Matrix that we required in GPU part GpuMat d_flowx, d_flowy, d_dest; GpuMat d_avgcurr, d_preimg; #endif // Matrix required by Opencv cv::Mat flow, dest, flowx, flowy; cv::Mat flowxPre, flowyPre; cv::Mat avgcurr, avgstep, preimg, preimg8, avgcurr8; cv::Mat planes[]={flowxPre, flowyPre}; int imagenum, cnt=2, div=0, flowCounter; int h, w, levelNum, levelCounter=1; motionInfFile=foname.replaceExtension("xmd"); std::string extension=fname.getExtension(); if (extension=="mrc") fname+=":mrcs"; movieStack.read(fname,HEADER); movieStack.getDimensions(aDim); imagenum = aDim.ndim; h = aDim.ydim; w = aDim.xdim; if (darkImageCorr) { II.read(darkRefFilename); darkImage=II(); } if (gainImageCorr) { II.read(gianRefFilename); gainImage=II(); } meanStdev.initZeros(4); //avgcurr=cv::Mat::zeros(h, w,CV_32FC1); avgCurr.initZeros(h, w); flowxPre=cv::Mat::zeros(h, w,CV_32FC1); flowyPre=cv::Mat::zeros(h, w,CV_32FC1); #ifdef GPU // Object for optical flow FarnebackOpticalFlow d_calc; setDevice(gpuDevice); // Initialize the parameters for optical flow structure d_calc.numLevels=6; d_calc.pyrScale=0.5; d_calc.fastPyramids=true; d_calc.winSize=winSize; d_calc.numIters=1; d_calc.polyN=5; d_calc.polySigma=1.1; d_calc.flags=0; #endif // Initialize the stack for the output movie if (saveCorrMovie) outputMovie.initZeros(imagenum, 1, h, w); // Correct for global motion from a cross-correlation based algorithms if (globalShiftCorr) { Matrix1D<double> shiftMatrix(2); shiftVector.reserve(imagenum); shiftMD.read(globalShiftFilename); FOR_ALL_OBJECTS_IN_METADATA(shiftMD) { shiftMD.getValue(MDL_SHIFT_X, XX(shiftMatrix), __iter.objId); shiftMD.getValue(MDL_SHIFT_Y, YY(shiftMatrix), __iter.objId); shiftVector.push_back(shiftMatrix); } } tStart2=clock(); // Compute the average of the whole stack fstFrame++; // Just to adapt to Li algorithm lstFrame++; // Just to adapt to Li algorithm if (lstFrame>=imagenum || lstFrame==1) lstFrame=imagenum; imagenum=lstFrame-fstFrame+1; levelNum=sqrt(double(imagenum)); computeAvg(fname, fstFrame, lstFrame, avgCurr); // if the user want to save the PSD if (doAverage) { II()=avgCurr; II.write(foname); return 0; } xmipp2Opencv(avgCurr, avgcurr); cout<<"Frames "<<fstFrame<<" to "<<lstFrame<<" under processing ..."<<std::endl; while (div!=groupSize) { div=int(imagenum/cnt); // avgStep to hold the sum of aligned frames of each group at each step avgstep=cv::Mat::zeros(h, w,CV_32FC1); cout<<"Level "<<levelCounter<<"/"<<levelNum<<" of the pyramid is under processing"<<std::endl; // Compute time for each level tStart = clock(); // Check if we are in the final step if (div==1) cnt=imagenum; flowCounter=1; for (int i=0;i<cnt;i++) { //Just compute the average in the last step if (div==1) { if (globalShiftCorr) { Matrix1D<double> shiftMatrix(2); MultidimArray<double> frameImage; movieStack.readMapped(fname,i+1); movieStack().getImage(frameImage); if (darkImageCorr) frameImage-=darkImage; if (gainImageCorr) frameImage/=gainImage; XX(shiftMatrix)=XX(shiftVector[i]); YY(shiftMatrix)=YY(shiftVector[i]); translate(BSPLINE3, preImg, frameImage, shiftMatrix, WRAP); } else { movieStack.readMapped(fname,fstFrame+i); movieStack().getImage(preImg); if (darkImageCorr) preImg-=darkImage; if (gainImageCorr) preImg/=gainImage; } xmipp2Opencv(preImg, preimg); } else { if (i==cnt-1) computeAvg(fname, i*div+fstFrame, lstFrame, preImg); else computeAvg(fname, i*div+fstFrame, (i+1)*div+fstFrame-1, preImg); } xmipp2Opencv(preImg, preimg); // Note: we should use the OpenCV conversion to use it in optical flow convert2Uint8(avgcurr,avgcurr8); convert2Uint8(preimg,preimg8); #ifdef GPU d_avgcurr.upload(avgcurr8); d_preimg.upload(preimg8); if (cnt==2) d_calc(d_avgcurr, d_preimg, d_flowx, d_flowy); else { flowXFileName=foname.removeLastExtension()+formatString("flowx%d%d.txt",div*2,flowCounter); flowYFileName=foname.removeLastExtension()+formatString("flowy%d%d.txt",div*2,flowCounter); readMat(flowXFileName.c_str(), flowx); readMat(flowYFileName.c_str(), flowy); d_flowx.upload(flowx); d_flowy.upload(flowy); d_calc.flags=cv::OPTFLOW_USE_INITIAL_FLOW; d_calc(d_avgcurr, d_preimg, d_flowx, d_flowy); } d_flowx.download(planes[0]); d_flowy.download(planes[1]); d_avgcurr.release(); d_preimg.release(); d_flowx.release(); d_flowy.release(); #else if (cnt==2) calcOpticalFlowFarneback(avgcurr8, preimg8, flow, 0.5, 6, winSize, 1, 5, 1.1, 0); else { flowFileName=foname.removeLastExtension()+formatString("flow%d%d.txt",div*2,flowCounter); readMat(flowFileName.c_str(), flow); calcOpticalFlowFarneback(avgcurr8, preimg8, flow, 0.5, 6, winSize, 1, 5, 1.1, cv::OPTFLOW_USE_INITIAL_FLOW); } split(flow, planes); #endif // Save the flows if we are in the last step if (div==groupSize) { if (i > 0) { std_dev2(planes,flowxPre,flowyPre,meanStdev); size_t id=MD.addObject(); MD.setValue(MDL_OPTICALFLOW_MEANX, double(meanStdev(0)), id); MD.setValue(MDL_OPTICALFLOW_MEANY, double(meanStdev(2)), id); MD.setValue(MDL_OPTICALFLOW_STDX, double(meanStdev(1)), id); MD.setValue(MDL_OPTICALFLOW_STDY, double(meanStdev(3)), id); MD.write(motionInfFile, MD_APPEND); } planes[0].copyTo(flowxPre); planes[1].copyTo(flowyPre); } else { #ifdef GPU flowXFileName=foname.removeLastExtension()+formatString("flowx%d%d.txt",div,i+1); flowYFileName=foname.removeLastExtension()+formatString("flowy%d%d.txt",div,i+1); saveMat(flowXFileName.c_str(), planes[0]); saveMat(flowYFileName.c_str(), planes[1]); #else flowFileName=foname.removeLastExtension()+formatString("flow%d%d.txt",div,i+1); saveMat(flowFileName.c_str(), flow); #endif if ((i+1)%2==0) flowCounter++; } for( int row = 0; row < planes[0].rows; row++ ) for( int col = 0; col < planes[0].cols; col++ ) { planes[0].at<float>(row,col) += (float)col; planes[1].at<float>(row,col) += (float)row; } cv::remap(preimg, dest, planes[0], planes[1], cv::INTER_CUBIC); if (div==1 && saveCorrMovie) { mappedImg.aliasImageInStack(outputMovie, i); opencv2Xmipp(dest, mappedImg); } avgstep+=dest; } avgcurr=avgstep/cnt; cout<<"Processing level "<<levelCounter<<"/"<<levelNum<<" has been finished"<<std::endl; printf("Processing time: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC); cnt*=2; levelCounter++; } opencv2Xmipp(avgcurr, avgCurr); II() = avgCurr; II.write(foname); printf("Total Processing time: %.2fs\n", (double)(clock() - tStart2)/CLOCKS_PER_SEC); if (saveCorrMovie) { II()=outputMovie; II.write(foname.replaceExtension("mrcs")); } // Release the memory avgstep.release(); preimg.release(); avgcurr8.release(); preimg8.release(); flow.release(); planes[0].release(); planes[1].release(); flowxPre.release(); flowyPre.release(); movieStack.clear(); preImg.clear(); avgCurr.clear(); II.clear(); return 0; }