Everytime we try and run binaryImage = camera.GetImage()->ThresholdHSL(huemin,huemax, saturationmin…) we get an ImaqThreshold error: -1074396154 in the driver station. Is it beause of the image that its receiving from the camera or the threshold itself and how can we solve it?
I can’t say for sure without seeing your code, but my guess is that you aren’t deleting your binary image (and color image if your calling new for it), and so you end up running out of memory pretty quickly and the threshold function cannot allocate memory for new binary images.
You guys have to be careful on how you write this code. When in doubt check the source code in the WPI library. For example, the AxisCamera::GetImage() code is:
HSLImage* AxisCamera::GetImage()
{
HSLImage *image = new HSLImage();
GetImage(image);
return image;
}
Notice that this function instantiated an HSLImage object. Since your code is directly using the returned HSLImage object to call its ThresholdHSL method and not saving the HSLImage pointer at all. This means you never free the HSLImage object. Instead, your code should look like this:
HSLImage *cameraImage = camera.GetImage();
BinaryImage *binaryImage = cameraImage->ThresholdHSL(.....);
...
...
delete binaryImage;
delete cameraImage;
The rule is: every new must be matched with a delete. So when calling a method that returns a pointer, you need to ask yourself that where that pointer comes from? If it is just returning a reference (such as GetInstance) and somebody is responsible for freeing it, you are fine. Otherwise, if the method created the object and did not keep a copy of it, you are responsible for freeing the object after you are done with it.
Regarding your error, the above probably doesn’t address that. It is probably related to how you created the camera object. So you need to post the code surrounding it too.
Aside from posting code, there are some generally good things to do which will also help diagnose…
a) After getting an image from GetImage, check to see if the pointer is non-null.
b) If it’s non-null, check that image->GetHeight() and image->GetWidth() are non-zero. I’ve seen other threads where zero sized images come back if the brightness is zero and things are rather dark.
c) Use GetError() to get the error back from WPILib and validate the reason via Code and Description.
in pseudo-code from the office…
while (IsOperatorControl())
{
HSLImage* image = camera.GetImage();
if (image)
{
if (image->GetHeight() && image->GetWidth())
{
// Now go forward with using image->Threshold operations
}
else
printf("image has zero width or zero height. Hmm
");
delete image;
}
else
printf("NULL Image returned. Hmm.
");
}
bob
According to the WPI source code, I believe it is impossible to get a NULL image pointer unless you are totally running out of memory. However, it is always a good practice to check for NULL anyway to guard against unforeseen circumstances. But it is true that you should check for zero image height and width because the camera needs some time to boot and you could “acquire” an image while the camera is not ready. Although you get a valid image pointer, there is really no image yet.
Interestingly, I am trying to figure out what happened to GetHeight() and GetWidth if the camera is not ready. According to the source code:
/**
* Gets the height of an image.
* @return The height of the image in pixels.
*/
int ImageBase::GetHeight()
{
int height;
imaqGetImageSize(m_imaqImage, NULL, &height);
return height;
}
/**
* Gets the width of an image.
* @return The width of the image in pixels.
*/
int ImageBase::GetWidth()
{
int width;
imaqGetImageSize(m_imaqImage, &width, NULL);
return width;
}
Both height and width are uninitialized local variables and imaqGetImageSize could have failed. Since there is no source code to the nivision module so I can’t tell if width and height will be zero initialized and both the GetHeight and GetWidth function do not check the return code of imaqGetImageSize.
MikeTS,
Two items –
-
while it is true that GetImage should not fail, the memory (out of memory) situation is exactly why you SHOULD check it.
-
Regarding the height/width item…I totally agree. I think the right thing to do would be to file a bug report on WPILib so that they could do “int height=0;” or “int height=-1;” at declaration in case the imaq call fails.
bob