Added a registration using thin-plate splines to retwisting so the user can retwist...
authorabokinsky <abokinsky@ba61647d-9d00-f842-95cd-605cb4296b96>
Wed, 14 Mar 2018 15:12:06 +0000 (15:12 +0000)
committerabokinsky <abokinsky@ba61647d-9d00-f842-95cd-605cb4296b96>
Wed, 14 Mar 2018 15:12:06 +0000 (15:12 +0000)
git-svn-id: https://citdcbmipav.cit.nih.gov/repos-pub/mipav/trunk@15414 ba61647d-9d00-f842-95cd-605cb4296b96

mipav/src/plugins/PlugInDialogUntwistedToTwisted.java

index b8a5c53..162b397 100644 (file)
@@ -25,6 +25,7 @@ This software may NOT be used for diagnostic purposes.
 \r
 import gov.nih.mipav.model.algorithms.AlgorithmBase;\r
 import gov.nih.mipav.model.algorithms.AlgorithmInterface;\r
+import gov.nih.mipav.model.algorithms.AlgorithmTPSpline;\r
 import gov.nih.mipav.model.file.FileIO;\r
 import gov.nih.mipav.model.structures.ModelImage;\r
 import gov.nih.mipav.model.structures.VOI;\r
@@ -88,9 +89,15 @@ public class PlugInDialogUntwistedToTwisted extends JDialogStandalonePlugin impl
        private String straightenedAnnotationFile;\r
        private JTextField targetLocText;\r
        private String targetDir;\r
+       \r
+\r
+       private JTextField targetPointsText;\r
+       private String targetPointsFile;\r
+       private JTextField inputPointsText;\r
+       private String inputPointsFile;\r
 //     private JTextField  baseFileNameText;\r
 //     private Vector<Integer> includeRange;\r
-       private JPanel inputsPanel;\r
+//     private JPanel inputsPanel;\r
 //     private JTextField rangeFusionText;\r
 \r
        public PlugInDialogUntwistedToTwisted()\r
@@ -244,15 +251,52 @@ public class PlugInDialogUntwistedToTwisted extends JDialogStandalonePlugin impl
                                "output_images" + File.separator + targetDirName + "_toTwisted.xml";\r
                File toTwistedFile = new File( toTwistedName );\r
                File annotationFile = new File( straightenedAnnotationFile );\r
+               File targetFile = new File( targetPointsFile );\r
+               File inputFile = new File( inputPointsFile );\r
                System.err.println(toTwistedName + " " + toTwistedFile.exists() );\r
                ModelImage toTwisted = null;\r
