use of georegression.struct.homography.Homography2D_F32 in project BoofCV by lessthanoptimal.
the class ExampleBackgroundRemovalMoving method main.
public static void main(String[] args) {
// Example with a moving camera. Highlights why motion estimation is sometimes required
String fileName = UtilIO.pathExample("tracking/chipmunk.mjpeg");
// Camera has a bit of jitter in it. Static kinda works but motion reduces false positives
// String fileName = UtilIO.pathExample("background/horse_jitter.mp4");
// Comment/Uncomment to switch input image type
ImageType imageType = ImageType.single(GrayF32.class);
// ImageType imageType = ImageType.il(3, InterleavedF32.class);
// ImageType imageType = ImageType.il(3, InterleavedU8.class);
// Configure the feature detector
ConfigGeneralDetector confDetector = new ConfigGeneralDetector();
confDetector.threshold = 10;
confDetector.maxFeatures = 300;
confDetector.radius = 6;
// Use a KLT tracker
PointTracker tracker = FactoryPointTracker.klt(new int[] { 1, 2, 4, 8 }, confDetector, 3, GrayF32.class, null);
// This estimates the 2D image motion
ImageMotion2D<GrayF32, Homography2D_F64> motion2D = FactoryMotion2D.createMotion2D(500, 0.5, 3, 100, 0.6, 0.5, false, tracker, new Homography2D_F64());
ConfigBackgroundBasic configBasic = new ConfigBackgroundBasic(30, 0.005f);
// Configuration for Gaussian model. Note that the threshold changes depending on the number of image bands
// 12 = gray scale and 40 = color
ConfigBackgroundGaussian configGaussian = new ConfigBackgroundGaussian(12, 0.001f);
configGaussian.initialVariance = 64;
configGaussian.minimumDifference = 5;
// Note that GMM doesn't interpolate the input image. Making it harder to model object edges.
// However it runs faster because of this.
ConfigBackgroundGmm configGmm = new ConfigBackgroundGmm();
configGmm.initialVariance = 1600;
configGmm.significantWeight = 1e-1f;
// Comment/Uncomment to switch background mode
BackgroundModelMoving background = FactoryBackgroundModel.movingBasic(configBasic, new PointTransformHomography_F32(), imageType);
// FactoryBackgroundModel.movingGaussian(configGaussian, new PointTransformHomography_F32(), imageType);
// FactoryBackgroundModel.movingGmm(configGmm,new PointTransformHomography_F32(), imageType);
background.setUnknownValue(1);
MediaManager media = DefaultMediaManager.INSTANCE;
SimpleImageSequence video = media.openVideo(fileName, background.getImageType());
// media.openCamera(null,640,480,background.getImageType());
// ====== Initialize Images
// storage for segmented image. Background = 0, Foreground = 1
GrayU8 segmented = new GrayU8(video.getNextWidth(), video.getNextHeight());
// Grey scale image that's the input for motion estimation
GrayF32 grey = new GrayF32(segmented.width, segmented.height);
// coordinate frames
Homography2D_F32 firstToCurrent32 = new Homography2D_F32();
Homography2D_F32 homeToWorld = new Homography2D_F32();
homeToWorld.a13 = grey.width / 2;
homeToWorld.a23 = grey.height / 2;
// Create a background image twice the size of the input image. Tell it that the home is in the center
background.initialize(grey.width * 2, grey.height * 2, homeToWorld);
BufferedImage visualized = new BufferedImage(segmented.width, segmented.height, BufferedImage.TYPE_INT_RGB);
ImageGridPanel gui = new ImageGridPanel(1, 2);
gui.setImages(visualized, visualized);
ShowImages.showWindow(gui, "Detections", true);
double fps = 0;
// smoothing factor for FPS
double alpha = 0.01;
while (video.hasNext()) {
ImageBase input = video.next();
long before = System.nanoTime();
GConvertImage.convert(input, grey);
if (!motion2D.process(grey)) {
throw new RuntimeException("Should handle this scenario");
}
Homography2D_F64 firstToCurrent64 = motion2D.getFirstToCurrent();
ConvertMatrixData.convert(firstToCurrent64, firstToCurrent32);
background.segment(firstToCurrent32, input, segmented);
background.updateBackground(firstToCurrent32, input);
long after = System.nanoTime();
fps = (1.0 - alpha) * fps + alpha * (1.0 / ((after - before) / 1e9));
VisualizeBinaryData.renderBinary(segmented, false, visualized);
gui.setImage(0, 0, (BufferedImage) video.getGuiImage());
gui.setImage(0, 1, visualized);
gui.repaint();
System.out.println("FPS = " + fps);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
}
}
}
use of georegression.struct.homography.Homography2D_F32 in project BoofCV by lessthanoptimal.
the class BenchmarkImageDistort method benchmark.
private void benchmark() {
Random rand = new Random(234);
Homography2D_F32 affine = new Homography2D_F32((float) rand.nextGaussian(), (float) rand.nextGaussian(), (float) rand.nextGaussian(), (float) rand.nextGaussian(), (float) rand.nextGaussian(), (float) rand.nextGaussian(), (float) rand.nextGaussian(), (float) rand.nextGaussian(), (float) rand.nextGaussian());
System.out.println("========= Profile Image Size " + imgWidth + " x " + imgHeight + " ==========");
System.out.println();
ProfileOperation.printOpsPerSec(new MapBilinear_F32(affine), TEST_TIME);
ProfileOperation.printOpsPerSec(new HomographyBilinear_F32(affine), TEST_TIME);
ProfileOperation.printOpsPerSec(new HomographyBilinearCrop_F32(affine), TEST_TIME);
}
use of georegression.struct.homography.Homography2D_F32 in project BoofCV by lessthanoptimal.
the class GenericBackgroundModelMovingChecks method checkTransform.
private <T extends ImageBase<T>> void checkTransform(T frame, GrayU8 segmented, BackgroundModelMoving<T, Homography2D_F32> alg, Homography2D_F32 homeToCurrent, double tol) {
Homography2D_F32 currentToHome = homeToCurrent.invert(null);
alg.reset();
alg.updateBackground(homeToCurrent, frame);
alg.segment(homeToCurrent, frame, segmented);
checkSegmented(currentToHome, segmented, tol);
alg.segment(new Homography2D_F32(), frame, segmented);
checkSegmented(homeToCurrent, segmented, tol);
}
use of georegression.struct.homography.Homography2D_F32 in project BoofCV by lessthanoptimal.
the class GenericBackgroundModelMovingChecks method currentOutsideBackground.
private <T extends ImageBase<T>> void currentOutsideBackground(ImageType<T> imageType) {
T frame = imageType.createImage(width, height);
GrayU8 segmented = new GrayU8(width, height);
BackgroundModelMoving<T, Homography2D_F32> alg = create(frame.getImageType());
Homography2D_F32 homeToWorld = new Homography2D_F32();
alg.initialize(width, height, homeToWorld);
alg.setUnknownValue(2);
double translationTol = backgroundOutsideTol / 2;
Homography2D_F32 homeToCurrent = new Homography2D_F32();
homeToCurrent.a13 = 5;
checkTransform(frame, segmented, alg, homeToCurrent, translationTol);
homeToCurrent.a13 = -5;
checkTransform(frame, segmented, alg, homeToCurrent, translationTol);
homeToCurrent.a13 = 0;
homeToCurrent.a23 = 5;
checkTransform(frame, segmented, alg, homeToCurrent, translationTol);
homeToCurrent.a23 = -5;
checkTransform(frame, segmented, alg, homeToCurrent, translationTol);
// make it more interesting
homeToCurrent.set(1.0f, 0.6f, 20, -0.6f, 0.95f, 20, 0, 0, 1);
checkTransform(frame, segmented, alg, homeToCurrent, backgroundOutsideTol);
}
use of georegression.struct.homography.Homography2D_F32 in project BoofCV by lessthanoptimal.
the class GenericBackgroundMovingGmmChecks method performStationaryTests.
@Test
public void performStationaryTests() {
GenericBackgroundStationaryGmmChecks stationary = new GenericBackgroundStationaryGmmChecks() {
@Override
public BackgroundModelStationary create(ImageType imageType) {
BackgroundModelMoving moving = GenericBackgroundMovingGmmChecks.this.create(imageType);
return new MovingToStationary((BackgroundMovingGmm) moving, new Homography2D_F32());
}
};
stationary.initialVariance();
stationary.learnRate();
stationary.checkBandsUsed();
}
Aggregations