Skip to content
Permalink
Browse files
NIVision Forward Port
Ported the NIVision libraries from the old NIVision implementation to
the new autogenerated JNI bindings.

Change-Id: I7c68ca6abef1185d59a9787e9a269d720c825e2f
  • Loading branch information
333fred committed Dec 24, 2014
1 parent a55f346 commit 3c4a1d9a1ac201e71dbce9c42070d994ed36ff86
Show file tree
Hide file tree
Showing 18 changed files with 189 additions and 2,039 deletions.
@@ -63,7 +63,6 @@
<source>1.7</source>
<target>1.7</target>
<excludes>
<exclude>edu/wpi/first/wpilibj/image/</exclude>
<exclude>edu/wpi/first/wpilibj/camera/</exclude>
</excludes>
</configuration>
@@ -77,7 +76,6 @@
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<sourceFileExcludes>
<exclude>edu/wpi/first/wpilibj/image/</exclude>
<exclude>edu/wpi/first/wpilibj/camera/</exclude>
</sourceFileExcludes>
</configuration>
@@ -139,19 +139,19 @@ public static class ResolutionT {
public final int height;
static final ResolutionT[] allValues = new ResolutionT[4];
/**
* Image is 640 pixels wide by 480 tall
* ImageBase is 640 pixels wide by 480 tall
*/
public static final ResolutionT k640x480 = new ResolutionT(0, 640, 480);
/**
* Image is 640 pixels wide by 360 tall
* ImageBase is 640 pixels wide by 360 tall
*/
public static final ResolutionT k640x360 = new ResolutionT(1, 640, 360);
/**
* Image is 320 pixels wide by 240 tall
* ImageBase is 320 pixels wide by 240 tall
*/
public static final ResolutionT k320x240 = new ResolutionT(2, 320, 240);
/**
* Image is 160 pixels wide by 120 tall
* ImageBase is 160 pixels wide by 120 tall
*/
public static final ResolutionT k160x120 = new ResolutionT(3, 160, 120);

@@ -7,11 +7,12 @@

package edu.wpi.first.wpilibj.image;

import com.sun.jna.Pointer;
import edu.wpi.first.wpilibj.util.SortedVector;
import com.ni.vision.NIVision;

/**
* An image where each pixel is treated as either on or off.
*
* @author dtjones
*/
public class BinaryImage extends MonoImage {
@@ -26,31 +27,35 @@ public class BinaryImage extends MonoImage {

/**
* Returns the number of particles.
*
* @return The number of particles
*/
public int getNumberParticles () throws NIVisionException {
public int getNumberParticles() throws NIVisionException {
if (numParticles < 0)
numParticles = NIVision.countParticles(image);
numParticles = NIVision.imaqCountParticles(image, 1);
return numParticles;
}


private class ParticleSizeReport {
final int index;
final double size;

public ParticleSizeReport(int index) throws NIVisionException {
if ((!(index < BinaryImage.this.getNumberParticles())) || index < 0)
throw new IndexOutOfBoundsException();
this.index = index;
size = ParticleAnalysisReport.getParticleToImagePercent(BinaryImage.this, index);
}
public ParticleAnalysisReport getParticleAnalysisReport () throws NIVisionException {

public ParticleAnalysisReport getParticleAnalysisReport() throws NIVisionException {
return new ParticleAnalysisReport(BinaryImage.this, index);
}
}

/**
* Get a particle analysis report for the particle at the given index.
*
* @param index The index of the particle to report on.
* @return The ParticleAnalysisReport for the particle at the given index
*/
@@ -61,6 +66,7 @@ public ParticleAnalysisReport getParticleAnalysisReport(int index) throws NIVisi

/**
* Gets all the particle analysis reports ordered from largest area to smallest.
*
* @param size The number of particles to return
* @return An array of ParticleReports from largest area to smallest
*/
@@ -70,8 +76,8 @@ public ParticleAnalysisReport[] getOrderedParticleAnalysisReports(int size) thro
ParticleSizeReport[] reports = new ParticleSizeReport[size];
SortedVector sorter = new SortedVector(new SortedVector.Comparator() {
public int compare(Object object1, Object object2) {
ParticleSizeReport p1 = (ParticleSizeReport)object1;
ParticleSizeReport p2 = (ParticleSizeReport)object2;
ParticleSizeReport p1 = (ParticleSizeReport) object1;
ParticleSizeReport p2 = (ParticleSizeReport) object2;
if (p1.size < p2.size)
return -1;
else if (p1.size > p2.size)
@@ -91,6 +97,7 @@ else if (p1.size > p2.size)

/**
* Gets all the particle analysis reports ordered from largest area to smallest.
*
* @return An array of ParticleReports from largest are to smallest
*/
public ParticleAnalysisReport[] getOrderedParticleAnalysisReports() throws NIVisionException {
@@ -99,19 +106,9 @@ public ParticleAnalysisReport[] getOrderedParticleAnalysisReports() throws NIVis


public void write(String fileName) throws NIVisionException {
Pointer colorTable = new Pointer(1024);
//Black Background
colorTable.setByte(0, (byte)0); //B
colorTable.setByte(1, (byte)0); //G
colorTable.setByte(2, (byte)0); //R
colorTable.setByte(3, (byte)0); //Alpha
//Red Particles:
colorTable.setByte(4, (byte)0); //B
colorTable.setByte(5, (byte)0); //G
colorTable.setByte(6, (byte)255); //R
colorTable.setByte(7, (byte)0); //Alpha
NIVision.RGBValue colorTable = new NIVision.RGBValue(0, 0, 255, 0);
try {
NIVision.writeFile(image, fileName, colorTable);
NIVision.imaqWriteFile(image, fileName, colorTable);
} finally {
colorTable.free();
}
@@ -121,60 +118,50 @@ public void write(String fileName) throws NIVisionException {
* removeSmallObjects filters particles based on their size.
* The algorithm erodes the image a specified number of times and keeps the
* particles from the original image that remain in the eroded image.
*
* @param connectivity8 true to use connectivity-8 or false for connectivity-4 to determine
* whether particles are touching. For more information about connectivity, see Chapter 9,
* Binary Morphology, in the NI Vision Concepts manual.
* @param erosions the number of erosions to perform
* whether particles are touching. For more information about connectivity, see Chapter 9,
* Binary Morphology, in the NI Vision Concepts manual.
* @param erosions the number of erosions to perform
* @return a BinaryImage after applying the filter
* @throws NIVisionException
*/
public BinaryImage removeSmallObjects(boolean connectivity8, int erosions) throws NIVisionException {
BinaryImage result = new BinaryImage();
try {
NIVision.sizeFilter(result.image, image, connectivity8, erosions, true);
} catch (NIVisionException ex) {
result.free();
throw ex;
}
NIVision.imaqSizeFilter(result.image, image, connectivity8 ? 1 : 0, erosions, NIVision.SizeType.KEEP_LARGE, null);
result.free();
return result;
}

/**
* removeLargeObjects filters particles based on their size.
* The algorithm erodes the image a specified number of times and discards the
* particles from the original image that remain in the eroded image.
* @param connectivity8 true to use connectivity-8 or false for connectivity-4 to determine
* whether particles are touching. For more information about connectivity, see Chapter 9,
* Binary Morphology, in the NI Vision Concepts manual.
* @param erosions the number of erosions to perform
* @return a BinaryImage after applying the filter
* @throws NIVisionException
*/
* removeLargeObjects filters particles based on their size.
* The algorithm erodes the image a specified number of times and discards the
* particles from the original image that remain in the eroded image.
*
* @param connectivity8 true to use connectivity-8 or false for connectivity-4 to determine
* whether particles are touching. For more information about connectivity, see Chapter 9,
* Binary Morphology, in the NI Vision Concepts manual.
* @param erosions the number of erosions to perform
* @return a BinaryImage after applying the filter
* @throws NIVisionException
*/
public BinaryImage removeLargeObjects(boolean connectivity8, int erosions) throws NIVisionException {
BinaryImage result = new BinaryImage();
try {
NIVision.sizeFilter(result.image, image, connectivity8, erosions, false);
} catch (NIVisionException ex) {
result.free();
throw ex;
}
NIVision.imaqSizeFilter(result.image, image, connectivity8 ? 1 : 0, erosions, NIVision.SizeType.KEEP_SMALL, null);
return result;
}

public BinaryImage convexHull(boolean connectivity8) throws NIVisionException {
BinaryImage result = new BinaryImage();
try {
NIVision.convexHull(result.image, image, connectivity8 ? 1 : 0);
} catch (NIVisionException ex) {
result.free();
throw ex;
}
NIVision.imaqConvexHull(result.image, image, connectivity8 ? 1 : 0);
return result;
}

public BinaryImage particleFilter(CriteriaCollection criteria) throws NIVisionException {
public BinaryImage particleFilter(NIVision.ParticleFilterCriteria2[] criteria) throws NIVisionException {
BinaryImage result = new BinaryImage();
NIVision.particleFilter(result.image, image, criteria);
NIVision.ParticleFilterOptions2 options = new NIVision.ParticleFilterOptions2(0, 0, 0, 1);
NIVision.imaqParticleFilter4(result.image, image, criteria, options, null);
options.free();
return result;
}
}

0 comments on commit 3c4a1d9

Please sign in to comment.