Image Threshold Always Returns Black Image - C++ / Vision / AXIS

We have been struggling to get hot goal detection working using vision processing. We have an AXIS camera mounted with green LED rings and looking at the live camera feed we see the tape light up nicely. We have gone through camera settings and in the code can dump the raw image file and we see an expected dulled green target in a very dark background.

No matter what we have tried we keep getting an all black image back from the threshold operation.

We originally had the camera settings “normal”, and that did the same thing (except produced a standard light image of the target).

We have messed around with all sorts of threshold values and code tweaks and nothing seems to work.

We are command-based / C++ (and a snapshot of the code section is below). It should basically be the reference code.

Some other items that we have tried - included changing to various sets of HSL and HSV settings (the reference code says one thing, the description online says another…), changed the ColorImage to HSLImage with no luck. The printf’s all go through so it’s running the threshold operation, but doesn’t give us anything useful.

Any ideas???:confused:


void AutonomousCommandN::Execute() {
	printf("Begin Auto Exe
");
	switch(state){
	case 0:
	{
		printf("Case 0:
");
	//	Scheduler::GetInstance()->AddCommand(new DriveForwardcommand(3.0));
		printf("Starting Autonomous");
		Scores *scores;
		TargetReport target;
		int verticalTargets[MAX_PARTICLES];
		int horizontalTargets[MAX_PARTICLES];
		int verticalTargetCount, horizontalTargetCount;
		Threshold threshold(104, 117, 221, 255, 139, 255);	//HSV threshold criteria, ranges are in that order ie. Hue is 60-100
		ParticleFilterCriteria2 criteria] = {
				{IMAQ_MT_AREA, AREA_MINIMUM, 65535, false, false}
		};												//Particle filter criteria, used to filter out small particles
		AxisCamera &camera = AxisCamera::GetInstance();	//To use the Axis camera uncomment this line

		ColorImage *image;
		printf("Now creating new image.");
		//image = new RGBImage("/testImage.jpg");		// get the sample image from the cRIO flash

		image = camera.GetImage();


		Scheduler::GetInstance()->AddCommand(new AutoDriveandLowerGroup());

		
		image->Write("rawI.bmp");
		//To get the images from the camera comment the line above and uncomment this one
		BinaryImage *thresholdImage = image->ThresholdHSL(threshold);	// get just the green target pixels
		thresholdImage->Write("/threshold.bmp");
		BinaryImage *filteredImage = thresholdImage->ParticleFilter(criteria, 1);	//Remove small particles
		filteredImage->Write("Filtered.bmp");

		vector<ParticleAnalysisReport> *reports = filteredImage->GetOrderedParticleAnalysisReports();  //get a particle analysis report for each particle

If the libraries work the same for C++ as they do for LabVIEW, a BinaryImage has values of 0 and 1. If you use a normal color palette to view it, the image will consist of pixels that are either truly black or so dark as to appear black. Change the palette to distinguish clearly between those two values and you’ll probably see what you expect.

In C++ when you write a binary image (eg: filtered.bmp) the 1 pixels show up as red, 0 as black.

A few comments:

The NI Vision Assistant tool is really helpful for this; it will allow you to open a raw image on your PC and build and tune your filtering process.

I’ve observed (but haven’t confirmed in code) that your first call to Camera::GetInstance() is what initiates your connection to the camera, however it takes some time before you can get an image; you should do this in DisabledInit to ensure that when auto begins, the camera has images ready. (we use a framerate of 5, so it could be due to this as well, but the delay seems > 1sec)

You don’t have a convex hull operation; you will want to do this to combine smaller particles before you get particle reports.

You can use Paint.net to inspect the HSV values, this can at least give you a rough idea of the hue and saturation ranges to use (since you are currently using an HSL filter). Keep in mind the numeric ranges for paint.net are different from those in the threshold
(ie paint.net H range is 0 to 360, S range is 0 to 100, but I believe threshold is 0 to 255 for each).

If you are using 320x240 (lower resolution than the default 640x480), the default value for #define AREA_MINIMUM is not appropriate, you need to divide that value by 4 since the resolution is halved in each direction.

You don’t show any delete operations in your code snippet, so the memory you use for each image is leaked/lost; not a major problem for one image, but it would be a problem if you run autonomous too many times. The following need to be deleted in if you are using the example code: image, thresholdImage, filteredImage, convexHullImage, reports, and scores. Scores should be deleted just before you leave the if condition where scores = New is called, filtered image and reports should be deleted at the end of your analysis, and the rest should be deleted after they are processed.

Hey - thanks everyone…that was all helpful…

DJScribbles - I had only posted the first bunch of code until the threshold operation knowing it wasn’t doing that, and figuring the rest didn’t matter (yet).

Our software guy will follow up with a more detailed analysis of what was wrong for everyone once we’re done with tomorrow ( he made the changes, I only hack at the software).

We were using paint.net previously, but didn’t realize the 0-100 vs 0-255. And the divide by 4 as well.

Thanks for all the help, again!