File: Selection.java

package info (click to toggle)
imagej 1.54g-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,520 kB
  • sloc: java: 132,209; sh: 286; xml: 255; makefile: 6
file content (1102 lines) | stat: -rw-r--r-- 34,669 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
package ij.plugin;
import ij.*;
import ij.gui.*;
import ij.process.*;
import ij.measure.*;
import ij.plugin.frame.*;
import ij.macro.Interpreter;
import ij.plugin.filter.*;
import ij.util.Tools;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Vector;
import java.awt.geom.*;


/** This plugin implements the commands in the Edit/Selection submenu. */
public class Selection implements PlugIn, Measurements {
	private ImagePlus imp;
	private float[] kernel = {1f, 1f, 1f, 1f, 1f};
	private float[] kernel3 = {1f, 1f, 1f};
	private static int bandSize = 15; // pixels
	private static Color linec, fillc;
	private static int lineWidth = 1;
	private static boolean smooth;
	private static boolean adjust;
	private static double translateX;
	private static double translateY;

	public void run(String arg) {
		imp = WindowManager.getCurrentImage();
		if (arg.equals("add")) {
			addToRoiManager(imp);
			return;
		}		
		if (imp==null) {
			if (!(IJ.isMacro()&&arg.equals("none")))
				IJ.noImage();
			return;
		}
		if (arg.equals("all")) {
			if (imp.okToDeleteRoi()) {
				imp.saveRoi();
				imp.setRoi(0,0,imp.getWidth(),imp.getHeight());
			}
		} else if (arg.equals("none")) {
			if (imp.okToDeleteRoi())
				imp.deleteRoi();
		} else if (arg.equals("restore"))
			imp.restoreRoi();
		else if (arg.equals("spline"))
			fitSpline();
		else if (arg.equals("interpolate"))
			interpolate();
		else if (arg.equals("circle"))
			fitCircle(imp);
		else if (arg.equals("ellipse"))
			createEllipse(imp);
		else if (arg.equals("hull"))
			convexHull(imp);
		else if (arg.equals("mask"))
			createMask(imp);		
		else if (arg.equals("from"))
			createSelectionFromMask(imp);		
		else if (arg.equals("inverse"))
			invert(imp); 
		else if (arg.equals("toarea"))
			lineToArea(imp); 
		else if (arg.equals("toline"))
			areaToLine(imp); 
		else if (arg.equals("properties"))
			{setProperties("Properties ", imp.getRoi()); imp.draw();}
		else if (arg.equals("band"))
			makeBand(imp);
		else if (arg.equals("tobox"))
			toBoundingBox(imp); 
		else if (arg.equals("rotate"))
			rotate(imp); 
		else if (arg.equals("translate"))
			translate(imp); 
		else if (arg.equals("enlarge"))
			enlarge(imp); 
		else if (arg.equals("rect"))
			fitRectangle(imp); 
	}
	
	private void rotate(ImagePlus imp) {
		if (!imp.okToDeleteRoi()) return;
		Roi roi = imp.getRoi();
		if (IJ.macroRunning()) {
			String options = Macro.getOptions();
			if (options!=null && (options.indexOf("grid=")!=-1||options.indexOf("interpolat")!=-1)) {
				IJ.run("Rotate... ", options); // run Image>Transform>Rotate
				return;
			}
		}
		(new RoiRotator()).run("");
	}
	
	private void enlarge(ImagePlus imp) {
		if (!imp.okToDeleteRoi()) return;
		Roi roi = imp.getRoi();
		if (roi!=null) {
			Undo.setup(Undo.ROI, imp);
			roi = (Roi)roi.clone();
			(new RoiEnlarger()).run("");
		} else
			noRoi("Enlarge");
	}

