void processImage(const FileName &fnImg, const FileName &fnImgOut, const MDRow &rowIn, MDRow &rowOut) { // Read input volume Image<double> volume; volume.read(fnImg); volume().setXmippOrigin(); mask_prm.generate_mask(volume()); // Compute center of mass Matrix1D<double> centerOfMass; volume().centerOfMass(centerOfMass, &mask_prm.get_binary_mask()); // Move origin to that center of mass selfTranslate(BSPLINE3,volume(),-centerOfMass, DONT_WRAP); volume.write(fnImgOut); }
void processImage(const FileName &fnImg, const FileName &fnImgOut, const MDRow &rowIn, MDRow &rowOut) { double amin = rnd_gaus(0, sigma); double amax = rnd_gaus(0, sigma); double minval = min_val + amin; double maxval = max_val + amax; Im.read(fnImg); MultidimArray<double> &ImP = Im(); ImP.setXmippOrigin(); if (mask_prm.type == NO_MASK) ImP.rangeAdjust(minval, maxval); else { mask_prm.generate_mask(ImP); ImP.rangeAdjust(minval, maxval,mask_prm.get_binary_mask()); } Im.write(fnImgOut); }
void run () { mask.allowed_data_types = INT_MASK; // Main program ========================================================= params.V1.read(fn1); params.V1().setXmippOrigin(); params.V2.read(fn2); params.V2().setXmippOrigin(); // Initialize best_fit double best_fit = 1e38; Matrix1D<double> best_align(8); bool first = true; // Generate mask if (mask_enabled) { mask.generate_mask(params.V1()); params.mask_ptr = &(mask.get_binary_mask()); } else params.mask_ptr = NULL; // Exhaustive search if (!usePowell && !useFRM) { // Count number of iterations int times = 1; if (!tell) { if (grey_scale0 != grey_scaleF) times *= FLOOR(1 + (grey_scaleF - grey_scale0) / step_grey); if (grey_shift0 != grey_shiftF) times *= FLOOR(1 + (grey_shiftF - grey_shift0) / step_grey_shift); if (rot0 != rotF) times *= FLOOR(1 + (rotF - rot0) / step_rot); if (tilt0 != tiltF) times *= FLOOR(1 + (tiltF - tilt0) / step_tilt); if (psi0 != psiF) times *= FLOOR(1 + (psiF - psi0) / step_psi); if (scale0 != scaleF) times *= FLOOR(1 + (scaleF - scale0) / step_scale); if (z0 != zF) times *= FLOOR(1 + (zF - z0) / step_z); if (y0 != yF) times *= FLOOR(1 + (yF - y0) / step_y); if (x0 != xF) times *= FLOOR(1 + (xF - x0) / step_x); init_progress_bar(times); } else std::cout << "#grey_factor rot tilt psi scale z y x fitness\n"; // Iterate int itime = 0; int step_time = CEIL((double)times / 60.0); Matrix1D<double> r(3); Matrix1D<double> trial(9); for (double grey_scale = grey_scale0; grey_scale <= grey_scaleF ; grey_scale += step_grey) for (double grey_shift = grey_shift0; grey_shift <= grey_shiftF ; grey_shift += step_grey_shift) for (double rot = rot0; rot <= rotF ; rot += step_rot) for (double tilt = tilt0; tilt <= tiltF ; tilt += step_tilt) for (double psi = psi0; psi <= psiF ; psi += step_psi) for (double scale = scale0; scale <= scaleF ; scale += step_scale) for (ZZ(r) = z0; ZZ(r) <= zF ; ZZ(r) += step_z) for (YY(r) = y0; YY(r) <= yF ; YY(r) += step_y) for (XX(r) = x0; XX(r) <= xF ; XX(r) += step_x) { // Form trial vector trial(0) = grey_scale; trial(1) = grey_shift; trial(2) = rot; trial(3) = tilt; trial(4) = psi; trial(5) = scale; trial(6) = ZZ(r); trial(7) = YY(r); trial(8) = XX(r); // Evaluate double fit = fitness(MATRIX1D_ARRAY(trial)); // The best? if (fit < best_fit || first) { best_fit = fit; best_align = trial; first = false; if (tell) std::cout << "Best so far\n"; } // Show fit if (tell) std::cout << trial << " " << fit << std::endl; else if (++itime % step_time == 0) progress_bar(itime); } if (!tell) progress_bar(times); } else if (usePowell) { // Use Powell optimization Matrix1D<double> x(9), steps(9); double fitness; int iter; steps.initConstant(1); if (onlyShift) steps(0)=steps(1)=steps(2)=steps(3)=steps(4)=steps(5)=0; if (params.alignment_method == COVARIANCE) steps(0)=steps(1)=0; x(0)=grey_scale0; x(1)=grey_shift0; x(2)=rot0; x(3)=tilt0; x(4)=psi0; x(5)=scale0; x(6)=z0; x(7)=y0; x(8)=x0; powellOptimizer(x,1,9,&wrapperFitness,NULL,0.01,fitness,iter,steps,true); best_align=x; best_fit=fitness; first=false; } else if (useFRM) { String scipionPython; initializeScipionPython(scipionPython); PyObject * pFunc = getPointerToPythonFRMFunction(); double rot,tilt,psi,x,y,z,score; Matrix2D<double> A; alignVolumesFRM(pFunc, params.V1(), params.V2(), Py_None, rot,tilt,psi,x,y,z,score,A,maxShift,maxFreq,params.mask_ptr); best_align.initZeros(9); best_align(0)=1; // Gray scale best_align(1)=0; // Gray shift best_align(2)=rot; best_align(3)=tilt; best_align(4)=psi; best_align(5)=1; // Scale best_align(6)=z; best_align(7)=y; best_align(8)=x; best_fit=-score; } if (!first) std::cout << "The best correlation is for\n" << "Scale : " << best_align(5) << std::endl << "Translation (X,Y,Z) : " << best_align(8) << " " << best_align(7) << " " << best_align(6) << std::endl << "Rotation (rot,tilt,psi): " << best_align(2) << " " << best_align(3) << " " << best_align(4) << std::endl << "Best grey scale : " << best_align(0) << std::endl << "Best grey shift : " << best_align(1) << std::endl << "Fitness value : " << best_fit << std::endl; Matrix1D<double> r(3); XX(r) = best_align(8); YY(r) = best_align(7); ZZ(r) = best_align(6); Matrix2D<double> A,Aaux; Euler_angles2matrix(best_align(2), best_align(3), best_align(4), A, true); translation3DMatrix(r,Aaux); A = A * Aaux; scale3DMatrix(vectorR3(best_align(5), best_align(5), best_align(5)),Aaux); A = A * Aaux; if (verbose!=0) std::cout << "xmipp_transform_geometry will require the following values" << "\n Angles: " << best_align(2) << " " << best_align(3) << " " << best_align(4) << "\n Shifts: " << A(0,3) << " " << A(1,3) << " " << A(2,3) << std::endl; if (apply) { applyTransformation(params.V2(),params.Vaux(),MATRIX1D_ARRAY(best_align)); params.V2()=params.Vaux(); params.V2.write(fnOut); } }