-               if ( toTwistedFile.exists() && annotationFile.exists() )\r
+               if ( toTwistedFile.exists() && annotationFile.exists() && targetFile.exists() && inputFile.exists() )\r
                {\r
                        System.err.println("opening image...");\r
                        FileIO fileIO = new FileIO();\r
                        toTwisted = fileIO.readImage(toTwistedName);  \r
                        if ( toTwisted != null )\r
                        {\r
+                               // read registration points and calculate thin-plate spline transform:\r
+                               VOI targetPts = LatticeModel.readAnnotationsCSV(targetPointsFile);\r
+                               VOI inputPts = LatticeModel.readAnnotationsCSV(inputPointsFile);\r
+                               int numTargetPts = targetPts.getCurves().size();\r
+                               int numInputPts = inputPts.getCurves().size();\r
+                               if ( numTargetPts != numInputPts ) {\r
+                                       MipavUtil.displayError( "Number of registration target points and input points must match" );\r
+                                       return;\r
+                               }\r
+                               double[] xSource = new double[ numTargetPts ]; \r
+                               double[] ySource = new double[ numTargetPts ]; \r
+                               double[] zSource = new double[ numTargetPts ]; \r
+                               double[] xTarget = new double[ numTargetPts ]; \r
+                               double[] yTarget = new double[ numTargetPts ]; \r
+                               double[] zTarget = new double[ numTargetPts ]; \r
+                               for ( int i = 0; i < numTargetPts; i++ ) {\r
+                                       Vector3f sourcePt = inputPts.getCurves().elementAt(i).elementAt(0);\r
+                                       Vector3f targetPt = targetPts.getCurves().elementAt(i).elementAt(0);\r
+                                       xSource[i] = sourcePt.X;                                        ySource[i] = sourcePt.Y;                                        zSource[i] = sourcePt.Z;\r
+                                       xTarget[i] = targetPt.X;                                        yTarget[i] = targetPt.Y;                                        zTarget[i] = targetPt.Z;\r
+                               }\r
+                               AlgorithmTPSpline spline = new AlgorithmTPSpline(xSource, ySource, zSource, xTarget, yTarget, zTarget, 0.0f, toTwisted,\r
+                                               toTwisted, true);\r
+\r
+                       spline.setRunningInSeparateThread(false);\r
+                       spline.run();\r
+\r
+//                             for ( int i = 0; i < numTargetPts; i++ ) {\r
+//                                     Vector3f sourcePt = inputPts.getCurves().elementAt(i).elementAt(0);\r
+//                                     Vector3f targetPt = targetPts.getCurves().elementAt(i).elementAt(0);\r
+//                                     float[] transformedPt = spline.getCorrespondingPoint(sourcePt.X, sourcePt.Y, sourcePt.Z);\r
+//                                     System.err.println( sourcePt + "   =>  " + targetPt + "    " + transformedPt[0] + " " + transformedPt[1] + " " + transformedPt[2] );\r
+//                             }\r
+                       \r
+                               \r
                                int dimX = toTwisted.getExtents().length > 0 ? toTwisted.getExtents()[0] : 1;\r
                                int dimY = toTwisted.getExtents().length > 1 ? toTwisted.getExtents()[1] : 1;\r
                                int dimZ = toTwisted.getExtents().length > 2 ? toTwisted.getExtents()[2] : 1;\r
@@ -271,11 +315,23 @@ public class PlugInDialogUntwistedToTwisted extends JDialogStandalonePlugin impl
                                                        Vector3f pos = text.elementAt(0);\r
                                                        String name = text.getText();\r
 \r
-                                                       if ( ((int)pos.X < dimX) && ((int)pos.Y < dimY)  && ((int)pos.Z < dimZ) )\r
+                                                       // do thin-plane spline registration on input:\r
+                                                       Vector3f transformedPt = new Vector3f(pos);\r
+                                                       if ( spline != null )\r
                                                        {\r
-                                                               float x = toTwisted.getFloatC( (int)pos.X, (int)pos.Y, (int)pos.Z, 1 );\r
-                                                               float y = toTwisted.getFloatC( (int)pos.X, (int)pos.Y, (int)pos.Z, 2 );\r
-                                                               float z = toTwisted.getFloatC( (int)pos.X, (int)pos.Y, (int)pos.Z, 3 );\r
+                                                               float[] temp = spline.getCorrespondingPoint(pos.X, pos.Y, pos.Z);\r
+                                                               transformedPt.X = temp[0];\r
+                                                               transformedPt.Y = temp[1];\r
+                                                               transformedPt.Z = temp[2];\r
+                                                       }\r
+                                                       \r
+                                                       if ( ((int)transformedPt.X >= 0) && ((int)transformedPt.X < dimX) && \r
+                                                                ((int)transformedPt.Y >= 0) && ((int)transformedPt.Y < dimY) &&\r
+                                                                ((int)transformedPt.Z >= 0) && ((int)transformedPt.Z < dimZ)    )\r
+                                                       {\r
+                                                               float x = toTwisted.getFloatC( (int)transformedPt.X, (int)transformedPt.Y, (int)transformedPt.Z, 1 );\r
+                                                               float y = toTwisted.getFloatC( (int)transformedPt.X, (int)transformedPt.Y, (int)transformedPt.Z, 2 );\r
+                                                               float z = toTwisted.getFloatC( (int)transformedPt.X, (int)transformedPt.Y, (int)transformedPt.Z, 3 );\r
                                                                Vector3f newPos = new Vector3f(x, y, z);\r
 \r
                                                                if ( !newPos.equals( Vector3f.ZERO ) )\r
@@ -296,7 +352,7 @@ public class PlugInDialogUntwistedToTwisted extends JDialogStandalonePlugin impl
                                                                failedList.add(name);\r
                                                        }\r
                                                }\r
-                                               LatticeModel.saveAnnotationsAsCSV(outputDirName, inputFileName + "_" + targetDirName + ".csv", annotationVOI);\r
+                                               LatticeModel.saveAnnotationsAsCSV(outputDirName, inputFileName + "_" + targetDirName + "_retwist.csv", annotationVOI);\r
                                                String msg = "Retwisted " + annotationVOI.getCurves().size() + " out of " + annotations.getCurves().size() + " annotations" + "\n";\r
                                                for ( int j = 0; j < failedList.size(); j++ )\r
                                                {\r
@@ -531,16 +587,36 @@ public class PlugInDialogUntwistedToTwisted extends JDialogStandalonePlugin impl
 \r
 \r
 \r
-               inputsPanel = new JPanel(new GridBagLayout());\r
-               inputsPanel.setBorder(JDialogBase.buildTitledBorder("Input Options"));\r
+               JPanel inputsPanel = new JPanel(new GridBagLayout());\r
+               inputsPanel.setBorder(JDialogBase.buildTitledBorder("Input - Untwisted Space"));\r
                inputsPanel.setForeground(Color.black);\r
-\r
-               straightenedAnnotationText = gui.buildFileField("Annotations to retwist: ", "", false, JFileChooser.FILES_ONLY, this);\r
+               \r
+               straightenedAnnotationText = gui.buildFileField("Annotations to retwist (csv): ", "", false, JFileChooser.FILES_ONLY, this);\r
                inputsPanel.add(straightenedAnnotationText.getParent(), gbc);\r
                gbc.gridy++;\r
 \r
+\r
+               \r
+               JPanel regPanel = new JPanel(new GridBagLayout());\r
+               regPanel.setBorder(JDialogBase.buildTitledBorder("Registration - Untwisted Space"));\r
+               regPanel.setForeground(Color.black);\r
+\r
+               targetPointsText = gui.buildFileField("Target points - register to this space (csv): ", "", false, JFileChooser.FILES_ONLY, this);\r
+               regPanel.add(targetPointsText.getParent(), gbc);\r
+               gbc.gridy++;\r
+\r
+               inputPointsText = gui.buildFileField("Input points - transform this space to target (csv): ", "", false, JFileChooser.FILES_ONLY, this);\r
+               regPanel.add(inputPointsText.getParent(), gbc);\r
+               gbc.gridy++;\r
+\r
+\r
+\r
+               JPanel dirPanel = new JPanel(new GridBagLayout());\r
+               dirPanel.setBorder(JDialogBase.buildTitledBorder("Directory Information"));\r
+               dirPanel.setForeground(Color.black);\r
+               \r
                targetLocText = gui.buildFileField("Target directory: ", "", false, JFileChooser.DIRECTORIES_ONLY, this);\r
-               inputsPanel.add(targetLocText.getParent(), gbc);\r
+               dirPanel.add(targetLocText.getParent(), gbc);\r
                gbc.gridy++;\r
 \r
 //             baseFileNameText = gui.buildField("Base images name: ", "Decon");\r
@@ -574,7 +650,12 @@ public class PlugInDialogUntwistedToTwisted extends JDialogStandalonePlugin impl
 //             group.add(noseCentric);\r
 //             gbc.gridy++;\r
 \r
-               getContentPane().add(inputsPanel, BorderLayout.NORTH);\r
+               JPanel panel = new JPanel(new BorderLayout());\r
+               panel.add(inputsPanel, BorderLayout.NORTH);\r
+               panel.add(regPanel, BorderLayout.CENTER);\r
+               panel.add(dirPanel, BorderLayout.SOUTH);\r
+               \r
+               getContentPane().add(panel, BorderLayout.NORTH);\r
 \r
                JPanel okCancelPanel = gui.buildOKCancelPanel();\r
                getContentPane().add(okCancelPanel, BorderLayout.SOUTH);\r
@@ -690,6 +771,8 @@ public class PlugInDialogUntwistedToTwisted extends JDialogStandalonePlugin impl
                straightenedAnnotationFile = straightenedAnnotationText.getText();\r
                targetDir = targetLocText.getText();\r
                \r
+               targetPointsFile = targetPointsText.getText();\r
+               inputPointsFile = inputPointsText.getText();\r
                \r
 //             baseFileDir = baseFileLocText.getText();\r
 //             includeRange = new Vector<Integer>();\r