IMage Width is 0

Our team has been having some trouble with vision… big surprise. Like most people we are having trouble ‘getting’ the image because the width of the image that the robot is using is 0. This has happened in two drafts of code written by two different programmers, and we have tried writing the compression to 30.

Here is the first draft:

 
#include "WPILib.h"

				



double Target::GetHorizontalAngle()
{
	double x = m_xPos;
	
	x = atan2(x, 1+cos(28)*-1);
	x = x * 180.0 / 3.14159;
	return x;
}

vector<Target>* Target::FindRectangleTarget()
{
	AxisCamera &camera = AxisCamera::GetInstance("10.4.86.11");
	//AxisCamera &camera = AxisCamera::GetInstance("10.4.86.11");
	camera.WriteCompression(0);
	camera.WriteResolution(AxisCamera::kResolution_320x240);

	camera.WriteBrightness(0);

	
		
		
		
	//threshold default 100,100,100,100,100,100
	
		//camera.GetImage()->Write("binaryImage.jpg")		
	 BinaryImage *binaryimage = camera.GetImage()->ThresholdHSL(105,202,1,22,241,250);
	
	 
	 
	 imaqConvexHull(binaryimage->GetImaqImage(),binaryimage->GetImaqImage(),1);
	 printf("AfterConvex
");
	 vector<ParticleAnalysisReport> *results = binaryimage->GetOrderedParticleAnalysisReports();
	 delete binaryimage;
	 //results->at(0).center_mass_x_normalized;
	 vector<Target> *returnresults;
	 if(results->size()<0)
	 {
		 return returnresults;
	 }
	 
	 printf("BeforeFoorLoop
");
	 int b;
	 b= results->size();
	 for(int x=0;x<b;x++)
	 {
		 if(results->at(x).particleArea>1000) //normally 800
		 {
			 Target t;
			 t.m_xPos = results->at(x).center_mass_x_normalized;
			 t.m_ypos = results->at(x).center_mass_y_normalized;
			 returnresults->push_back(t);
		 }
	 }
	 
	 
	 delete results;
	 
	 return returnresults;
	 
}

and here is the second:


#include "CameraSubsystem.h"
#include "../Robotmap.h"
#include "nivision.h"
#include "Vision/MonoImage.h"
#include "Vision/BinaryImage.h"

#include <algorithm>
#include <math.h>
#include "WPILib.h"
#include "../Commands/CameraCommand.h"

CameraSubsystem::CameraSubsystem() : Subsystem("CameraSubsystem") {
	
}
    
void CameraSubsystem::InitDefaultCommand() {
	SetDefaultCommand(new CameraCommand);
	// Set the default command for a subsystem here.
	//SetDefaultCommand(new MySpecialCommand());
}
void CameraSubsystem::Track()
{
	AxisCamera &camera = AxisCamera::GetInstance("10.4.86.11");

		camera.WriteCompression(0);
		camera.WriteResolution(AxisCamera::kResolution_320x240);	
		camera.WriteBrightness(30);
	ColorImage *colorimage;
		
	colorimage = camera.GetImage();
	double size; 
	size = colorimage->GetWidth();
	if (size==0)//the size is zero
	{
		printf("size is zero");
	}
	BinaryImage *binaryimage = colorimage->ThresholdHSL(100,100,100,100,100,100);
}

and both have the same error: 0xBFF60406 in ColorImage.cpp line 37.

Any help would be greatly appreciated

I had this happen recently.
I did 2 things.
#1 Reset the camera to factory settings.
#2 Change the camera.WriteBrightness(0); to camera.WriteBrightness(20);

I see a few issues here.

  1. brightness needs to be set non-zero - 50 may even be a nice number. You can try different values by going to the webpage for the camera and experimenting but in the real deal, you’ll need to set this in the pits based upon regional lighting.

  2. Doing the AxisCamera::GetInstance() followed by setting settings for each image/trial is likely a bad thing for the camera. The settings are not sent to the camera right away as they are sent en-batch in a separate task. You’re MUCH more advised to do your GetInstance and settings at initialization time and simply use the instance you get back throughout your code in autonomous and teleop (or at least one for each). Also, you may want to do what many are doing…after setting the settings, do a Wait(3.0); (I’m not sure that 3 seconds is required, but that’s what I’ve seen a lot of and haven’t experimented to refute the number)

  3. You have a line where you get the binary image by using:

  BinaryImage *binaryimage = camera.GetImage()->ThresholdHSL(105,202,1,22,241,250);

There is a memory leak here. camera.GetImage() returns you a ColorImage* which IT ALLOCATED – it is your responsibility to delete it after you are done with it. So you’ll need a temp variable in there to allow for that.

  1. With the following code block I am seeing some issues…
	 vector<Target> *returnresults;
	 if(results->size()<0)
	 {
		 return returnresults;
	 }

a) ‘results->size() < 0’ … I don’t think this is even possible. Did you mean ‘results->size() <= 0’ which would check to see if there are NO PARTICLES ?
b) if you wind up in the if-block, you return ‘returnresults’ which is a pointer that is uninitialized. Gonna cause you issues. I’d suggest maybe prepping the caller that you might return NULL in this case and that NULL is their signal that you got nothing in terms of particles.

  1. Lastly - when you do return ‘returnresults’ to the caller, you must be careful and have the caller delete returnresults (and take care of each item in the vector<> deleting them along the way iteratively prior to deletion of the vector* itself).

bob