use of boofcv.struct.RectangleRotate_F32 in project BoofCV by lessthanoptimal.
the class TrackerMeanShiftComaniciu2003 method track.
/**
* Searches for the target in the most recent image.
* @param image Most recent image in the sequence
*/
public void track(T image) {
// configure the different regions based on size
region0.set(region);
region1.set(region);
region2.set(region);
region0.width *= 1 - scaleChange;
region0.height *= 1 - scaleChange;
region2.width *= 1 + scaleChange;
region2.height *= 1 + scaleChange;
// distance from histogram
double distance0 = 1, distance1, distance2 = 1;
// perform mean-shift at the different sizes and compute their distance
if (!constantScale) {
if (region0.width >= minimumWidth) {
updateLocation(image, region0);
distance0 = distanceHistogram(keyHistogram, calcHistogram.getHistogram());
if (updateHistogram)
System.arraycopy(calcHistogram.getHistogram(), 0, histogram0, 0, histogram0.length);
}
updateLocation(image, region2);
distance2 = distanceHistogram(keyHistogram, calcHistogram.getHistogram());
if (updateHistogram)
System.arraycopy(calcHistogram.getHistogram(), 0, histogram2, 0, histogram2.length);
}
// update the no scale change hypothesis
updateLocation(image, region1);
if (!constantScale) {
distance1 = distanceHistogram(keyHistogram, calcHistogram.getHistogram());
} else {
// force it to select
distance1 = 0;
}
if (updateHistogram)
System.arraycopy(calcHistogram.getHistogram(), 0, histogram1, 0, histogram1.length);
RectangleRotate_F32 selected = null;
float[] selectedHist = null;
switch(selectBest(distance0, distance1, distance2)) {
case 0:
selected = region0;
selectedHist = histogram0;
break;
case 1:
selected = region1;
selectedHist = histogram1;
break;
case 2:
selected = region2;
selectedHist = histogram2;
break;
default:
throw new RuntimeException("Bug in selectBest");
}
// Set region to the best scale, but reduce sensitivity by weighting it against the original size
// equation 14
float w = selected.width * (1 - gamma) + gamma * region.width;
float h = selected.height * (1 - gamma) + gamma * region.height;
region.set(selected);
region.width = w;
region.height = h;
if (updateHistogram) {
System.arraycopy(selectedHist, 0, keyHistogram, 0, keyHistogram.length);
}
}
use of boofcv.struct.RectangleRotate_F32 in project BoofCV by lessthanoptimal.
the class TestLocalWeightedHistogramRotRect method computeHistogram.
@Test
public void computeHistogram() {
Planar<GrayF32> image = new Planar<>(GrayF32.class, 40, 50, 3);
InterpolatePixelMB interp = FactoryInterpolation.createPixelPL(FactoryInterpolation.bilinearPixelS(GrayF32.class, BorderType.EXTENDED));
GImageMiscOps.fillUniform(image, rand, 0, 100);
interp.setImage(image);
RectangleRotate_F32 rect = new RectangleRotate_F32(20, 25, 10, 15, 0);
LocalWeightedHistogramRotRect alg = new LocalWeightedHistogramRotRect(10, 3, 12, 3, 255, interp);
alg.computeHistogram(image, rect);
float[] hist = alg.getHistogram().clone();
int[] histIndex = alg.getSampleHistIndex().clone();
// crude sanity check
int numNotZero = 0;
for (int i = 0; i < hist.length; i++) if (hist[i] != 0)
numNotZero++;
assertTrue(numNotZero > 0);
for (int i = 0; i < histIndex.length; i++) assertTrue(histIndex[i] != 0);
// should produce the same answer after a second call
alg.computeHistogram(image, rect);
for (int i = 0; i < hist.length; i++) assertEquals(hist[i], alg.getHistogram()[i], 1e-4);
for (int i = 0; i < histIndex.length; i++) assertEquals(histIndex[i], alg.getSampleHistIndex()[i], 1e-4);
}
use of boofcv.struct.RectangleRotate_F32 in project BoofCV by lessthanoptimal.
the class TestLocalWeightedHistogramRotRect method computeHistogramBorder_outside.
/**
* Make sure it handles pixels outside the image correctly
*/
@Test
public void computeHistogramBorder_outside() {
int numSamples = 10;
InterleavedF32 image = new InterleavedF32(40, 50, 3);
DummyInterpolate interp = new DummyInterpolate();
RectangleRotate_F32 rect = new RectangleRotate_F32(4, 5, 10, 20, 0);
LocalWeightedHistogramRotRect alg = new LocalWeightedHistogramRotRect(numSamples, 3, 12, 3, 255, interp);
alg.c = 1;
alg.s = 0;
alg.computeHistogramBorder(image, rect);
int numInside = 0;
int i = 0;
for (int y = 0; y < numSamples; y++) {
for (int x = 0; x < numSamples; x++, i++) {
alg.squareToImageSample((float) x / (numSamples - 1) - 0.5f, (float) y / (numSamples - 1) - 0.5f, rect);
boolean inside = alg.imageX >= 0 && alg.imageX < 40 && alg.imageY >= 0 && alg.imageY < 50;
if (inside) {
numInside++;
assertTrue(alg.sampleHistIndex[i] >= 0);
assertTrue(alg.histogram[alg.sampleHistIndex[i]] > 0);
} else {
assertTrue(alg.sampleHistIndex[i] == -1);
}
}
}
assertTrue(numInside != numSamples * numSamples);
}
use of boofcv.struct.RectangleRotate_F32 in project BoofCV by lessthanoptimal.
the class TestLocalWeightedHistogramRotRect method squareToImage.
@Test
public void squareToImage() {
RectangleRotate_F32 rect = new RectangleRotate_F32(4, 5, 10, 20, 0);
LocalWeightedHistogramRotRect alg = new LocalWeightedHistogramRotRect(10, 3, 12, 3, 255, null);
alg.c = 1;
alg.s = 0;
alg.squareToImageSample(0, 0, rect);
assertEquals(4, alg.imageX, 1e-4f);
assertEquals(5, alg.imageY, 1e-4f);
alg.squareToImageSample(-0.5f, 0.5f, rect);
assertEquals(4f - 4.5f, alg.imageX, 1e-4f);
assertEquals(5f + 9.5f, alg.imageY, 1e-4f);
alg.c = 0.5f;
alg.s = -0.5f;
alg.squareToImageSample(-0.5f, 0.5f, rect);
// -4.5 + 9.5
assertEquals(4f - 2.25f + 4.75f, alg.imageX, 1e-4f);
assertEquals(5f + 2.25f + 4.75f, alg.imageY, 1e-4f);
}
use of boofcv.struct.RectangleRotate_F32 in project BoofCV by lessthanoptimal.
the class TestLocalWeightedHistogramRotRect method isInFastBounds.
@Test
public void isInFastBounds() {
DummyInterpolate interp = new DummyInterpolate();
RectangleRotate_F32 rect = new RectangleRotate_F32(4, 5, 10, 20, 0);
LocalWeightedHistogramRotRect alg = new LocalWeightedHistogramRotRect(10, 3, 12, 3, 255, interp);
alg.c = 1;
alg.s = 0;
assertTrue(alg.isInFastBounds(rect));
// see if it checked to see if the four corners are in bounds
assertEquals(4, interp.list.size());
Point2D_F32 p0 = interp.list.get(0);
Point2D_F32 p1 = interp.list.get(1);
Point2D_F32 p2 = interp.list.get(2);
Point2D_F32 p3 = interp.list.get(3);
// the order really doesn't matter, but easier to code the test this way
assertEquals(4f - 4.5f, p0.x, 1e-4f);
assertEquals(5f - 9.5f, p0.y, 1e-4f);
assertEquals(4f - 4.5f, p1.x, 1e-4f);
assertEquals(5f + 9.5f, p1.y, 1e-4f);
assertEquals(4f + 4.5f, p2.x, 1e-4f);
assertEquals(5f + 9.5f, p2.y, 1e-4f);
assertEquals(4f + 4.5f, p3.x, 1e-4f);
assertEquals(5f - 9.5f, p3.y, 1e-4f);
}
Aggregations