void subtractBackgroundRamp(Image<DOUBLE> &I, int bg_radius) { int bg_radius2 = bg_radius * bg_radius; fit_point3D point; std::vector<fit_point3D> allpoints; DOUBLE pA, pB, pC, avgbg, stddevbg, minbg, maxbg; if (I().getDim() == 3) REPORT_ERROR("ERROR %% calculateBackgroundRamp is not implemented for 3D data!"); FOR_ALL_ELEMENTS_IN_ARRAY2D(I()) { if (i*i + j*j > bg_radius2) { point.x = j; point.y = i; point.z = A2D_ELEM(I(), i, j); point.w = 1.; allpoints.push_back(point); } } fitLeastSquaresPlane(allpoints, pA, pB, pC); // Substract the plane from the image FOR_ALL_ELEMENTS_IN_ARRAY2D(I()) { A2D_ELEM(I(), i, j) -= pA * j + pB * i + pC; } }
TEST( MultidimTest, getRealFromComplex) { MultidimArray<std::complex <double> > mSource(2,2); MultidimArray<double> mdTarget; A2D_ELEM(mSource,0,0) = (std::complex<double>(0,0)); A2D_ELEM(mSource,1,0) = (std::complex<double>(1,0)); A2D_ELEM(mSource,0,1) = (std::complex<double>(2,0)); A2D_ELEM(mSource,1,1) = (std::complex<double>(3,0)); mSource.getReal(mdTarget); EXPECT_EQ(DIRECT_MULTIDIM_ELEM(mSource, 0).real(),DIRECT_MULTIDIM_ELEM(mdTarget,0)); EXPECT_EQ(DIRECT_MULTIDIM_ELEM(mSource, 1).real(),DIRECT_MULTIDIM_ELEM(mdTarget,1)); EXPECT_EQ(DIRECT_MULTIDIM_ELEM(mSource, 2).real(),DIRECT_MULTIDIM_ELEM(mdTarget,2)); EXPECT_EQ(DIRECT_MULTIDIM_ELEM(mSource, 3).real(),DIRECT_MULTIDIM_ELEM(mdTarget,3)); }
//#define DEBUG blobtype best_blob(double alpha_0, double alpha_F, double inc_alpha, double a_0, double a_F, double inc_a, double w, double *target_att, int target_length) { blobtype retval; retval.order = 2; int alpha_size = FLOOR((alpha_F - alpha_0) / inc_alpha + 1); int a_size = FLOOR((a_F - a_0) / inc_a + 1); Image<double> att, ops; att().resize(alpha_size, a_size); ops().resize(alpha_size, a_size); int i, j; double a, alpha, best_a = -1, best_alpha = -1; double best_ops = 1e10, best_att = 0; for (i = 0, alpha = alpha_0; i < alpha_size; alpha += inc_alpha, i++) for (j = 0, a = a_0; j < a_size; a += inc_a, j++) { retval.radius = a; retval.alpha = alpha; A2D_ELEM(att(), i, j) = blob_att(w, retval); A2D_ELEM(ops(), i, j) = blob_ops(w, retval); if (j > 0) for (int n = target_length - 1; n >= 0; n--) if (A2D_ELEM(att(), i, j - 1) > target_att[n] && A2D_ELEM(att(), i, j) < target_att[n]) { A2D_ELEM(att(), i, j) = 0; if (A2D_ELEM(ops(), i, j - 1) < best_ops && A2D_ELEM(att(), i, j - 1) >= best_att) { best_alpha = alpha; best_a = a - inc_a; best_ops = A2D_ELEM(ops(), i, j - 1); best_att = target_att[n]; } } } #ifdef DEBUG att.write((std::string)"att" + floatToString(w) + ".xmp"); ops.write((std::string)"ops" + floatToString(w) + ".xmp"); #endif retval.radius = best_a; retval.alpha = best_alpha; return retval; }
void CTF::getCenteredImage(MultidimArray<DOUBLE> &result, DOUBLE Tm, bool do_abs, bool do_only_flip_phases, bool do_intact_until_first_peak, bool do_damping) { result.setXmippOrigin(); DOUBLE xs = (DOUBLE)XSIZE(result) * Tm; DOUBLE ys = (DOUBLE)YSIZE(result) * Tm; FOR_ALL_ELEMENTS_IN_ARRAY2D(result) { DOUBLE x = (DOUBLE)j / xs; DOUBLE y = (DOUBLE)i / ys; A2D_ELEM(result, i, j) = getCTF(x, y, do_abs, do_only_flip_phases, do_intact_until_first_peak, do_damping); } }
//#define DEBUG void normalize_tomography(MultidimArray<double> &I, double tilt, double &mui, double &sigmai, bool tiltMask, bool tomography0, double mu0, double sigma0) { // Tilt series are always 2D I.checkDimension(2); const int L=2; // Build a mask using the tilt angle I.setXmippOrigin(); MultidimArray<int> mask; mask.initZeros(I); int Xdimtilt=(int)XMIPP_MIN(FLOOR(0.5*(XSIZE(I)*cos(DEG2RAD(tilt)))), 0.5*(XSIZE(I)-(2*L+1))); int N=0; for (int i=STARTINGY(I); i<=FINISHINGY(I); i++) for (int j=-Xdimtilt; j<=Xdimtilt;j++) { A2D_ELEM(mask,i,j)=1; N++; } // Estimate the local variance MultidimArray<double> localVariance; localVariance.initZeros(I); double meanVariance=0; FOR_ALL_ELEMENTS_IN_ARRAY2D(I) { // Center a mask of size 5x5 and estimate the variance within the mask double meanPiece=0, variancePiece=0; double Npiece=0; for (int ii=i-L; ii<=i+L; ii++) { if (INSIDEY(I,ii)) for (int jj=j-L; jj<=j+L; jj++) { if (INSIDEX(I,jj)) { double pixval=A2D_ELEM(I,ii,jj); meanPiece+=pixval; variancePiece+=pixval*pixval; ++Npiece; } } } meanPiece/=Npiece; variancePiece=variancePiece/(Npiece-1)- Npiece/(Npiece-1)*meanPiece*meanPiece; A2D_ELEM(localVariance,i,j)=variancePiece; meanVariance+=variancePiece; } meanVariance*=1.0/N; // Test the hypothesis that the variance in this piece is // the same as the variance in the whole image double iFu=1/icdf_FSnedecor(4*L*L+4*L,N-1,0.975); double iFl=1/icdf_FSnedecor(4*L*L+4*L,N-1,0.025); double iMeanVariance=1.0/meanVariance; FOR_ALL_ELEMENTS_IN_ARRAY2D(localVariance) { if (A2D_ELEM(localVariance,i,j)==0) A2D_ELEM(mask,i,j)=-2; if (A2D_ELEM(mask,i,j)==1) { double ratio=A2D_ELEM(localVariance,i,j)*iMeanVariance; double thl=ratio*iFu; double thu=ratio*iFl; if (thl>1 || thu<1) A2D_ELEM(mask,i,j)=-1; } } #ifdef DEBUG Image<double> save; save()=I; save.write("PPP.xmp"); save()=localVariance; save.write("PPPLocalVariance.xmp"); Image<int> savemask; savemask()=mask; savemask.write("PPPmask.xmp"); #endif // Compute the statistics again in the reduced mask double avg, stddev, min, max; computeStats_within_binary_mask(mask, I, min, max, avg, stddev); double cosTilt=cos(DEG2RAD(tilt)); double iCosTilt=1.0/cosTilt; if (tomography0) { double iadjustedStddev=1.0/(sigma0*cosTilt); FOR_ALL_ELEMENTS_IN_ARRAY2D(I) switch (A2D_ELEM(mask,i,j)) { case -2: A2D_ELEM(I,i,j)=0; break; case 0: if (tiltMask) A2D_ELEM(I,i,j)=0; else A2D_ELEM(I,i,j)=(A2D_ELEM(I,i,j)*iCosTilt-mu0)*iadjustedStddev; break; case -1: case 1: A2D_ELEM(I,i,j)=(A2D_ELEM(I,i,j)*iCosTilt-mu0)*iadjustedStddev; break; } } else {
TEST( MultidimTest, modulo) { MultidimArray<double> mSource(3,3); MultidimArray<double> mTarget(3,3); A2D_ELEM(mSource,0,0) = 0; A2D_ELEM(mSource,0,1) = 10; A2D_ELEM(mSource,0,2) = 3.14159265; A2D_ELEM(mSource,1,0) = (20*3.14159265); A2D_ELEM(mSource,1,1) = (3.14159265/2); A2D_ELEM(mSource,1,2) = (3*3.14159265/2); A2D_ELEM(mSource,2,0) = (10*3.14159265/2); A2D_ELEM(mSource,2,1) = (50*3.14159265); A2D_ELEM(mSource,2,2) = (2*3.14159265); double value = 2*3.14159265; mod(mSource,mTarget,value); //We test the obtained values with the results obtained from method "mod" of Matlab ASSERT_NEAR( A2D_ELEM(mTarget,0,0), 0, 1e-3); ASSERT_NEAR( A2D_ELEM(mTarget,0,1), 3.7168, 1e-3 ); ASSERT_NEAR( A2D_ELEM(mTarget,0,2), 3.1416, 1e-3); ASSERT_NEAR( A2D_ELEM(mTarget,1,0), 0, 1e-3); ASSERT_NEAR( A2D_ELEM(mTarget,1,1), 1.5708, 1e-3); ASSERT_NEAR( A2D_ELEM(mTarget,1,2), 4.7124, 1e-2); ASSERT_NEAR( A2D_ELEM(mTarget,2,0), 3.1416, 1e-3); if (A2D_ELEM(mTarget,2,1) > (value - 1e-3)) ASSERT_NEAR( A2D_ELEM(mTarget,2,1), value, 1e-3); else ASSERT_NEAR( A2D_ELEM(mTarget,2,1), 0, 1e-3); ASSERT_NEAR( A2D_ELEM(mTarget,2,2), 0, 1e-3); }
TEST( MultidimTest, sincos) { MultidimArray<double> mSource(2,2); MultidimArray<double> mdSIN; MultidimArray<double> mdCOS; A2D_ELEM(mSource,0,0) = 0; A2D_ELEM(mSource,1,0) = 3.14159265/2; A2D_ELEM(mSource,0,1) = 3.14159265; A2D_ELEM(mSource,1,1) = (3*3.14159265)/2; sincos(mSource,mdSIN,mdCOS); ASSERT_TRUE( (A2D_ELEM(mdSIN,0,0) - 0)<0.0001); ASSERT_TRUE( (A2D_ELEM(mdSIN,1,0) - 1)<0.0001); ASSERT_TRUE( (A2D_ELEM(mdSIN,0,1) - 0)<0.0001); ASSERT_TRUE( (A2D_ELEM(mdSIN,1,1) + 1)<0.0001); ASSERT_TRUE( (A2D_ELEM(mdCOS,0,0) - 1)<0.0001); ASSERT_TRUE( (A2D_ELEM(mdCOS,1,0) - 0)<0.0001); ASSERT_TRUE( (A2D_ELEM(mdCOS,0,1) + 1)<0.0001); ASSERT_TRUE( (A2D_ELEM(mdCOS,1,1) - 0)<0.0001); }
TEST( MultidimTest, selfCoreArrayByArrayMask) { MultidimArray<double> m1(2,2); MultidimArray<double> m2(2,2); MultidimArray<double> mOut(2,2); MultidimArray<double> mref(2,2); MultidimArray<double> mask(2,2); A2D_ELEM(m1,0,0) = 1.; A2D_ELEM(m1,1,0) = 2.; A2D_ELEM(m1,0,1) = 3.; A2D_ELEM(m1,1,1) = 4.; A2D_ELEM(m2,0,0) = 11.; A2D_ELEM(m2,1,0) = 22.; A2D_ELEM(m2,0,1) = 33.; A2D_ELEM(m2,1,1) = 44.; A2D_ELEM(mask,0,0) = 0.; A2D_ELEM(mask,1,0) = 1.; A2D_ELEM(mask,0,1) = 1.; A2D_ELEM(mask,1,1) = 1.; A2D_ELEM(mOut,0,0) = 0.; A2D_ELEM(mOut,1,0) = 0.; A2D_ELEM(mOut,0,1) = 0.; A2D_ELEM(mOut,1,1) = 1.; selfCoreArrayByArrayMask(m1, m2, mOut, '+', &mask); A2D_ELEM(mref,0,0) = 1.; A2D_ELEM(mref,1,0) = 22.; A2D_ELEM(mref,0,1) = 33.; A2D_ELEM(mref,1,1) = 45.; EXPECT_EQ(mOut,mref); }
TEST( MultidimTest, coreArrayByArray) { MultidimArray<double> m1(2,2); MultidimArray<double> m2(2,2); MultidimArray<double> mOut(2,2); MultidimArray<double> mref(2,2); A2D_ELEM(m1,0,0) = 1.; A2D_ELEM(m1,1,0) = 2.; A2D_ELEM(m1,0,1) = 3.; A2D_ELEM(m1,1,1) = 4.; A2D_ELEM(m2,0,0) = 11.; A2D_ELEM(m2,1,0) = 22.; A2D_ELEM(m2,0,1) = 33.; A2D_ELEM(m2,1,1) = 44.; coreArrayByArray(m1, m2, mOut, '+'); A2D_ELEM(mref,0,0) = 12.; A2D_ELEM(mref,1,0) = 24.; A2D_ELEM(mref,0,1) = 36.; A2D_ELEM(mref,1,1) = 48.; EXPECT_EQ(mOut,mref); }
void Projector::rotate2D(MultidimArray<Complex > &f2d, Matrix2D<DOUBLE> &A, bool inv) { DOUBLE fx, fy, xp, yp; int x0, x1, y0, y1, y, y2, r2; bool is_neg_x; Complex d00, d01, d10, d11, dx0, dx1; Matrix2D<DOUBLE> Ainv; // f2d should already be in the right size (ori_size,orihalfdim) // AND the points outside max_r should already be zero... // f2d.initZeros(); // Use the inverse matrix if (inv) Ainv = A; else Ainv = A.transpose(); // The f2d image may be smaller than r_max, in that case also make sure not to fill the corners! int my_r_max = XMIPP_MIN(r_max, XSIZE(f2d) - 1); // Go from the 2D slice coordinates to the map coordinates Ainv *= (DOUBLE)padding_factor; // take scaling into account directly int max_r2 = my_r_max * my_r_max; int min_r2_nn = r_min_nn * r_min_nn; #ifdef DEBUG std::cerr << " XSIZE(f2d)= "<< XSIZE(f2d) << std::endl; std::cerr << " YSIZE(f2d)= "<< YSIZE(f2d) << std::endl; std::cerr << " XSIZE(data)= "<< XSIZE(data) << std::endl; std::cerr << " YSIZE(data)= "<< YSIZE(data) << std::endl; std::cerr << " STARTINGX(data)= "<< STARTINGX(data) << std::endl; std::cerr << " STARTINGY(data)= "<< STARTINGY(data) << std::endl; std::cerr << " STARTINGZ(data)= "<< STARTINGZ(data) << std::endl; std::cerr << " max_r= "<< r_max << std::endl; std::cerr << " Ainv= " << Ainv << std::endl; #endif for (int i=0; i < YSIZE(f2d); i++) { // Don't search beyond square with side max_r if (i <= my_r_max) { y = i; } else if (i >= YSIZE(f2d) - my_r_max) { y = i - YSIZE(f2d); } else continue; y2 = y * y; for (int x=0; x <= my_r_max; x++) { // Only include points with radius < max_r (exclude points outside circle in square) r2 = x * x + y2; if (r2 > max_r2) continue; // Get logical coordinates in the 3D map xp = Ainv(0,0) * x + Ainv(0,1) * y; yp = Ainv(1,0) * x + Ainv(1,1) * y; if (interpolator == TRILINEAR || r2 < min_r2_nn) { // Only asymmetric half is stored if (xp < 0) { // Get complex conjugated hermitian symmetry pair xp = -xp; yp = -yp; is_neg_x = true; } else { is_neg_x = false; } // Trilinear interpolation (with physical coords) // Subtract STARTINGY to accelerate access to data (STARTINGX=0) // In that way use DIRECT_A3D_ELEM, rather than A3D_ELEM x0 = FLOOR(xp); fx = xp - x0; x1 = x0 + 1; y0 = FLOOR(yp); fy = yp - y0; y0 -= STARTINGY(data); y1 = y0 + 1; // Matrix access can be accelerated through pre-calculation of z0*xydim etc. d00 = DIRECT_A2D_ELEM(data, y0, x0); d01 = DIRECT_A2D_ELEM(data, y0, x1); d10 = DIRECT_A2D_ELEM(data, y1, x0); d11 = DIRECT_A2D_ELEM(data, y1, x1); // Set the interpolated value in the 2D output array #ifndef FLOAT_PRECISION __m256d __interpx = LIN_INTERP_AVX(_mm256_setr_pd(d00.real, d00.imag, d10.real, d10.imag), _mm256_setr_pd(d01.real, d01.imag, d11.real, d11.imag), _mm256_set1_pd(fx)); #else __m128 __interpx = LIN_INTERP_AVX(_mm_setr_ps(d00.real, d00.imag, d10.real, d10.imag), _mm_setr_ps(d01.real, d01.imag, d11.real, d11.imag), _mm_set1_ps(fx)); #endif Complex* interpx = (Complex*)&__interpx; DIRECT_A2D_ELEM(f2d, i, x) = LIN_INTERP(fy, interpx[0], interpx[1]); // Take complex conjugated for half with negative x if (is_neg_x) DIRECT_A2D_ELEM(f2d, i, x) = conj(DIRECT_A2D_ELEM(f2d, i, x)); } // endif TRILINEAR else if (interpolator == NEAREST_NEIGHBOUR ) { x0 = ROUND(xp); y0 = ROUND(yp); if (x0 < 0) DIRECT_A2D_ELEM(f2d, i, x) = conj(A2D_ELEM(data, -y0, -x0)); else DIRECT_A2D_ELEM(f2d, i, x) = A2D_ELEM(data, y0, x0); } // endif NEAREST_NEIGHBOUR else REPORT_ERROR("Unrecognized interpolator in Projector::project"); } // endif x-loop } // endif y-loop }
//majorAxis and minorAxis is the estimated particle size in px void ProgSortByStatistics::processInprocessInputPrepareSPTH(MetaData &SF, bool trained) { //#define DEBUG PCAMahalanobisAnalyzer tempPcaAnalyzer0; PCAMahalanobisAnalyzer tempPcaAnalyzer1; PCAMahalanobisAnalyzer tempPcaAnalyzer2; PCAMahalanobisAnalyzer tempPcaAnalyzer3; PCAMahalanobisAnalyzer tempPcaAnalyzer4; //Morphology tempPcaAnalyzer0.clear(); //Signal to noise ratio tempPcaAnalyzer1.clear(); tempPcaAnalyzer2.clear(); tempPcaAnalyzer3.clear(); //Histogram analysis, to detect black points and saturated parts tempPcaAnalyzer4.clear(); double sign = 1;//;-1; int numNorm = 3; int numDescriptors0=numNorm; int numDescriptors2=4; int numDescriptors3=11; int numDescriptors4 = 10; MultidimArray<float> v0(numDescriptors0); MultidimArray<float> v2(numDescriptors2); MultidimArray<float> v3(numDescriptors3); MultidimArray<float> v4(numDescriptors4); if (verbose>0) { std::cout << " Sorting particle set by new xmipp method..." << std::endl; } int nr_imgs = SF.size(); if (verbose>0) init_progress_bar(nr_imgs); int c = XMIPP_MAX(1, nr_imgs / 60); int imgno = 0, imgnoPCA=0; bool thereIsEnable=SF.containsLabel(MDL_ENABLED); bool first=true; // We assume that at least there is one particle size_t Xdim, Ydim, Zdim, Ndim; getImageSize(SF,Xdim,Ydim,Zdim,Ndim); //Initialization: MultidimArray<double> nI, modI, tempI, tempM, ROI; MultidimArray<bool> mask; nI.resizeNoCopy(Ydim,Xdim); modI.resizeNoCopy(Ydim,Xdim); tempI.resizeNoCopy(Ydim,Xdim); tempM.resizeNoCopy(Ydim,Xdim); mask.resizeNoCopy(Ydim,Xdim); mask.initConstant(true); MultidimArray<double> autoCorr(2*Ydim,2*Xdim); MultidimArray<double> smallAutoCorr; Histogram1D hist; Matrix2D<double> U,V,temp; Matrix1D<double> D; MultidimArray<int> radial_count; MultidimArray<double> radial_avg; Matrix1D<int> center(2); MultidimArray<int> distance; int dim; center.initZeros(); v0.initZeros(numDescriptors0); v2.initZeros(numDescriptors2); v3.initZeros(numDescriptors3); v4.initZeros(numDescriptors4); ROI.resizeNoCopy(Ydim,Xdim); ROI.setXmippOrigin(); FOR_ALL_ELEMENTS_IN_ARRAY2D(ROI) { double temp = std::sqrt(i*i+j*j); if ( temp < (Xdim/2)) A2D_ELEM(ROI,i,j)= 1; else A2D_ELEM(ROI,i,j)= 0; } Image<double> img; FourierTransformer transformer(FFTW_BACKWARD); FOR_ALL_OBJECTS_IN_METADATA(SF) { if (thereIsEnable) { int enabled; SF.getValue(MDL_ENABLED,enabled,__iter.objId); if ( (enabled==-1) ) { imgno++; continue; } } img.readApplyGeo(SF,__iter.objId); if (targetXdim!=-1 && targetXdim!=XSIZE(img())) selfScaleToSize(LINEAR,img(),targetXdim,targetXdim,1); MultidimArray<double> &mI=img(); mI.setXmippOrigin(); mI.statisticsAdjust(0,1); mask.setXmippOrigin(); //The size of v1 depends on the image size and must be declared here int numDescriptors1 = XSIZE(mI)/2; //=100; MultidimArray<float> v1(numDescriptors1); v1.initZeros(numDescriptors1); double var = 1; normalize(transformer,mI,tempI,modI,0,var,mask); modI.setXmippOrigin(); tempI.setXmippOrigin(); nI = sign*tempI*(modI*modI); tempM = (modI*modI); A1D_ELEM(v0,0) = (tempM*ROI).sum(); int index = 1; var+=2; while (index < numNorm) { normalize(transformer,mI,tempI,modI,0,var,mask); modI.setXmippOrigin(); tempI.setXmippOrigin(); nI += sign*tempI*(modI*modI); tempM += (modI*modI); A1D_ELEM(v0,index) = (tempM*ROI).sum(); index++; var+=2; } nI /= tempM; tempPcaAnalyzer0.addVector(v0); nI=(nI*ROI); auto_correlation_matrix(mI,autoCorr); if (first) { radialAveragePrecomputeDistance(autoCorr, center, distance, dim); first=false; } fastRadialAverage(autoCorr, distance, dim, radial_avg, radial_count); for (int n = 0; n < numDescriptors1; ++n) A1D_ELEM(v1,n)=(float)DIRECT_A1D_ELEM(radial_avg,n); tempPcaAnalyzer1.addVector(v1); #ifdef DEBUG //String name = "000005@Images/Extracted/run_002/extra/BPV_1386.stk"; String name = "000010@Images/Extracted/run_001/extra/KLH_Dataset_I_Training_0028.stk"; //String name = "001160@Images/Extracted/run_001/DefaultFamily5"; std::cout << img.name() << std::endl; if (img.name()==name2) { FileName fpName = "test_1.txt"; mI.write(fpName); fpName = "test_2.txt"; nI.write(fpName); fpName = "test_3.txt"; tempM.write(fpName); fpName = "test_4.txt"; ROI.write(fpName); //exit(1); } #endif nI.binarize(0); int im = labelImage2D(nI,nI,8); compute_hist(nI, hist, 0, im, im+1); size_t l; int k,i,j; hist.maxIndex(l,k,i,j); A1D_ELEM(hist,j)=0; hist.maxIndex(l,k,i,j); nI.binarizeRange(j-1,j+1); double x0=0,y0=0,majorAxis=0,minorAxis=0,ellipAng=0; size_t area=0; fitEllipse(nI,x0,y0,majorAxis,minorAxis,ellipAng,area); A1D_ELEM(v2,0)=majorAxis/((img().xdim) ); A1D_ELEM(v2,1)=minorAxis/((img().xdim) ); A1D_ELEM(v2,2)= (fabs((img().xdim)/2-x0)+fabs((img().ydim)/2-y0))/((img().xdim)/2); A1D_ELEM(v2,3)=area/( (double)((img().xdim)/2)*((img().ydim)/2) ); for (int n=0 ; n < numDescriptors2 ; n++) { if ( std::isnan(std::abs(A1D_ELEM(v2,n)))) A1D_ELEM(v2,n)=0; } tempPcaAnalyzer2.addVector(v2); //mI.setXmippOrigin(); //auto_correlation_matrix(mI*ROI,autoCorr); //auto_correlation_matrix(nI,autoCorr); autoCorr.window(smallAutoCorr,-5,-5, 5, 5); smallAutoCorr.copy(temp); svdcmp(temp,U,D,V); for (int n = 0; n < numDescriptors3; ++n) A1D_ELEM(v3,n)=(float)VEC_ELEM(D,n); //A1D_ELEM(v3,n)=(float)VEC_ELEM(D,n)/VEC_ELEM(D,0); tempPcaAnalyzer3.addVector(v3); double minVal=0.; double maxVal=0.; mI.computeDoubleMinMax(minVal,maxVal); compute_hist(mI, hist, minVal, maxVal, 100); for (int n=0 ; n <= numDescriptors4-1 ; n++) { A1D_ELEM(v4,n)= (hist.percentil((n+1)*10)); } tempPcaAnalyzer4.addVector(v4); #ifdef DEBUG if (img.name()==name1) { FileName fpName = "test.txt"; mI.write(fpName); fpName = "test3.txt"; nI.write(fpName); } #endif imgno++; imgnoPCA++; if (imgno % c == 0 && verbose>0) progress_bar(imgno); } tempPcaAnalyzer0.evaluateZScore(2,20,trained); tempPcaAnalyzer1.evaluateZScore(2,20,trained); tempPcaAnalyzer2.evaluateZScore(2,20,trained); tempPcaAnalyzer3.evaluateZScore(2,20,trained); tempPcaAnalyzer4.evaluateZScore(2,20,trained); pcaAnalyzer.push_back(tempPcaAnalyzer0); pcaAnalyzer.push_back(tempPcaAnalyzer1); pcaAnalyzer.push_back(tempPcaAnalyzer1); pcaAnalyzer.push_back(tempPcaAnalyzer3); pcaAnalyzer.push_back(tempPcaAnalyzer4); }
void PolyZernikes::fit(const Matrix1D<int> & coef, MultidimArray<double> & im, MultidimArray<double> &weight, MultidimArray<bool> & ROI, int verbose) { this->create(coef); size_t xdim = XSIZE(im); size_t ydim = YSIZE(im); //int numZer = (size_t)coef.sum(); int numZer = (size_t)coef.sum(); //Actually polOrder corresponds to the polynomial order +1 int polOrder=(int)ZERNIKE_ORDER(coef.size()); im.setXmippOrigin(); Matrix2D<double> polValue(polOrder,polOrder); //First argument means number of images //Second argument means number of pixels WeightedLeastSquaresHelper weightedLeastSquaresHelper; Matrix2D<double>& zerMat=weightedLeastSquaresHelper.A; zerMat.resizeNoCopy((size_t)ROI.sum(), numZer); double iMaxDim2 = 2./std::max(xdim,ydim); size_t pixel_idx=0; weightedLeastSquaresHelper.b.resizeNoCopy((size_t)ROI.sum()); weightedLeastSquaresHelper.w.resizeNoCopy(weightedLeastSquaresHelper.b); FOR_ALL_ELEMENTS_IN_ARRAY2D(im) { if ( (A2D_ELEM(ROI,i,j))) { //For one i we swap the different j double y=i*iMaxDim2; double x=j*iMaxDim2; //polValue = [ 0 y y2 y3 ... // x xy xy2 xy3 ... // x2 x2y x2y2 x2y3 ] //dMij(polValue,py,px) py es fila, px es columna for (int py = 0; py < polOrder; ++py) { double ypy=std::pow(y,py); for (int px = 0; px < polOrder; ++px) dMij(polValue,px,py) = ypy*std::pow(x,px); } Matrix2D<int> *fMat; //We generate the representation of the Zernike polynomials for (int k=0; k < numZer; ++k) { fMat = &fMatV[k]; if (fMat == NULL) continue; double temp = 0; for (size_t px = 0; px < (*fMat).Xdim(); ++px) for (size_t py = 0; py < (*fMat).Ydim(); ++py) temp += dMij(*fMat,py,px)*dMij(polValue,py,px); dMij(zerMat,pixel_idx,k) = temp; } VEC_ELEM(weightedLeastSquaresHelper.b,pixel_idx)=A2D_ELEM(im,i,j); VEC_ELEM(weightedLeastSquaresHelper.w,pixel_idx)=std::abs(A2D_ELEM(weight,i,j)); ++pixel_idx; } } Matrix1D<double> zernikeCoefficients; weightedLeastSquares(weightedLeastSquaresHelper, zernikeCoefficients); fittedCoeffs = zernikeCoefficients; // Pointer to the image to be fitted MultidimArray<double> reconstructed; reconstructed.resizeNoCopy(im); pixel_idx=0; FOR_ALL_ELEMENTS_IN_ARRAY2D(im) if (A2D_ELEM(ROI,i,j)) { double temp=0; for (int k=0; k < numZer; ++k) temp+=dMij(zerMat,pixel_idx,k)*VEC_ELEM(fittedCoeffs,k); A2D_ELEM(reconstructed,i,j)=temp; if ( fabs(A2D_ELEM(reconstructed,i,j)-A2D_ELEM(im,i,j)) > PI) A2D_ELEM(ROI,i,j) = false; ++pixel_idx; } pixel_idx=0; if (verbose > 0) { Image<double> save; save()=reconstructed; save.write("reconstructedZernikes.xmp"); ROI.write("ROI.txt"); } }
void PolyZernikes::zernikePols(const Matrix1D<int> coef, MultidimArray<double> & im, MultidimArray<bool> & ROI, int verbose) { this->create(coef); int polOrder=(int)ZERNIKE_ORDER(coef.size()); int numZer = coef.size(); int xdim = XSIZE(im); int ydim = YSIZE(im); im.setXmippOrigin(); Matrix2D<double> polValue(polOrder,polOrder); double iMaxDim2 = 2./std::max(xdim,ydim); double temp = 0; FOR_ALL_ELEMENTS_IN_ARRAY2D(im) { if (A2D_ELEM(ROI,i,j)) { //For one i we swap the different j double y=i*iMaxDim2; double x=j*iMaxDim2; //polValue = [ 0 y y2 y3 ... // x xy xy2 xy3 ... // x2 x2y x2y2 x2y3 ] //dMij(polValue,py,px) py es fila, px es columna for (int py = 0; py < polOrder; ++py) { double ypy=std::pow(y,py); for (int px = 0; px < polOrder; ++px) dMij(polValue,px,py) = ypy*std::pow(x,px); } Matrix2D<int> *fMat; //We generate the representation of the Zernike polynomials for (int k=0; k < numZer; ++k) { fMat = &fMatV[k]; if ( (dMij(*fMat,0,0) == 0) && MAT_SIZE(*fMat) == 1 ) continue; for (size_t px = 0; px < (*fMat).Xdim(); ++px) for (size_t py = 0; py < (*fMat).Ydim(); ++py) temp += dMij(*fMat,py,px)*dMij(polValue,py,px)*VEC_ELEM(coef,k); } A2D_ELEM(im,i,j) = temp; temp = 0; } } STARTINGX(im)=STARTINGY(im)=0; if (verbose == 1) { Image<double> save; save()=im; save.write("PPP1.xmp"); } }