Fixed potential threading issue with Swing and the Layer panels when
[trakem2.git] / mpi / fruitfly / general / ImageArrayConverter.java
blob23c30f156b6cf473a3f486d69b4c7e86f040b172
1 package mpi.fruitfly.general;
3 /**
4 * <p>Title: </p>
6 * <p>Description: </p>
8 * <p>Copyright: Copyright (c) 2006</p>
10 * <p>Company: </p>
12 * @author not attributable
13 * @version 1.0
16 import ij.process.*;
17 import ij.*;
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);
36 if (min == max)
37 ip.resetMinAndMax();
38 else
39 ip.setMinAndMax(min, max);
41 imp.updateAndDraw();
43 return imp;
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);
66 if (min == max)
67 ipResult.resetMinAndMax();
68 else
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);
93 if (min == max)
94 ipResult.resetMinAndMax();
95 else
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.");
119 return null;
122 if (imageStack[0] instanceof int[])
124 System.out.println("RGB images supported at the moment.");
125 return null;
129 //float[][][] pixels = new float[wZP][hZP][dZP];
130 FloatArray3D pixels = new FloatArray3D(wZP,hZP,dZP);
132 int count, pos;
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];
140 count = 0;
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);
150 pos += stepY;
154 else if (imageStack[0] instanceof short[])
155 for (int countSlice = 0; countSlice < nstacks; countSlice++)
157 short[] pixelTmp = (short[])imageStack[countSlice];
158 count = 0;
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);
168 pos += stepY;
171 else // instance of float[]
172 for (int countSlice = 0; countSlice < nstacks; countSlice++)
174 float[] pixelTmp = (float[])imageStack[countSlice];
175 count = 0;
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++];
185 pos += stepY;
189 return pixels;
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.");
207 return null;
210 if (imageStack[0] instanceof int[])
212 System.out.println("RGB images supported at the moment.");
213 return null;
217 //float[][][] pixels = new float[wZP][hZP][dZP];
218 FloatArray4D pixels = new FloatArray4D(wZP,hZP,dZP,2);
220 int count, pos;
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];
228 count = 0;
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);
238 pos += stepY;
241 else if (imageStack[0] instanceof short[])
242 for (int countSlice = 0; countSlice < nstacks; countSlice++)
244 short[] pixelTmp = (short[])imageStack[countSlice];
245 count = 0;
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);
255 pos += stepY;
258 else // instance of float[]
259 for (int countSlice = 0; countSlice < nstacks; countSlice++)
261 float[] pixelTmp = (float[])imageStack[countSlice];
262 count = 0;
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++];
272 pos += stepY;
276 return pixels;
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.");
294 return null;
297 if (imageStack[0] instanceof int[])
299 System.out.println("RGB images supported at the moment.");
300 return null;
304 double[] pixels = new double[wZP * hZP * dZP];
305 int count;
307 if (imageStack[0] instanceof byte[])
308 for (int countSlice = 0; countSlice < nstacks; countSlice++)
310 byte[] pixelTmp = (byte[])imageStack[countSlice];
311 count = 0;
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];
321 count = 0;
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];
331 count = 0;
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++];
338 return pixels;
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.");
352 return null;
355 if (imageStack[0] instanceof int[])
357 System.out.println("RGB images supported at the moment.");
358 return null;
361 double[] pixels = new double[width * height * nstacks];
362 int count;
365 if (imageStack[0] instanceof byte[])
366 for (int countSlice = 0; countSlice < nstacks; countSlice++)
368 byte[] pixelTmp = (byte[])imageStack[countSlice];
369 count = 0;
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];
379 count = 0;
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];
389 count = 0;
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++];
397 return pixels;
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.");
410 return null;
413 if (imageStack[0] instanceof int[])
415 System.out.println("RGB images supported at the moment.");
416 return null;
419 FloatArray3D pixels = new FloatArray3D(width, height, nstacks);
420 //float[][][] pixels = new float[width][height][nstacks];
421 int count;
424 if (imageStack[0] instanceof byte[])
425 for (int countSlice = 0; countSlice < nstacks; countSlice++)
427 byte[] pixelTmp = (byte[])imageStack[countSlice];
428 count = 0;
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];
438 count = 0;
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];
448 count = 0;
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++];
456 return pixels;
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.");
469 return null;
472 if (imageStack[0] instanceof int[])
474 System.out.println("RGB images supported at the moment.");
475 return null;
478 float[][][] pixels = new float[width][height][nstacks];
479 int count;
482 if (imageStack[0] instanceof byte[])
483 for (int countSlice = 0; countSlice < nstacks; countSlice++)
485 byte[] pixelTmp = (byte[])imageStack[countSlice];
486 count = 0;
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];
496 count = 0;
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];
506 count = 0;
508 for (int y = 0; y < height; y++)
509 for (int x = 0; x < width; x++)
510 pixels[x][y][countSlice] = pixelTmp[count++];
514 return pixels;
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.");
527 return null;
530 if (imageStack[0] instanceof int[])
532 System.out.println("RGB images supported at the moment.");
533 return null;
536 FloatArray4D pixels = new FloatArray4D(width, height, nstacks, 2);
537 //float[][][] pixels = new float[width][height][nstacks];
538 int count;
541 if (imageStack[0] instanceof byte[])
542 for (int countSlice = 0; countSlice < nstacks; countSlice++)
544 byte[] pixelTmp = (byte[])imageStack[countSlice];
545 count = 0;
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];
555 count = 0;
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];
565 count = 0;
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++];
573 return pixels;
576 public static int[][] ImageToIntArray(ImageProcessor ip)
578 int[][] image;
579 Object pixelArray = ip.getPixels();
580 int count = 0;
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++];
609 else //RGB
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;
623 return image;
626 public static float[][] ImageToFloatArray2DDeprecated(ImageProcessor ip)
628 float[][] image;
629 Object pixelArray = ip.getPixels();
630 int count = 0;
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++];
659 else //RGB
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;
673 return image;
676 public static double[] ImageToDoubleArray1D(ImageProcessor ip)
678 double[] image;
679 Object pixelArray = ip.getPixels();
680 int count = 0;
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++];
709 else //RGB
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;
723 return image;
726 public static FloatArray2D ImageToFloatArray2D(ImageProcessor ip)
728 FloatArray2D image;
729 Object pixelArray = ip.getPixels();
730 int count = 0;
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++];
759 else //RGB
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;
773 return image;
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)
783 if (f > max)
784 max = f;
786 if (f < min)
787 min = f;
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)
799 int w1 = img1.width;
800 int h1 = img1.height;
802 int w2 = img2.width;
803 int h2 = img2.height;
805 boolean createOverlappingImages = (drawType == DRAWTYPE_OVERLAP);
806 boolean createErrorMap = (drawType == DRAWTYPE_ERRORMAP);
808 int sx = shift.x;
809 int sy = shift.y;
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)
839 if (createErrorMap)
840 outputImg.set( (pixel1 + pixel2) / 2f, x, y);
844 return outputImg;
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;
854 if (offsetX < 0)
856 System.err.println("mpi.fruitfly.general.ImageArrayConverter.ZeroPad(): Zero-Padding size in X smaller than image! " + width + " < " + ip.width);
857 return null;
860 if (offsetY < 0)
862 System.err.println("mpi.fruitfly.general.ImageArrayConverter.ZeroPad(): Zero-Padding size in Y smaller than image! " + height + " < " + ip.height);
863 return null;
866 int count = 0;
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);
872 return image;
876 public static FloatArray2D ImageToFloatArray2DZeroPadding(ImageProcessor ip, int width, int height)
878 FloatArray2D image = new FloatArray2D(width, height);
879 Object pixelArray = ip.getPixels();
880 int count = 0;
882 int offsetX = (width - ip.getWidth())/2;
883 int offsetY = (height - ip.getHeight())/2;
885 if (offsetX < 0)
887 System.err.println("mpi.fruitfly.general.ImageArrayConverter.ImageToFloatArray2DZeroPadding(): Zero-Padding size in X smaller than image! " + width + " < " + ip.getWidth());
888 return null;
891 if (offsetY < 0)
893 System.err.println("mpi.fruitfly.general.ImageArrayConverter.ImageToFloatArray2DZeroPadding(): Zero-Padding size in Y smaller than image! " + height + " < " + ip.getHeight());
894 return null;
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);
921 else //RGB
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;
934 return image;
938 public static double[] ImageToDoubleArray1DZeroPadding(ImageProcessor ip, int width, int height)
940 double[] image;
941 Object pixelArray = ip.getPixels();
942 int count = 0;
944 int offsetX = (width - ip.getWidth())/2;
945 int offsetY = (height - ip.getHeight())/2;
947 if (offsetX < 0)
949 System.err.println("mpi.fruitfly.general.ImageArrayConverter.ImageToDoubleArray1DZeroPadding(): Zero-Padding size in X smaller than image! " + width + " < " + ip.getWidth());
950 return null;
953 if (offsetY < 0)
955 System.err.println("mpi.fruitfly.general.ImageArrayConverter.ImageToDoubleArray1DZeroPadding(): Zero-Padding size in Y smaller than image! " + height + " < " + ip.getHeight());
956 return null;
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++];
986 else //RGB
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;
1000 return image;
1003 public static void ArrayToByteProcessor(ImageProcessor ip, int[][] pixels)
1005 byte[] data = new byte[pixels.length * pixels[0].length];
1007 int count = 0;
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);
1012 ip.setPixels(data);
1015 public static void ArrayToByteProcessor(ImageProcessor ip, float[][] pixels)
1017 byte[] data = new byte[pixels.length * pixels[0].length];
1019 int count = 0;
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);
1024 ip.setPixels(data);
1027 public static void ArrayToFloatProcessor(ImageProcessor ip, double[] pixels, int width, int height)
1029 float[] data = new float[width * height];
1031 int count = 0;
1032 for (int y = 0; y < height; y++)
1033 for (int x = 0; x < width; x++)
1034 data[count] = (float)pixels[count++];
1036 ip.setPixels(data);
1037 ip.resetMinAndMax();
1040 public static void ArrayToFloatProcessor(ImageProcessor ip, float[] pixels, int width, int height)
1042 float[] data = new float[width * height];
1044 int count = 0;
1045 for (int y = 0; y < height; y++)
1046 for (int x = 0; x < width; x++)
1047 data[count] = (float)pixels[count++];
1049 ip.setPixels(data);
1050 ip.resetMinAndMax();
1053 public static void FloatArrayToFloatProcessor(ImageProcessor ip, FloatArray2D pixels)
1055 float[] data = new float[pixels.width * pixels.height];
1057 int count = 0;
1058 for (int y = 0; y < pixels.height; y++)
1059 for (int x = 0; x < pixels.width; x++)
1060 data[count] = pixels.data[count++];
1062 ip.setPixels(data);
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++)
1074 if (cutoff)
1076 if (pixels[x][y] < 0)
1077 pixels[x][y] = 0;
1079 if (pixels[x][y] > 255)
1080 pixels[x][y] = 255;
1082 else
1084 if (pixels[x][y] < min)
1085 min = pixels[x][y];
1087 if (pixels[x][y] > max)
1088 max = pixels[x][y];
1092 if (cutoff)
1093 return;
1096 // if they do not match bytevalues we have to do something
1097 if (max > 255 || min < 0)
1099 double factor;
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++)
1117 if (cutoff)
1119 if (pixels[x][y] < 0)
1120 pixels[x][y] = 0;
1122 if (pixels[x][y] > 255)
1123 pixels[x][y] = 255;
1125 else
1127 if (pixels[x][y] < min)
1128 min = pixels[x][y];
1130 if (pixels[x][y] > max)
1131 max = pixels[x][y];
1135 if (cutoff)
1136 return;
1139 // if they do not match bytevalues we have to do something
1140 if (max > 255 || min < 0)
1142 double factor;
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++)
1161 if (cutoff)
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;
1169 else
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];
1179 if (cutoff)
1180 return;
1183 // if they do not match bytevalues we have to do something
1184 if (max > 255 || min < 0)
1186 double factor;
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);