	/*
	if selection is closed shape, create a circle with the same area and centroid, otherwise use<br>
	the Pratt method to fit a circle to the points that define the line or multi-point selection.<br>
	Reference: Pratt V., Direct least-squares fitting of algebraic surfaces", Computer Graphics, Vol. 21, pages 145-152 (1987).<br>
	Original code: Nikolai Chernov's MATLAB script for Newton-based Pratt fit.<br>
	(http://www.math.uab.edu/~chernov/cl/MATLABcircle.html)<br>
	Java version: https://github.com/mdoube/BoneJ/blob/master/src/org/doube/geometry/FitCircle.java<br>
	Authors: Nikolai Chernov, Michael Doube, Ved Sharma
	*/
	void fitCircle(ImagePlus imp) {
		if (!imp.okToDeleteRoi()) return;
		Roi roi = imp.getRoi();
		if (roi==null) {
			noRoi("Fit Circle");
			return;
		}
		
		if (roi.isArea()) {	  //create circle with the same area and centroid
			Undo.setup(Undo.ROI, imp);
			ImageProcessor ip = imp.getProcessor();
			ip.setRoi(roi);
			ImageStatistics stats = ImageStatistics.getStatistics(ip, Measurements.AREA+Measurements.CENTROID, null);
			double r = Math.sqrt(stats.pixelCount/Math.PI);
			imp.deleteRoi();
			int d = (int)Math.round(2.0*r);
			Roi roi2 = new OvalRoi((int)Math.round(stats.xCentroid-r), (int)Math.round(stats.yCentroid-r), d, d);
			transferProperties(roi, roi2);
			imp.setRoi(roi2);
			return;
		}
		
		Polygon poly = roi.getPolygon();
		int n=poly.npoints;
		int[] x = poly.xpoints;
		int[] y = poly.ypoints;
		if (n<3) {
			IJ.error("Fit Circle", "At least 3 points are required to fit a circle.");
			return;
		}
		
		// calculate point centroid
		double sumx = 0, sumy = 0;
		for (int i=0; i<n; i++) {
			sumx = sumx + poly.xpoints[i];
			sumy = sumy + poly.ypoints[i];
		}
		double meanx = sumx/n;
		double meany = sumy/n;
		
		// calculate moments
		double[] X = new double[n], Y = new double[n];
		double Mxx=0, Myy=0, Mxy=0, Mxz=0, Myz=0, Mzz=0;
		for (int i=0; i<n; i++) {
			X[i] = x[i] - meanx;
			Y[i] = y[i] - meany;
			double Zi = X[i]*X[i] + Y[i]*Y[i];
			Mxy = Mxy + X[i]*Y[i];
			Mxx = Mxx + X[i]*X[i];
			Myy = Myy + Y[i]*Y[i];
			Mxz = Mxz + X[i]*Zi;
			Myz = Myz + Y[i]*Zi;
			Mzz = Mzz + Zi*Zi;
		}
		Mxx = Mxx/n;
		Myy = Myy/n;
		Mxy = Mxy/n;
		Mxz = Mxz/n;
		Myz = Myz/n;
		Mzz = Mzz/n;
		
		// calculate the coefficients of the characteristic polynomial
		double Mz = Mxx + Myy;
		double Cov_xy = Mxx*Myy - Mxy*Mxy;
		double Mxz2 = Mxz*Mxz;
		double Myz2 = Myz*Myz;
		double A2 = 4*Cov_xy - 3*Mz*Mz - Mzz;
		double A1 = Mzz*Mz + 4*Cov_xy*Mz - Mxz2 - Myz2 - Mz*Mz*Mz;
		double A0 = Mxz2*Myy + Myz2*Mxx - Mzz*Cov_xy - 2*Mxz*Myz*Mxy + Mz*Mz*Cov_xy;
		double A22 = A2 + A2;
		double epsilon = 1e-12; 
		double ynew = 1e+20;
		int IterMax= 20;
		double xnew = 0;
		int iterations = 0;
		
		// Newton's method starting at x=0
		for (int iter=1; iter<=IterMax; iter++) {
			iterations = iter;
			double yold = ynew;
			ynew = A0 + xnew*(A1 + xnew*(A2 + 4.*xnew*xnew));
			if (Math.abs(ynew)>Math.abs(yold)) {
				if (IJ.debugMode) IJ.log("Fit Circle: wrong direction: |ynew| > |yold|");
				xnew = 0;
				break;
			}
			double Dy = A1 + xnew*(A22 + 16*xnew*xnew);
			double xold = xnew;
			xnew = xold - ynew/Dy;
			if (Math.abs((xnew-xold)/xnew) < epsilon)
				break;
			if (iter >= IterMax) {
				if (IJ.debugMode) IJ.log("Fit Circle: will not converge");
				xnew = 0;
			}
			if (xnew<0) {
				if (IJ.debugMode) IJ.log("Fit Circle: negative root:  x = "+xnew);
				xnew = 0;
			}
		}
		if (IJ.debugMode) IJ.log("Fit Circle: n="+n+", xnew="+IJ.d2s(xnew,2)+", iterations="+iterations);
		
		// calculate the circle parameters
		double DET = xnew*xnew - xnew*Mz + Cov_xy;
		double CenterX = (Mxz*(Myy-xnew)-Myz*Mxy)/(2*DET);
		double CenterY = (Myz*(Mxx-xnew)-Mxz*Mxy)/(2*DET);
		double radius = Math.sqrt(CenterX*CenterX + CenterY*CenterY + Mz + 2*xnew);
		if (Double.isNaN(radius)) {
			IJ.error("Fit Circle", "Points are collinear.");
			return;
		}
		CenterX = CenterX + meanx;
		CenterY = CenterY + meany;
		Undo.setup(Undo.ROI, imp);
		imp.deleteRoi();
		IJ.makeOval((int)Math.round(CenterX-radius), (int)Math.round(CenterY-radius), (int)Math.round(2*radius), (int)Math.round(2*radius));
	}

	void fitSpline() {
		Roi roi = imp.getRoi();
		if (roi==null)
			{noRoi("Spline"); return;}
		int type = roi.getType();
		boolean segmentedSelection = type==Roi.POLYGON||type==Roi.POLYLINE;
		if (!(segmentedSelection||type==Roi.FREEROI||type==Roi.TRACED_ROI||type==Roi.FREELINE))
			{IJ.error("Spline Fit", "Polygon or polyline selection required"); return;}
		if (roi instanceof EllipseRoi)
			return;
		PolygonRoi p = (PolygonRoi)roi;
		Undo.setup(Undo.ROI, imp);
		if (!segmentedSelection && p.getNCoordinates()>3) {
			if (p.subPixelResolution())
				p = trimFloatPolygon(p, p.getUncalibratedLength());
			else
				p = trimPolygon(p, p.getUncalibratedLength());
		}
		String options = Macro.getOptions();
		if (options!=null && options.indexOf("straighten")!=-1)
			p.fitSplineForStraightening();
		else if (options!=null && options.indexOf("remove")!=-1)
			p.removeSplineFit();
		else
			p.fitSpline();
		imp.draw();
		LineWidthAdjuster.update(); 
	}
	
