Skip to content
Snippets Groups Projects
Commit e2a05947 authored by Walt Mankowski's avatar Walt Mankowski
Browse files

multiple changes:

* position new ellipse by lining up a random point to a random point in
  the existing region (copied from matlab code)
* zoom in to a rough ROI (copied from xlim/ylim code in matlab)
parent db541f79
Branches
No related tags found
No related merge requests found
...@@ -24,17 +24,12 @@ public class Demo_Random_PR implements PlugInFilter { ...@@ -24,17 +24,12 @@ public class Demo_Random_PR implements PlugInFilter {
public static final int IMSIZE = 1000; public static final int IMSIZE = 1000;
public static final int CX = 500; public static final int CX = 500;
public static final int CY = 500; public static final int CY = 500;
int CX2;
int CY2;
public static final double RADII_0 = 10; public static final double RADII_0 = 10;
public static final double RADII_1 = 20; public static final double RADII_1 = 20;
public static final double JITTER = 40;
public Demo_Random_PR() { public Demo_Random_PR() {
rnd = new Random(); rnd = new Random();
allPts = new ArrayList<Point2f>(); allPts = new ArrayList<Point2f>();
CX2 = 100;
CY2 = 100;
} }
public int setup(String arg, ImagePlus imp) { public int setup(String arg, ImagePlus imp) {
...@@ -64,67 +59,28 @@ public class Demo_Random_PR implements PlugInFilter { ...@@ -64,67 +59,28 @@ public class Demo_Random_PR implements PlugInFilter {
ByteProcessor bw = new ByteProcessor(IMSIZE, IMSIZE); ByteProcessor bw = new ByteProcessor(IMSIZE, IMSIZE);
AddRandomEllipses(bw); AddRandomEllipses(bw);
ImagePlus bwIP = new ImagePlus("Random Ellipses", bw); ByteProcessor bwZoomed = ZoomIn(bw);
ImagePlus bwIP = new ImagePlus("Random Ellipses", bwZoomed);
bwIP.show(); bwIP.show();
} }
private void AddRandomEllipses(ImageProcessor bw) { private void AddRandomEllipses(ImageProcessor bw) {
bw.setColor(Color.white); bw.setColor(Color.white);
IJ.log(String.format("ellipse %d", 1));
allPts = RandomEllipsePts(); allPts = RandomEllipsePts();
showPts(1);
for (int i = 1; i < k; i++) { for (int i = 1; i < k; i++) {
CX2 += 100;
CY2 += 100;
ArrayList<Point2f> merged; ArrayList<Point2f> merged;
IJ.log(String.format("ellipse %d", i));
do { do {
ArrayList<Point2f> pts = RandomEllipsePts(); ArrayList<Point2f> pts = RandomEllipsePts();
merged = MergePoints(allPts, pts); merged = MergePoints(allPts, pts);
IJ.log(String.format("all: %d, merged: %d, pts: %d", allPts.size(), merged.size(), pts.size())); } while (merged.size() <= (int) (allPts.size() * 1.25));
} while (merged.size() <= (int) (allPts.size() * 1.5));
allPts = merged; allPts = merged;
showPts(i+1);
} }
for (Point2f pt : allPts) { for (Point2f pt : allPts) {
bw.set((int) pt.x(), (int) pt.y(), 255); bw.set((int) pt.x(), (int) pt.y(), 255);
} }
// Mat cv = RandomCov();
// // compute eigenvalues and eigenvectors of the covariance matrix
// Mat eigVal = new Mat();
// Mat eigVec = new Mat();
// int lo, hi;
// eigen(cv, eigVal, eigVec);
// // showMat(eigVec);
// Mat eigVecRot = randRotate(eigVec);
// IJ.log("rotated matrix");
// showMat(eigVecRot);
// DoubleIndexer valIdx = eigVal.createIndexer();
// if (valIdx.get(0,0) > valIdx.get(1,0)) {
// hi = 0;
// lo = 1;
// } else {
// hi = 1;
// lo = 0;
// }
// double A = Math.sqrt(valIdx.get(hi,0));
// double B = Math.sqrt(valIdx.get(lo,0));
// IJ.log(String.format("A = %f, B = %f", A, B));
// EllipseRoi elRoi = makeEllipseRoi(eigVecRot.row(hi), A, B);
// Rectangle r = elRoi.getBounds();
// for (int y=r.y; y<(r.y+r.height); y++) {
// for (int x=r.x; x<(r.x+r.width); x++) {
// if (elRoi.contains(x, y)) {
// bw.set(x, y, 255);
// }
// }
// }
// }
} }
// return an arraylist of the points in a random ellipse // return an arraylist of the points in a random ellipse
...@@ -135,10 +91,7 @@ public class Demo_Random_PR implements PlugInFilter { ...@@ -135,10 +91,7 @@ public class Demo_Random_PR implements PlugInFilter {
Mat eigVec = new Mat(); Mat eigVec = new Mat();
int lo, hi; int lo, hi;
eigen(cv, eigVal, eigVec); eigen(cv, eigVal, eigVec);
showMat(eigVec);
Mat eigVecRot = randRotate(eigVec); Mat eigVecRot = randRotate(eigVec);
IJ.log("rotated matrix");
showMat(eigVecRot);
DoubleIndexer valIdx = eigVal.createIndexer(); DoubleIndexer valIdx = eigVal.createIndexer();
if (valIdx.get(0,0) > valIdx.get(1,0)) { if (valIdx.get(0,0) > valIdx.get(1,0)) {
hi = 0; hi = 0;
...@@ -150,7 +103,7 @@ public class Demo_Random_PR implements PlugInFilter { ...@@ -150,7 +103,7 @@ public class Demo_Random_PR implements PlugInFilter {
double A = Math.sqrt(valIdx.get(hi,0)); double A = Math.sqrt(valIdx.get(hi,0));
double B = Math.sqrt(valIdx.get(lo,0)); double B = Math.sqrt(valIdx.get(lo,0));
IJ.log(String.format("A = %f, B = %f", A, B)); // IJ.log(String.format("A = %f, B = %f", A, B));
// make an elliptical ROI, and return the points inside // make an elliptical ROI, and return the points inside
EllipseRoi elRoi = makeEllipseRoi(eigVecRot.row(hi), A, B); EllipseRoi elRoi = makeEllipseRoi(eigVecRot.row(hi), A, B);
...@@ -183,20 +136,13 @@ public class Demo_Random_PR implements PlugInFilter { ...@@ -183,20 +136,13 @@ public class Demo_Random_PR implements PlugInFilter {
} }
private Mat randRotate(Mat m) { private Mat randRotate(Mat m) {
double theta = rnd.nextDouble() * 360 - 180; double theta = rnd.nextDouble() * 2 * Math.PI - Math.PI;
// theta = 45;
IJ.log(String.format("theta = %f", theta));
Point2f center = new Point2f(0.5f, 0.5f);
// Point2f center = new Point2f(0f, 0f);
Size size = new Size(2,2);
Mat rot = getRotationMatrix2D(center, theta, 1);
Mat rotM = new Mat(2,2,CV_64FC1); Mat rotM = new Mat(2,2,CV_64FC1);
warpAffine(m, rotM, rot, size);
DoubleIndexer idx = rotM.createIndexer(); DoubleIndexer idx = rotM.createIndexer();
idx.put(0, 0, Math.cos(theta*Math.PI/180)); idx.put(0, 0, Math.cos(theta));
idx.put(0, 1, -Math.sin(theta*Math.PI/180)); idx.put(0, 1, -Math.sin(theta));
idx.put(1, 0, Math.sin(theta*Math.PI/180)); idx.put(1, 0, Math.sin(theta));
idx.put(1, 1, Math.cos(theta*Math.PI/180)); idx.put(1, 1, Math.cos(theta));
return rotM; return rotM;
} }
...@@ -213,28 +159,8 @@ public class Demo_Random_PR implements PlugInFilter { ...@@ -213,28 +159,8 @@ public class Demo_Random_PR implements PlugInFilter {
Mat c = new Mat(1,2,CV_64FC1); Mat c = new Mat(1,2,CV_64FC1);
DoubleIndexer idx = c.createIndexer(); DoubleIndexer idx = c.createIndexer();
if (allPts.isEmpty()) {
idx.put(0, 0, CX); idx.put(0, 0, CX);
idx.put(0, 1, CY); idx.put(0, 1, CY);
} else {
Point2f p = allPts.get(rnd.nextInt(allPts.size()));
idx.put(0, 0, p.x());
idx.put(0, 1, p.y());
}
// sign = rnd.nextInt(2) == 0 ? 1 : -1;
// idx.put(0, 0, CX + sign * 200 * rnd.nextDouble());
// sign = rnd.nextInt(2) == 0 ? 1 : -1;
// idx.put(0, 1, CY + sign * 200 * rnd.nextDouble());
return c;
}
private Mat center2() {
int sign;
Mat c = new Mat(1,2,CV_64FC1);
DoubleIndexer idx = c.createIndexer();
idx.put(0, 0, CX2);
idx.put(0, 1, CY2);
return c; return c;
} }
...@@ -250,11 +176,6 @@ public class Demo_Random_PR implements PlugInFilter { ...@@ -250,11 +176,6 @@ public class Demo_Random_PR implements PlugInFilter {
DoubleIndexer end1Idx = end1.createIndexer(); DoubleIndexer end1Idx = end1.createIndexer();
DoubleIndexer end2Idx = end2.createIndexer(); DoubleIndexer end2Idx = end2.createIndexer();
// IJ.log(String.format("end1: (%f,%f), end2: (%f,%f), aspectRatio: %f",
// end1Idx.get(0,0), end1Idx.get(0,1),
// end2Idx.get(0,0), end2Idx.get(0,1),
// aspectRatio));
return new EllipseRoi(end1Idx.get(0,0), end1Idx.get(0,1), return new EllipseRoi(end1Idx.get(0,0), end1Idx.get(0,1),
end2Idx.get(0,0), end2Idx.get(0,1), end2Idx.get(0,0), end2Idx.get(0,1),
aspectRatio); aspectRatio);
...@@ -266,8 +187,15 @@ public class Demo_Random_PR implements PlugInFilter { ...@@ -266,8 +187,15 @@ public class Demo_Random_PR implements PlugInFilter {
for (Point2f p : a) for (Point2f p : a)
im.set((int) p.x(), (int) p.y(), 255); im.set((int) p.x(), (int) p.y(), 255);
// pick random points from a and b, and move all the points in b so
// that those 2 points are on top of each other
Point2f pa = a.get(rnd.nextInt(a.size()));
Point2f pb = b.get(rnd.nextInt(b.size()));
double dx = pb.x() - pa.x();
double dy = pb.y() - pa.y();
for (Point2f p : b) for (Point2f p : b)
im.set((int) p.x(), (int) p.y(), 255); im.set((int) (p.x() - dx), (int) (p.y() - dy), 255);
ArrayList<Point2f> pts = new ArrayList<Point2f>(); ArrayList<Point2f> pts = new ArrayList<Point2f>();
for (int x = 0; x < im.getWidth(); x++) { for (int x = 0; x < im.getWidth(); x++) {
...@@ -289,4 +217,21 @@ public class Demo_Random_PR implements PlugInFilter { ...@@ -289,4 +217,21 @@ public class Demo_Random_PR implements PlugInFilter {
ImagePlus bwIP = new ImagePlus(String.format("k = %d", k), im); ImagePlus bwIP = new ImagePlus(String.format("k = %d", k), im);
bwIP.show(); bwIP.show();
} }
private ByteProcessor ZoomIn(ByteProcessor bw) {
// Based on this Matlab code:
// RMAX=K*30;
// rLim = [500-RMAX,500+RMAX];
// xlim(rLim); ylim(rLim)
int RMAX = k * 30;
int OFFSET = CX - RMAX;
ByteProcessor bwZoomed = new ByteProcessor(RMAX * 2, RMAX * 2);
for (int i = 0; i < RMAX * 2; i++) {
for (int j = 0; j < RMAX * 2; j++) {
bwZoomed.set(i, j, bw.get(i+OFFSET, j+OFFSET));
}
}
return bwZoomed;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment