Standalone image analysis.

Hello,

I am stuck at home without the possibility to connect to a cRio but I would like to help my team in the image processing part. How can I use the java vision library to process a local image (on my c drive) ?

I have modified the CameraTest project but when I execute “new RGBImage (filename)” then Netbeans wants to connect to the cRio and program fails.

Any clue to help me to work stand alone ? Thanks.

Ok - I will reply to myself as it may help others.
1- I downloaded and installed smartDashboard on my laptop (has a built-in camera).
2- In Netbeans, I created the following (from New Project but no Main Class - in other words it is a class - bot a package).

import edu.wpi.first.smartdashboard.camera.WPICameraExtension;
import edu.wpi.first.wpijavacv.WPIColorImage;
import edu.wpi.first.wpijavacv.WPIImage;

public class BasicCameraExtension extends WPICameraExtension {
@Override
public WPIImage processImage(WPIColorImage rawImage) {
return rawImage.getRedChannel();
}

}
3- Right click on project - select properties and add the following jar to the libraries (Compile):

C:\Program Files\SmartDashboard\extension\WPICameraExtension.jar
C:\Program Files\SmartDashboard\SmartDashboard.jar
C:\Program Files\SmartDashboard\extension\WPICameraExtension.jar

This should solve the problems with the imports.

4- Right click on the project and select BUILD
5- Copy the resulting jar file from folder NetBeansProjects\projectname\dist
to folder C:\Program Files\SmartDashboard\extensions (next to WPICameraExtension.jar)
6- Double click on C:\Program Files\SmartDashboard\SmartDashboard.jar, the smartdashboard should launch. Click on View\Add and select the class name of your program : BasicCameraExtension in this example.

You should see the red channel of the image taken by the laptop camera since this is what is returned.

Now…from this how to do image analysis is another story…

You can either work with OpenCV directly (using the Java interface that WPI has supplied), or you can use the WPIImage object’s method’s to do a lot of basic image processing.
Someone named Greg Granito wrote this code that my team has been riffing off of.

Awesome. I spent 2 days getting detection to work in openCV (the c++ version) and then I see this … Doh. This will help get me the rest of the way home though.

Thanks,

Paul

We are trying to use the WPIBinaryImage class as well for finding contour lines. It seems to work well. However, we are struggling to use it continuously.

To us it appears that there is a huge native memory leak (something inside the native code) when the findContours() method actually finds contour lines.

Could you tell me whether or not your code includes a line like the following (where findBin is instance of a WPIBinaryImage)?

WPIContour] contours = finalBin.findContours();

If so, do you see your process start to consume vast amounts of memory when continuously processing images from the camera (looking at its status using the Windows Task Manager)?

We’ve found that we can process a few images without issues, but if we try to run continuously (processing hundreds of frames), our process hits 1 GB of RAM usage and quickly continues to grow until memory is exhausted.

We don’t seem to hit the memory issue if we don’t use the findContours() method, or if the findContours() method doesn’t locate anything (memory usage only appears to explode when contours are found).

In any case, I’d be curious to know if you’ve run across this issue as well and if so how you worked around it.

Here’s our “do nothing” minimal vision processing code which demonstrates the memory leak using only the WPI classes and methods:

package com.techhounds.camera;
import edu.wpi.first.smartdashboard.camera.WPICameraExtension;
import edu.wpi.first.wpijavacv.WPIBinaryImage;
import edu.wpi.first.wpijavacv.WPIColorImage;
import edu.wpi.first.wpijavacv.WPIContour;
import edu.wpi.first.wpijavacv.WPIGrayscaleImage;
import edu.wpi.first.wpijavacv.WPIImage;

/**
 * A "trivial" SmartDashboard camera extension demonstrating the memory
 * leak issue in the
 *
 * @author pkb
 */
public class MemoryLeakExtension extends WPICameraExtension {

    @Override
    public WPIImage processImage(WPIColorImage colorImg) {

        // Pull out one color
        WPIGrayscaleImage grayImg = colorImg.getBlueChannel();

        // Reduce to black and white for contour tracing
        WPIBinaryImage binImg = grayImg.getThreshold(200);

        // Find the contours within the image (unfortunately memory
        // is leaked big time as soon as it starts finding contours)
        WPIContour] contours = binImg.findContours();

        // Let the dash board display the gray image
        return grayImg;
    }

}

Thanks,
Paul

It looks like the WPI guys are working on the memory leak in the WPIJavaCV.jar file (there were some commits yesterday - revision 213).

I was successful in downloading the latest source from the SmartDashboard project and rebuilding the WPIJavaCV.jar file. Things are running much better now. I’ve been able to process the same image over 250000 times using the WPI methods without running out of memory (which is a lot more than we’ll ever hope to get through in a single match).

In case anyone else is hitting this issue and wants to try an updated build of the WPIJavaCV.jar file I’ve attached a zip file containing what I’m currently running with. To use:

  1. Download the ZIP file
  2. Extract the WPIJavaCV.jar file from the ZIP file
  3. Rename your existing WPIJavaCV.jar file (in the SmartDashboard/extensions/lib directory).
  4. Copy the new WPIJavaCV.jar file into your SmarDashboard/extensions/lib/ directory (folder).

    WPIJavaCV-20120205.zip (12.8 KB)


    WPIJavaCV-20120205.zip (12.8 KB)