	void interpolate() {
		Roi roi = imp.getRoi();
		if (roi==null)
			{noRoi("Interpolate"); return;}
		if (roi.getType()==Roi.POINT)
			return;
		if (IJ.isMacro()&&Macro.getOptions()==null)
			Macro.setOptions("interval=1");
		GenericDialog gd = new GenericDialog("Interpolate");
		gd.addNumericField("Interval:", 1.0, 1, 4, "pixel");
		gd.addCheckbox("Smooth", IJ.isMacro()?false:smooth);
		gd.addCheckbox("Adjust interval to match", IJ.isMacro()?false:adjust);
		gd.showDialog();
		if (gd.wasCanceled())
			return;
		double interval = gd.getNextNumber();
		smooth = gd.getNextBoolean();
		Undo.setup(Undo.ROI, imp);
		adjust = gd.getNextBoolean();
		int sign = adjust ? -1 : 1;
		Roi newRoi = null;
		if (roi instanceof ShapeRoi && ((ShapeRoi)roi).getRois().length>1) {
			// handle composite roi, thanks to Michael Ellis
			Roi[] rois = ((ShapeRoi) roi).getRois();
			ShapeRoi newShapeRoi = null;
			for (Roi roi2 : rois) {
				FloatPolygon fPoly = roi2.getInterpolatedPolygon(interval,smooth);
				PolygonRoi polygon = new PolygonRoi(fPoly,PolygonRoi.POLYGON);
				if (newShapeRoi==null) // First Roi is the outer boundary
					newShapeRoi = new ShapeRoi(polygon);
				else {
					// Assume subsequent Rois are holes to be subtracted
					ShapeRoi tempRoi = new ShapeRoi(polygon);
					tempRoi.not(newShapeRoi);
					newShapeRoi = tempRoi;
				}
			}
			newRoi = newShapeRoi;
		} else {
			FloatPolygon poly = roi.getInterpolatedPolygon(sign*interval, smooth);
			int t = roi.getType();
			int type = roi.isLine()?Roi.FREELINE:Roi.FREEROI;
			if (t==Roi.POLYGON && interval>1.0)
				type = Roi.POLYGON;
			if ((t==Roi.RECTANGLE||t==Roi.OVAL||t==Roi.FREEROI) && interval>=8.0)
				type = Roi.POLYGON;
			if ((t==Roi.LINE||t==Roi.FREELINE) && interval>=8.0)
				type = Roi.POLYLINE;
			if (t==Roi.POLYLINE && interval>=8.0)
				type = Roi.POLYLINE;
			ImageCanvas ic = imp.getCanvas();
			if (poly.npoints<=150 && ic!=null && ic.getMagnification()>=12.0)
				type = roi.isLine()?Roi.POLYLINE:Roi.POLYGON;
			newRoi = new PolygonRoi(poly,type);
		}
		if (roi.getStroke()!=null)
			newRoi.setStrokeWidth(roi.getStrokeWidth());
		newRoi.setStrokeColor(roi.getStrokeColor());
		newRoi.setName(roi.getName());
		transferProperties(roi, newRoi);
		imp.setRoi(newRoi);
	}
	
	private static void transferProperties(Roi roi1, Roi roi2) {
		if (roi1==null || roi2==null)
			return;
		roi2.setStrokeColor(roi1.getStrokeColor());
		if (roi1.getStroke()!=null)
			roi2.setStroke(roi1.getStroke());
		roi2.setDrawOffset(roi1.getDrawOffset());
	}
	
