Added thin-plate spline registration for an input image to the worm retwisting so...
authorabokinsky <abokinsky@ba61647d-9d00-f842-95cd-605cb4296b96>
Mon, 26 Mar 2018 21:02:19 +0000 (21:02 +0000)
committerabokinsky <abokinsky@ba61647d-9d00-f842-95cd-605cb4296b96>
Mon, 26 Mar 2018 21:02:19 +0000 (21:02 +0000)
git-svn-id: https://citdcbmipav.cit.nih.gov/repos-pub/mipav/trunk@15431 ba61647d-9d00-f842-95cd-605cb4296b96

mipav/src/gov/nih/mipav/view/renderer/WildMagic/VOI/VOIManagerInterface.java
mipav/src/plugins/PlugInDialogUntwistedToTwisted.java

index ccb2d84..c9eb76c 100644 (file)
@@ -5039,12 +5039,18 @@ public class VOIManagerInterface implements ActionListener, VOIHandlerInterface,
             int nVOI = VOIs.size();\r
 \r
             for (int i = 0; i < nVOI; i++) {\r
+               String voiName = JDialogBase.makeImageName( VOIs.VOIAt(i).getName(), "");\r
+                int index = voiName.lastIndexOf(File.separator);\r
+                if ( index != -1 )\r
+                {\r
+                       voiName = voiName.substring(index+1, voiName.length() );\r
+                }\r
                 if (VOIs.VOIAt(i).getCurveType() != VOI.ANNOTATION) {\r
                     FileVOI fileVOI = new FileVOI(VOIs.VOIAt(i).getName() + ".xml", voiDir, currentImage);\r
                     fileVOI.writeVOI(VOIs.VOIAt(i), true);\r
                 } else {\r
-                    FileVOI fileVOI = new FileVOI(VOIs.VOIAt(i).getName() + ".lbl", voiDir, currentImage);\r
-                    fileVOI.writeAnnotationInVoiAsXML(VOIs.VOIAt(i).getName(),true);\r
+                    FileVOI fileVOI = new FileVOI(voiName + ".lbl", voiDir, currentImage);\r
+                    fileVOI.writeAnnotationInVoiAsXML(voiName,true);\r
                 }\r
             }\r
 \r
index 162b397..c47813a 100644 (file)
@@ -95,6 +95,10 @@ public class PlugInDialogUntwistedToTwisted extends JDialogStandalonePlugin impl
        private String targetPointsFile;\r
        private JTextField inputPointsText;\r
        private String inputPointsFile;\r
+       private JTextField targetImageText;\r
+       private String targetImageName;\r
+       private JTextField inputImageText;\r
+       private String inputImageName;\r
 //     private JTextField  baseFileNameText;\r
 //     private Vector<Integer> includeRange;\r
 //     private JPanel inputsPanel;\r
@@ -255,46 +259,102 @@ public class PlugInDialogUntwistedToTwisted extends JDialogStandalonePlugin impl
                File inputFile = new File( inputPointsFile );\r
                System.err.println(toTwistedName + " " + toTwistedFile.exists() );\r
                ModelImage toTwisted = null;\r
