1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
20 #include "vvToolRigidReg.h"
21 #include "vvImageReader.h"
25 #include <vtkImageData.h>
26 #include <vtkSmartPointer.h>
27 #include <vtkTransform.h>
30 #include "clitkTransformUtilities.h"
31 #include "clitkAffineRegistrationGenericFilter.h"
33 #include <QMessageBox>
34 #include <QFileDialog>
35 #include <QTextStream>
40 //------------------------------------------------------------------------------
41 // Create the tool and automagically (I like this word) insert it in
42 // the main window menu.
43 ADD_TOOL(vvToolRigidReg);
44 //------------------------------------------------------------------------------
46 //------------------------------------------------------------------------------
47 vvToolRigidReg::vvToolRigidReg(vvMainWindowBase * parent, Qt::WindowFlags f):
48 vvToolWidgetBase(parent, f),
49 vvToolBase<vvToolRigidReg>(parent),
53 Ui_vvToolRigidReg::setupUi(mToolWidget);
55 // qsize.setHeight(470);
56 // qsize.setWidth(850);
57 // mToolWidget->setFixedSize(qsize);
58 // Set how many inputs are needed for this tool
60 cb_interpolator->hide();
63 cb_selectoutput->hide();
69 interpollabel->hide();
73 // Set how many inputs are needed for this tool
74 AddInputSelector("Select moving image");
75 AddInputSelector("Select fixed image");
77 //------------------------------------------------------------------------------
79 //------------------------------------------------------------------------------
80 vvToolRigidReg::~vvToolRigidReg()
83 //------------------------------------------------------------------------------
85 //------------------------------------------------------------------------------
86 bool vvToolRigidReg::close()
90 return vvToolWidgetBase::close();
92 //------------------------------------------------------------------------------
94 //------------------------------------------------------------------------------
95 void vvToolRigidReg::reject()
98 return vvToolWidgetBase::reject();
100 //------------------------------------------------------------------------------
102 //------------------------------------------------------------------------------
103 void vvToolRigidReg::GetArgsInfoFromGUI()
105 QFont font=QFont("Courier",10);
106 tab2textedit->setTextColor(QColor(255,0,0));
107 tab2textedit->setCurrentFont(font);
108 tab2textedit->update();
110 QString file = QFileDialog::getOpenFileName(
112 "Locate the Config File",
113 mMainWindow->GetInputPathName(),
114 "Text (*.conf *.txt *.rtf *.doc)");
120 mConfigFile= file.toStdString();
121 CmdlineParser(1, 1);//1,1 - override, initialize
122 cb_transform->setCurrentIndex(mArgsInfo.transform_arg);
123 cb_interpolator->setCurrentIndex(mArgsInfo.interp_arg);
124 cb_optimizer->setCurrentIndex(mArgsInfo.optimizer_arg);
125 cb_metric->setCurrentIndex(mArgsInfo.metric_arg);
127 //------------------------------------------------------------------------------
129 //------------------------------------------------------------------------------
130 void vvToolRigidReg::Presets()
132 mConfigFile="Presets";
133 std::string matrixfilename;
134 if(cb_presets->currentIndex()==0)
136 mArgsInfo.reference_arg=new char;
137 mArgsInfo.reference_given=0;
138 mArgsInfo.target_arg=new char;
139 mArgsInfo.target_given=0;
140 mArgsInfo.output_arg=new char;
141 mArgsInfo.referenceMask_arg=new char;
142 mArgsInfo.targetMask_arg=new char;
143 mArgsInfo.initMatrix_arg=new char;
144 mArgsInfo.matrix_arg=new char;
145 mArgsInfo.referenceMask_given=0;
146 mArgsInfo.reference_given=0;
147 mArgsInfo.reference_arg=new char;
148 mArgsInfo.target_given=0;
149 mArgsInfo.target_arg=new char;
150 mArgsInfo.output_given=0;
151 mArgsInfo.output_arg=new char;
152 mArgsInfo.checker_after_given=0;
153 mArgsInfo.checker_before_given=0;
154 mArgsInfo.after_given=0;
155 mArgsInfo.before_given=0;
156 mArgsInfo.threads_given=1;
157 mArgsInfo.threads_arg=3;
158 mArgsInfo.normalize_flag=0;
159 mArgsInfo.blur_arg=0.0;
160 mArgsInfo.referenceMask_arg=new char;
161 mArgsInfo.targetMask_arg=new char;
162 mArgsInfo.targetMask_given=0;
163 mArgsInfo.levels_given=1;
164 mArgsInfo.levels_arg=2;
165 mArgsInfo.moment_flag=1;
166 mArgsInfo.intThreshold_given=0;
167 mArgsInfo.intThreshold_arg=0.0;
168 mArgsInfo.transX_arg=0.0;
169 mArgsInfo.transY_arg=0.0;
170 mArgsInfo.transZ_arg=0.0;
171 mArgsInfo.transform_arg=2;
172 mArgsInfo.gradient_flag=1;
173 mArgsInfo.interp_given=1;
174 mArgsInfo.interp_arg=1;
175 mArgsInfo.interpOrder_given=1;
176 mArgsInfo.interpOrder_arg=3;
177 mArgsInfo.interpSF_given=1;
178 mArgsInfo.interpSF_arg=20;//default
179 mArgsInfo.metric_given=1;
180 mArgsInfo.metric_arg=0;
181 mArgsInfo.samples_arg=1;//default
182 mArgsInfo.stdDev_arg=0.4;
183 mArgsInfo.step_arg=2.0;
184 mArgsInfo.relax_arg=0.7;
185 mArgsInfo.valueTol_arg=0.01;
186 mArgsInfo.stepTol_arg=0.1;
187 mArgsInfo.gradTol_arg=1e-5;
188 mArgsInfo.lineAcc_arg=0.9;
189 mArgsInfo.convFactor_arg=1e+12;
190 mArgsInfo.maxIt_arg=500;
191 mArgsInfo.maxLineIt_arg=50;
192 mArgsInfo.maxEval_arg=500;
193 mArgsInfo.maxCorr_arg=5;
194 mArgsInfo.selectBound_arg=0;
195 mArgsInfo.inc_arg=1.2;
197 mArgsInfo.optimizer_arg=1;
198 mArgsInfo.initMatrix_given=0;
199 mArgsInfo.initMatrix_arg=new char;
200 mArgsInfo.tWeight_given=1;
201 mArgsInfo.tWeight_arg=1.0;
202 mArgsInfo.rWeight_given=1.0;
203 mArgsInfo.rWeight_arg=50.0;
204 mArgsInfo.matrix_given=1;
205 matrixfilename="/home/bharath/bin/writematrix.txt";//put ur path here for retreiving your matrix
206 mArgsInfo.matrix_arg=const_cast<char*>(matrixfilename.c_str());
210 QMessageBox::information(this,"Sorry", "Other Presets are not available for the moment!");
214 //------------------------------------------------------------------------------
216 //------------------------------------------------------------------------------
217 void vvToolRigidReg::UpdateTextEditor2()
220 QString str1,str2,str3;
222 tab2textedit->clear();
223 tab2textedit->setAcceptRichText(true);
224 str2=tab2textedit->toPlainText();
225 tab2textedit->setTextColor(QColor(255,0,0));
226 str2.append(str3.append("threads="+str1.setNum(mArgsInfo.threads_arg)));
227 tab2textedit->setText(str2);
231 str2=tab2textedit->toPlainText();
233 tab2textedit->setText(str2);
235 str2=tab2textedit->toPlainText();
236 str2.append(str3.append("reference="+str1.append(mArgsInfo.reference_arg)));
237 tab2textedit->setText(str2);
241 str2=tab2textedit->toPlainText();
243 tab2textedit->setText(str2);
245 str2=tab2textedit->toPlainText();
246 str2.append(str3.append("target="+str1.append(mArgsInfo.target_arg)));
247 tab2textedit->setText(str2);
251 str2=tab2textedit->toPlainText();
253 tab2textedit->setText(str2);
255 str2=tab2textedit->toPlainText();
256 str2.append(str3.append("matrix="+str1.append(mArgsInfo.matrix_arg)));
257 tab2textedit->setText(str2);
261 str2=tab2textedit->toPlainText();
263 tab2textedit->setText(str2);
265 str2=tab2textedit->toPlainText();
266 str2.append(str3.append("interp="+str1.setNum(mArgsInfo.interp_arg)));
267 tab2textedit->setText(str2);
271 str2=tab2textedit->toPlainText();
273 tab2textedit->setText(str2);
275 str2=tab2textedit->toPlainText();
276 str2.append(str3.append("transform="+str1.setNum(mArgsInfo.transform_arg)));
277 tab2textedit->setText(str2);
281 str2=tab2textedit->toPlainText();
283 tab2textedit->setText(str2);
285 str2=tab2textedit->toPlainText();
286 str2.append(str3.append("transX="+str1.setNum(mArgsInfo.transX_arg)));
287 tab2textedit->setText(str2);
291 str2=tab2textedit->toPlainText();
293 tab2textedit->setText(str2);
295 str2=tab2textedit->toPlainText();
296 str2.append(str3.append("transY="+str1.setNum(mArgsInfo.transY_arg)));
297 tab2textedit->setText(str2);
301 str2=tab2textedit->toPlainText();
303 tab2textedit->setText(str2);
305 str2=tab2textedit->toPlainText();
306 str2.append(str3.append("transZ="+str1.setNum(mArgsInfo.transZ_arg)));
307 tab2textedit->setText(str2);
311 str2=tab2textedit->toPlainText();
313 tab2textedit->setText(str2);
315 str2=tab2textedit->toPlainText();
316 str2.append(str3.append("metric="+str1.setNum(mArgsInfo.metric_arg)));
317 tab2textedit->setText(str2);
321 str2=tab2textedit->toPlainText();
323 tab2textedit->setText(str2);
325 str2=tab2textedit->toPlainText();
326 str2.append(str3.append("samples="+str1.setNum(mArgsInfo.samples_arg)));
327 tab2textedit->setText(str2);
331 str2=tab2textedit->toPlainText();
333 tab2textedit->setText(str2);
335 str2=tab2textedit->toPlainText();
336 str2.append(str3.append("intThreshold="+str1.setNum(mArgsInfo.intThreshold_arg)));
337 tab2textedit->setText(str2);
341 str2=tab2textedit->toPlainText();
343 tab2textedit->setText(str2);
345 str2=tab2textedit->toPlainText();
346 str2.append(str3.append("stdDev="+str1.setNum(mArgsInfo.stdDev_arg)));
347 tab2textedit->setText(str2);
351 str2=tab2textedit->toPlainText();
353 tab2textedit->setText(str2);
356 str2=tab2textedit->toPlainText();
357 str2.append(str3.append("blur="+str1.setNum(mArgsInfo.blur_arg)));
358 tab2textedit->setText(str2);
362 str2=tab2textedit->toPlainText();
364 tab2textedit->setText(str2);
367 str2=tab2textedit->toPlainText();
368 str2.append(str3.append("optimizer="+str1.setNum(mArgsInfo.optimizer_arg)));
369 tab2textedit->setText(str2);
373 str2=tab2textedit->toPlainText();
375 tab2textedit->setText(str2);
378 str2=tab2textedit->toPlainText();
379 str2.append(str3.append("step="+str1.setNum(mArgsInfo.step_arg)));
380 tab2textedit->setText(str2);
384 str2=tab2textedit->toPlainText();
386 tab2textedit->setText(str2);
389 str2=tab2textedit->toPlainText();
390 str2.append(str3.append("relax="+str1.setNum(mArgsInfo.relax_arg)));
391 tab2textedit->setText(str2);
395 str2=tab2textedit->toPlainText();
397 tab2textedit->setText(str2);
399 str2=tab2textedit->toPlainText();
400 str2.append(str3.append("valueTol="+str1.setNum(mArgsInfo.valueTol_arg)));
401 tab2textedit->setText(str2);
405 str2=tab2textedit->toPlainText();
407 tab2textedit->setText(str2);
409 str2=tab2textedit->toPlainText();
410 str2.append(str3.append("stepTol="+str1.setNum(mArgsInfo.stepTol_arg)));
411 tab2textedit->setText(str2);
415 str2=tab2textedit->toPlainText();
417 tab2textedit->setText(str2);
420 str2=tab2textedit->toPlainText();
421 str2.append(str3.append("gradTol="+str1.setNum(mArgsInfo.gradTol_arg)));
422 tab2textedit->setText(str2);
426 str2=tab2textedit->toPlainText();
428 tab2textedit->setText(str2);
430 str2=tab2textedit->toPlainText();
431 str2.append(str3.append("lineAcc="+str1.setNum(mArgsInfo.lineAcc_arg)));
432 tab2textedit->setText(str2);
436 str2=tab2textedit->toPlainText();
438 tab2textedit->setText(str2);
439 str2=tab2textedit->toPlainText();
440 str2.append(str3.append("convFactor="+str1.setNum(mArgsInfo.convFactor_arg)));
441 tab2textedit->setText(str2);
445 str2=tab2textedit->toPlainText();
447 tab2textedit->setText(str2);
449 str2=tab2textedit->toPlainText();
450 str2.append(str3.append("maxIt="+str1.setNum(mArgsInfo.maxIt_arg)));
451 tab2textedit->setText(str2);
455 str2=tab2textedit->toPlainText();
457 tab2textedit->setText(str2);
459 str2=tab2textedit->toPlainText();
460 str2.append(str3.append("maxLineIt="+str1.setNum(mArgsInfo.maxLineIt_arg)));
461 tab2textedit->setText(str2);
465 str2=tab2textedit->toPlainText();
467 tab2textedit->setText(str2);
469 str2=tab2textedit->toPlainText();
470 str2.append(str3.append("maxEval="+str1.setNum(mArgsInfo.maxEval_arg)));
471 tab2textedit->setText(str2);
475 str2=tab2textedit->toPlainText();
477 tab2textedit->setText(str2);
479 str2=tab2textedit->toPlainText();
480 str2.append(str3.append("maxCorr="+str1.setNum(mArgsInfo.maxCorr_arg)));
481 tab2textedit->setText(str2);
485 str2=tab2textedit->toPlainText();
487 tab2textedit->setText(str2);
489 str2=tab2textedit->toPlainText();
490 str2.append(str3.append("selectBound="+str1.setNum(mArgsInfo.selectBound_arg)));
491 tab2textedit->setText(str2);
495 str2=tab2textedit->toPlainText();
497 tab2textedit->setText(str2);
499 str2=tab2textedit->toPlainText();
500 str2.append(str3.append("rWeight="+str1.setNum(mArgsInfo.rWeight_arg)));
501 tab2textedit->setText(str2);
505 str2=tab2textedit->toPlainText();
507 tab2textedit->setText(str2);
509 str2=tab2textedit->toPlainText();
510 str2.append(str3.append("tWeight="+str1.setNum(mArgsInfo.tWeight_arg)));
511 tab2textedit->setText(str2);
515 str2=tab2textedit->toPlainText();
517 tab2textedit->setText(str2);
519 str2=tab2textedit->toPlainText();
520 str2.append(str3.append("levels="+str1.setNum(mArgsInfo.levels_arg)));
521 tab2textedit->setText(str2);
525 str2=tab2textedit->toPlainText();
527 tab2textedit->setText(str2);
529 str2=tab2textedit->toPlainText();
530 str2.append(str3.append("inc="+str1.setNum(mArgsInfo.inc_arg)));
531 tab2textedit->setText(str2);
535 str2=tab2textedit->toPlainText();
537 tab2textedit->setText(str2);
539 str2=tab2textedit->toPlainText();
540 str2.append(str3.append("dec="+str1.setNum(mArgsInfo.dec_arg)));
541 tab2textedit->setText(str2);
545 str2=tab2textedit->toPlainText();
547 tab2textedit->setText(str2);
549 //------------------------------------------------------------------------------
551 //------------------------------------------------------------------------------
552 void vvToolRigidReg::TransformSelect()
554 if(!mConfigFile.empty()){
555 mArgsInfo.transform_arg=cb_transform->currentIndex();
559 QMessageBox::information(this,"Warning","Load the Config File First!..");
562 //------------------------------------------------------------------------------
564 //------------------------------------------------------------------------------
565 void vvToolRigidReg::CmdlineParser(int override, int initialize)
567 //0 opened, 1 not opened fine
568 int opened=cmdline_parser_clitkAffineRegistration_configfile(const_cast<char*>(mConfigFile.c_str()),&mArgsInfo,override,initialize,1);
570 mArgsInfo.gradient_flag=1;
572 //Read the transformation parameters from the path in the config file(mArgsInfo.matrix_arg) and display it on the TextBox 2
574 std::vector<QString> Qstr;
577 readfile.open(mConfigFile.c_str());
580 QMessageBox::information(this,"Warning","Load the Config File First..");
582 if (readfile.is_open())
584 while (!readfile.eof())
586 readfile >> mConfigFile;
587 Qstr.push_back(QString(mConfigFile.c_str()));
592 QMessageBox::information(this,"Warning","Cannot Open File!");
595 for(unsigned int i=0;i<Qstr.size()-1;i++)
597 str=tab2textedit->toPlainText();
598 str.append(Qstr.at(i));
599 tab2textedit->setText(str);
601 tab2textedit->setText(str);
604 //------------------------------------------------------------------------------
606 //------------------------------------------------------------------------------
607 void vvToolRigidReg::OptimizerSelect()
609 if(!mConfigFile.empty()){
610 mArgsInfo.optimizer_arg=cb_optimizer->currentIndex();
614 QMessageBox::information(this,"Warning","Load the Config File First!..");
617 //------------------------------------------------------------------------------
619 //------------------------------------------------------------------------------
620 void vvToolRigidReg::InterpolatorSelect()
622 if(!mConfigFile.empty()){
623 mArgsInfo.interp_arg=cb_interpolator->currentIndex();
627 QMessageBox::information(this,"Warning","Load the Config File First!..");
630 //------------------------------------------------------------------------------
632 //------------------------------------------------------------------------------
633 void vvToolRigidReg::MetricSelect()
635 if(!mConfigFile.empty()){
636 mArgsInfo.metric_arg=cb_metric->currentIndex();
640 QMessageBox::information(this,"Warning","Load the Config File First!..");
643 //------------------------------------------------------------------------------
645 //------------------------------------------------------------------------------
646 void vvToolRigidReg::OutputSelect()
648 std::ostringstream osstream;
649 if(!mConfigFile.empty()){
650 if(cb_selectoutput->currentIndex()==0){
651 mArgsInfo.output_given=0;
652 mArgsInfo.checker_after_given=0;
653 mArgsInfo.checker_before_given=0;
654 mArgsInfo.after_given=0;
655 mArgsInfo.before_given=0;
656 }//get transformed output image
657 if(cb_selectoutput->currentIndex()==1){
658 mArgsInfo.output_given=0;
659 mArgsInfo.checker_after_given=1;
660 mArgsInfo.checker_before_given=0;
661 mArgsInfo.after_given=0;
662 mArgsInfo.before_given=0;
663 }//get checkered image after reg
664 if(cb_selectoutput->currentIndex()==2){
665 mArgsInfo.output_given=0;
666 mArgsInfo.checker_after_given=0;
667 mArgsInfo.checker_before_given=1;
668 mArgsInfo.after_given=0;
669 mArgsInfo.before_given=0;
670 }//get checkered image before reg
671 if(cb_selectoutput->currentIndex()==3){
672 mArgsInfo.output_given=0;
673 mArgsInfo.checker_after_given=0;
674 mArgsInfo.checker_before_given=0;
675 mArgsInfo.after_given=1;
676 mArgsInfo.before_given=0;
677 }//get difference image after reg
678 if(cb_selectoutput->currentIndex()==4){
679 mArgsInfo.output_given=0;
680 mArgsInfo.checker_after_given=0;
681 mArgsInfo.checker_before_given=0;
682 mArgsInfo.after_given=0;
683 mArgsInfo.before_given=1;
684 }//get difference image before reg
687 QMessageBox::information(this,"Warning","Load the Config File First!..");
690 //------------------------------------------------------------------------------
692 //------------------------------------------------------------------------------
693 void vvToolRigidReg::SaveTextEdit()
695 DD(mArgsInfo.transform_arg);
696 QString f1 = QFileDialog::getSaveFileName(this, tr("Save Config File"),
697 mMainWindow->GetInputPathName(),
698 tr("Text (*.mat *.txt *.doc *.rtf)"));
700 if(file.open(QFile::WriteOnly | QFile::Truncate) & !mConfigFile.empty() ){
701 QTextStream stream( &file );
702 stream << tab2textedit->toPlainText();
706 QMessageBox::information(this,"Warning","Nothing to Save!");
709 tab2textedit->clear();
710 DD(mArgsInfo.transform_arg);
712 //------------------------------------------------------------------------------
714 //------------------------------------------------------------------------------
715 void vvToolRigidReg::InputIsSelected(std::vector<vvSlicerManager *> & l)
720 UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix(),textEdit_2);
722 for(int i =0;i<4;i++)
726 mInitialMatrix[i*4+j]=mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()->GetElement(i,j);
730 if(mInput1->GetFileName()==mInput2->GetFileName())
732 QMessageBox::information(this, "Warning","Your Reference and Target Images are the same");
735 SetOverlay(mInput2->GetImage());
736 mImageSize=mInput1->GetImage()->GetSize();
740 //connect all sigs to slots
741 connect(resetbutton, SIGNAL(pressed()), this, SLOT(ResetTransform()));
742 connect(tab2loadbutton, SIGNAL(pressed()), this, SLOT(GetArgsInfoFromGUI()));
743 connect(tab2applybutton, SIGNAL(pressed()), this, SLOT(AutoRegister()));
746 connect(Xval, SIGNAL(editingFinished()), this, SLOT(SetXvalue()));
747 connect(Yval, SIGNAL(editingFinished()), this, SLOT(SetYvalue()));
748 connect(Zval, SIGNAL(editingFinished()), this, SLOT(SetZvalue()));
750 connect(xtrans_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateTransform_sliders()));
751 connect(ytrans_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateTransform_sliders()));
752 connect(ztrans_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateTransform_sliders()));
755 connect(xrot_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateTransform_sliders()));
756 connect(yrot_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateTransform_sliders()));
757 connect(zrot_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateTransform_sliders()));
759 connect(xtrans_sb, SIGNAL(valueChanged(double)), this, SLOT(UpdateTransform_sb()));
760 connect(ytrans_sb, SIGNAL(valueChanged(double)), this, SLOT(UpdateTransform_sb()));
761 connect(ztrans_sb, SIGNAL(valueChanged(double)), this, SLOT(UpdateTransform_sb()));
762 connect(xrot_sb, SIGNAL(valueChanged(double)), this, SLOT(UpdateTransform_sb()));
763 connect(yrot_sb, SIGNAL(valueChanged(double)), this, SLOT(UpdateTransform_sb()));
764 connect(zrot_sb, SIGNAL(valueChanged(double)), this, SLOT(UpdateTransform_sb()));
766 connect(loadbutton, SIGNAL(pressed()), this, SLOT(LoadFile()));
767 connect(savebutton, SIGNAL(pressed()), this, SLOT(SaveFile()));
769 connect(checkBox_rigid, SIGNAL(clicked(bool)), this, SLOT(CheckRigidReg()));
770 connect(checkBox_deformable, SIGNAL(clicked(bool)), this, SLOT(CheckDeformableReg()));
772 connect(cb_presets, SIGNAL(activated(int)), this, SLOT(Presets()));
773 connect(cb_transform, SIGNAL(activated(int)), this, SLOT(TransformSelect()));
774 connect(cb_optimizer, SIGNAL(activated(int)), this, SLOT(OptimizerSelect()));
775 connect(cb_interpolator, SIGNAL(activated(int)), this, SLOT(InterpolatorSelect()));
776 connect(cb_metric, SIGNAL(activated(int)), this, SLOT(MetricSelect()));
777 connect(cb_selectoutput, SIGNAL(activated(int)), this, SLOT(OutputSelect()));
778 connect(tab2savebutton, SIGNAL(pressed()), this, SLOT(SaveTextEdit()));
781 //------------------------------------------------------------------------------
783 //------------------------------------------------------------------------------
784 void vvToolRigidReg::apply()
788 //------------------------------------------------------------------------------
790 //------------------------------------------------------------------------------
791 void vvToolRigidReg::CheckRigidReg()
793 checkBox_deformable->setChecked(false);
794 cb_transform->show();
797 cb_interpolator->show();
798 cb_optimizer->show();
799 cb_selectoutput->show();
804 interpollabel->show();
808 //------------------------------------------------------------------------------
810 //------------------------------------------------------------------------------
811 void vvToolRigidReg::CheckDeformableReg()
813 checkBox_rigid->setChecked(false);
815 cb_transform->show();
817 cb_interpolator->show();
818 cb_optimizer->show();
819 cb_selectoutput->show();
825 interpollabel->show();
827 //------------------------------------------------------------------------------
829 //------------------------------------------------------------------------------
830 void vvToolRigidReg::SetOverlay(vvImage::Pointer Image)
832 for (int i =0; i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
833 mCurrentSlicerManager->GetSlicer(i)->SetOverlay(Image);
834 mCurrentSlicerManager->GetSlicer(i)->SetActorVisibility("overlay",0,true);
835 mCurrentSlicerManager->SetColorMap();
836 mCurrentSlicerManager->Render();
839 //------------------------------------------------------------------------------
841 //------------------------------------------------------------------------------
842 void vvToolRigidReg::RemoveOverlay()
844 for(int i=0;i<mCurrentSlicerManager->GetNumberOfSlicers();i++)
846 mInput1->RemoveActor("overlay",0);
847 mInput1->SetColorMap(0);
852 //------------------------------------------------------------------------------
854 //------------------------------------------------------------------------------
855 void vvToolRigidReg::SetXvalue()
857 QString xstr = Xval->text();
859 //------------------------------------------------------------------------------
861 //------------------------------------------------------------------------------
862 void vvToolRigidReg::SetYvalue()
864 QString ystr = Yval->text();
866 //------------------------------------------------------------------------------
868 //------------------------------------------------------------------------------
869 void vvToolRigidReg::SetZvalue()
871 QString zstr = Zval->text();
873 //------------------------------------------------------------------------------
875 //------------------------------------------------------------------------------
876 void vvToolRigidReg::SetTransform(double tX, double tY, double tZ, double aX, double aY, double aZ,bool update)
878 vtkSmartPointer<vtkTransform> transform = mInput1->GetImage()->GetTransform();
879 transform->PostMultiply();
881 if (aX!=0 || aY!=0 || aZ!=0) {
883 x= Xval->text().toDouble();
884 y= Yval->text().toDouble();
885 z= Zval->text().toDouble();
886 transform->Translate(-x,-y,-z);
887 if (aX!=0) transform->RotateX(aX);
888 if (aY!=0) transform->RotateY(aY);
889 if (aZ!=0) transform->RotateZ(aZ);
890 transform->Translate(x,y,z);
893 if (tX!=0||tY!=0||tZ!=0)
894 transform->Translate(tX*mInput1->GetImage()->GetSpacing()[0],
895 tY*mInput1->GetImage()->GetSpacing()[1],
896 tZ*mInput1->GetImage()->GetSpacing()[2]);
898 //------------------------------------------------------------------------------
900 //------------------------------------------------------------------------------
901 void vvToolRigidReg::SetTransform(vtkMatrix4x4 *matrix)
903 vtkSmartPointer<vtkTransform> transform=vtkSmartPointer<vtkTransform>::New();
904 for(int i=0; i<4;i++)
906 mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()->SetElement(i,j,matrix->GetElement(i,j));
910 //------------------------------------------------------------------------------
912 //------------------------------------------------------------------------------
913 void vvToolRigidReg::LoadFile()
917 //------------------------------------------------------------------------------
919 //------------------------------------------------------------------------------
920 void vvToolRigidReg::Render()
922 for (int i=0; i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
923 mCurrentSlicerManager->GetSlicer(i)->ForceUpdateDisplayExtent();
924 mCurrentSlicerManager->GetSlicer(i)->Render();
927 //------------------------------------------------------------------------------
929 //------------------------------------------------------------------------------
930 void vvToolRigidReg::UpdateTextEditor(vtkMatrix4x4 *matrix,QTextEdit* textEdit)
932 QFont font=QFont("Courier",11);
933 textEdit->setCurrentFont(font);
936 QString str1,str2,str3;
939 textEdit->setAcceptRichText(true);
940 str2=textEdit->toPlainText();
941 str2.append("#Rotation Center(mm): \n#");
942 textEdit->setText(str2);
944 str2=textEdit->toPlainText();
945 textEdit->setTextColor(QColor(255,0,0));
946 str2.append(str3.append(Xval->text()));
947 textEdit->setText(str2);
950 str2=textEdit->toPlainText();
952 textEdit->setText(str2);
954 str2=textEdit->toPlainText();
955 str2.append(str3.append(Yval->text()));
956 textEdit->setText(str2);
959 str2=textEdit->toPlainText();
961 textEdit->setText(str2);
964 str2=textEdit->toPlainText();
965 str2.append(str3.append(Zval->text()));
966 textEdit->setText(str2);
969 str2=textEdit->toPlainText();
971 textEdit->setText(str2);
974 str2=textEdit->toPlainText();
975 str2.append("#Transformation Matrix(mm):\n");
976 textEdit->setText(str2);
981 str2=textEdit->toPlainText();
982 // str2.append("\t"+str1.setNum(matrix->Element[i][j]));
983 str2.append(QString("%1\t").arg(str1.setNum(matrix->Element[i][j]),2));
984 textEdit->setText(str2);
986 str2=textEdit->toPlainText();
988 textEdit->setText(str2);
990 //QString str = QFileDialog::getOpenFileName();
991 textEdit->setTextColor(QColor(255,0,0));
992 textEdit->setFont(QFont("courrier new",12,4,true));
993 textEdit->toPlainText().toAscii();
995 str2=textEdit->toPlainText();
996 textEdit->setText(str2);
998 //------------------------------------------------------------------------------
1000 //------------------------------------------------------------------------------
1001 void vvToolRigidReg::UpdateTransform_sliders()
1003 InitializeSliders(xtrans_slider->value()*mInput1->GetImage()->GetSpacing()[0],
1004 ytrans_slider->value()*mInput1->GetImage()->GetSpacing()[1],
1005 ztrans_slider->value()*mInput1->GetImage()->GetSpacing()[2],
1006 xrot_slider->value(),yrot_slider->value(),zrot_slider->value(),false);
1007 UpdateTransform(true);
1010 //------------------------------------------------------------------------------
1012 //------------------------------------------------------------------------------
1013 void vvToolRigidReg::UpdateTransform_sb()
1015 InitializeSliders(xtrans_sb->value(),
1018 xrot_sb->value(),yrot_sb->value(),zrot_sb->value(),false);
1019 UpdateTransform(false);
1022 //------------------------------------------------------------------------------
1024 //------------------------------------------------------------------------------
1025 void vvToolRigidReg::AutoRegister()
1027 if (!mCurrentSlicerManager) close();
1029 if(!mConfigFile.empty()){
1030 std::vector<vvImage::Pointer> inputs;
1032 inputs.push_back(mInput1->GetImage());
1033 inputs.push_back(mInput2->GetImage());
1036 clitk::AffineRegistrationGenericFilter::Pointer filter =
1037 clitk::AffineRegistrationGenericFilter::New();
1038 filter->SetInputVVImages(inputs);
1039 filter->SetArgsInfo(mArgsInfo);
1040 filter->EnableReadOnDisk(false);
1042 std::ostringstream osstream;
1043 vvImage::Pointer output;
1044 output = filter->GetOutputVVImage();
1047 if(!cb_selectoutput->currentIndex()==0){
1048 std::string outputstring;
1049 output = filter->GetOutputVVImages()[1];
1050 if(cb_selectoutput->currentIndex()==1){outputstring="Checkered_after_reg";}
1051 if(cb_selectoutput->currentIndex()==2){outputstring="Checkered_before_reg";}
1052 if(cb_selectoutput->currentIndex()==3){outputstring="Diff_after_reg";}
1053 if(cb_selectoutput->currentIndex()==4){outputstring="Diff_before_reg";}
1054 osstream << outputstring << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd";
1055 AddImage(output,osstream.str());
1056 filter->DeleteLastOutputImage();
1058 DD(filter->GetOutputVVImages().capacity());
1059 QApplication::restoreOverrideCursor();
1064 QMessageBox::information(this, "Warning","Load the Config File First!...");
1068 //------------------------------------------------------------------------------
1070 //------------------------------------------------------------------------------
1071 void vvToolRigidReg::UpdateTransform(bool slider_enabled)
1073 vtkSmartPointer<vtkTransform> transform_final=mInput1->GetImage()->GetTransform();
1074 transform_final->SetMatrix(mInitialMatrix);
1075 transform_final->PostMultiply();
1077 double x=0, y=0 ,z=0;
1078 x= Xval->text().toDouble();
1079 y= Yval->text().toDouble();
1080 z= Zval->text().toDouble();
1081 transform_final->Translate(-x,-y,-z);
1083 transform_final->RotateY(yrot_slider->value());
1084 transform_final->RotateX(xrot_slider->value());
1085 transform_final->RotateZ(zrot_slider->value());
1088 transform_final->RotateY(yrot_sb->value());
1089 transform_final->RotateX(xrot_sb->value());
1090 transform_final->RotateZ(zrot_sb->value());
1092 transform_final->Translate(x,y,z);
1093 transform_final->PreMultiply();
1095 transform_final->Translate(xtrans_slider->value()*mInput1->GetImage()->GetSpacing()[0],0,0);
1096 transform_final->Translate(0,ytrans_slider->value()*mInput1->GetImage()->GetSpacing()[1],0);
1097 transform_final->Translate(0,0,ztrans_slider->value()*mInput1->GetImage()->GetSpacing()[2]);
1100 transform_final->Translate(xtrans_sb->value(),0,0);
1101 transform_final->Translate(0,ytrans_sb->value(),0);
1102 transform_final->Translate(0,0,ztrans_sb->value());
1104 transform_final->Update();
1106 UpdateTextEditor(transform_final->GetMatrix(),textEdit);
1108 //------------------------------------------------------------------------------
1111 //------------------------------------------------------------------------------
1112 void vvToolRigidReg::SaveFile()
1114 //Write the Transformation Matrix
1115 QString f1 = QFileDialog::getSaveFileName(this, tr("Save Transformation Matrix File"),
1116 mMainWindow->GetInputPathName(),
1117 tr("Text (*.mat *.txt *.doc *.rtf)"));
1119 std::vector<QString> transparameters;
1122 for(int i =0;i<4;i++)
1123 for(int j=0;j<4;j++)
1124 transparameters.push_back(line1.setNum(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()->Element[i][j]));
1126 if (file1.open(QFile::WriteOnly | QFile::Truncate)) {
1127 QTextStream out1(&file1);
1128 for(int i =0;i<4;i++){
1129 for(int j=0;j<4;j++) {
1130 out1<<transparameters[i*4+j]+"\t";
1137 QMessageBox::information(this,"Warning","Error Reading Parameters");
1140 //------------------------------------------------------------------------------
1142 //------------------------------------------------------------------------------
1143 void vvToolRigidReg::ReadFile(bool matrix_given)
1147 double * orientations=new double[3];
1148 double * translations=new double[3];
1149 vtkMatrix4x4 *matrix=vtkMatrix4x4::New();
1150 std::string transfile;
1151 vtkSmartPointer<vtkTransform> transform = mCurrentSlicerManager->GetImage()->GetTransform();
1154 //Open File to read the transformation parameters
1155 QString file1 = QFileDialog::getOpenFileName(
1157 "Choose the Transformation Parameters file",
1158 mMainWindow->GetInputPathName(),
1159 "Text (*.mat *.txt *.rtf *.doc)");
1160 if (file1.isEmpty())
1162 QFile Qfile1(file1);
1163 // ifstream readfile;
1164 transfile= file1.toStdString();
1168 transfile=mArgsInfo.matrix_arg;
1171 std::string filename1(transfile);
1172 std::ifstream f1(filename1.c_str());
1176 itk::Matrix<double, 4, 4> itkMat = clitk::ReadMatrix3D(transfile);
1177 for(int j=0; j<4; j++)
1178 for(int i=0; i<4; i++)
1179 matrix->SetElement(i,j,rint(itkMat[i][j]));
1181 UpdateTextEditor(matrix,textEdit);
1182 transform->SetMatrix(matrix);
1183 transform->GetOrientation(orientations);
1184 transform->PostMultiply();
1186 //Obtain the Rotation Center , set it to origin
1187 Xval->setText(center.setNum(0));
1188 Yval->setText(center.setNum(0));
1189 Zval->setText(center.setNum(0));
1191 //In the Order or Y X Z //
1192 //now postmultiply for the rotations
1193 SetTransform(0,0,0,0,0,-rint(orientations[2]),false);
1194 SetTransform(0,0,0,-rint(orientations[0]),0,0,false);
1195 SetTransform(0,0,0,0,-rint(orientations[1]),0,false);
1197 transform->GetPosition(translations);
1198 transform->Identity();
1200 DD(translations[0]/mInput1->GetImage()->GetSpacing()[0]);
1201 DD(translations[1]/mInput1->GetImage()->GetSpacing()[1]);
1202 DD(translations[2]/mInput1->GetImage()->GetSpacing()[2]);
1203 DD(mInput1->GetImage()->GetSpacing()[0]);
1204 DD(mInput1->GetImage()->GetSpacing()[1]);
1205 DD(mInput1->GetImage()->GetSpacing()[2]);
1206 DD(orientations[0]);
1207 DD(orientations[1]);
1208 DD(orientations[2]);
1209 //set the sliders and spin box values
1210 InitializeSliders(rint(translations[0]),rint(translations[1])
1211 ,rint(translations[2]),rint(orientations[0]),rint(orientations[1]),rint(orientations[2]),true);
1212 SetTransform(matrix);
1214 //------------------------------------------------------------------------------
1216 //------------------------------------------------------------------------------
1217 void vvToolRigidReg::ResetTransform()
1219 vtkSmartPointer<vtkTransform> transform = mCurrentSlicerManager->GetImage()->GetTransform();
1220 transform->SetMatrix(mInitialMatrix);
1221 transform->Update();
1224 SetRotationCenter();
1226 UpdateTextEditor(transform->GetMatrix(),textEdit);
1228 //------------------------------------------------------------------------------
1230 //------------------------------------------------------------------------------
1231 void vvToolRigidReg::SetRotationCenter()
1233 //default image rotation center is the center of the image
1234 QString xcord,ycord,zcord;
1235 std::vector<double> imageorigin;
1236 imageorigin=mInput1->GetImage()->GetOrigin();
1238 xcord=xcord.setNum(imageorigin[0]+mImageSize[0]*mInput1->GetImage()->GetSpacing()[0]/2, 'g', 3);
1239 ycord=ycord.setNum(imageorigin[1]+mImageSize[1]*mInput1->GetImage()->GetSpacing()[1]/2, 'g', 3);
1240 zcord=zcord.setNum(imageorigin[2]+mImageSize[2]*mInput1->GetImage()->GetSpacing()[2]/2, 'g', 3);
1242 Xval->setText(xcord);
1243 Yval->setText(ycord);
1244 Zval->setText(zcord);
1245 InitializeSliders(0,0,0,0,0,0,true);
1247 //------------------------------------------------------------------------------
1249 //------------------------------------------------------------------------------
1250 void vvToolRigidReg::InitializeSliders(double xtrans,double ytrans, double ztrans, double xrot, double yrot, double zrot,bool sliders)
1253 xtrans_sb->blockSignals(true);
1254 xtrans_sb->setSingleStep(mInput1->GetImage()->GetSpacing()[0]);
1255 xtrans_sb->setValue(xtrans);
1256 xtrans_sb->blockSignals(false);
1257 ytrans_sb->blockSignals(true);
1258 ytrans_sb->setSingleStep(mInput1->GetImage()->GetSpacing()[1]);
1259 ytrans_sb->setValue(ytrans);
1260 ytrans_sb->blockSignals(false);
1261 ztrans_sb->blockSignals(true);
1262 ztrans_sb->setSingleStep(mInput1->GetImage()->GetSpacing()[2]);
1263 ztrans_sb->setValue(ztrans);
1264 ztrans_sb->blockSignals(false);
1267 xtrans_slider->blockSignals(true);
1268 xtrans_slider->setValue(rint(xtrans/mInput1->GetImage()->GetSpacing()[0]));
1269 xtrans_slider->blockSignals(false);
1270 ytrans_slider->blockSignals(true);
1271 ytrans_slider->setValue(rint(ytrans/mInput1->GetImage()->GetSpacing()[1]));
1272 ytrans_slider->blockSignals(false);
1273 ztrans_slider->blockSignals(true);
1274 ztrans_slider->setValue(rint(ztrans/mInput1->GetImage()->GetSpacing()[2]));
1275 ztrans_slider->blockSignals(false);
1277 xrot_sb->blockSignals(true);
1278 xrot_sb->setValue(xrot);
1279 xrot_sb->blockSignals(false);
1280 yrot_sb->blockSignals(true);
1281 yrot_sb->setValue(yrot);
1282 yrot_sb->blockSignals(false);
1283 zrot_sb->blockSignals(true);
1284 zrot_sb->setValue(zrot);
1285 zrot_sb->blockSignals(false);
1286 xrot_slider->blockSignals(true);
1287 xrot_slider->setValue(xrot);
1288 xrot_slider->blockSignals(false);
1289 yrot_slider->blockSignals(true);
1290 yrot_slider->setValue(yrot);
1291 yrot_slider->blockSignals(false);
1292 zrot_slider->blockSignals(true);
1293 zrot_slider->setValue(zrot);
1294 zrot_slider->blockSignals(false);
1296 //------------------------------------------------------------------------------
1298 //------------------------------------------------------------------------------
1299 void vvToolRigidReg::SetSliderRanges()
1301 xtrans_slider->blockSignals(true);
1302 xtrans_slider->setRange(-2000,2000);
1303 xtrans_slider->blockSignals(false);
1305 ytrans_slider->blockSignals(true);
1306 ytrans_slider->setRange(-2000,2000);
1307 ytrans_slider->blockSignals(false);
1309 ztrans_slider->blockSignals(true);
1310 ztrans_slider->setRange(-2000,2000);
1311 ztrans_slider->blockSignals(false);
1313 xtrans_sb->blockSignals(true);
1314 xtrans_sb->setRange(-2000,2000);
1315 xtrans_sb->setDecimals(3);
1316 xtrans_sb->blockSignals(false);
1318 ytrans_sb->blockSignals(true);
1319 ytrans_sb->setRange(-2000,2000);
1320 ytrans_sb->setDecimals(3);
1321 ytrans_sb->blockSignals(false);
1323 ztrans_sb->blockSignals(true);
1324 ztrans_sb->setRange(-2000,2000);
1325 ztrans_sb->setDecimals(3);
1326 ztrans_sb->blockSignals(false);
1328 xrot_slider->blockSignals(true);
1329 xrot_slider->setRange(-360,360);
1330 xrot_slider->blockSignals(false);
1332 yrot_slider->blockSignals(true);
1333 yrot_slider->setRange(-360,360);
1334 yrot_slider->blockSignals(false);
1336 zrot_slider->blockSignals(true);
1337 zrot_slider->setRange(-360,360);
1338 zrot_slider->blockSignals(false);
1340 xrot_sb->blockSignals(true);
1341 xrot_sb->setRange(-360,360);
1342 xrot_sb->blockSignals(false);
1344 yrot_sb->blockSignals(true);
1345 yrot_sb->setRange(-360,360);
1346 yrot_sb->blockSignals(false);
1348 zrot_sb->blockSignals(true);
1349 zrot_sb->setRange(-360,360);
1350 zrot_sb->blockSignals(false);