	PolygonRoi trimPolygon(PolygonRoi roi, double length) {
		int[] x = roi.getXCoordinates();
		int[] y = roi.getYCoordinates();
		int n = roi.getNCoordinates();
		x = smooth(x, n);
		y = smooth(y, n);
		float[] curvature = getCurvature(x, y, n);
		Rectangle r = roi.getBounds();
		double threshold = rodbard(length);
		//IJ.log("trim: "+length+" "+threshold);
		double distance = Math.sqrt((x[1]-x[0])*(x[1]-x[0])+(y[1]-y[0])*(y[1]-y[0]));
		x[0] += r.x; y[0]+=r.y;
		int i2 = 1;
		int x1,y1,x2=0,y2=0;
		for (int i=1; i<n-1; i++) {
			x1=x[i]; y1=y[i]; x2=x[i+1]; y2=y[i+1];
			distance += Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)) + 1;
			distance += curvature[i]*2;
			if (distance>=threshold) {
				x[i2] = x2 + r.x;
				y[i2] = y2 + r.y;
				i2++;
				distance = 0.0;
			}
		}
		int type = roi.getType()==Roi.FREELINE?Roi.POLYLINE:Roi.POLYGON;
		if (type==Roi.POLYLINE && distance>0.0) {
			x[i2] = x2 + r.x;
			y[i2] = y2 + r.y;
			i2++;
		}		
		PolygonRoi p = new PolygonRoi(x, y, i2, type);
		if (roi.getStroke()!=null)
			p.setStrokeWidth(roi.getStrokeWidth());
		p.setStrokeColor(roi.getStrokeColor());
		p.setName(roi.getName());
		imp.setRoi(p);
		return p;
	}
	
	double rodbard(double x) {
		// y = c*((a-x/(x-d))^(1/b)
		// a=3.9, b=.88, c=712, d=44
		double ex;
		if (x == 0.0)
			ex = 5.0;
		else
			ex = Math.exp(Math.log(x/700.0)*0.88);
		double y = 3.9-44.0;
		y = y/(1.0+ex);
		return y+44.0;
	}

	/**
		Smooth the specified line, preserving the end points.<br>
		Author: Eugene Katrukha
	*/
	int[] smooth(int[] a, int n) {
		float [] out = new float[n];
		int i,j;
		//no point in averaging 2 points
		if(n<3)
			return a;
		//preserve end points
		out[0]=	a[0];
		out[n-1] = a[n-1];
		//average middle points
		for (i=1; i<(n-1); i++) {
			out[i]=0.0f;			
			for(j=(i-1);j<(i+2);j++){
				out[i] += a[j];
			}
			out[i] /= 3.0f;
		}
		for (i=0; i<n; i++)
			a[i] = (int)Math.round(out[i]);
		return a;
	}

	/**
		Smooth the specified line, preserving the end points.<br>
		Author: Eugene Katrukha
	*/
	float[] smooth(float[] a, int n) {
		float [] out = new float[n];
		int i,j;
		//no point in averaging 2 points
		if(n<3)
			return a;
		//preserve end points
		out[0]=	a[0];
		out[n-1] = a[n-1];
		for (i=1; i<(n-1); i++) {
			out[i]=0.0f;
			for(j=(i-1);j<(i+2);j++)
				out[i] += a[j];
			out[i] /= 3.0f;
		}
		return out;
	}
		
	float[] getCurvature(int[] x, int[] y, int n) {
		float[] x2 = new float[n];
		float[] y2 = new float[n];
		for (int i=0; i<n; i++) {
			x2[i] = x[i];
			y2[i] = y[i];
		}
		ImageProcessor ipx = new FloatProcessor(n, 1, x2, null);
		ImageProcessor ipy = new FloatProcessor(n, 1, y2, null);
		ipx.convolve(kernel, kernel.length, 1);
		ipy.convolve(kernel, kernel.length, 1);
		float[] indexes = new float[n];
		float[] curvature = new float[n];
		for (int i=0; i<n; i++) {
			indexes[i] = i;
			curvature[i] = (float)Math.sqrt((x2[i]-x[i])*(x2[i]-x[i])+(y2[i]-y[i])*(y2[i]-y[i]));
		}
		return curvature;
	}
	
	PolygonRoi trimFloatPolygon(PolygonRoi roi, double length) {
		FloatPolygon poly = roi.getFloatPolygon();
		float[] x = poly.xpoints;
		float[] y = poly.ypoints;
		int n = poly.npoints;
		x = smooth(x, n);
		y = smooth(y, n);
		float[] curvature = getCurvature(x, y, n);
		double threshold = rodbard(length);
		//IJ.log("trim: "+length+" "+threshold);
		double distance = Math.sqrt((x[1]-x[0])*(x[1]-x[0])+(y[1]-y[0])*(y[1]-y[0]));
		int i2 = 1;
		double x1,y1,x2=0,y2=0;
		for (int i=1; i<n-1; i++) {
			x1=x[i]; y1=y[i]; x2=x[i+1]; y2=y[i+1];
			distance += Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)) + 1;
			distance += curvature[i]*2;
			if (distance>=threshold) {
				x[i2] = (float)x2;
				y[i2] = (float)y2;
				i2++;
				distance = 0.0;
			}
		}
		int type = roi.getType()==Roi.FREELINE?Roi.POLYLINE:Roi.POLYGON;
		if (type==Roi.POLYLINE && distance>0.0) {
			x[i2] = (float)x2;
			y[i2] = (float)y2;
			i2++;
		}		
		PolygonRoi p = new PolygonRoi(x, y, i2, type);
		if (roi.getStroke()!=null)
			p.setStrokeWidth(roi.getStrokeWidth());
		p.setStrokeColor(roi.getStrokeColor());
		p.setDrawOffset(roi.getDrawOffset());
		p.setName(roi.getName());
		imp.setRoi(p);
		return p;
	}
	
	float[] getCurvature(float[] x, float[] y, int n) {
		float[] x2 = new float[n];
		float[] y2 = new float[n];
		for (int i=0; i<n; i++) {
			x2[i] = x[i];
			y2[i] = y[i];
		}
		ImageProcessor ipx = new FloatProcessor(n, 1, x, null);
		ImageProcessor ipy = new FloatProcessor(n, 1, y, null);
		ipx.convolve(kernel, kernel.length, 1);
		ipy.convolve(kernel, kernel.length, 1);
		float[] indexes = new float[n];
		float[] curvature = new float[n];
		for (int i=0; i<n; i++) {
			indexes[i] = i;
			curvature[i] = (float)Math.sqrt((x2[i]-x[i])*(x2[i]-x[i])+(y2[i]-y[i])*(y2[i]-y[i]));
		}
		return curvature;
	}

	void createEllipse(ImagePlus imp) {
		if (!imp.okToDeleteRoi()) return;
		IJ.showStatus("Fitting ellipse");
		Roi roi = imp.getRoi();
		if (roi==null)
			{noRoi("Fit Ellipse"); return;}
		if (roi.isLine())
			{IJ.error("Fit Ellipse", "\"Fit Ellipse\" does not work with line selections"); return;}
		ImageProcessor ip = imp.getProcessor();
		ip.setRoi(roi);
		int options = Measurements.CENTROID+Measurements.ELLIPSE;
		ImageStatistics stats = ImageStatistics.getStatistics(ip, options, null);
		double dx = stats.major*Math.cos(stats.angle/180.0*Math.PI)/2.0;
		double dy = - stats.major*Math.sin(stats.angle/180.0*Math.PI)/2.0;
		double x1 = stats.xCentroid - dx;
		double x2 = stats.xCentroid + dx;
		double y1 = stats.yCentroid - dy;
		double y2 = stats.yCentroid + dy;
		double aspectRatio = stats.minor/stats.major;
		Undo.setup(Undo.ROI, imp);
		imp.deleteRoi();
		Roi roi2 = new EllipseRoi(x1,y1,x2,y2,aspectRatio);
		transferProperties(roi, roi2);
		imp.setRoi(roi2);
	}

	void convexHull(ImagePlus imp) {
		if (!imp.okToDeleteRoi()) return;
		long startTime = System.currentTimeMillis();
		Roi roi = imp.getRoi();
		if (roi == null) { IJ.error("Convex Hull", "Selection required"); return; }
		if (roi instanceof Line) { IJ.error("Convex Hull", "Area selection, point selection, or segmented or free line required"); return; }
		FloatPolygon p = roi.getFloatConvexHull();
		if (p!=null) {
			Undo.setup(Undo.ROI, imp);
			imp.deleteRoi();
			Roi roi2 = new PolygonRoi(p, Roi.POLYGON);
			transferProperties(roi, roi2);
			imp.setRoi(roi2);
			IJ.showTime(imp, startTime, "Convex Hull ", 1);
		}
	}
	
	// Finds the index of the upper right point that is guaranteed to be on convex hull
	/*int findFirstPoint(int[] xCoordinates, int[] yCoordinates, int n, ImagePlus imp) {
		int smallestY = imp.getHeight();
		int x, y;
		for (int i=0; i<n; i++) {
			y = yCoordinates[i];
			if (y<smallestY)
			smallestY = y;
		}
		int smallestX = imp.getWidth();
		int p1 = 0;
		for (int i=0; i<n; i++) {
			x = xCoordinates[i];
			y = yCoordinates[i];
			if (y==smallestY && x<smallestX) {
				smallestX = x;
				p1 = i;
			}
		}
		return p1;
	}*/
	
	void createMask(ImagePlus imp) {
		Roi roi = imp.getRoi();
		boolean useInvertingLut = Prefs.useInvertingLut;
		Prefs.useInvertingLut = false;
		boolean selectAll = roi!=null && roi.getType()==Roi.RECTANGLE && roi.getBounds().width==imp.getWidth()
			&& roi.getBounds().height==imp.getHeight() && imp.isThreshold();
		boolean overlay = imp.getOverlay()!=null && !imp.isThreshold();
		if (!overlay && (roi==null || selectAll)) {
			createMaskFromThreshold(imp);
			Prefs.useInvertingLut = useInvertingLut;
			return;
		}
		if (roi==null && imp.getOverlay()==null) {
			IJ.error("Create Mask", "Selection, overlay or threshold required");
			return;
		}
		ByteProcessor mask = imp.createRoiMask();
		if (!Prefs.blackBackground)
			mask.invertLut();
		mask.setThreshold(255, 255, ImageProcessor.NO_LUT_UPDATE);
		ImagePlus maskImp = null;
		Frame frame = WindowManager.getFrame("Mask");
		if (frame!=null && (frame instanceof ImageWindow))
			maskImp = ((ImageWindow)frame).getImagePlus();
		if (maskImp!=null && maskImp.getBitDepth()==8) {
			ImageProcessor ip = maskImp.getProcessor();
			ip.copyBits(mask, 0, 0, Blitter.OR);
			maskImp.setProcessor(ip);
		} else {
			maskImp = new ImagePlus("Mask", mask);
			maskImp.show();
		}
		Calibration cal = imp.getCalibration();
		if (cal.scaled()) {
			Calibration cal2 = maskImp.getCalibration();
			cal2.pixelWidth = cal.pixelWidth;
			cal2.pixelHeight = cal.pixelHeight;
			cal2.setUnit(cal.getUnit());
		}
		maskImp.updateAndRepaintWindow();
		Prefs.useInvertingLut = useInvertingLut;
		Recorder.recordCall("mask = imp.createRoiMask();");
	}
	
	void createMaskFromThreshold(ImagePlus imp) {
		ImageProcessor ip = imp.getProcessor();
		if (!ip.isThreshold()) {
			IJ.error("Create Mask", "Area selection, overlay or thresholded image required");
			return;
		}
		ByteProcessor mask = imp.createThresholdMask();
		if (!Prefs.blackBackground)
			mask.invertLut();
		mask.setThreshold(255, 255, ImageProcessor.NO_LUT_UPDATE);
		ImagePlus maskImp = new ImagePlus("mask",mask);
		Calibration cal = imp.getCalibration();
		if (cal.scaled()) {
			Calibration cal2 = maskImp.getCalibration();
			cal2.pixelWidth = cal.pixelWidth;
			cal2.pixelHeight = cal.pixelHeight;
			cal2.setUnit(cal.getUnit());
		}
		maskImp.show();
		Recorder.recordCall("mask = imp.createThresholdMask();");
	}

	void createSelectionFromMask(ImagePlus imp) {
		ImageProcessor ip = imp.getProcessor();
		if (ip.isThreshold()) {
			IJ.runPlugIn("ij.plugin.filter.ThresholdToSelection", "");
			return;
		}
		if (!ip.isBinary()) {
			IJ.error("Create Selection",
				"This command creates a composite selection from\n"+
				"a mask (8-bit binary image) or from an image\n"+
				"thresholded using Image>Adjust>Threshold\n"+
				"The current image is not a mask and has not\n"+
				"been thresholded.");
			return;
		}
		int threshold = ip.isInvertedLut()?255:0;
		if (Prefs.blackBackground)
			threshold = (threshold==255)?0:255;
		ip.setThreshold(threshold, threshold, ImageProcessor.NO_LUT_UPDATE);
		if (!IJ.isMacro())
			IJ.log("Create Selection: threshold not set; assumed to be "+threshold+"-"+threshold);
		IJ.runPlugIn("ij.plugin.filter.ThresholdToSelection", "");
	}
	
	void invert(ImagePlus imp) {
		Roi roi = imp.getRoi();
		if (roi == null)
			{run("all"); return;}
		if (!roi.isArea())
			{IJ.error("Inverse", "Area selection required"); return;}
		Roi inverse = roi.getInverse(imp);
		Undo.setup(Undo.ROI, imp);
		imp.setRoi(inverse);
	}
	
	private void lineToArea(ImagePlus imp) {
		Roi roi = imp.getRoi();
		if (roi==null || !roi.isLine())
			{IJ.error("Line to Area", "Line selection required"); return;}
		Undo.setup(Undo.ROI, imp);
		Roi roi2 = lineToArea(roi);
		imp.setRoi(roi2);
		Roi.setPreviousRoi(roi);
	}
	
	/** Converts a line selection into an area selection. */
	public static Roi lineToArea(Roi roi) {
		return Roi.convertLineToArea(roi);
	}
	
	void areaToLine(ImagePlus imp) {
		Roi roi = imp.getRoi();
		if (roi==null || !roi.isArea()) {
			IJ.error("Area to Line", "Area selection required");
			return;
		}
		Undo.setup(Undo.ROI, imp);
		Polygon p = roi.getPolygon();
		FloatPolygon fp = (roi.subPixelResolution()) ? roi.getFloatPolygon() : null;
		if (p==null && fp==null)
			return;
		int type1 = roi.getType();
		if (type1==Roi.COMPOSITE) {
			IJ.error("Area to Line", "Composite selections cannot be converted to lines.");
			return;
		}
		if (fp==null && type1==Roi.TRACED_ROI) {
			for (int i=0; i<p.npoints; i++) {
				if (p.xpoints[i]>=imp.getWidth()) p.xpoints[i]=imp.getWidth()-1;
				if (p.ypoints[i]>=imp.getHeight()) p.ypoints[i]=imp.getHeight()-1;
			}
		}
		int type2 = Roi.POLYLINE;
		if (type1==Roi.OVAL||type1==Roi.FREEROI||type1==Roi.TRACED_ROI
		||((roi instanceof PolygonRoi)&&((PolygonRoi)roi).isSplineFit()))
			type2 = Roi.FREELINE;
		Roi roi2 = fp==null ? new PolygonRoi(p, type2) : new PolygonRoi(fp, type2);
		transferProperties(roi, roi2);
		Rectangle2D.Double bounds = roi.getFloatBounds();
		roi2.setLocation(bounds.x - 0.5, bounds.y -0.5);	//area and line roi coordinates are 0.5 pxl different
		imp.setRoi(roi2);
	}

	void toBoundingBox(ImagePlus imp) {
		Roi roi = imp.getRoi();
		if (roi==null) {
			noRoi("To Bounding Box");
			return;
		}
		if (!imp.okToDeleteRoi())
			return;
		Undo.setup(Undo.ROI, imp);
		Rectangle r = roi.getBounds();
		imp.deleteRoi();
		Roi roi2 = new Roi(r.x, r.y, r.width, r.height);
		transferProperties(roi, roi2);
		imp.setRoi(roi2);
	}

	void addToRoiManager(ImagePlus imp) {
		if (IJ.macroRunning() &&  Interpreter.isBatchModeRoiManager())
			IJ.error("run(\"Add to Manager\") may not work in batch mode macros");
		Frame frame = WindowManager.getFrame("ROI Manager");
		if (frame==null)
			IJ.run("ROI Manager...");
		if (imp==null) return;
		Roi roi = imp.getRoi();
		if (roi==null) return;
		frame = WindowManager.getFrame("ROI Manager");
		if (frame==null || !(frame instanceof RoiManager))
			IJ.error("ROI Manager not found");
		RoiManager rm = (RoiManager)frame;
		boolean altDown= IJ.altKeyDown();
		IJ.setKeyUp(IJ.ALL_KEYS);
		if (altDown && !IJ.macroRunning())
			IJ.setKeyDown(KeyEvent.VK_SHIFT);
		if (roi.getState()==Roi.CONSTRUCTING) { //wait (up to 2 sec.) until ROI finished
			long start = System.currentTimeMillis();
			while (true) {
				IJ.wait(10);
				if (roi.getState()!=Roi.CONSTRUCTING)
					break;
				if ((System.currentTimeMillis()-start)>2000) {
					IJ.beep();
					IJ.error("Add to Manager", "Selection is not complete");
					return;
				}
			}
		}
		if (Recorder.record && imp.getStackSize()>1) {
			if (imp.isHyperStack()) {
				int C = imp.getChannel();
				int Z = imp.getSlice();
				int T = imp.getFrame();
				if (Recorder.scriptMode()) {
					Recorder.recordCall("roi = imp.getRoi();");
					Recorder.recordCall("roi.setPosition("+C+", "+Z+", "+T+");");
				} else
					Recorder.record("Roi.setPosition", C, Z, T);				
			} else {
				int position = imp.getCurrentSlice();
				if (Recorder.scriptMode()) {
					Recorder.recordCall("roi = imp.getRoi();");
					Recorder.recordCall("roi.setPosition("+position+");");
				} else
					Recorder.record("Roi.setPosition", position);
			}
		}
		rm.allowRecording(true);
		rm.runCommand("add");
		rm.allowRecording(false);
		IJ.setKeyUp(IJ.ALL_KEYS);
	}
	
	boolean setProperties(String title, Roi roi) {
		if ((roi instanceof PointRoi) && Toolbar.getMultiPointMode() && IJ.altKeyDown()) {
			((PointRoi)roi).displayCounts();
			return true;
		}
		Frame f = WindowManager.getFrontWindow();
		if (f!=null && f.getTitle().indexOf("3D Viewer")!=-1)
			return false;
		if (roi==null) {
			IJ.error("This command requires a selection.");
			return false;
		}
		String name = roi.getName();
		if (name==null) name = "";
		int position = roi.getPosition();
		int group = roi.getGroup();
		Color color = roi.getStrokeColor();
		Color fillColor = roi.getFillColor();
		int width = (int)roi.getStrokeWidth();
		RoiProperties rp = new RoiProperties(title, roi);
		boolean ok = rp.showDialog();
		if (Recorder.record) {
			boolean groupChanged = false;
			String name2 = roi.getName();
			if (name2==null) name2 = "";
			int position2 = roi.getPosition();
			int group2 = roi.getGroup();
			if (group2!=group) groupChanged=true;
			Color color2 = roi.getStrokeColor();
			Color fillColor2 = roi.getFillColor();
			int width2 = (int)roi.getStrokeWidth();
			if (Recorder.scriptMode()) {
				Recorder.recordCall("roi = imp.getRoi();");
				if (name2!=name)
					Recorder.recordCall("roi.setName(\""+name2+"\");");
				if (position2!=position)
					Recorder.recordCall("roi.setPosition("+position2+");");
				if (group2!=group)
					Recorder.recordCall("roi.setGroup("+group2+");");
				if (width2!=width)
					Recorder.recordCall("roi.setStrokeWidth("+width2+");");
				if (color2!=color && !groupChanged)
					Recorder.recordCall("roi.setStrokeColor("+getColor(color2)+");");
				if (fillColor2!=fillColor)
					Recorder.recordCall("roi.setFillColor("+getColor(fillColor2)+");");
				Recorder.recordCall("imp.draw();");
			} else {
				if (name2!=name)
					Recorder.record("Roi.setName", name2);
				if (groupChanged)
					Recorder.record("Roi.setGroup", group2);
				if (position2!=position)
					Recorder.record("Roi.setPosition", position2);
				if (color2!=color && !groupChanged)
					Recorder.record("Roi.setStrokeColor", Colors.colorToString(color2));
				if (fillColor2!=fillColor)
					Recorder.record("Roi.setFillColor", Colors.colorToString(fillColor2));
				if (width2!=width)
					Recorder.record("Roi.setStrokeWidth", width2);
			}
			Recorder.disableCommandRecording();
		}
		if (IJ.debugMode)
			IJ.log(roi.getDebugInfo());
		return ok;
	}
	
	private String getColor(Color color) {
		return "new Color("+color.getRed()+","+color.getGreen()+","+color.getBlue()+")";
	}
	
	private void makeBand(ImagePlus imp) {
		Roi roi = imp.getRoi();
		if (roi==null) {
			noRoi("Make Band");
			return;
		}
		Roi roiOrig = roi;
		if (!roi.isArea()) {
			IJ.error("Make Band", "Area selection required");
			return;
		}
		Calibration cal = imp.getCalibration();
		double pixels = bandSize;
		double size = pixels*cal.pixelWidth;
		int decimalPlaces = 0;
		if ((int)size!=size)
			decimalPlaces = 2;
		GenericDialog gd = new GenericDialog("Make Band");
		gd.addNumericField("Band Size:", size, decimalPlaces, 4, cal.getUnits());
		gd.showDialog();
		if (gd.wasCanceled())
			return;
		size = gd.getNextNumber();
		if (Double.isNaN(size)) {
			IJ.error("Make Band", "invalid number");
			return;
		}
		int n = (int)Math.round(size/cal.pixelWidth); 
		if (n >255) {
			IJ.error("Make Band", "Cannot make bands wider that 255 pixels");
			return;
		}
		int width = imp.getWidth();
		int height = imp.getHeight();
		Rectangle r = roi.getBounds();
		ImageProcessor ip = roi.getMask();
		if (ip==null) {
			ip = new ByteProcessor(r.width, r.height);
			ip.invert();
		}
		ImageProcessor mask = new ByteProcessor(width, height);
		mask.insert(ip, r.x, r.y);
		ImagePlus edm = new ImagePlus("mask", mask);
		boolean saveBlackBackground = Prefs.blackBackground;
		Prefs.blackBackground = false;
		int saveType = EDM.getOutputType();
		EDM.setOutputType(EDM.BYTE_OVERWRITE);
		IJ.run(edm, "Distance Map", "");
		EDM.setOutputType(saveType);
		Prefs.blackBackground = saveBlackBackground;
		ip = edm.getProcessor();
		ip.setThreshold(0, n, ImageProcessor.NO_LUT_UPDATE);
		int xx=-1, yy=-1;	//will become the start position for the Wand
		for (int y=Math.max(r.y,0); y<Math.min(r.y+r.height, height); y++) {
			for (int x=Math.max(r.x,0); x<Math.min(r.x+r.width, width); x++) {
				if (ip.getPixel(x, y)<n) {
					xx=x; yy=y;
					break;
				}
			}
			if (xx>=0||yy>=0)
			break;
		}
		int count = IJ.doWand(edm, xx, yy, 0, null);
		if (count<=0) {
			IJ.error("Make Band", "Unable to make band");
			return;
		}
		ShapeRoi roi2 = new ShapeRoi(edm.getRoi());
		if (!(roi instanceof ShapeRoi))
			roi = new ShapeRoi(roi);
		ShapeRoi roi1 = (ShapeRoi)roi;
		roi2 = roi2.not(roi1);
		Undo.setup(Undo.ROI, imp);
		transferProperties(roiOrig, roi2);
		imp.setRoi(roi2);
		bandSize = n;
	}
	
	/*  Fits a minimum area rectangle into a ROI, by searching for the minimum area bounding rectangles
	 *  among the ones having a side that is colinear with an edge of the convex hull.
	 *	
	 * 	Loosely based on:
	 * 	H. Freeman and R. Shapira. 1975. Determining the minimum-area encasing rectangle for an arbitrary 
	 * 	closed curve. Commun. ACM 18, 7 (July 1975), 409–413. DOI:https://doi.org/10.1145/360881.360919	
	*/
	private void fitRectangle(ImagePlus imp) {
		if (!imp.okToDeleteRoi()) return;
		long startTime = System.currentTimeMillis();
		Roi roi = imp.getRoi();
		if (roi == null)
			{noRoi("Fit Rectangle"); return;}
		if (roi instanceof Line || roi.isDrawingTool()) 
			{IJ.error("Fit Rectangle", "Area selection, point selection, or segmented or free line required"); return;}
		if (!roi.isArea()) {
			// check number of points and colinearity before proceeding
			FloatPolygon poly = roi.getFloatPolygon();
			int n = poly.npoints;
			if (n < 3)
				{IJ.error("Fit Rectangle", "At least three points are required"); return;}
			float[] x = poly.xpoints;
			float[] y = poly.ypoints;
			boolean colinear = true;
			for(int i=2; i<n; i++) {
				float prod = (x[i] - x[0]) * (y[i] - y[0]) - (x[i] - x[1]) * (y[i] - y[1]);
				if (prod != 0)	colinear = false;	
			}
			if (colinear)
				{IJ.error("Fit Rectangle", "Points are colinear"); return;}
		}
		FloatPolygon p = roi.getFloatConvexHull();
		if (p!=null) {		
			int np = p.npoints;
			float[] xp = p.xpoints;
			float[] yp = p.ypoints;
			Rectangle r = roi.getBounds();
			double minArea = 2 * r.width * r.height; // generous overestimation
			double minFD = 0;
			int imin = -1;
			int i2min = -1;
			int jmin = -1;
			double min_hmin = 0;
			double min_hmax = 0;
			for (int i = 0; i < np; i++) {
				double maxLD = 0;
				int imax = -1;
				int i2max = -1;
				int jmax = -1;
				int i2 = i + 1;
				if(i == np-1) i2 = 0;
				for (int j = 0; j < np; j++) {
					// distance based on vector cross product
					double d = Math.abs( ((xp[i2] - xp[i]) * (yp[j] - yp[i]) - (xp[j] - xp[i]) * (yp[i2] - yp[i])) / Math.sqrt(Math.pow(xp[i2] - xp[i], 2) + Math.pow(yp[i2] - yp [i], 2)) );
					if (maxLD < d) {
						maxLD = d;
						imax = i;
						jmax = j;
						i2max = i2;
					}
				}
				double hmin = 0;
				double hmax = 0;
				for (int k = 0; k < np; k++) { // rotating calipers
					// projected distance based on vector dot product, includes sign
					double hd = ((xp[i2max] - xp[imax]) * (xp[k] -  xp[imax]) + (yp[k] - yp[imax]) * (yp[i2max] - yp[imax])) / Math.sqrt(Math.pow(xp[i2max] - xp[imax], 2) + Math.pow(yp[i2max] - yp [imax], 2));
					hmin = Math.min(hmin, hd);
					hmax = Math.max(hmax, hd);
				}
				double area = maxLD * (hmax - hmin);
				if (minArea > area) {
					minArea = area;
					minFD = maxLD;
					min_hmin = hmin;
					min_hmax = hmax;

					imin = imax;
					i2min = i2max;
					jmin = jmax;
				}
			}
			double pd = ((xp[i2min] - xp[imin]) * (yp[jmin] - yp[imin]) - (xp[jmin] - xp[imin]) * (yp[i2min] - yp[imin])) / Math.sqrt(Math.pow(xp[i2min] - xp[imin], 2) + Math.pow(yp[i2min] - yp [imin], 2)); // signed feret diameter
			double pairAngle = Math.atan2( yp[i2min]- yp[imin], xp[i2min]- xp[imin]);
			double minAngle = pairAngle + Math.PI/2;

			// rectangle center and signed full height
			double xm = xp[imin] + Math.cos(pairAngle) * (min_hmax + min_hmin)/2 + Math.cos(minAngle) * pd/2;
			double ym = yp[imin] + Math.sin(pairAngle) * (min_hmax + min_hmin)/2 + Math.sin(minAngle) * pd/2;
			double hm = min_hmax - min_hmin;
			
			if (minFD > Math.abs(hm)) { // ensure control axis is parallel to longer side
				pairAngle = pairAngle - Math.PI/2;
				minFD = Math.abs(hm);
				hm = pd;
				}
			
			if (pairAngle * hm > 0)	hm = -hm; // ensure first control point at the top
	
			double x1 = xm + Math.cos(pairAngle) * hm/2;
			double y1 = ym + Math.sin(pairAngle) * hm/2;
			double x2 = xm - Math.cos(pairAngle) * hm/2;
			double y2 = ym - Math.sin(pairAngle) * hm/2;
			Undo.setup(Undo.ROI, imp);
			imp.deleteRoi();
			Roi roi2 = new RotatedRectRoi(x1,  y1,  x2,  y2,  minFD);
			transferProperties(roi, roi2);
			imp.setRoi(roi2);
			IJ.showTime(imp, startTime, "Fit Rectangle ", 1);
		}
	}
	
	private void translate(ImagePlus imp) {
		if (!imp.okToDeleteRoi())
			return;
		Roi roi = imp.getRoi();
		String options = Macro.getOptions();
		if (options!=null && options.contains("interpolation=")) {
			IJ.run("Translate...", options); // run Image>Transform>Translate
			return;
		}
		if (roi==null) {
			noRoi("Translate");
			return;
		}
		double dx = translateX;
		double dy = translateY;
		GenericDialog gd = new GenericDialog("Translate");
		gd.addNumericField("X offset (pixels): ", dx, 0);
		gd.addNumericField("Y offset (pixels): ", dy, 0);
		gd.showDialog();
		if (gd.wasCanceled())
			return;
		dx = gd.getNextNumber();
		dy = gd.getNextNumber();
		Rectangle2D r = roi.getFloatBounds();
		roi.setLocation(r.getX()+dx, r.getY()+dy);
		if (imp!=null)
			imp.draw();
		if (options==null) {
			translateX = dx;
			translateY = dy;
		}
	}
	
	void noRoi(String command) {
		IJ.error(command, "This command requires a selection");
	}

}