WPILib vision error in ColorImage.cpp and BinaryImage.cpp

Hi yall,
I am playing around with WPILib for vision and all I am doing is thresholding a colorimage acquired from the axis camera into a binaryimage and then getting a particleanalysisreport vector from that image. Then, I simply find out the center of mass of the two particles.

All I am doing is calling for print statements for NumOfParticles(), IsTargetFound(), and CenterOfMass() in my autonomous code (Functions defined below).

All of these are outputting exactly what I expect, but, after about 5 seconds, errors alternate between
“Error on line 37 of ColorImage.cpp: 0: ImaqThreshold error” and
“Error on line 30 of BinaryImage.cpp: 0: Error counting particles”.

Then, autonomous stops and driver station shows “No Robot Code”.

Here’s the code of my Vision class:

#pragma once

#include "WPILib.h"
#include <vector>

namespace Subsystems {
	class Vision {
		AxisCamera* axis;
		ColorImage* ci;
		BinaryImage* bi;
		std::vector<ParticleAnalysisReport>* particles;
		void GetImage();

                int NumOfParticles();
		bool IsTargetFound();
		int CenterOfMass();

#include "Vision.h"

namespace Subsystems {

	Vision::Vision() {                    //note the vector "particles" is not initialized. I didn't find a need to
		axis = new AxisCamera("");
		ci = new ColorImage(ImageType::IMAQ_IMAGE_RGB);
		bi = new BinaryImage();
	 Vision::~Vision() {
		delete axis;
		delete ci;
		 delete bi;

	//private functions
	void Vision::GetImage() {
		ci = axis->GetImage();
		bi = ci->ThresholdRGB(0,255,175,255,0,255);	//use green LED


	//public functions
	int Vision::NumOfParticles() {
		return bi->GetNumberParticles();
	bool Vision::IsTargetFound() {
		if(NumOfParticles()==2) return true;
		return false;
	int Vision::CenterOfMass() {
		if(!IsTargetFound()) return false;
		particles = bi->GetOrderedParticleAnalysisReports(); //this is a pointer to a vector

		int center_mass = ((*particles)[0].center_mass_x+ (*particles)[1].center_mass_x)/2;
				//the center of mass of the two vision targets
		particles->clear();			//clear vector
		return center_mass;

Why do you think the program just stops like that with these strange errors?

Error on line 37 of ColorImage.cpp: 0: ImaqThreshold error
at /home/lvuser/FRCUserProgram() [0x1a8e8]
at /home/lvuser/FRCUserProgram() [0x1a964]
at /home/lvuser/FRCUserProgram() [0x1084c]
at /home/lvuser/FRCUserProgram() [0x10878]
at /home/lvuser/FRCUserProgram() [0x108b8]
at /home/lvuser/FRCUserProgram() [0x11124]
at /home/lvuser/FRCUserProgram() [0x1a350]
at /home/lvuser/FRCUserProgram() [0x11570]
at /home/lvuser/FRCUserProgram() [0x1057c]
at __libc_start_main

Error on line 30 of BinaryImage.cpp: 0: Error counting particles
at /home/lvuser/FRCUserProgram() [0x1b694]
at /home/lvuser/FRCUserProgram() [0x10888]
at /home/lvuser/FRCUserProgram() [0x108b8]
at /home/lvuser/FRCUserProgram() [0x11124]
at /home/lvuser/FRCUserProgram() [0x1a350]
at /home/lvuser/FRCUserProgram() [0x11570]
at /home/lvuser/FRCUserProgram() [0x1057c]
at __libc_start_main

Here is line 37 of the ColorImage.cpp

 BinaryImage * ColorImage::ComputeThreshold(ColorMode colorMode,
                 int low1, int high1,
                 int low2, int high2,
                 int low3, int high3)
         BinaryImage *result = new BinaryImage();
         Range range1 = {low1, high1},
                 range2 = {low2, high2},
                 range3 = {low3, high3};
         int success = imaqColorThreshold(result->GetImaqImage(),   
m_imaqImage, 1, colorMode, &range1, &range2, &range3);

         wpi_imaqAssert(success, "ImaqThreshold error"); 
//here's the assert that's producing the error. What can I do to fix this?
         return result;

Note that an assert is failing. What can I do to rectify this on my end?

That’s an obnoxious assert.

But the problem is likely one of two things, you end up running out of memory or you are passing in an image of zero size (likely the second one). Check that your image has a size before you do any operations on it.