51 erode2D(dmask,maskEroded,8,0,patchSize);
59 save.
write(
"PPP.xmp");
61 save.
write(
"PPPmask.xmp");
62 std::cout <<
"Masks saved. Press any key\n";
75 std::vector< MultidimArray<double> *>
I1;
76 std::vector< MultidimArray<double> *>
I2;
77 std::vector< MultidimArray<double> *>
Mask1;
78 std::vector< MultidimArray<double> *>
Mask2;
90 for (
size_t i=0;
i<I1.size();
i++)
92 for (
size_t i=0;
i<I2.size();
i++)
94 for (
size_t i=0;
i<Mask1.size();
i++)
96 for (
size_t i=0;
i<Mask2.size();
i++)
121 if (!showMode && checkRotation)
124 std::complex<double>
a=A12(0,0);
125 std::complex<double>
b=A12(0,1);
126 std::complex<double>
c=A12(1,0);
127 std::complex<double>
d=A12(1,1);
128 std::complex<double> eig1=(a+
d)/2.0+
sqrt(4.0*b*c+(a-d)*(a-
d))/2.0;
129 std::complex<double> eig2=(a+
d)/2.0-
sqrt(4.0*b*c+(a-d)*(a-
d))/2.0;
130 double abs_eig1=
abs(eig1);
131 double abs_eig2=
abs(eig2);
133 if (abs_eig1<0.95 || abs_eig1>1.05)
134 return 1e20*
ABS(abs_eig1-1);
135 if (abs_eig2<0.95 || abs_eig2>1.05)
136 return 1e20*
ABS(abs_eig2-1);
141 a=A12(0,0)*A12(0,0)+A12(0,1)*A12(0,1);
142 b=A12(0,0)*A12(1,0)+A12(0,1)*A12(1,1);
144 d=A12(1,0)*A12(1,0)+A12(1,1)*A12(1,1);
145 eig1=(a+
d)/2.0+
sqrt(4.0*b*c+(a-d)*(a-
d))/2.0;
146 eig2=(a+
d)/2.0-
sqrt(4.0*b*c+(a-d)*(a-
d))/2.0;
150 if (abs_eig1<0.95 || abs_eig1>1.05)
151 return 1e20*
ABS(abs_eig1-1);
152 if (abs_eig2<0.95 || abs_eig2>1.05)
153 return 1e20*
ABS(abs_eig2-1);
162 for (
size_t level=0; level<I1.size(); level++)
195 double distLevel=0.5*(
203 save.
write(
"PPPimg1.xmp");
205 save.
write(
"PPPimg2.xmp");
206 save()=*(Mask1[level]);
207 save.
write(
"PPPmask1.xmp");
208 save()=*(Mask2[level]);
209 save.
write(
"PPPmask2.xmp");
210 save()=transformedI1;
211 save.
write(
"PPPTransformedImg1.xmp");
212 save()=transformedI2;
213 save.
write(
"PPPTransformedImg2.xmp");
215 save.
write(
"PPPmaskInTheSpaceOf1.xmp");
217 save.
write(
"PPPmaskInTheSpaceOf2.xmp");
218 save()=*(I1[level])-transformedI2;
219 save.
write(
"PPPDiffImg1.xmp");
220 save()=*(I2[level])-transformedI1;
221 save.
write(
"PPPDiffImg2.xmp");
222 std::cout <<
"Level=" << level <<
"\nA12level=\n" << A12level <<
"A21level=\n" << A21level << std::endl;
223 std::cout <<
"distLevel=" << distLevel << std::endl;
224 std::cout <<
"Do you like the alignment? (y/n)\n";
247 return ((
AffineFitness*)prm)->affine_fitness_individual(p+1);
293 fitness.
I1.push_back(dummy);
296 fitness.
I2.push_back(dummy);
299 fitness.
Mask1.push_back(dummy);
302 fitness.
Mask2.push_back(dummy);
306 if (pyramidLevel>=level)
318 while (level<=pyramidLevel);
344 static pthread_mutex_t globalAffineMutex = PTHREAD_MUTEX_INITIALIZER;
348 double thresholdAffine,
bool globalAffine,
bool isMirror,
390 pthread_mutex_lock( &globalAffineMutex );
403 pthread_mutex_unlock( &globalAffineMutex );
414 cost, iter, steps,
false);
454 fnSel=getParam(
"-i");
455 fnSelOrig=getParam(
"--iorig");
456 fnRoot=getParam(
"--oroot");
458 fnRoot=fnSel.withoutExtension();
459 globalAffine=checkParam(
"--globalAffine");
460 useCriticalPoints=checkParam(
"--useCriticalPoints");
461 if (useCriticalPoints)
462 Ncritical=getIntParam(
"--useCriticalPoints");
465 seqLength=getIntParam(
"--seqLength");
466 blindSeqLength=getIntParam(
"--blindSeqLength");
467 maxStep=getIntParam(
"--maxStep");
468 gridSamples=getIntParam(
"--gridSamples");
469 psiMax=getDoubleParam(
"--psiMax");
470 deltaRot=getDoubleParam(
"--deltaRot");
471 localSize=getDoubleParam(
"--localSize");
472 optimizeTiltAngle=checkParam(
"--optimizeTiltAngle");
473 isCapillar=checkParam(
"--isCapillar");
474 dontNormalize=checkParam(
"--dontNormalize");
475 difficult=checkParam(
"--difficult");
476 corrThreshold=getDoubleParam(
"--threshold");
477 maxShiftPercentage=getDoubleParam(
"--maxShiftPercentage");
478 maxIterDE=getIntParam(
"--maxIterDE");
479 showAffine=checkParam(
"--showAffine");
480 thresholdAffine=getDoubleParam(
"--thresholdAffine");
481 identifyOutliersZ = getDoubleParam(
"--identifyOutliers");
482 doNotIdentifyOutliers = checkParam(
"--noOutliers");
483 pyramidLevel = getIntParam(
"--pyramid");
484 numThreads = getIntParam(
"--thr");
487 lastStep=getIntParam(
"--lastStep");
492 std::cout <<
"Input images: " << fnSel << std::endl
493 <<
"Original images: " << fnSelOrig << std::endl
494 <<
"Output rootname: " << fnRoot << std::endl
495 <<
"Global affine: " << globalAffine << std::endl
496 <<
"Use critical points:" << useCriticalPoints << std::endl
497 <<
"Num critical points:" << Ncritical << std::endl
498 <<
"SeqLength: " << seqLength << std::endl
499 <<
"BlindSeqLength: " << blindSeqLength << std::endl
500 <<
"MaxStep: " << maxStep << std::endl
501 <<
"Grid samples: " << gridSamples << std::endl
502 <<
"Maximum psi: " << psiMax << std::endl
503 <<
"Delta rot: " << deltaRot << std::endl
504 <<
"Local size: " << localSize << std::endl
505 <<
"Optimize tilt angle:" << optimizeTiltAngle << std::endl
506 <<
"isCapillar: " << isCapillar << std::endl
507 <<
"DontNormalize: " << dontNormalize << std::endl
508 <<
"Difficult: " << difficult << std::endl
509 <<
"Threshold: " << corrThreshold << std::endl
510 <<
"MaxShift Percentage:" << maxShiftPercentage << std::endl
511 <<
"MaxIterDE: " << maxIterDE << std::endl
512 <<
"Show Affine: " << showAffine << std::endl
513 <<
"Threshold Affine: " << thresholdAffine << std::endl
514 <<
"Identify outliers Z:" << identifyOutliersZ << std::endl
515 <<
"No outliers: " << doNotIdentifyOutliers << std::endl
516 <<
"Pyramid level: " << pyramidLevel << std::endl
517 <<
"Threads to use: " << numThreads << std::endl
518 <<
"Last step: " << lastStep << std::endl
524 addUsageLine(
"Align a single-axis tilt series without any marker.");
525 addSeeAlsoLine(
"tomo_align_dual_tilt_series");
526 addParamsLine(
" == General Options == ");
527 addParamsLine(
" -i <metadatafile> : Input images");
528 addParamsLine(
" : The selfile must contain the list of micrographs");
529 addParamsLine(
" : and its tilt angles");
530 addParamsLine(
" [--iorig <metadatafile=\"\">] : Metadata with images at original scale");
531 addParamsLine(
" [--oroot <fn_out=\"\">] : Output alignment");
532 addParamsLine(
" : If not given, the input selfile without extension");
533 addParamsLine(
" [--thr <num=1>] : Parallel processing using \"num\" threads");
534 addParamsLine(
" [--lastStep+ <step=-1>] : Last step to perform");
535 addParamsLine(
" : Step -1 -> Perform all steps");
536 addParamsLine(
" : Step 0 -> Determination of affine transformations");
537 addParamsLine(
" : Step 1 -> Determination of landmark chains");
538 addParamsLine(
" : Step 2 -> Determination of alignment parameters");
539 addParamsLine(
" : Step 3 -> Writing aligned images");
540 addParamsLine(
" == Step 0 (Affine alignment) Options == ");
541 addParamsLine(
" [--maxShiftPercentage <p=0.2>] : Maximum shift as percentage of image size");
542 addParamsLine(
" [--thresholdAffine <th=0.85>] : Threshold affine");
543 addParamsLine(
" [--globalAffine] : Look globally for affine transformations");
544 addParamsLine(
" [--difficult+] : Apply some filters before affine alignment");
545 addParamsLine(
" [--maxIterDE+ <n=30>] : Maximum number of iteration in Differential Evolution");
546 addParamsLine(
" [--showAffine+] : Show affine transformations as PPP*");
547 addParamsLine(
" [--identifyOutliers+ <z=5>] : Z-score to be an outlier");
548 addParamsLine(
" [--noOutliers+] : Do not identify outliers");
549 addParamsLine(
" [--pyramid+ <level=1>] : Multiresolution for affine transformations");
550 addParamsLine(
" == Step 1 (Landmark chain) Options == ");
551 addParamsLine(
" [--seqLength <n=5>] : Sequence length");
552 addParamsLine(
" [--localSize <size=0.04>] : In percentage");
553 addParamsLine(
" [--useCriticalPoints <n=0>] : Use critical points instead of a grid");
554 addParamsLine(
" : n is the number of critical points to choose");
555 addParamsLine(
" : in each image");
556 addParamsLine(
" [--threshold+ <th=-1>] : Correlation threshold");
557 addParamsLine(
" [--blindSeqLength+ <n=-1>] : Blind sequence length, -1=No blind landmarks");
558 addParamsLine(
" [--maxStep+ <step=4>] : Maximum step for chain refinement");
559 addParamsLine(
" [--gridSamples+ <n=40>] : Total number of samples=n*n");
560 addParamsLine(
" [--isCapillar+] : Set this flag if the tilt series is of a capillar");
561 addParamsLine(
" == Step 2 (Determination of alignment parameters) Options == ");
562 addParamsLine(
" [--psiMax+ <psi=-1>] : Maximum psi in absolute value (degrees)");
563 addParamsLine(
" : -1 -> do not optimize for psi");
564 addParamsLine(
" [--deltaRot+ <rot=5>] : In degrees. For the first optimization stage");
565 addParamsLine(
" [--optimizeTiltAngle+] : Optimize tilt angle");
566 addParamsLine(
" == Step 3 (Produce aligned images) Options == ");
567 addParamsLine(
" [--dontNormalize+] : Don't normalize the output images");
568 addExampleLine(
"Typical run",
false);
569 addExampleLine(
"xmipp_tomo_align_tilt_series -i tiltseries.sel --thr 8");
570 addExampleLine(
"If there are image with large shifts",
false);
571 addExampleLine(
"xmipp_tomo_align_tilt_series -i tiltseries.sel --thr 8 --globalAffine");
572 addExampleLine(
"If there are clear landmarks that can be tracked",
false);
573 addExampleLine(
"xmipp_tomo_align_tilt_series -i tiltseries.sel --thr 8 --criticalPoints");
577 static pthread_mutex_t printingMutex = PTHREAD_MUTEX_INITIALIZER;
590 int thread_id = master->myThreadID;
593 int Nimg = parent->
Nimg;
598 std::vector < MultidimArray<unsigned char> *> & img = parent->
img;
599 std::vector< std::vector< Matrix2D<double> > > & affineTransformations = parent->
affineTransformations;
602 int maxShift=
FLOOR(
XSIZE(*img[0])*maxShiftPercentage);
610 for (
int jj=initjj; jj<Nimg; jj+= localnumThreads)
619 bool isMirror=(jj==0) && (jj_1==Nimg-1);
621 if (
MAT_XSIZE(affineTransformations[jj_1][jj])==0)
626 showAffine, thresholdAffine, globalAffine,
630 pthread_mutex_lock( &printingMutex );
631 affineTransformations[jj_1][jj]=Aij;
632 affineTransformations[jj][jj_1]=Aji;
635 parent->
fnRoot+
"_transformations.txt");
636 std::cout <<
"Cost for [" << jj_1 <<
"] - [" 637 << jj <<
"] = " << cost << std::endl;
639 pthread_mutex_unlock( &printingMutex );
644 Aij=affineTransformations[jj_1][jj];
663 pthread_mutex_lock( &printingMutex );
664 std::cout <<
"Cost for [" << jj_1 <<
"] - [" 665 << jj <<
"] = " << cost << std::endl;
666 pthread_mutex_unlock( &printingMutex );
675 bool globalAffineToUse)
677 bool oldglobalAffine=globalAffine;
678 globalAffine=globalAffineToUse;
680 auto * th_ids =
new pthread_t[numThreads];
683 for(
int nt = 0 ; nt < numThreads ; nt ++ )
686 th_args[nt].
parent =
this;
687 th_args[nt].myThreadID = nt;
692 for(
int nt = 0 ; nt < numThreads ; nt ++ )
693 pthread_join(*(th_ids+nt),
nullptr);
698 globalAffine=oldglobalAffine;
703 isOutlier.initZeros(Nimg);
705 for (
int i=0;
i<Nimg;
i++)
706 correlationListAux(
i)=correlationList[
i];
710 diff=correlationListAux-medianCorr;
714 std::cout <<
"Cost distribution= " << 1-medianCorr <<
" +- " 715 << madCorr << std::endl;
717 double thresholdCorr=medianCorr-identifyOutliersZ*madCorr;
720 bool potentialOutlier=(correlationListAux(
i)<thresholdCorr);
721 if (potentialOutlier)
723 affineTransformations[
i-1][
i].clear();
727 std::cout << name_list[
i-1] <<
" [" <<
i 728 <<
"] is considered as an outlier. " 729 <<
"Its cost is " << 1-correlationListAux(
i)
733 std::cout << name_list[
i-1] <<
" [" <<
i 734 <<
"] might be an outlier. " 735 <<
"Its cost is " << 1-correlationListAux(
i)
755 bestPreviousAlignment=
new Alignment(
this);
757 SF.read(fnSel,
nullptr);
766 for (
size_t i=0;
i<img.size();
i++)
772 if (!maskImg.empty())
774 for (
size_t i=0;
i<maskImg.size();
i++)
779 std::cerr <<
"Reading input data\n";
786 bool nonZeroTilt=
false;
789 for (
size_t objId : SF.ids())
809 XSIZE(Ifiltered)/10);
814 FilterBP.
w1=1.0/
XSIZE(Ifiltered);
815 FilterBP.
w2=100.0/
XSIZE(Ifiltered);
828 if (!useCriticalPoints)
833 maskImg.push_back(mask_i);
837 imgaux().rangeAdjust(0,255);
839 img_i->setXmippOrigin();
840 img.push_back(img_i);
846 tiltAngle=imgaux.
tilt();
847 tiltList.push_back(tiltAngle);
850 if (
ABS(tiltAngle)<minTilt)
853 minTilt=
ABS(tiltAngle);
855 name_list.push_back(imgaux.
name());
866 if (!fnSelOrig.empty())
868 SForig.read(fnSelOrig,
nullptr);
869 SForig.removeDisabled();
871 if (SForig.size()!=SF.size())
876 std::vector< Matrix2D<double> > emptyRow;
877 for (
int i=0;
i<Nimg;
i++)
880 emptyRow.push_back(A);
882 for (
int i=0;
i<Nimg;
i++)
884 affineTransformations.push_back(emptyRow);
885 correlationList.push_back(-1);
889 FileName fn_tmp = fnRoot+
"_transformations.txt";
893 fhIn.open(fn_tmp.c_str());
901 if (linesRead<affineTransformations.size()-1 && line!=
"")
903 std::vector< double > data;
907 double bestDistance=
ABS(tilt-tiltList[0]);
908 for (
int j=0;
j<Nimg;
j++)
911 if (bestDistance>distance)
917 affineTransformations[
i][i+1].resize(3,3);
918 affineTransformations[
i][i+1].initIdentity(3);
919 affineTransformations[
i][i+1](0,0)=data[2];
920 affineTransformations[
i][i+1](1,0)=data[3];
921 affineTransformations[
i][i+1](0,1)=data[4];
922 affineTransformations[
i][i+1](1,1)=data[5];
923 affineTransformations[
i][i+1](0,2)=data[6];
924 affineTransformations[
i][i+1](1,2)=data[7];
926 affineTransformations[i+1][
i]=
927 affineTransformations[
i][i+1].inv();
933 int maxShift=
FLOOR(
XSIZE(*img[0])*maxShiftPercentage);
935 Aij=affineTransformations[
i][i+1];
936 Aji=affineTransformations[i+1][
i];
938 std::cout <<
"Transformation between " << i <<
" (" 939 << tiltList[
i] <<
") and " << i+1 <<
" (" 940 << tiltList[i+1] << std::endl;
942 maxShift, maxIterDE, Aij, Aji,
943 showAffine, thresholdAffine, globalAffine,
944 isMirror,
true, pyramidLevel);
947 affineTransformations[
i][i+1].clear();
948 affineTransformations[i+1][
i].clear();
949 writeTransformations(fn_tmp);
959 computeAffineTransformations(globalAffine);
962 showRefinement =
false;
965 if (!useCriticalPoints)
971 avgForwardPatchCorr.initZeros(Nimg);
972 avgBackwardPatchCorr.initZeros(Nimg);
973 avgForwardPatchCorr.initConstant(1);
974 avgBackwardPatchCorr.initConstant(1);
976 for (
int ii=0; ii<Nimg; ++ii)
990 rjj=affineTransformations[ii][ii+1]*rii;
991 refineLandmark(ii,ii+1,rii,rjj,corrList(
i),
false);
993 while (corrList(
i)<-0.99);
995 avgForwardPatchCorr(ii)=corrList.
computeAvg();
1009 rjj=affineTransformations[ii][ii-1]*rii;
1010 refineLandmark(ii,ii-1,rii,rjj,corrList(
i),
false);
1012 while (corrList(
i)<-0.99);
1014 avgBackwardPatchCorr(ii)=corrList.
computeAvg();
1017 std::cout <<
"Image " << ii <<
" Average correlation forward=" 1018 << avgForwardPatchCorr(ii)
1019 <<
" backward=" << avgBackwardPatchCorr(ii) << std::endl;
1025 if (!doNotIdentifyOutliers)
1027 identifyOutliers(
false);
1028 computeAffineTransformations(
false);
1029 identifyOutliers(
true);
1032 isOutlier.initZeros(Nimg);
1052 int thread_id = master->myThreadID;
1053 int Nimg=parent->
Nimg;
1055 const std::vector< std::vector< Matrix2D<double> > > &affineTransformations=
1059 auto deltaShift=(int)
floor(
XSIZE(*(parent->
img)[0])/gridSamples);
1060 master->chainList=
new std::vector<LandmarkChain>;
1066 int includedPoints=0;
1068 for (
int nx=thread_id; nx<gridSamples; nx+=numThreads)
1071 for (
int ny=0; ny<gridSamples; ny+=1)
1074 for (
int ii=0; ii<=Nimg-1; ++ii)
1077 if (!(*(parent->
maskImg[ii]))((
int)
YY(rii),(
int)
XX(rii)))
1093 bool acceptLandmark=
true;
1101 while (jj>=0 && acceptLandmark && !visited(jj) &&
1111 Aij=affineTransformations[jj][jj_1];
1112 Aji=affineTransformations[jj_1][jj];
1132 acceptLandmark=
true;
1138 while (jj<Nimg && acceptLandmark && !visited(jj) &&
1148 Aij=affineTransformations[jj_1][jj];
1149 Aji=affineTransformations[jj][jj_1];
1169 std::cout <<
"img=" << ii <<
" chain length=" 1170 << chain.
size() <<
" [" << jjleft
1171 <<
" - " << jjright <<
"]";
1177 bool accepted=parent->
refineChain(chain,corrChain);
1181 std::cout <<
" Accepted with length= " 1183 <<
" [" << chain[0].imgIdx <<
" - " 1184 << chain[chain.size()-1].imgIdx <<
"]= ";
1185 for (
int i=0;
i<chain.size();
i++)
1186 std::cout << chain[
i].imgIdx <<
" ";
1189 master->chainList->push_back(chain);
1190 includedPoints+=chain.size();
1194 std::cout << std::endl;
1199 std::cout <<
"Point nx=" << nx <<
" ny=" << ny
1200 <<
" Number of points=" 1202 <<
" Number of chains=" << master->chainList->size()
1203 <<
" ( " << ((double) includedPoints)/
1204 master->chainList->size() <<
" )\n";
1221 int thread_id = master->myThreadID;
1222 int Nimg=parent->
Nimg;
1224 const std::vector< std::vector< Matrix2D<double> > > &affineTransformations=
1228 auto deltaShift=(int)
floor(
XSIZE(*(parent->
img)[0])/gridSamples);
1229 master->chainList=
new std::vector<LandmarkChain>;
1235 int includedPoints=0;
1237 for (
int nx=thread_id; nx<gridSamples; nx+=numThreads)
1240 for (
int ny=0; ny<gridSamples; ny+=1)
1243 for (
int ii=0; ii<=Nimg-1; ++ii)
1246 if (!(*(parent->
maskImg[ii]))((
int)
YY(rii),(
int)
XX(rii)))
1260 bool acceptLandmark=
true;
1269 while (jj>=0 && acceptLandmark && sideLength<maxSideLength &&
1278 Aij=affineTransformations[jj][jj_1];
1279 Aji=affineTransformations[jj_1][jj];
1281 auto iYYrjj=(int)
YY(rjj);
1282 auto iXXrjj=(int)
XX(rjj);
1283 if (!(*(parent->
maskImg[jj])).outside(iYYrjj,iXXrjj))
1284 acceptLandmark=(*(parent->
maskImg[jj]))(iYYrjj,iXXrjj);
1286 acceptLandmark=
false;
1303 acceptLandmark=
true;
1310 while (jj<Nimg && acceptLandmark && sideLength<maxSideLength &&
1319 Aij=affineTransformations[jj_1][jj];
1320 Aji=affineTransformations[jj][jj_1];
1322 auto iYYrjj=(int)
YY(rjj);
1323 auto iXXrjj=(int)
XX(rjj);
1324 if (!(*(parent->
maskImg[jj])).outside(iYYrjj,iXXrjj))
1325 acceptLandmark=(*(parent->
maskImg[jj]))(iYYrjj,iXXrjj);
1327 acceptLandmark=
false;
1344 std::cout <<
"img=" << ii <<
" chain length=" 1345 << chain.
size() <<
" [" << jjleft
1346 <<
" - " << jjright <<
"]\n";
1349 master->chainList->push_back(chain);
1350 includedPoints+=chain.size();
1353 std::cout <<
"Point nx=" << nx <<
" ny=" << ny
1354 <<
" Number of points=" 1356 <<
" Number of chains=" << master->chainList->size()
1357 <<
" ( " << ((double) includedPoints)/
1358 master->chainList->size() <<
" )\n";
1376 int thread_id = master->myThreadID;
1377 int Nimg=parent->
Nimg;
1379 const std::vector< std::vector< Matrix2D<double> > > &affineTransformations=
1382 master->chainList=
new std::vector<LandmarkChain>;
1383 std::vector<LandmarkChain> candidateChainList;
1391 mask.
resize(2*radius+1,2*radius+1);
1396 for (
int ii=thread_id; ii<=Nimg-1; ii+=numThreads)
1413 FilterBP.
w1=2.0/
XSIZE(Ifiltered);
1414 FilterBP.
w2=128.0/
XSIZE(Ifiltered);
1428 std::vector< Matrix1D<double> > Q;
1430 if (Ifiltered(
i,
j)<th && largeMask(
i,
j))
1433 bool localMinimum=
true;
1455 size_t qmax=Q.size();
1457 minDistance.
resize(qmax);
1459 for (
size_t q1=0;
q1<qmax;
q1++)
1460 for (
size_t q2=
q1+1;
q2< qmax;
q2++)
1463 double diffY=
YY(Q[q1])-
YY(Q[q2]);
1464 double d12=diffX*diffX+diffY*diffY;
1465 if (d12<minDistance(q1))
1466 minDistance(q1)=d12;
1467 if (d12<minDistance(q2))
1468 minDistance(q2)=d12;
1472 std::vector< Matrix1D<double> > Qaux;
1474 for (
int q=0; q<qlimit; q++)
1475 Qaux.push_back(Q[idxDistanceSort(qmax-1-q)-1]);
1486 for (
int q=0; q<qmax; q++)
1501 int jjmin=
XMIPP_MAX(0,ii-halfSeqLength);
1505 for (
int jj=jjmax; jj>=jjmin; --jj)
1512 Aij=affineTransformations[jj][jj_1];
1513 Aji=affineTransformations[jj_1][jj];
1526 jjmax=
XMIPP_MIN(Nimg-1,ii+halfSeqLength);
1528 for (
int jj=jjmin; jj<=jjmax; ++jj)
1535 Aij=affineTransformations[jj_1][jj];
1536 Aji=affineTransformations[jj][jj_1];
1549 candidateChainList.push_back(chain);
1558 for (
int iq=0; iq<imax; iq++)
1560 int q=idx(
XSIZE(idx)-1-iq)-1;
1563 master->chainList->push_back(candidateChainList[q]);
1566 std::cout <<
"Corr " << iq <<
": " << corrQ(q) <<
":";
1567 for (
int i=0;
i<candidateChainList[q].size();
i++)
1568 std::cout << candidateChainList[q][
i].imgIdx <<
" ";
1569 std::cout << std::endl;
1574 std::cout <<
"It's recommended to reduce the number of critical points\n";
1576 candidateChainList.
clear();
1582 save.
write(
"PPPoriginal.xmp");
1584 save.
write(
"PPPfiltered.xmp");
1587 if (Ifiltered(
i,
j)>=th || !largeMask(
i,
j))
1589 for (
int q=0; q<Q.size(); q++)
1593 save(
YY(Q[q])+
i,
XX(Q[q])+
j)=minval;
1595 for (
int iq=0; iq<imax; iq++)
1597 int q=idx(
XSIZE(idx)-1-iq)-1;
1601 save(
YY(Q[q])+
i,
XX(Q[q])+
j)=(minval+th)/2;
1604 save.
write(
"PPPcritical.xmp");
1605 std::cout <<
"Number of critical points=" << Q.size() << std::endl;
1606 std::cout <<
"CorrQ stats:";
1608 std::cout << std::endl;
1609 std::cout <<
"Press any key\n";
1626 for (
size_t i=0;
i<img.size();
i++)
1631 if (!maskImg.empty())
1633 for (
size_t i=0;
i<maskImg.size();
i++)
1641 FileName fn_tmp = fnRoot+
"_landmarks.txt";
1644 auto * th_ids =
new pthread_t[numThreads];
1647 for(
int nt = 0 ; nt < numThreads ; nt ++ )
1649 th_args[nt].
parent =
this;
1650 th_args[nt].myThreadID = nt;
1651 if (useCriticalPoints)
1657 std::vector<LandmarkChain> chainList;
1658 int includedPoints=0;
1659 for(
int nt = 0 ; nt < numThreads ; nt ++ )
1661 pthread_join(*(th_ids+nt),
nullptr);
1662 int imax=th_args[nt].chainList->size();
1663 for (
int i=0;
i<imax;
i++)
1665 chainList.push_back((*th_args[nt].chainList)[
i]);
1666 includedPoints+=(*th_args[nt].chainList)[i].size();
1668 delete th_args[nt].chainList;
1674 if (blindSeqLength>0)
1676 th_ids =
new pthread_t[numThreads];
1678 for(
int nt = 0 ; nt < numThreads ; nt ++ )
1680 th_args[nt].
parent =
this;
1681 th_args[nt].myThreadID = nt;
1685 for(
int nt = 0 ; nt < numThreads ; nt ++ )
1687 pthread_join(*(th_ids+nt),
nullptr);
1688 int imax=th_args[nt].chainList->size();
1689 for (
int i=0;
i<imax;
i++)
1691 chainList.push_back((*th_args[nt].chainList)[
i]);
1692 includedPoints+=(*th_args[nt].chainList)[i].size();
1694 delete th_args[nt].chainList;
1701 allLandmarksX.resize(chainList.size(),Nimg);
1702 allLandmarksY.resize(chainList.size(),Nimg);
1703 allLandmarksX.initConstant(
XSIZE(*img[0]));
1704 allLandmarksY.initConstant(
YSIZE(*img[0]));
1705 for (
size_t i=0;
i<chainList.size();
i++)
1707 for (
size_t j=0;
j<chainList[
i].size();
j++)
1709 int idx=chainList[
i][
j].imgIdx;
1710 allLandmarksX(
i,idx)=chainList[
i][
j].x;
1711 allLandmarksY(
i,idx)=chainList[
i][
j].y;
1716 if (includedPoints>0)
1718 writeLandmarkSet(fnRoot+
"_landmarks.txt");
1719 std::cout <<
" Number of points=" 1721 <<
" Number of chains=" << chainList.size()
1722 <<
" ( " << ((double) includedPoints)/chainList.size() <<
" )\n";
1729 readLandmarkSet(fnRoot+
"_landmarks.txt");
1737 bool tryFourier)
const 1752 bool reversed=isCapillar &&
ABS(ii-jj)>Nimg/2;
1760 (
int)(
YY(rii)+
i),(
int)(
XX(rii)+
j));
1765 save.
write(
"PPPpieceii.xmp");
1766 std::cout <<
"ii=" << ii <<
" jj=" << jj << std::endl;
1767 std::cout <<
"rii=" << rii.
transpose() << std::endl;
1771 double actualCorrThreshold=corrThreshold;
1772 if (actualCorrThreshold<0 &&
VEC_XSIZE(avgForwardPatchCorr)>0)
1775 actualCorrThreshold=
XMIPP_MIN(avgForwardPatchCorr(ii),
1776 avgBackwardPatchCorr(jj));
1778 actualCorrThreshold=
XMIPP_MIN(avgBackwardPatchCorr(ii),
1779 avgForwardPatchCorr(jj));
1782 std::cout <<
"actualCorrThreshold=" << actualCorrThreshold << std::endl;
1798 (
int)(
YY(rjj)+
i),(
int)(
XX(rjj)+
j));
1806 save.
write(
"PPPpiecejjOriginal.xmp");
1807 std::cout <<
"Corr original=" << corrOriginal << std::endl;
1811 double shiftX,shiftY;
1818 if (corrFFT>corrOriginal)
1828 save.
write(
"PPPpiecejjFFT.xmp");
1829 std::cout <<
"FFT shift=" << fftShift.
transpose() << std::endl;
1830 std::cout <<
"Corr FFT=" << corrFFT << std::endl;
1831 if (corrFFT>corrOriginal)
1840 (
int)(
YY(rjj)+
i),(
int)(
XX(rjj)+
j));
1842 save.
write(
"PPPpiecejjNew.xmp");
1849 bool retval=refineLandmark(pieceii,jj,rjj,actualCorrThreshold,
1856 bool reversed,
double &maxCorr)
const 1858 int halfSize=
XSIZE(pieceii)/2;
1860 double mean_ii=0, stddev_ii=0;
1867 stddev_ii = stddev_ii /
MULTIDIM_SIZE(pieceii) - mean_ii * mean_ii;
1869 stddev_ii =
sqrt(static_cast<double>((
ABS(stddev_ii))));
1886 std::queue< Matrix1D<double> > Q;
1891 auto shifty=(int)
YY(Q.front());
1892 auto shiftx=(int)
XX(Q.front());
1894 if (!corr(shifty,shiftx)<-1)
1898 double mean_jj=0, stddev_jj=0;
1908 (
int)(
YY(rjj)+shifty+
i),
1909 (
int)(
XX(rjj)+shiftx+
j));
1912 stddev_jj+=pixval*pixval;
1915 stddev_jj = stddev_jj /
MULTIDIM_SIZE(piecejj) - mean_jj * mean_jj;
1917 stddev_jj =
sqrt(static_cast<double>((
ABS(stddev_jj))));
1922 corr(shifty,shiftx)=0;
1923 double &corrRef=corr(shifty,shiftx);
1926 double istddev_jj=1.0/stddev_jj;
1939 for (
int step=1; step<=5; step+=2)
1940 for (
int stepy=-1; stepy<=1; stepy++)
1941 for (
int stepx=-1; stepx<=1; stepx++)
1943 int newshifty=shifty+stepy*step;
1944 int newshiftx=shiftx+stepx*step;
1953 if (corr(newshifty,newshiftx)<-1)
1954 Q.push(
vectorR2(newshiftx,newshifty));
1959 if (maxval>actualCorrThreshold)
1972 piecejj(
i,
j)=(*img[jj])((
int)(
YY(rjj)+
i),(
int)(
XX(rjj)+
j));
1976 save.
write(
"PPPpiecejj.xmp");
1978 save.
write(
"PPPcorr.xmp");
1979 std::cout <<
"jj=" << jj <<
" rjj=" << rjj.
transpose() << std::endl;
1980 std::cout <<
"imax=" << imax <<
" jmax=" << jmax << std::endl;
1981 std::cout <<
"maxval=" << maxval << std::endl;
1986 std::cout <<
"Accepted=" << accept << std::endl;
1987 std::cout <<
"Press any key\n";
2001 std::cout <<
"Chain for refinement: ";
2002 for (
int i=0;
i<chain.size();
i++)
2003 std::cout << chain[
i].imgIdx <<
" ";
2004 std::cout << std::endl;
2007 for (
int K=0;
K<2 && (chain.size()>seqLength || useCriticalPoints);
K++)
2009 sort(chain.begin(), chain.end());
2012 if (useCriticalPoints)
2016 int chainLength=chain.size();
2020 for (
int n=0;
n<chainLength;
n++)
2022 int ii=chain[
n].imgIdx;
2033 pieceAux(
i,
j)=(*img[ii])((
int)(
YY(rii)+
i),(
int)(
XX(rii)+
j));
2034 if (isCapillar &&
ABS(chain[
n].imgIdx-chain[0].imgIdx)>Nimg/2)
2038 avgPiece/=chainLength;
2043 save.
write(
"PPPavg.xmp");
2044 std::cout <<
"Average " <<
K <<
" ------------------- \n";
2045 showRefinement=
true;
2050 for (
int j=0;
j<chainLength;
j++)
2052 int jj=chain[
j].imgIdx;
2055 bool accepted=refineLandmark(avgPiece,jj,rjj,0,
false,corr);
2065 showRefinement=
false;
2072 for (
int step=2; step<=maxStep; step++)
2074 int chainLength=chain.size();
2078 for (
int i=0;
i<chainLength-step;
i++)
2080 int ii=chain[
i].imgIdx;
2082 int jj=chain[
i+step].imgIdx;
2086 bool accepted=refineLandmark(ii,jj,rii,newrjj,corr,
false);
2087 if (((newrjj-rjj).module()<4 && accepted) || useCriticalPoints)
2089 chain[
i+step].x=
XX(newrjj);
2090 chain[
i+step].y=
YY(newrjj);
2100 int iright=chainLength;
2102 for (
int i=chainLength-1;
i>=1;
i--)
2104 int ii=chain[
i].imgIdx;
2106 int jj=chain[
i-1].imgIdx;
2110 bool accepted=refineLandmark(ii,jj,rii,newrjj,corr,
false);
2112 if (((newrjj-rjj).module()<4 && accepted) || useCriticalPoints)
2114 chain[
i-1].x=
XX(newrjj);
2115 chain[
i-1].y=
YY(newrjj);
2125 for (
int ll=ileft+1; ll<=iright-1; ll++)
2126 refinedChain.push_back(chain[ll]);
2128 double tilt0=tiltList[chain[0].imgIdx];
2129 double tiltF=tiltList[chain[chain.size()-1].imgIdx];
2131 if (chain.size()<lengthThreshold && !useCriticalPoints)
2138 double tilt0=tiltList[chain[0].imgIdx];
2139 double tiltF=tiltList[chain[chain.size()-1].imgIdx];
2141 return chain.size()>lengthThreshold;
2148 std::ofstream fhOut;
2149 fhOut.open(fnLandmark.c_str());
2152 fhOut <<
"Point x y slice color " 2158 if (allLandmarksX(
j,
i)!=
XSIZE(*img[0]))
2160 fhOut << counter <<
" \t" 2163 <<
i+1 <<
" \t" <<
j << std::endl;
2173 fhIn.open(fnLandmark.c_str());
2176 std::string dummyStr;
2178 fhIn >> dummyStr >> dummyStr >> dummyStr >> dummyStr >> dummyStr
2179 >> Nlandmark >> Nimg;
2182 allLandmarksX.resize(Nlandmark,Nimg);
2183 allLandmarksY.resize(Nlandmark,Nimg);
2184 allLandmarksX.initConstant(
XSIZE(*img[0]));
2185 allLandmarksY.initConstant(
XSIZE(*img[0]));
2186 fhIn.exceptions ( std::ifstream::eofbit |
2187 std::ifstream::failbit |
2188 std::ifstream::badbit );
2193 int dummyInt,
x,
y,
i,
j;
2194 fhIn >> dummyInt >> x >> y >> i >>
j;
2196 allLandmarksX(j,i)=x+
STARTINGX(*img[0]);
2197 allLandmarksY(j,i)=y+
STARTINGY(*img[0]);
2199 catch (std::ifstream::failure e)
2205 std::cout <<
"The file " << fnLandmark <<
" has been read for the landmarks\n" 2206 << Nlandmark <<
" landmarks are read\n";
2211 const FileName &fnTransformations)
const 2213 std::ofstream fhOut;
2214 fhOut.open(fnTransformations.c_str());
2217 int imax=affineTransformations.size();
2219 for (
int i=0;
i<imax;
i++)
2221 int jmax=affineTransformations[
i].size();
2222 for (
int j=
i+1;
j<jmax;
j++)
2225 fhOut << tiltList[
i] <<
"\t0.0\t" 2226 << affineTransformations[
i][
j](0,0) <<
"\t" 2227 << affineTransformations[
i][j](1,0) <<
"\t" 2228 << affineTransformations[
i][
j](0,1) <<
"\t" 2229 << affineTransformations[
i][j](1,1) <<
"\t" 2230 << affineTransformations[
i][
j](0,2) <<
"\t" 2231 << affineTransformations[
i][j](1,2) << std::endl;
2235 if (counter==imax-1)
2237 fhOut << tiltList[tiltList.size()-1] <<
"\t0.0\t1.0\t0.0\t0.0\t1.0\t0.0\t0.0\n";
2238 fhOut <<
"1 0 0 1 0 0\n";
2254 if (allLandmarksX(
j,
i)!=
XSIZE(*img[0]))
2258 allLandmarksX(
j,
i)=
XX(r);
2259 allLandmarksY(
j,
i)=
YY(r);
2268 if (allLandmarksX(
j,iMinTilt)!=
XSIZE(*img[0]))
2281 std::cout <<
"Average height of the landmarks at 0 degrees=" << z0 << std::endl;
2284 std::unique_ptr<MetaDataVec::id_iterator> idIter;
2286 if (!fnSelOrig.empty())
2288 idIter = memoryUtils::make_unique<MetaDataVec::id_iterator>(SForig.ids().begin());
2290 DF.
setComment(
"First shift by -(shiftX,shiftY), then rotate by psi");
2298 for (
int n=0;
n<Nimg;
n++)
2301 I.
read(name_list[
n]);
2314 double minval, maxval, avg, stddev;
2319 else if (!dontNormalize)
2322 double tilt=tiltList[
n];
2325 fn_corrected.
compose(n+1, fnRoot+
"_corrected_",
"stk");
2326 I.
write(fn_corrected);
2332 SForig.getValue(
MDL_IMAGE, auxFn, **idIter);
2334 Iorig.
read( auxFn );
2342 (((
double)
XSIZE(Iorig()))/
XSIZE(I())),0),M1);
2356 else if (!dontNormalize)
2359 fn_corrected.
compose(n+1, fnRoot+
"_corrected_originalsize_",
"stk");
2360 Iorig.
write(fn_corrected);
2370 DF.
write(fnRoot+
"_correction_parameters.txt");
2374 save().initZeros(*img[0]);
2375 save().setXmippOrigin();
2376 for (
int j=0;
j<
YSIZE(allLandmarksX);
j++)
2377 if (allLandmarksX(
j,iMinTilt)!=
XSIZE(*img[0]))
2383 RtiltYmin.resize(3,3);
2385 std::cout << rjp.
transpose() << std::endl;
2386 for (
int i=0;
i<
XSIZE(allLandmarksX);
i++)
2387 if (allLandmarksX(
j,
i)!=
XSIZE(*img[0]))
2389 save(allLandmarksY(
j,
i),allLandmarksX(
j,
i))=
ABS(
i-iMinTilt);
2405 save(
YY(p),
XX(p))=-
ABS(i-iMinTilt);
2408 save.
write(
"PPPmovementsLandmarks0.xmp");
2417 std::vector<int> emptyVector;
2421 Vseti.push_back(emptyVector);
2425 Vsetj.push_back(emptyVector);
2429 if (allLandmarksX(
j,
i)!=
XSIZE(*img[0]))
2431 Vseti[
i].push_back(
j);
2432 Vsetj[
j].push_back(
i);
2439 for (
int i=0;
i<Nimg;
i++)
2441 ni(
i)=
static_cast<int>(Vseti[
i].size());
2443 for (
int jj=0; jj<
ni(
i); jj++)
2446 XX(pi)+=allLandmarksX(j,
i);
2447 YY(pi)+=allLandmarksY(j,
i);
2450 barpi.push_back(pi);
2458 std::cout <<
"Removing outliers ...\n";
2467 std::vector<bool> validLandmark;
2468 int invalidCounter=0;
2470 for (
int j=0;
j<Nlandmark;
j++)
2474 validLandmark.push_back(
false);
2478 validLandmark.push_back(
true);
2484 for (
int j=0;
j<Nlandmark;
j++)
2486 if (!validLandmark[
j])
2488 for (
int i=0;
i<Nimg;
i++)
2490 newAllLandmarksX(jj,
i)=allLandmarksX(j,
i);
2491 newAllLandmarksY(jj,
i)=allLandmarksY(j,
i);
2495 allLandmarksX=newAllLandmarksX;
2496 allLandmarksY=newAllLandmarksY;
2498 std::cout << invalidCounter <<
" out of " << Nlandmark
2499 <<
" landmarks have been removed\n";
2513 alignment.
tilt=p[2];
2521 std::cerr <<
"generateLandmarkSet"<< std::endl;
2522 generateLandmarkSet();
2523 std::cerr <<
"produceInformationFromLandmarks" << std::endl;
2524 produceInformationFromLandmarks();
2525 std::cerr <<
"alignment" << std::endl;
2529 double bestError=0, bestRot=-1;
2530 for (
double rot=0; rot<=180-deltaRot; rot+=deltaRot)
2534 double error=alignment->optimizeGivenAxisDirection();
2537 std::cout <<
"rot= " << rot
2538 <<
" error= " << error << std::endl;
2541 if (bestRot<0 || bestError>error)
2545 *bestPreviousAlignment=*alignment;
2549 std::cout <<
"Best rot=" << bestRot
2550 <<
" Best error=" << bestError << std::endl;
2554 axisAngles(0)=bestRot;
2557 if (!optimizeTiltAngle)
2563 0.01,fitness,iter,steps,
true);
2566 for (
int i=0;
i<3;
i++)
2569 bestPreviousAlignment->rot=axisAngles(0);
2570 bestPreviousAlignment->tilt=axisAngles(1);
2571 fitness=bestPreviousAlignment->optimizeGivenAxisDirection();
2572 bestPreviousAlignment->computeErrorForLandmarks();
2575 removeOutlierLandmarks(*bestPreviousAlignment);
2576 produceInformationFromLandmarks();
2577 delete bestPreviousAlignment;
2578 bestPreviousAlignment=
new Alignment(
this);
2579 bestPreviousAlignment->rot=axisAngles(0);
2580 bestPreviousAlignment->tilt=axisAngles(1);
2581 fitness=bestPreviousAlignment->optimizeGivenAxisDirection();
2585 0.01,fitness,iter,steps,
true);
2587 bestPreviousAlignment->rot=axisAngles(0);
2588 bestPreviousAlignment->tilt=axisAngles(1);
2589 fitness=bestPreviousAlignment->optimizeGivenAxisDirection();
2592 writeLandmarkSet(fnRoot+
"_good_landmarks.txt");
2593 std::ofstream fh_out;
2594 fh_out.open((fnRoot+
"_alignment.txt").c_str());
2597 (std::string)
"Cannot open "+fnRoot+
"_alignment.txt for output");
2598 fh_out << *bestPreviousAlignment;
2603 writeLandmarkSet(fnRoot+
"_good_landmarks_corrected.txt");
2604 delete bestPreviousAlignment;
2612 double bestError=1e38;
2613 bool firstIteration=
true, finish=
false;
2615 computeGeometryDependentOfAxis();
2618 computeGeometryDependentOfRotation();
2619 double error=computeError();
2622 std::cout <<
"it=" << Niterations <<
" Error= " << error
2623 <<
" raxis=" << raxis.transpose() << std::endl;
2631 firstIteration=
false;
2635 finish=((error>bestError) || (Niterations>1000) ||
2636 (
ABS(error-bestError)/bestError<0.001)) && Niterations>20;
2637 if (error<bestError)
2658 for (
int i=0;
i<Nimg;
i++)
2661 Aipt[
i](0,0)=Aip[
i](0,0)=Raxis(0,0);
2662 Aipt[
i](1,0)=Aip[
i](0,1)=Raxis(0,1);
2663 Aipt[
i](2,0)=Aip[
i](0,2)=Raxis(0,2);
2664 Aipt[
i](0,1)=Aip[
i](1,0)=Raxis(1,0);
2665 Aipt[
i](1,1)=Aip[
i](1,1)=Raxis(1,1);
2666 Aipt[
i](2,1)=Aip[
i](1,2)=Raxis(1,2);
2670 Binvraxis+=((double)
prm->ni(
i))*(B+B.transpose());
2673 B2i[
i]=B1i[
i]*Ip*Raxis;
2683 Binvraxis=Binvraxis.
inv();
2692 for (
int i=0;
i<Nimg;
i++)
2698 Ai[
i]=Rinplane*Aip[
i];
2700 diaxis[
i]=Rinplane*(I-Aip[
i])*raxis;
2717 for (
int i=0;
i<Nimg;
i++)
2719 int jjmax=
prm->Vseti[
i].size();
2720 for (
int jj=0; jj<jjmax; jj++)
2722 int j=
prm->Vseti[
i][jj];
2723 pijp=Ai[
i]*rj[
j]+
di[
i]+diaxis[
i];
2728 error+=diffx*diffx+diffy*diffy;
2732 return sqrt(error/N);
2738 errorLandmark.initZeros(Nlandmark);
2739 for (
int j=0;
j<Nlandmark;
j++)
2742 for (
int i=0;
i<Nimg;
i++)
2750 errorLandmark(
j)+=
sqrt(diffx*diffx+diffy*diffy);
2754 errorLandmark(
j)/=counterj;
2769 std::cout <<
"Step 0: error=" << computeError() << std::endl;
2773 for (
int j=0;
j<Nlandmark;
j++)
2778 int iimax=
prm->Vsetj[
j].size();
2779 for (
int ii=0; ii<iimax; ii++)
2781 int i=
prm->Vsetj[
j][ii];
2785 b+=Ait[
i]*(pij-(
di[
i]+diaxis[
i]));
2792 std::cout <<
"Step 1: error=" << computeError() << std::endl;
2796 for (
int i=0;
i<Nimg;
i++)
2798 barri[
i].initZeros();
2799 for (
int jj=0; jj<
prm->ni(
i); jj++)
2801 int j=
prm->Vseti[
i][jj];
2804 barri[
i]/=
prm->ni(
i);
2808 if (
prm->isCapillar)
2812 for (
int i=0;
i<Nimg;
i++)
2814 for (
int jj=0; jj<
prm->ni(
i); jj++)
2816 int j=
prm->Vseti[
i][jj];
2819 tmp2+=B1i[
i]*Pij-B2i[
i]*rj[
j];
2823 tmp2=Binvraxis*tmp2;
2826 computeGeometryDependentOfRotation();
2831 for (
int i=0;
i<Nimg;
i++)
2832 if (
i!=
prm->iMinTilt)
2834 di[
i] =
prm->barpi[
i]-Ai[
i]*barri[
i]-diaxis[
i];
2836 if (
di[
i].isAnyNaN())
2841 std::cout <<
"Step 2: error=" << computeError() << std::endl;
2849 for (
int i=0;
i<Nimg;
i++)
2852 dim.fromVector(
di[
i]+diaxis[
i]);
2853 for (
int jj=0; jj<
prm->ni(i); jj++)
2855 int j=
prm->Vseti[
i][jj];
2856 Aiprj.fromVector(Aip[i]*rj[j]);
2857 Aiprjt=Aiprj.transpose();
2861 Ri+=(dim-Pij)*Aiprjt;
2863 psi(i)=
CLIP(
RAD2DEG(atan(((Ri(0,1)-Ri(1,0))/(Ri(0,0)+Ri(1,1))))),
2864 -(
prm->psiMax),
prm->psiMax);
2870 std::cout <<
"Step 3: error=" << computeError() << std::endl;
2874 computeGeometryDependentOfRotation();
2881 out <<
"Alignment parameters ===========================================\n" 2882 <<
"rot=" << alignment.
rot <<
" tilt=" << alignment.
tilt << std::endl
2884 out <<
"Images ---------------------------------------------------------\n";
2885 for (
int i=0;
i<alignment.
Nimg;
i++)
2886 out <<
"Image " <<
i <<
" psi= " << alignment.
psi(
i)
2887 <<
" di= " << alignment.
di[
i].transpose()
2888 <<
" diaxis= " << alignment.
diaxis[
i].transpose()
2889 <<
" (" << alignment.
prm->
ni(
i) <<
")\n";
2890 out <<
"Landmarks ------------------------------------------------------\n";
2893 out <<
"Landmark " <<
j <<
" rj= " << alignment.
rj[
j].transpose()
#define FOR_ALL_ELEMENTS_IN_MATRIX2D(m)
#define VECTOR_R2(v, x, y)
bool globalAffine
Look for local affine transformation.
void init_progress_bar(long total)
void selfWindow(int n0, int z0, int y0, int x0, int nF, int zF, int yF, int xF, T init_value=0)
bool refineLandmark(int ii, int jj, const Matrix1D< double > &rii, Matrix1D< double > &rjj, double &maxCorr, bool tryFourier) const
std::vector< Matrix1D< double > > di
#define A2D_ELEM(v, i, j)
void * threadgenerateLandmarkSetGrid(void *args)
void resize(size_t Ndim, size_t Zdim, size_t Ydim, size_t Xdim, bool copy=true)
double maxShiftPercentage
Maxshift percentage.
const ProgTomographAlignment * prm
std::vector< LandmarkChain > * chainList
std::vector< Matrix1D< double > > rj
__host__ __device__ float2 floor(const float2 v)
n The following was calculated during iteration
double alignImages(const MultidimArray< double > &Iref, const AlignmentTransforms &IrefTransforms, MultidimArray< double > &I, Matrix2D< double > &M, bool wrap, AlignmentAux &aux, CorrelationAux &aux2, RotationalCorrelationAux &aux3)
#define REPORT_ERROR(nerr, ErrormMsg)
size_t seqLength
Sequence Length.
void Euler_direction(double alpha, double beta, double gamma, Matrix1D< double > &v)
void defineParams()
Usage.
void generateMask(const MultidimArray< double > &I, MultidimArray< unsigned char > &mask, int patchSize)
MultidimArray< double > errorLandmark
Matrix1D< int > isOutlier
void sqrt(Image< double > &op)
std::vector< MultidimArray< unsigned char > * > maskImg
void run()
Run: do the real work.
void BinaryCircularMask(MultidimArray< int > &mask, double radius, int mode, double x0, double y0, double z0)
Couldn't write to file.
std::vector< MultidimArray< double > * > Mask2
void readFloatList(const char *str, int N, std::vector< T > &v)
void write(const FileName &name="", size_t select_img=ALL_IMAGES, bool isStack=false, int mode=WRITE_OVERWRITE, CastWriteMode castMode=CW_CAST, int _swapWrite=0)
double correlationIndex(const MultidimArray< T > &x, const MultidimArray< T > &y, const MultidimArray< int > *mask=NULL, MultidimArray< double > *Contributions=NULL)
Matrix1D< double > minAllowed
void compose(const String &str, const size_t no, const String &ext="")
void inv(Matrix2D< T > &result) const
int maxIterDE
Maximum number of iterations in Differential Evolution.
void abs(Image< double > &op)
const FileName & name() const
double percentil(double percent_mass)
Matrix2D< T > transpose() const
FileName fnRoot
Output root.
Matrix1D< double > maxAllowed
~ProgTomographAlignment()
Destructor.
static double Powell_affine_fitness_individual(double *p, void *prm)
bool refineChain(LandmarkChain &chain, double &corrChain)
void bestNonwrappingShift(const MultidimArray< double > &I1, const MultidimArray< double > &I2, double &shiftX, double &shiftY, CorrelationAux &aux)
void computeAffineTransformations(bool globalAffineToUse)
Compute affine transformations.
void computeGeometryDependentOfAxis()
void computeStats_within_binary_mask(const MultidimArray< T1 > &mask, const MultidimArray< T > &m, double &min_val, double &max_val, double &avg, double &stddev)
Incorrect number of objects in Metadata.
void removeOutlierLandmarks(const Alignment &alignment)
Remove Outliers.
void * threadgenerateLandmarkSetCriticalPoints(void *args)
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
Matrix1D< T > transpose() const
size_t Ncritical
Number of critical points.
std::vector< std::string > name_list
double tilt(const size_t n=0) const
#define MAT_ELEM(m, i, j)
void readParams()
Read parameters from argument line.
#define FOR_ALL_ELEMENTS_IN_MATRIX1D(v)
int gridSamples
Grid samples.
void generateLandmarkSet()
Generate landmark set using a grid.
std::vector< MultidimArray< double > * > I2
std::vector< Matrix1D< double > > diaxis
void compute_hist(const MultidimArrayGeneric &array, Histogram1D &hist, int no_steps)
void erode2D(const MultidimArray< double > &in, MultidimArray< double > &out, int neig, int count, int size)
void binarize(double val=0, double accuracy=XMIPP_EQUAL_ACCURACY, MultidimArray< int > *mask=NULL)
int numThreads
Number of threads to use for parallel computing.
void reject_outliers(T &v, double percentil_out=0.25)
void computeErrorForLandmarks()
void computeGeometryDependentOfRotation()
void alignImages(const Alignment &alignment)
Align images.
#define XMIPP_EQUAL_ACCURACY
std::vector< Landmark > LandmarkChain
void resize(size_t Xdim, bool copy=true)
void progress_bar(long rlen)
void write(const FileName &fn) const
double thresholdAffine
Threshold affine.
void show()
Show parameters.
std::vector< MultidimArray< double > * > Mask1
void * threadComputeTransform(void *args)
#define FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(v)
void readLandmarkSet(const FileName &fnLandmark)
Read landmark set.
double wrapperError(double *p, void *prm)
#define DIRECT_MULTIDIM_ELEM(v, n)
double affine_fitness_individual(double *p)
double computeError() const
File or directory does not exist.
void produceSideInfo()
Produce side info.
Matrix1D< double > vectorR2(double x, double y)
double computeAffineTransformation(const MultidimArray< unsigned char > &I1, const MultidimArray< unsigned char > &I2, int maxShift, int maxIterDE, Matrix2D< double > &A12, Matrix2D< double > &A21, bool show, double thresholdAffine, bool globalAffine, bool isMirror, bool checkRotation, int pyramidLevel)
const ProgTomographAlignment * global_prm
Alignment * bestPreviousAlignment
double computeMedian() const
void setupAffineFitness(AffineFitness &fitness, const MultidimArray< double > &I1, const MultidimArray< double > &I2, int maxShift, bool isMirror, bool checkRotation, int pyramidLevel)
std::vector< MultidimArray< double > * > I1
std::ostream & operator<<(std::ostream &out, Alignment &alignment)
void printStats(std::ostream &out=std::cout) const
void sort(struct DCEL_T *dcel)
std::vector< MultidimArray< unsigned char > * > img
void setEulerAngles(double rot, double tilt, double psi, const size_t n=0)
std::vector< std::vector< Matrix2D< double > > > affineTransformations
double localSize
Local refinement size.
double optimizeGivenAxisDirection()
TYPE distance(struct Point_T *p, struct Point_T *q)
ProgTomographAlignment * parent
#define FOR_ALL_ELEMENTS_IN_ARRAY1D(v)
#define MATRIX1D_ARRAY(v)
void typeCast(const Matrix1D< T1 > &v1, Matrix1D< T2 > &v2)
double psi(const double x)
bool isCapillar
This tilt series comes from a capillar.
void substractBackgroundRollingBall(MultidimArray< double > &I, int radius)
void identifyOutliers(bool mark)
Identify outliers.
void produceInformationFromLandmarks()
Produce information from landmarks.
void writeLandmarkSet(const FileName &fnLandmark) const
Write landmark set.
template float bestShift(MultidimArray< float > &, float &, float &, const MultidimArray< int > *, int)
void writeTransformations(const FileName &fnTransformations) const
Write affine transformations.
void * threadgenerateLandmarkSetBlind(void *args)
constexpr int OUTSIDE_MASK
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
void initZeros(const MultidimArray< T1 > &op)
double computeAvg() const
bool showAffine
Show affine transformations.
Incorrect value received.
void generateMask(MultidimArray< double > &v)
void substractBackgroundPlane(MultidimArray< double > &I)
double fitness(double *p)
std::vector< double > correlationList
void resize(size_t Ydim, size_t Xdim, bool noCopy=false)
#define intWRAP(x, x0, xF)
void indexSort(MultidimArray< int > &indx) const
void applyMaskSpace(MultidimArray< double > &v)