Gear Vision: Can only retrieve number of rectangles on first button push.

For the gear vision code, we have it set to detect how many rectangles there are (their may be three rectangles if the robot is at an angle and spring hook cuts in half one of the rectangles). We are using OpenCV for the vision processing. We have the code set to print to the console the number of rectangles with: System.out.println( gtbr.filterContoursOutput().size() ) (gtbr is the gear vision pipeline). The first time the button is pressed, it returns the number of rectangles, but each time after that it gives a value of zero. I’m not sure why this happens. Any ideas?

Here is our code that gets called in Command. First time through it prints out the correct number of contours. All subsequent calls print 0 as the size of the filterContours list.

Mat mat = new Mat();
if(cvSink.grabFrame(mat) == 0) {
} else {
	GearPipeline gtbr = new GearPipeline();
	System.out.println("size: " + gtbr.filterContoursOutput().size());

Thanks in advance.

Can’t help you without seeing the code.

Just added it above. If you need anything else just ask.


Is this in execute()?

Yes it is


There’s nothing glaringly wrong in the code you shared. You are creating a new Mat and new pipeline object every time execute is called (which is very expensive, and will cause memory issues). It may be something with cscore, but I’m not familiar enough with its internals to say anything definitively.

Something you can do pretty easily is compare the center-points of the three rectangles to find which two are the closest. Or you can try and find the two rectangles with the smallest area. Then basically create a new rectangle using the top left point of the top rectangle and the bottom right point of the bottom rectangle and you will have now combined the split target together. There are probably better ways to do this but this would be a very simple way of doing it.

Thanks for the replies.

@Rangel - we are good as to computing the resultant Rect. Our issue is filterContours is always empty after the first time the command is run.

We solved our Vision issues by moving all of the CameraServer initialization to initDefaultCommand in our USB and Axis camera subsystems.

It is not obvious to me why that would fix our issue.