1 package mpi
.fruitfly
.general
;
8 * <p>Copyright: Copyright (c) 2006</p>
12 * @author not attributable
19 import mpi
.fruitfly
.math
.datastructures
.*;
20 import java
.awt
.Point
;
22 import static mpi
.fruitfly
.math
.General
.*;
25 public class ImageArrayConverter
27 public static boolean CUTOFF_VALUES
= true;
28 public static boolean NORM_VALUES
= false;
30 public static ImagePlus
FloatArrayToImagePlus(FloatArray2D image
, String name
, float min
, float max
)
32 ImagePlus imp
= IJ
.createImage(name
,"32-Bit Black", image
.width
, image
.height
, 1);
33 FloatProcessor ip
= (FloatProcessor
)imp
.getProcessor();
34 FloatArrayToFloatProcessor(ip
, image
);
39 ip
.setMinAndMax(min
, max
);
46 public static ImagePlus
FloatArrayToStack(FloatArray3D image
, String name
, float min
, float max
)
48 int width
= image
.width
;
49 int height
= image
.height
;
50 int nstacks
= image
.depth
;
52 ImageStack stack
= new ImageStack(width
, height
);
54 for (int slice
= 0; slice
< nstacks
; slice
++)
56 ImagePlus impResult
= IJ
.createImage("Result", "32-Bit Black", width
, height
, 1);
57 ImageProcessor ipResult
= impResult
.getProcessor();
58 float[] sliceImg
= new float[width
* height
];
60 for (int x
= 0; x
< width
; x
++)
61 for (int y
= 0; y
< height
; y
++)
62 sliceImg
[y
* width
+ x
] = image
.get(x
,y
,slice
);
64 ipResult
.setPixels(sliceImg
);
67 ipResult
.resetMinAndMax();
69 ipResult
.setMinAndMax(min
, max
);
71 stack
.addSlice("Slice " + slice
, ipResult
);
74 return new ImagePlus(name
, stack
);
77 public static ImagePlus
DoubleArrayToStack(double[] image
, int width
, int height
, int nstacks
, String name
, float min
, float max
)
79 ImageStack stack
= new ImageStack(width
, height
);
81 for (int slice
= 0; slice
< nstacks
; slice
++)
83 ImagePlus impResult
= IJ
.createImage("Result", "32-Bit Black", width
, height
, 1);
84 ImageProcessor ipResult
= impResult
.getProcessor();
85 float[] sliceImg
= new float[width
* height
];
87 for (int x
= 0; x
< width
; x
++)
88 for (int y
= 0; y
< height
; y
++)
89 sliceImg
[y
* width
+ x
] = (float)image
[x
+ width
* (y
+ slice
* height
)];
91 ipResult
.setPixels(sliceImg
);
94 ipResult
.resetMinAndMax();
96 ipResult
.setMinAndMax(min
, max
);
98 stack
.addSlice("Slice " + slice
, ipResult
);
101 return new ImagePlus(name
, stack
);
104 public static FloatArray3D
StackToFloatArrayZeroPadding(ImageStack stack
, int wZP
, int hZP
, int dZP
)
106 Object
[] imageStack
= stack
.getImageArray();
108 int width
= stack
.getWidth();
109 int height
= stack
.getHeight();
110 int nstacks
= stack
.getSize();
112 int offsetX
= (wZP
- width
)/2;
113 int offsetY
= (hZP
- height
)/2;
114 int offsetZ
= (dZP
- nstacks
)/2;
116 if (imageStack
== null || imageStack
.length
== 0)
118 System
.out
.println("Image Stack is empty.");
122 if (imageStack
[0] instanceof int[])
124 System
.out
.println("RGB images supported at the moment.");
129 //float[][][] pixels = new float[wZP][hZP][dZP];
130 FloatArray3D pixels
= new FloatArray3D(wZP
,hZP
,dZP
);
133 int stepY
= pixels
.getPos(0,1,0) - width
;
135 if (imageStack
[0] instanceof byte[])
137 for (int countSlice
= 0; countSlice
< nstacks
; countSlice
++)
139 byte[] pixelTmp
= (byte[]) imageStack
[countSlice
];
141 pos
= pixels
.getPos(offsetX
, offsetY
, offsetZ
+ countSlice
);
143 for (int y
= 0; y
< height
; y
++)
145 for (int x
= 0; x
< width
; x
++)
147 //pixels.data[pixels.getPos(x + offsetX, y + offsetY, countSlice + offsetZ)] = (float) (pixelTmp[count++] & 0xff);
148 pixels
.data
[pos
++] = (float) (pixelTmp
[count
++] & 0xff);
154 else if (imageStack
[0] instanceof short[])
155 for (int countSlice
= 0; countSlice
< nstacks
; countSlice
++)
157 short[] pixelTmp
= (short[])imageStack
[countSlice
];
159 pos
= pixels
.getPos(offsetX
, offsetY
, offsetZ
+ countSlice
);
161 for (int y
= 0; y
< height
; y
++)
163 for (int x
= 0; x
< width
; x
++)
165 //pixels.data[pixels.getPos(x + offsetX, y + offsetY, countSlice + offsetZ)] = (float) (pixelTmp[count++] & 0xffff);
166 pixels
.data
[pos
++] = (float) (pixelTmp
[count
++] & 0xffff);
171 else // instance of float[]
172 for (int countSlice
= 0; countSlice
< nstacks
; countSlice
++)
174 float[] pixelTmp
= (float[])imageStack
[countSlice
];
176 pos
= pixels
.getPos(offsetX
, offsetY
, offsetZ
+ countSlice
);
178 for (int y
= 0; y
< height
; y
++)
180 for (int x
= 0; x
< width
; x
++)
182 //pixels.data[pixels.getPos(x + offsetX, y + offsetY, countSlice + offsetZ)] = pixelTmp[count++];
183 pixels
.data
[pos
++] = pixelTmp
[count
++];
192 public static FloatArray4D
StackToFloatArrayComplexZeroPadding(ImageStack stack
, int wZP
, int hZP
, int dZP
)
194 Object
[] imageStack
= stack
.getImageArray();
196 int width
= stack
.getWidth();
197 int height
= stack
.getHeight();
198 int nstacks
= stack
.getSize();
200 int offsetX
= (wZP
- width
)/2;
201 int offsetY
= (hZP
- height
)/2;
202 int offsetZ
= (dZP
- nstacks
)/2;
204 if (imageStack
== null || imageStack
.length
== 0)
206 System
.out
.println("Image Stack is empty.");
210 if (imageStack
[0] instanceof int[])
212 System
.out
.println("RGB images supported at the moment.");
217 //float[][][] pixels = new float[wZP][hZP][dZP];
218 FloatArray4D pixels
= new FloatArray4D(wZP
,hZP
,dZP
,2);
221 int stepY
= pixels
.getPos(0,1,0,0) - width
;
224 if (imageStack
[0] instanceof byte[])
225 for (int countSlice
= 0; countSlice
< nstacks
; countSlice
++)
227 byte[] pixelTmp
= (byte[])imageStack
[countSlice
];
229 pos
= pixels
.getPos(offsetX
, offsetY
, offsetZ
+ countSlice
,0);
231 for (int y
= 0; y
< height
; y
++)
233 for (int x
= 0; x
< width
; x
++)
235 //pixels.data[pixels.getPos(x + offsetX, y + offsetY, countSlice + offsetZ, 0)] = (float) (pixelTmp[count++] & 0xff);
236 pixels
.data
[pos
++] = (float) (pixelTmp
[count
++] & 0xff);
241 else if (imageStack
[0] instanceof short[])
242 for (int countSlice
= 0; countSlice
< nstacks
; countSlice
++)
244 short[] pixelTmp
= (short[])imageStack
[countSlice
];
246 pos
= pixels
.getPos(offsetX
, offsetY
, offsetZ
+ countSlice
,0);
248 for (int y
= 0; y
< height
; y
++)
250 for (int x
= 0; x
< width
; x
++)
252 //pixels.data[pixels.getPos(x + offsetX, y + offsetY, countSlice + offsetZ, 0)] = (float) (pixelTmp[count++] & 0xffff);
253 pixels
.data
[pos
++] = (float) (pixelTmp
[count
++] & 0xffff);
258 else // instance of float[]
259 for (int countSlice
= 0; countSlice
< nstacks
; countSlice
++)
261 float[] pixelTmp
= (float[])imageStack
[countSlice
];
263 pos
= pixels
.getPos(offsetX
, offsetY
, offsetZ
+ countSlice
,0);
265 for (int y
= 0; y
< height
; y
++)
267 for (int x
= 0; x
< width
; x
++)
269 //pixels.data[pixels.getPos(x + offsetX, y + offsetY, countSlice + offsetZ, 0)] = pixelTmp[count++];
270 pixels
.data
[pos
++] = pixelTmp
[count
++];
279 public static double[] StackToDoubleArrayZeroPadding(ImageStack stack
, int wZP
, int hZP
, int dZP
)
281 Object
[] imageStack
= stack
.getImageArray();
283 int width
= stack
.getWidth();
284 int height
= stack
.getHeight();
285 int nstacks
= stack
.getSize();
287 int offsetX
= (wZP
- width
)/2;
288 int offsetY
= (hZP
- height
)/2;
289 int offsetZ
= (dZP
- nstacks
)/2;
291 if (imageStack
== null || imageStack
.length
== 0)
293 System
.out
.println("Image Stack is empty.");
297 if (imageStack
[0] instanceof int[])
299 System
.out
.println("RGB images supported at the moment.");
304 double[] pixels
= new double[wZP
* hZP
* dZP
];
307 if (imageStack
[0] instanceof byte[])
308 for (int countSlice
= 0; countSlice
< nstacks
; countSlice
++)
310 byte[] pixelTmp
= (byte[])imageStack
[countSlice
];
313 for (int y
= 0; y
< height
; y
++)
314 for (int x
= 0; x
< width
; x
++)
315 pixels
[(x
+offsetX
) + width
* ((y
+offsetY
) + (countSlice
+offsetZ
) * height
)] = (pixelTmp
[count
++] & 0xff);
317 else if (imageStack
[0] instanceof short[])
318 for (int countSlice
= 0; countSlice
< nstacks
; countSlice
++)
320 short[] pixelTmp
= (short[])imageStack
[countSlice
];
323 for (int y
= 0; y
< height
; y
++)
324 for (int x
= 0; x
< width
; x
++)
325 pixels
[(x
+offsetX
) + width
* ((y
+offsetY
) + (countSlice
+offsetZ
) * height
)] = (pixelTmp
[count
++] & 0xffff);
327 else // instance of float[]
328 for (int countSlice
= 0; countSlice
< nstacks
; countSlice
++)
330 float[] pixelTmp
= (float[])imageStack
[countSlice
];
333 for (int y
= 0; y
< height
; y
++)
334 for (int x
= 0; x
< width
; x
++)
335 pixels
[(x
+offsetX
) + wZP
* ((y
+offsetY
) + (countSlice
+offsetZ
) * hZP
)] = pixelTmp
[count
++];
341 //id + nd * (id-1 + nd-1 * (... + n2 * i1))
342 public static double[] StackToDoubleArray1D(ImageStack stack
)
344 Object
[] imageStack
= stack
.getImageArray();
345 int width
= stack
.getWidth();
346 int height
= stack
.getHeight();
347 int nstacks
= stack
.getSize();
349 if (imageStack
== null || imageStack
.length
== 0)
351 System
.out
.println("Image Stack is empty.");
355 if (imageStack
[0] instanceof int[])
357 System
.out
.println("RGB images supported at the moment.");
361 double[] pixels
= new double[width
* height
* nstacks
];
365 if (imageStack
[0] instanceof byte[])
366 for (int countSlice
= 0; countSlice
< nstacks
; countSlice
++)
368 byte[] pixelTmp
= (byte[])imageStack
[countSlice
];
371 for (int y
= 0; y
< height
; y
++)
372 for (int x
= 0; x
< width
; x
++)
373 pixels
[x
+ width
* (y
+ countSlice
* height
)] = (pixelTmp
[count
++] & 0xff);
375 else if (imageStack
[0] instanceof short[])
376 for (int countSlice
= 0; countSlice
< nstacks
; countSlice
++)
378 short[] pixelTmp
= (short[])imageStack
[countSlice
];
381 for (int y
= 0; y
< height
; y
++)
382 for (int x
= 0; x
< width
; x
++)
383 pixels
[x
+ width
* (y
+ countSlice
* height
)] = (float)(pixelTmp
[count
++] & 0xffff);
385 else // instance of float[]
386 for (int countSlice
= 0; countSlice
< nstacks
; countSlice
++)
388 float[] pixelTmp
= (float[])imageStack
[countSlice
];
391 for (int y
= 0; y
< height
; y
++)
392 for (int x
= 0; x
< width
; x
++)
393 pixels
[x
+ width
* (y
+ countSlice
* height
)] = pixelTmp
[count
++];
400 public static FloatArray3D
StackToFloatArray(ImageStack stack
)
402 Object
[] imageStack
= stack
.getImageArray();
403 int width
= stack
.getWidth();
404 int height
= stack
.getHeight();
405 int nstacks
= stack
.getSize();
407 if (imageStack
== null || imageStack
.length
== 0)
409 System
.out
.println("Image Stack is empty.");
413 if (imageStack
[0] instanceof int[])
415 System
.out
.println("RGB images supported at the moment.");
419 FloatArray3D pixels
= new FloatArray3D(width
, height
, nstacks
);
420 //float[][][] pixels = new float[width][height][nstacks];
424 if (imageStack
[0] instanceof byte[])
425 for (int countSlice
= 0; countSlice
< nstacks
; countSlice
++)
427 byte[] pixelTmp
= (byte[])imageStack
[countSlice
];
430 for (int y
= 0; y
< height
; y
++)
431 for (int x
= 0; x
< width
; x
++)
432 pixels
.data
[pixels
.getPos(x
,y
,countSlice
)] = (float)(pixelTmp
[count
++] & 0xff);
434 else if (imageStack
[0] instanceof short[])
435 for (int countSlice
= 0; countSlice
< nstacks
; countSlice
++)
437 short[] pixelTmp
= (short[])imageStack
[countSlice
];
440 for (int y
= 0; y
< height
; y
++)
441 for (int x
= 0; x
< width
; x
++)
442 pixels
.data
[pixels
.getPos(x
,y
,countSlice
)] = (float)(pixelTmp
[count
++] & 0xffff);
444 else // instance of float[]
445 for (int countSlice
= 0; countSlice
< nstacks
; countSlice
++)
447 float[] pixelTmp
= (float[])imageStack
[countSlice
];
450 for (int y
= 0; y
< height
; y
++)
451 for (int x
= 0; x
< width
; x
++)
452 pixels
.data
[pixels
.getPos(x
,y
,countSlice
)] = pixelTmp
[count
++];
459 public static float[][][] StackToFloatArrayDeprecated(ImageStack stack
)
461 Object
[] imageStack
= stack
.getImageArray();
462 int width
= stack
.getWidth();
463 int height
= stack
.getHeight();
464 int nstacks
= stack
.getSize();
466 if (imageStack
== null || imageStack
.length
== 0)
468 System
.out
.println("Image Stack is empty.");
472 if (imageStack
[0] instanceof int[])
474 System
.out
.println("RGB images supported at the moment.");
478 float[][][] pixels
= new float[width
][height
][nstacks
];
482 if (imageStack
[0] instanceof byte[])
483 for (int countSlice
= 0; countSlice
< nstacks
; countSlice
++)
485 byte[] pixelTmp
= (byte[])imageStack
[countSlice
];
488 for (int y
= 0; y
< height
; y
++)
489 for (int x
= 0; x
< width
; x
++)
490 pixels
[x
][y
][countSlice
] = (float)(pixelTmp
[count
++] & 0xff);
492 else if (imageStack
[0] instanceof short[])
493 for (int countSlice
= 0; countSlice
< nstacks
; countSlice
++)
495 short[] pixelTmp
= (short[])imageStack
[countSlice
];
498 for (int y
= 0; y
< height
; y
++)
499 for (int x
= 0; x
< width
; x
++)
500 pixels
[x
][y
][countSlice
] = (float)(pixelTmp
[count
++] & 0xffff);
502 else // instance of float[]
503 for (int countSlice
= 0; countSlice
< nstacks
; countSlice
++)
505 float[] pixelTmp
= (float[])imageStack
[countSlice
];
508 for (int y
= 0; y
< height
; y
++)
509 for (int x
= 0; x
< width
; x
++)
510 pixels
[x
][y
][countSlice
] = pixelTmp
[count
++];
517 public static FloatArray4D
StackToFloatArrayComplex(ImageStack stack
)
519 Object
[] imageStack
= stack
.getImageArray();
520 int width
= stack
.getWidth();
521 int height
= stack
.getHeight();
522 int nstacks
= stack
.getSize();
524 if (imageStack
== null || imageStack
.length
== 0)
526 System
.out
.println("Image Stack is empty.");
530 if (imageStack
[0] instanceof int[])
532 System
.out
.println("RGB images supported at the moment.");
536 FloatArray4D pixels
= new FloatArray4D(width
, height
, nstacks
, 2);
537 //float[][][] pixels = new float[width][height][nstacks];
541 if (imageStack
[0] instanceof byte[])
542 for (int countSlice
= 0; countSlice
< nstacks
; countSlice
++)
544 byte[] pixelTmp
= (byte[])imageStack
[countSlice
];
547 for (int y
= 0; y
< height
; y
++)
548 for (int x
= 0; x
< width
; x
++)
549 pixels
.data
[pixels
.getPos(x
,y
,countSlice
,0)] = (float)(pixelTmp
[count
++] & 0xff);
551 else if (imageStack
[0] instanceof short[])
552 for (int countSlice
= 0; countSlice
< nstacks
; countSlice
++)
554 short[] pixelTmp
= (short[])imageStack
[countSlice
];
557 for (int y
= 0; y
< height
; y
++)
558 for (int x
= 0; x
< width
; x
++)
559 pixels
.data
[pixels
.getPos(x
,y
,countSlice
,0)] = (float)(pixelTmp
[count
++] & 0xffff);
561 else // instance of float[]
562 for (int countSlice
= 0; countSlice
< nstacks
; countSlice
++)
564 float[] pixelTmp
= (float[])imageStack
[countSlice
];
567 for (int y
= 0; y
< height
; y
++)
568 for (int x
= 0; x
< width
; x
++)
569 pixels
.data
[pixels
.getPos(x
,y
,countSlice
,0)] = pixelTmp
[count
++];
576 public static int[][] ImageToIntArray(ImageProcessor ip
)
579 Object pixelArray
= ip
.getPixels();
582 if (ip
instanceof ByteProcessor
)
584 image
= new int[ip
.getWidth()][ip
.getHeight()];
585 byte[] pixels
= (byte[])pixelArray
;
587 for (int y
= 0; y
< ip
.getHeight(); y
++)
588 for (int x
= 0; x
< ip
.getWidth(); x
++)
589 image
[x
][y
] = pixels
[count
++] & 0xff;
591 else if (ip
instanceof ShortProcessor
)
593 image
= new int[ip
.getWidth()][ip
.getHeight()];
594 short[] pixels
= (short[])pixelArray
;
596 for (int y
= 0; y
< ip
.getHeight(); y
++)
597 for (int x
= 0; x
< ip
.getWidth(); x
++)
598 image
[x
][y
] = pixels
[count
++] & 0xffff;
600 else if (ip
instanceof FloatProcessor
)
602 image
= new int[ip
.getWidth()][ip
.getHeight()];
603 float[] pixels
= (float[])pixelArray
;
605 for (int y
= 0; y
< ip
.getHeight(); y
++)
606 for (int x
= 0; x
< ip
.getWidth(); x
++)
607 image
[x
][y
] = (int)pixels
[count
++];
611 image
= new int[ip
.getWidth()][ip
.getHeight()];
612 int[] pixels
= (int[])pixelArray
;
614 // still unknown how to do...
617 for (int y = 0; y < ip.getHeight(); y++)
618 for (int x = 0; x < ip.getWidth(); x++)
619 image[x][y] = pixels[count++];// & 0xffffff;
626 public static float[][] ImageToFloatArray2DDeprecated(ImageProcessor ip
)
629 Object pixelArray
= ip
.getPixels();
632 if (ip
instanceof ByteProcessor
)
634 image
= new float[ip
.getWidth()][ip
.getHeight()];
635 byte[] pixels
= (byte[])pixelArray
;
637 for (int y
= 0; y
< ip
.getHeight(); y
++)
638 for (int x
= 0; x
< ip
.getWidth(); x
++)
639 image
[x
][y
] = pixels
[count
++] & 0xff;
641 else if (ip
instanceof ShortProcessor
)
643 image
= new float[ip
.getWidth()][ip
.getHeight()];
644 short[] pixels
= (short[])pixelArray
;
646 for (int y
= 0; y
< ip
.getHeight(); y
++)
647 for (int x
= 0; x
< ip
.getWidth(); x
++)
648 image
[x
][y
] = pixels
[count
++] & 0xffff;
650 else if (ip
instanceof FloatProcessor
)
652 image
= new float[ip
.getWidth()][ip
.getHeight()];
653 float[] pixels
= (float[])pixelArray
;
655 for (int y
= 0; y
< ip
.getHeight(); y
++)
656 for (int x
= 0; x
< ip
.getWidth(); x
++)
657 image
[x
][y
] = pixels
[count
++];
661 image
= new float[ip
.getWidth()][ip
.getHeight()];
662 int[] pixels
= (int[])pixelArray
;
664 // still unknown how to do...
667 for (int y = 0; y < ip.getHeight(); y++)
668 for (int x = 0; x < ip.getWidth(); x++)
669 image[x][y] = pixels[count++];// & 0xffffff;
676 public static double[] ImageToDoubleArray1D(ImageProcessor ip
)
679 Object pixelArray
= ip
.getPixels();
682 if (ip
instanceof ByteProcessor
)
684 image
= new double[ip
.getWidth() * ip
.getHeight()];
685 byte[] pixels
= (byte[])pixelArray
;
687 for (int y
= 0; y
< ip
.getHeight(); y
++)
688 for (int x
= 0; x
< ip
.getWidth(); x
++)
689 image
[count
] = pixels
[count
++] & 0xff;
691 else if (ip
instanceof ShortProcessor
)
693 image
= new double[ip
.getWidth() * ip
.getHeight()];
694 short[] pixels
= (short[])pixelArray
;
696 for (int y
= 0; y
< ip
.getHeight(); y
++)
697 for (int x
= 0; x
< ip
.getWidth(); x
++)
698 image
[count
] = pixels
[count
++] & 0xffff;
700 else if (ip
instanceof FloatProcessor
)
702 image
= new double[ip
.getWidth() * ip
.getHeight()];
703 float[] pixels
= (float[])pixelArray
;
705 for (int y
= 0; y
< ip
.getHeight(); y
++)
706 for (int x
= 0; x
< ip
.getWidth(); x
++)
707 image
[count
] = pixels
[count
++];
711 image
= new double[ip
.getWidth() * ip
.getHeight()];
712 int[] pixels
= (int[])pixelArray
;
714 // still unknown how to do...
717 for (int y = 0; y < ip.getHeight(); y++)
718 for (int x = 0; x < ip.getWidth(); x++)
719 image[x][y] = pixels[count++];// & 0xffffff;
726 public static FloatArray2D
ImageToFloatArray2D(ImageProcessor ip
)
729 Object pixelArray
= ip
.getPixels();
732 if (ip
instanceof ByteProcessor
)
734 image
= new FloatArray2D(ip
.getWidth(), ip
.getHeight());
735 byte[] pixels
= (byte[])pixelArray
;
737 for (int y
= 0; y
< ip
.getHeight(); y
++)
738 for (int x
= 0; x
< ip
.getWidth(); x
++)
739 image
.data
[count
] = pixels
[count
++] & 0xff;
741 else if (ip
instanceof ShortProcessor
)
743 image
= new FloatArray2D(ip
.getWidth(), ip
.getHeight());
744 short[] pixels
= (short[])pixelArray
;
746 for (int y
= 0; y
< ip
.getHeight(); y
++)
747 for (int x
= 0; x
< ip
.getWidth(); x
++)
748 image
.data
[count
] = pixels
[count
++] & 0xffff;
750 else if (ip
instanceof FloatProcessor
)
752 image
= new FloatArray2D(ip
.getWidth(), ip
.getHeight());
753 float[] pixels
= (float[])pixelArray
;
755 for (int y
= 0; y
< ip
.getHeight(); y
++)
756 for (int x
= 0; x
< ip
.getWidth(); x
++)
757 image
.data
[count
] = pixels
[count
++];
761 image
= new FloatArray2D(ip
.getWidth(), ip
.getHeight());
762 int[] pixels
= (int[])pixelArray
;
764 // still unknown how to do...
767 for (int y = 0; y < ip.getHeight(); y++)
768 for (int x = 0; x < ip.getWidth(); x++)
769 image[x][y] = pixels[count++];// & 0xffffff;
776 public static void normFloatArray(FloatArray img
)
778 float min
= img
.data
[0];
779 float max
= img
.data
[0];
781 for (float f
: img
.data
)
790 for (int i
= 0; i
< img
.data
.length
; i
++)
791 img
.data
[i
] = (img
.data
[i
] - min
)/(max
-min
);
794 public static int DRAWTYPE_OVERLAP
= 0;
795 public static int DRAWTYPE_ERRORMAP
= 1;
797 public static FloatArray2D
drawTranslatedImages(FloatArray2D img1
, FloatArray2D img2
, Point shift
, int drawType
)
800 int h1
= img1
.height
;
803 int h2
= img2
.height
;
805 boolean createOverlappingImages
= (drawType
== DRAWTYPE_OVERLAP
);
806 boolean createErrorMap
= (drawType
== DRAWTYPE_ERRORMAP
);
811 int imgW
= max(w1
, w2
) + max(0, Math
.abs(sx
) - Math
.abs((w1
- w2
)));
812 int imgH
= max(h1
, h2
) + max(0, Math
.abs(sy
) - Math
.abs((h1
- h2
)));
814 FloatArray2D outputImg
= new FloatArray2D(imgW
, imgH
);
816 int offsetImg1X
= max(0, -sx
); // + max(0, max(0, -sx) - max(0, (w1 - w2)/2));
817 int offsetImg1Y
= max(0, -sy
); // + max(0, max(0, -sy) - max(0, (h1 - h2)/2));
818 int offsetImg2X
= max(0, sx
); // + max(0, max(0, sx) - max(0, (w2 - w1)/2));
819 int offsetImg2Y
= max(0, sy
); // + max(0, max(0, sy) - max(0, (h2 - h1)/2));
821 float pixel1
, pixel2
;
823 // iterate over whole image
824 for (int y
= 0; y
< imgH
; y
++)
825 for (int x
= 0; x
< imgW
; x
++)
827 pixel1
= img1
.getZero(x
- offsetImg1X
, y
- offsetImg1Y
);
828 pixel2
= img2
.getZero(x
- offsetImg2X
, y
- offsetImg2Y
);
830 if (createOverlappingImages
)
831 outputImg
.set( (pixel1
+ pixel2
) / 2f
, x
, y
);
833 // compute errors only if the images overlap
834 if (x
>= offsetImg1X
&& x
>= offsetImg2X
&&
835 x
< offsetImg1X
+ w1
&& x
< offsetImg2X
+ w2
&&
836 y
>= offsetImg1Y
&& y
>= offsetImg2Y
&&
837 y
< offsetImg1Y
+ h1
&& y
< offsetImg2Y
+ h2
)
840 outputImg
.set( (pixel1
+ pixel2
) / 2f
, x
, y
);
847 public static FloatArray2D
zeroPad(FloatArray2D ip
, int width
, int height
)
849 FloatArray2D image
= new FloatArray2D(width
, height
);
851 int offsetX
= (width
- ip
.width
)/2;
852 int offsetY
= (height
- ip
.height
)/2;
856 System
.err
.println("mpi.fruitfly.general.ImageArrayConverter.ZeroPad(): Zero-Padding size in X smaller than image! " + width
+ " < " + ip
.width
);
862 System
.err
.println("mpi.fruitfly.general.ImageArrayConverter.ZeroPad(): Zero-Padding size in Y smaller than image! " + height
+ " < " + ip
.height
);
868 for (int y
= 0; y
< ip
.height
; y
++)
869 for (int x
= 0; x
< ip
.width
; x
++)
870 image
.set(ip
.data
[count
++], x
+offsetX
, y
+offsetY
);
876 public static FloatArray2D
ImageToFloatArray2DZeroPadding(ImageProcessor ip
, int width
, int height
)
878 FloatArray2D image
= new FloatArray2D(width
, height
);
879 Object pixelArray
= ip
.getPixels();
882 int offsetX
= (width
- ip
.getWidth())/2;
883 int offsetY
= (height
- ip
.getHeight())/2;
887 System
.err
.println("mpi.fruitfly.general.ImageArrayConverter.ImageToFloatArray2DZeroPadding(): Zero-Padding size in X smaller than image! " + width
+ " < " + ip
.getWidth());
893 System
.err
.println("mpi.fruitfly.general.ImageArrayConverter.ImageToFloatArray2DZeroPadding(): Zero-Padding size in Y smaller than image! " + height
+ " < " + ip
.getHeight());
897 if (ip
instanceof ByteProcessor
)
899 byte[] pixels
= (byte[])pixelArray
;
901 for (int y
= 0; y
< ip
.getHeight(); y
++)
902 for (int x
= 0; x
< ip
.getWidth(); x
++)
903 image
.set(pixels
[count
++] & 0xff, x
+offsetX
, y
+offsetY
);
905 else if (ip
instanceof ShortProcessor
)
907 short[] pixels
= (short[])pixelArray
;
909 for (int y
= 0; y
< ip
.getHeight(); y
++)
910 for (int x
= 0; x
< ip
.getWidth(); x
++)
911 image
.set(pixels
[count
++] & 0xffff, x
+offsetX
, y
+offsetY
);
913 else if (ip
instanceof FloatProcessor
)
915 float[] pixels
= (float[])pixelArray
;
917 for (int y
= 0; y
< ip
.getHeight(); y
++)
918 for (int x
= 0; x
< ip
.getWidth(); x
++)
919 image
.set(pixels
[count
++], x
+offsetX
, y
+offsetY
);
923 int[] pixels
= (int[])pixelArray
;
925 // still unknown how to do...
928 for (int y = 0; y < ip.getHeight(); y++)
929 for (int x = 0; x < ip.getWidth(); x++)
930 image[x][y] = pixels[count++];// & 0xffffff;
938 public static double[] ImageToDoubleArray1DZeroPadding(ImageProcessor ip
, int width
, int height
)
941 Object pixelArray
= ip
.getPixels();
944 int offsetX
= (width
- ip
.getWidth())/2;
945 int offsetY
= (height
- ip
.getHeight())/2;
949 System
.err
.println("mpi.fruitfly.general.ImageArrayConverter.ImageToDoubleArray1DZeroPadding(): Zero-Padding size in X smaller than image! " + width
+ " < " + ip
.getWidth());
955 System
.err
.println("mpi.fruitfly.general.ImageArrayConverter.ImageToDoubleArray1DZeroPadding(): Zero-Padding size in Y smaller than image! " + height
+ " < " + ip
.getHeight());
959 if (ip
instanceof ByteProcessor
)
961 image
= new double[width
* height
];
962 byte[] pixels
= (byte[])pixelArray
;
964 for (int y
= 0; y
< ip
.getHeight(); y
++)
965 for (int x
= 0; x
< ip
.getWidth(); x
++)
966 image
[(y
+offsetY
)*width
+ x
+ offsetX
] = pixels
[count
++] & 0xff;
968 else if (ip
instanceof ShortProcessor
)
970 image
= new double[width
* height
];
971 short[] pixels
= (short[])pixelArray
;
973 for (int y
= 0; y
< ip
.getHeight(); y
++)
974 for (int x
= 0; x
< ip
.getWidth(); x
++)
975 image
[(y
+offsetY
)*width
+ x
+ offsetX
] = pixels
[count
++] & 0xffff;
977 else if (ip
instanceof FloatProcessor
)
979 image
= new double[width
* height
];
980 float[] pixels
= (float[])pixelArray
;
982 for (int y
= 0; y
< ip
.getHeight(); y
++)
983 for (int x
= 0; x
< ip
.getWidth(); x
++)
984 image
[(y
+offsetY
)*width
+ x
+ offsetX
] = pixels
[count
++];
988 image
= new double[width
* height
];
989 int[] pixels
= (int[])pixelArray
;
991 // still unknown how to do...
994 for (int y = 0; y < ip.getHeight(); y++)
995 for (int x = 0; x < ip.getWidth(); x++)
996 image[x][y] = pixels[count++];// & 0xffffff;
1003 public static void ArrayToByteProcessor(ImageProcessor ip
, int[][] pixels
)
1005 byte[] data
= new byte[pixels
.length
* pixels
[0].length
];
1008 for (int y
= 0; y
< pixels
[0].length
; y
++)
1009 for (int x
= 0; x
< pixels
.length
; x
++)
1010 data
[count
++] = (byte)(pixels
[x
][y
] & 0xff);
1015 public static void ArrayToByteProcessor(ImageProcessor ip
, float[][] pixels
)
1017 byte[] data
= new byte[pixels
.length
* pixels
[0].length
];
1020 for (int y
= 0; y
< pixels
[0].length
; y
++)
1021 for (int x
= 0; x
< pixels
.length
; x
++)
1022 data
[count
++] = (byte)(((int)pixels
[x
][y
]) & 0xff);
1027 public static void ArrayToFloatProcessor(ImageProcessor ip
, double[] pixels
, int width
, int height
)
1029 float[] data
= new float[width
* height
];
1032 for (int y
= 0; y
< height
; y
++)
1033 for (int x
= 0; x
< width
; x
++)
1034 data
[count
] = (float)pixels
[count
++];
1037 ip
.resetMinAndMax();
1040 public static void ArrayToFloatProcessor(ImageProcessor ip
, float[] pixels
, int width
, int height
)
1042 float[] data
= new float[width
* height
];
1045 for (int y
= 0; y
< height
; y
++)
1046 for (int x
= 0; x
< width
; x
++)
1047 data
[count
] = (float)pixels
[count
++];
1050 ip
.resetMinAndMax();
1053 public static void FloatArrayToFloatProcessor(ImageProcessor ip
, FloatArray2D pixels
)
1055 float[] data
= new float[pixels
.width
* pixels
.height
];
1058 for (int y
= 0; y
< pixels
.height
; y
++)
1059 for (int x
= 0; x
< pixels
.width
; x
++)
1060 data
[count
] = pixels
.data
[count
++];
1063 ip
.resetMinAndMax();
1066 public static void normPixelValuesToByte(int[][] pixels
, boolean cutoff
)
1068 int max
= 0, min
= 255;
1070 // check minmal and maximal values or cut of values that are higher or lower than 255 resp. 0
1071 for (int y
= 0; y
< pixels
[0].length
; y
++)
1072 for (int x
= 0; x
< pixels
.length
; x
++)
1076 if (pixels
[x
][y
] < 0)
1079 if (pixels
[x
][y
] > 255)
1084 if (pixels
[x
][y
] < min
)
1087 if (pixels
[x
][y
] > max
)
1096 // if they do not match bytevalues we have to do something
1097 if (max
> 255 || min
< 0)
1101 factor
= (max
-min
) / 255.0;
1103 for (int y
= 0; y
< pixels
[0].length
; y
++)
1104 for (int x
= 0; x
< pixels
.length
; x
++)
1105 pixels
[x
][y
] = (int)((pixels
[x
][y
] - min
) / factor
);
1109 public static void normPixelValuesToByte(float[][] pixels
, boolean cutoff
)
1111 float max
= 0, min
= 255;
1113 // check minmal and maximal values or cut of values that are higher or lower than 255 resp. 0
1114 for (int y
= 0; y
< pixels
[0].length
; y
++)
1115 for (int x
= 0; x
< pixels
.length
; x
++)
1119 if (pixels
[x
][y
] < 0)
1122 if (pixels
[x
][y
] > 255)
1127 if (pixels
[x
][y
] < min
)
1130 if (pixels
[x
][y
] > max
)
1139 // if they do not match bytevalues we have to do something
1140 if (max
> 255 || min
< 0)
1144 factor
= (max
-min
) / 255.0;
1146 for (int y
= 0; y
< pixels
[0].length
; y
++)
1147 for (int x
= 0; x
< pixels
.length
; x
++)
1148 pixels
[x
][y
] = (int)((pixels
[x
][y
] - min
) / factor
);
1152 public static void normPixelValuesToByte(float[][][] pixels
, boolean cutoff
)
1154 float max
= 0, min
= 255;
1156 // check minmal and maximal values or cut of values that are higher or lower than 255 resp. 0
1157 for (int z
= 0; z
< pixels
[0][0].length
; z
++)
1158 for (int y
= 0; y
< pixels
[0].length
; y
++)
1159 for (int x
= 0; x
< pixels
.length
; x
++)
1163 if (pixels
[x
][y
][z
] < 0)
1164 pixels
[x
][y
][z
] = 0;
1166 if (pixels
[x
][y
][z
] > 255)
1167 pixels
[x
][y
][z
] = 255;
1171 if (pixels
[x
][y
][z
] < min
)
1172 min
= pixels
[x
][y
][z
];
1174 if (pixels
[x
][y
][z
] > max
)
1175 max
= pixels
[x
][y
][z
];
1183 // if they do not match bytevalues we have to do something
1184 if (max
> 255 || min
< 0)
1188 factor
= (max
-min
) / 255.0;
1190 for (int z
= 0; z
< pixels
[0][0].length
; z
++)
1191 for (int y
= 0; y
< pixels
[0].length
; y
++)
1192 for (int x
= 0; x
< pixels
.length
; x
++)
1193 pixels
[x
][y
][z
] = (int)((pixels
[x
][y
][z
] - min
) / factor
);