Skip to content

Commit

Permalink
Example formatting and minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
lessthanoptimal committed Jul 12, 2021
1 parent 692c541 commit 29857de
Show file tree
Hide file tree
Showing 17 changed files with 191 additions and 188 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Peter Abeles. All Rights Reserved.
* Copyright (c) 2021, Peter Abeles. All Rights Reserved.
*
* This file is part of BoofCV (http://boofcv.org).
*
Expand Down Expand Up @@ -39,7 +39,7 @@ public static void main( String[] args ) {
BufferedImage image = UtilImageIO.loadImage(UtilIO.pathExample("sunflowers.jpg"));

// Convert input image into a BoofCV RGB image
Planar<GrayF32> rgb = ConvertBufferedImage.convertFromPlanar(image, null,true, GrayF32.class);
Planar<GrayF32> rgb = ConvertBufferedImage.convertFromPlanar(image, null, true, GrayF32.class);

//---- convert RGB image into different color formats
Planar<GrayF32> hsv = rgb.createSameShape();
Expand All @@ -50,20 +50,20 @@ public static void main( String[] args ) {

//---- Convert individual pixels into different formats
float[] pixelHsv = new float[3];
ColorHsv.rgbToHsv(10,50.6f,120,pixelHsv);
System.out.printf("Found RGB->HSV = %5.2f %5.3f %5.1f\n",pixelHsv[0],pixelHsv[1],pixelHsv[2]);
ColorHsv.rgbToHsv(10, 50.6f, 120, pixelHsv);
System.out.printf("Found RGB->HSV = %5.2f %5.3f %5.1f\n", pixelHsv[0], pixelHsv[1], pixelHsv[2]);

float[] pixelRgb = new float[3];
ColorHsv.hsvToRgb(pixelHsv[0],pixelHsv[1],pixelHsv[2],pixelRgb);
ColorHsv.hsvToRgb(pixelHsv[0], pixelHsv[1], pixelHsv[2], pixelRgb);
System.out.printf("Found HSV->RGB = %5.1f %5.1f %5.1f expected 10 50.6 120\n",
pixelRgb[0],pixelRgb[1],pixelRgb[2]);
pixelRgb[0], pixelRgb[1], pixelRgb[2]);

float[] pixelYuv = new float[3];
ColorYuv.rgbToYuv(10,50.6f,120,pixelYuv);
System.out.printf("Found RGB->YUV = %5.1f %5.1f %5.1f\n",pixelYuv[0],pixelYuv[1],pixelYuv[2]);
ColorYuv.rgbToYuv(10, 50.6f, 120, pixelYuv);
System.out.printf("Found RGB->YUV = %5.1f %5.1f %5.1f\n", pixelYuv[0], pixelYuv[1], pixelYuv[2]);

ColorYuv.yuvToRgb(pixelYuv[0],pixelYuv[1],pixelYuv[2],pixelRgb);
ColorYuv.yuvToRgb(pixelYuv[0], pixelYuv[1], pixelYuv[2], pixelRgb);
System.out.printf("Found YUV->RGB = %5.1f %5.1f %5.1f expected 10 50.6 120\n",
pixelRgb[0],pixelRgb[1],pixelRgb[2]);
pixelRgb[0], pixelRgb[1], pixelRgb[2]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
*/
public class ExampleConvolution {

private static ListDisplayPanel panel = new ListDisplayPanel();
private static final ListDisplayPanel panel = new ListDisplayPanel();

public static void main(String[] args) {
public static void main( String[] args ) {
BufferedImage image = UtilImageIO.loadImage(UtilIO.pathExample("sunflowers.jpg"));

GrayU8 gray = ConvertBufferedImage.convertFromSingle(image, null, GrayU8.class);
Expand All @@ -54,20 +54,20 @@ public static void main(String[] args) {
convolve2D(gray);
normalize2D(gray);

ShowImages.showWindow(panel,"Convolution Examples",true);
ShowImages.showWindow(panel, "Convolution Examples", true);
}

/**
* Convolves a 1D kernel horizontally and vertically
*/
private static void convolve1D(GrayU8 gray) {
private static void convolve1D( GrayU8 gray ) {
ImageBorder<GrayU8> border = FactoryImageBorder.wrap(BorderType.EXTENDED, gray);
Kernel1D_S32 kernel = new Kernel1D_S32(2);
kernel.offset = 1; // specify the kernel's origin
kernel.data[0] = 1;
kernel.data[1] = -1;

GrayS16 output = new GrayS16(gray.width,gray.height);
GrayS16 output = new GrayS16(gray.width, gray.height);

GConvolveImageOps.horizontal(kernel, gray, output, border);
panel.addImage(VisualizeImageData.standard(output, null), "1D Horizontal");
Expand All @@ -79,17 +79,17 @@ private static void convolve1D(GrayU8 gray) {
/**
* Convolves a 2D kernel
*/
private static void convolve2D(GrayU8 gray) {
private static void convolve2D( GrayU8 gray ) {
// By default 2D kernels will be centered around width/2
Kernel2D_S32 kernel = new Kernel2D_S32(3);
kernel.set(1,0,2);
kernel.set(2,1,2);
kernel.set(0,1,-2);
kernel.set(1,2,-2);
kernel.set(1, 0, 2);
kernel.set(2, 1, 2);
kernel.set(0, 1, -2);
kernel.set(1, 2, -2);

// Output needs to handle the increased domain after convolution. Can't be 8bit
GrayS16 output = new GrayS16(gray.width,gray.height);
ImageBorder<GrayU8> border = FactoryImageBorder.wrap( BorderType.EXTENDED,gray);
GrayS16 output = new GrayS16(gray.width, gray.height);
ImageBorder<GrayU8> border = FactoryImageBorder.wrap(BorderType.EXTENDED, gray);

GConvolveImageOps.convolve(kernel, gray, output, border);
panel.addImage(VisualizeImageData.standard(output, null), "2D Kernel");
Expand All @@ -98,14 +98,14 @@ private static void convolve2D(GrayU8 gray) {
/**
* Convolves a 2D normalized kernel. This kernel is divided by its sum after computation.
*/
private static void normalize2D(GrayU8 gray) {
private static void normalize2D( GrayU8 gray ) {
// Create a Gaussian kernel with radius of 3
Kernel2D_S32 kernel = FactoryKernelGaussian.gaussian2D(GrayU8.class, -1, 3);
// Note that there is a more efficient way to compute this convolution since it is a separable kernel
// just use BlurImageOps instead.

// Since it's normalized it can be saved inside an 8bit image
GrayU8 output = new GrayU8(gray.width,gray.height);
GrayU8 output = new GrayU8(gray.width, gray.height);

GConvolveImageOps.convolveNormalized(kernel, gray, output);
panel.addImage(VisualizeImageData.standard(output, null), "2D Normalized Kernel");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
@SuppressWarnings({"unchecked"})
public class ExampleImageFilter {

private static int blurRadius = 10;
private static final int blurRadius = 10;

private static ListDisplayPanel panel = new ListDisplayPanel();
private static final ListDisplayPanel panel = new ListDisplayPanel();

public static void procedural( GrayU8 input ) {
GrayU8 blurred = new GrayU8(input.width, input.height);
Expand All @@ -74,7 +74,7 @@ void generalized( T input ) {
Class<T> inputType = (Class<T>)input.getClass();
Class<D> derivType = GImageDerivativeOps.getDerivativeType(inputType);

T blurred = (T)input.createSameShape();
T blurred = input.createSameShape();
D derivX = GeneralizedImageOps.createSingleBand(derivType, input.width, input.height);
D derivY = GeneralizedImageOps.createSingleBand(derivType, input.width, input.height);

Expand All @@ -94,7 +94,7 @@ void filter( T input ) {
Class<T> inputType = (Class<T>)input.getClass();
Class<D> derivType = GImageDerivativeOps.getDerivativeType(inputType);

T blurred = (T)input.createSameShape();
T blurred = input.createSameShape();
D derivX = GeneralizedImageOps.createSingleBand(derivType, input.width, input.height);
D derivY = GeneralizedImageOps.createSingleBand(derivType, input.width, input.height);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void process( BufferedImage image ) {
ConfigDiscreteLevels.levels(4), -1, 2, true, ImageType.single(imageType));
pyramid.process(input);

DiscretePyramidPanel gui = new DiscretePyramidPanel();
var gui = new DiscretePyramidPanel<T>();
gui.setPyramid(pyramid);
gui.render();

Expand All @@ -72,8 +72,8 @@ public void process( BufferedImage image ) {
public static void main( String[] args ) {
BufferedImage image = UtilImageIO.loadImage(UtilIO.pathExample("standard/barbara.jpg"));

ExamplePyramidDiscrete<GrayF32> app = new ExamplePyramidDiscrete<>(GrayF32.class);
// ExamplePyramidDiscrete<GrayU8> app = new ExamplePyramidDiscrete<>(GrayU8.class);
var app = new ExamplePyramidDiscrete<>(GrayF32.class);
// var app = new ExamplePyramidDiscrete<>(GrayU8.class);

app.process(image);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public void process( BufferedImage image ) {
public static void main( String[] args ) {
BufferedImage image = UtilImageIO.loadImage(UtilIO.pathExample("standard/barbara.jpg"));

ExamplePyramidFloat<GrayF32> app = new ExamplePyramidFloat<>(GrayF32.class);
// ExamplePyramidFloat<GrayU8> app = new ExamplePyramidFloat<>(GrayU8.class);
var app = new ExamplePyramidFloat<>(GrayF32.class);
// var app = new ExamplePyramidFloat<>(GrayU8.class);

app.standard();
app.process(image);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
* @author Peter Abeles
*/
public class ExampleSceneRecognition {

public static void main( String[] args ) {
String imagePath = UtilIO.pathExample("recognition/scene");
List<String> images = UtilIO.listByPrefix(imagePath, null, ".jpg");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,21 @@ public class ExampleTemplateMatching {
/**
* Demonstrates how to search for matches of a template inside an image
*
* @param image Image being searched
* @param template Template being looked for
* @param mask Mask which determines the weight of each template pixel in the match score
* @param image Image being searched
* @param template Template being looked for
* @param mask Mask which determines the weight of each template pixel in the match score
* @param expectedMatches Number of expected matches it hopes to find
* @return List of match location and scores
*/
private static List<Match> findMatches(GrayF32 image, GrayF32 template, GrayF32 mask,
int expectedMatches) {
private static List<Match> findMatches( GrayF32 image, GrayF32 template, GrayF32 mask,
int expectedMatches ) {
// create template matcher.
TemplateMatching<GrayF32> matcher =
FactoryTemplateMatching.createMatcher(TemplateScoreType.SUM_SQUARE_ERROR, GrayF32.class);

// Find the points which match the template the best
matcher.setImage(image);
matcher.setTemplate(template, mask,expectedMatches);
matcher.setTemplate(template, mask, expectedMatches);
matcher.process();

return matcher.getResults().toList();
Expand All @@ -71,7 +71,7 @@ private static List<Match> findMatches(GrayF32 image, GrayF32 template, GrayF32
* Computes the template match intensity image and displays the results. Brighter intensity indicates
* a better match to the template.
*/
private static void showMatchIntensity(GrayF32 image, GrayF32 template, GrayF32 mask) {
private static void showMatchIntensity( GrayF32 image, GrayF32 template, GrayF32 mask ) {
// create algorithm for computing intensity image
TemplateMatchingIntensity<GrayF32> matchIntensity =
FactoryTemplateMatching.createIntensity(TemplateScoreType.SUM_SQUARE_ERROR, GrayF32.class);
Expand All @@ -97,51 +97,54 @@ private static void showMatchIntensity(GrayF32 image, GrayF32 template, GrayF32
ShowImages.showWindow(output, "Match Intensity", true);
}

public static void main(String[] args) {
public static void main( String[] args ) {
// Load image and templates
String directory = UtilIO.pathExample("template");

GrayF32 image = UtilImageIO.loadImage(directory ,"desktop.png", GrayF32.class);
GrayF32 templateCursor = UtilImageIO.loadImage(directory , "cursor.png", GrayF32.class);
GrayF32 maskCursor = UtilImageIO.loadImage(directory , "cursor_mask.png", GrayF32.class);
GrayF32 templatePaint = UtilImageIO.loadImage(directory , "paint.png", GrayF32.class);
GrayF32 image = UtilImageIO.loadImage(directory, "desktop.png", GrayF32.class);
GrayF32 templateCursor = UtilImageIO.loadImage(directory, "cursor.png", GrayF32.class);
GrayF32 maskCursor = UtilImageIO.loadImage(directory, "cursor_mask.png", GrayF32.class);
GrayF32 templatePaint = UtilImageIO.loadImage(directory, "paint.png", GrayF32.class);

// create output image to show results
BufferedImage output = new BufferedImage(image.width, image.height, BufferedImage.TYPE_INT_BGR);
ConvertBufferedImage.convertTo(image, output);
Graphics2D g2 = output.createGraphics();

// Search for the cursor in the image. For demonstration purposes it has been pasted 3 times
g2.setColor(Color.RED); g2.setStroke(new BasicStroke(5));
g2.setColor(Color.RED);
g2.setStroke(new BasicStroke(5));
drawRectangles(g2, image, templateCursor, maskCursor, 3);
// show match intensity image for this template
showMatchIntensity(image, templateCursor, maskCursor);

// Now it's try finding the cursor without a mask. it will get confused when the background is black
g2.setColor(Color.BLUE); g2.setStroke(new BasicStroke(2));
g2.setColor(Color.BLUE);
g2.setStroke(new BasicStroke(2));
drawRectangles(g2, image, templateCursor, null, 3);

// Now it searches for a specific icon for which there is only one match
g2.setColor(Color.ORANGE); g2.setStroke(new BasicStroke(3));
g2.setColor(Color.ORANGE);
g2.setStroke(new BasicStroke(3));
drawRectangles(g2, image, templatePaint, null, 1);

ShowImages.showWindow(output, "Found Matches",true);
ShowImages.showWindow(output, "Found Matches", true);
}

/**
* Helper function will is finds matches and displays the results as colored rectangles
*/
private static void drawRectangles(Graphics2D g2,
GrayF32 image, GrayF32 template, GrayF32 mask,
int expectedMatches) {
private static void drawRectangles( Graphics2D g2,
GrayF32 image, GrayF32 template, GrayF32 mask,
int expectedMatches ) {
List<Match> found = findMatches(image, template, mask, expectedMatches);

int r = 2;
int w = template.width + 2 * r;
int h = template.height + 2 * r;
int w = template.width + 2*r;
int h = template.height + 2*r;

for (Match m : found) {
System.out.printf("Match %3d %3d score = %6.2f\n",m.x,m.y,m.score);
System.out.printf("Match %3d %3d score = %6.2f\n", m.x, m.y, m.score);
// this demonstrates how to filter out false positives
// the meaning of score will depend on the template technique
// if( m.score < -5 ) // This line is commented out for demonstration purposes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -167,7 +170,7 @@ public void compute( String videoName, boolean sequential ) {
computePairwiseGraph();
if (working == null)
metricFromPairwise();
if (scene == null )
if (scene == null)
bundleAdjustmentRefine();

Rodrigues_F64 rod = new Rodrigues_F64();
Expand Down Expand Up @@ -396,8 +399,6 @@ public void visualizeSparseCloud() {

try (FileOutputStream out = new FileOutputStream("saved_cloud.ply")) {
PointCloudIO.save3D(PointCloudIO.Format.PLY, PointCloudReader.wrapF64RGB(copy.toList()), true, out);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Expand Down

0 comments on commit 29857de

Please sign in to comment.