-               if ( toTwistedFile.exists() && annotationFile.exists() && targetFile.exists() && inputFile.exists() )\r
+               if ( toTwistedFile.exists() && annotationFile.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
+                               AlgorithmTPSpline spline = null;\r
+                               if ( targetFile.exists() && inputFile.exists() )\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
+                                       \r
+                                       ModelImage inputImage = null;\r
+                                       if ( inputImageName != null ) {\r
+                                               File inputImageFile = new File( inputImageName );\r
+                                               if ( inputImageFile.exists() )\r
+                                               {\r
+                                                       fileIO = new FileIO();\r
+                                                       inputImage = fileIO.readImage(inputImageName);\r
+                                               }\r
+                                       }\r
+                                       boolean writeOutput = true;\r
+                                       if ( inputImage == null )\r
+                                       {\r
+                                               // no input image to warp, use the toTwisted image for dimensions, etc.\r
+                                               inputImage = toTwisted;\r
+                                               writeOutput = false;\r
+                                       }\r
+                                       ModelImage targetImage = null;\r
+                                       if ( (targetImageName != null) && writeOutput) {\r
+                                               File inputImageFile = new File( targetImageName );\r
+                                               if ( inputImageFile.exists() )\r
+                                               {\r
+                                                       fileIO = new FileIO();\r
+                                                       targetImage = fileIO.readImage(targetImageName);\r
+                                               }\r
+                                       }\r
+                                       if ( targetImage == null )\r
+                                       {\r
+                                               targetImage = inputImage;\r
+                                       }\r
+                                       spline = new AlgorithmTPSpline(xSource, ySource, zSource, xTarget, yTarget, zTarget, 0.0f, targetImage,\r
+                                                       inputImage, !writeOutput);\r
 \r
-                       spline.setRunningInSeparateThread(false);\r
-                       spline.run();\r
+                                       spline.setRunningInSeparateThread(false);\r
+                                       spline.run();\r
+                                       \r
+                                       if ( writeOutput )\r
+                                       {\r
+                                               ModelImage outputImage = spline.getResultImage();\r
+                                               outputImage.calcMinMax();\r
+                                               new ViewJFrameImage((ModelImage) outputImage.clone());\r
+                                               System.err.println( outputImage.getImageName() + "   " + outputImage.getImageDirectory() );\r
+                                               ModelImage.saveImage(outputImage);\r
+                                               outputImage.disposeLocal(false);\r
+                                               outputImage = null;\r
+                                               inputImage.disposeLocal(false);\r
+                                               inputImage = null;\r
+                                               \r
+                                               if ( targetImage != null )\r
+                                               {\r
+                                                       targetImage.disposeLocal(false);\r
+                                                       targetImage = null;\r
+                                               }\r
+                                       }\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
+                                       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
+                                               if ( (Math.round(targetPt.X) != Math.round(transformedPt[0])) || (Math.round(targetPt.Y) != Math.round(transformedPt[1])) || (Math.round(targetPt.Z) != Math.round(transformedPt[2])) )\r
+                                               {\r
+                                                       System.err.println( sourcePt + "   =>  " + transformedPt[0] + " " + transformedPt[1] + " " + transformedPt[2] );\r
+                                               }\r
+                                       }\r
+                               }\r
                        \r
                                \r
                                int dimX = toTwisted.getExtents().length > 0 ? toTwisted.getExtents()[0] : 1;\r
@@ -333,7 +393,7 @@ public class PlugInDialogUntwistedToTwisted extends JDialogStandalonePlugin impl
                                                                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
+                                                               System.err.println(newPos);\r
                                                                if ( !newPos.equals( Vector3f.ZERO ) )\r
                                                                {\r
                                                                        VOIText newText = new VOIText();\r
@@ -361,12 +421,20 @@ public class PlugInDialogUntwistedToTwisted extends JDialogStandalonePlugin impl
                                                MipavUtil.displayInfo( msg );\r
                                        }\r
                                }\r
+                               toTwisted.disposeLocal(false);\r
+                               toTwisted = null;\r
                        }\r
                        else\r
                        {\r
+                               MipavUtil.displayError( "Error reading file: " + \r
+                                               toTwistedFile.getName() + " " +  annotationFile.getName() + " " + targetFile.getName() + " " + inputFile.getName() );\r
                                System.err.println("image open failed");\r
                        }\r
                }\r
+               else\r
+               {\r
+                       MipavUtil.displayError( "Error reading file: " + toTwistedFile.getName() );\r
+               }\r
                \r
                \r
 //             if ( includeRange != null )\r
@@ -598,7 +666,7 @@ public class PlugInDialogUntwistedToTwisted extends JDialogStandalonePlugin impl
 \r
                \r
                JPanel regPanel = new JPanel(new GridBagLayout());\r
-               regPanel.setBorder(JDialogBase.buildTitledBorder("Registration - Untwisted Space"));\r
+               regPanel.setBorder(JDialogBase.buildTitledBorder("Registration (optional) - 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
@@ -609,6 +677,14 @@ public class PlugInDialogUntwistedToTwisted extends JDialogStandalonePlugin impl
                regPanel.add(inputPointsText.getParent(), gbc);\r
                gbc.gridy++;\r
 \r
+               targetImageText = gui.buildFileField("Target image (optional) - transform to this space: ", "", false, JFileChooser.FILES_ONLY, this);\r
+               regPanel.add(targetImageText.getParent(), gbc);\r
+               gbc.gridy++;\r
+\r
+               inputImageText = gui.buildFileField("Input image (optional) - transform this image to target space: ", "", false, JFileChooser.FILES_ONLY, this);\r
+               regPanel.add(inputImageText.getParent(), gbc);\r
+               gbc.gridy++;\r
+\r
 \r
 \r
                JPanel dirPanel = new JPanel(new GridBagLayout());\r
@@ -773,6 +849,9 @@ public class PlugInDialogUntwistedToTwisted extends JDialogStandalonePlugin impl
                \r
                targetPointsFile = targetPointsText.getText();\r
                inputPointsFile = inputPointsText.getText();\r
+\r
+               targetImageName = targetImageText.getText();\r
+               inputImageName = inputImageText.getText();\r
                \r
 //             baseFileDir = baseFileLocText.getText();\r
 //             includeRange = new Vector<Integer>();\r