Hye everyone I am a first year programmer in FIRST. we are trying to do some vision programing this year. I have the following thread that should perform an RBG max. and then find all the convex hulls. This part works. I then want to draw a rectangle around each of the convex hulls it finds. This is where the code bugs up saying “to many instances of camera”
new Thread(() -> {
UsbCamera camera = CameraServer.getInstance().startAutomaticCapture();
camera.setResolution(640, 480);
camera.setExposureManual(-100);
camera.setBrightness(15);
CvSink cvSink = CameraServer.getInstance().getVideo();
CvSource outputStream = CameraServer.getInstance().putVideo("Radaszkiewicz", IMG_WIDTH, IMG_HEIGHT);
Mat source = new Mat();
Mat output = new Mat();
double] red = {0,255};
double] green = {40,255};
double] blue = {0,255};
ArrayList<MatOfPoint> findContoursOutput = new ArrayList<MatOfPoint>();
boolean externalOnly=false;
Mat hierarchy = new Mat();
MatOfInt hull = new MatOfInt();
ArrayList<MatOfPoint> convexHullsOutput = new ArrayList<MatOfPoint>();
ArrayList<Point> points = new ArrayList<Point>();
while(!Thread.interrupted()) {
cvSink.grabFrame(source);
Imgproc.cvtColor(source, output, Imgproc.COLOR_BGR2RGB);
Core.inRange(source, new Scalar(red[0], green[0], blue[0]),
new Scalar(red[1], green[1], blue[1]), output);
int mode;
if (externalOnly) {
mode = Imgproc.RETR_EXTERNAL;
}
else {
mode = Imgproc.RETR_LIST;
}
int method = Imgproc.CHAIN_APPROX_SIMPLE;
findContoursOutput.clear();
Imgproc.findContours(output, findContoursOutput, hierarchy, mode, method);
convexHullsOutput.clear();
for (int i = 0; i < findContoursOutput.size(); i++) {
final MatOfPoint contour = findContoursOutput.get(i);
final MatOfPoint mopHull = new MatOfPoint();
Imgproc.convexHull(contour, hull);
mopHull.create((int) hull.size().height, 1, CvType.CV_32SC2);
for (int j = 0; j < hull.size().height; j++) {
int index = (int) hull.get(j, 0)[0];
double] point = new double] {contour.get(index, 0)[0], contour.get(index, 0)[1]};
mopHull.put(j, 0, point);
}
convexHullsOutput.add(mopHull);
}
points.clear();
for(int i=0;i<convexHullsOutput.size();i++)
{
Point] pointsTemp = convexHullsOutput.get(i).toArray();
//System.out.println("Ratio is: " + getRatio(pointsTemp));
for(int q=0;q<pointsTemp.length;q++)
{
points.add(pointsTemp[q]);
}
int maxXTemp = getMax(points,0);
int maxYTemp = getMax(points,1);
int minXTemp = getMin(points,0);
int minYTemp = getMin(points,1);
Imgproc.rectangle(source, new Point(minXTemp, minYTemp), new Point(maxXTemp, maxYTemp),
new Scalar(0, 0, 255), 5);
}
//error correcting. find the center of each convex hull, then find the midpoint of the two centers.
int maxX = getMax(points,0);
int maxY = getMax(points,1);
int minX = getMin(points,0);
int minY = getMin(points,1);
int middleX = (maxX+minX)/2;
int middleY = (maxY+minY)/2;
pegX=middleX;
pegY=middleY;
SmartDashboard.putNumber("PegX",pegX);
SmartDashboard.putNumber("pegY", pegY);
int minRecX = middleX-5;
int maxRecX = middleX+5;
int minRecY = middleY-5;
int maxRecY = middleY+5;
sizeOfTarget=maxRecX-minRecX;
// Put a rectangle on the image
Imgproc.rectangle(source, new Point(minRecX, minRecY), new Point(maxRecX, maxRecY),
new Scalar(255, 0, 0), 5);
// Give the output stream a new image to display
outputStream.putFrame(source);
}
}).start();
/**
* This function is called periodically during test mode
*/
@Override
public void testPeriodic() {
}
public int getMax(ArrayList<Point> listOfPoints, int z)
{
int max=Integer.MIN_VALUE;
for(int i=0;i<listOfPoints.size();i++)
{
int temp=0;
if(z==0)
{
temp=(int) listOfPoints.get(i).x;
}
else
{
temp=(int) listOfPoints.get(i).y;
}
if(temp>max)
max=temp;
}
return max;
}
public int getMin(ArrayList<Point> listOfPoints, int z)
{
int min=Integer.MAX_VALUE;
for(int i=0;i<listOfPoints.size();i++)
{
int temp=0;
if(z==0)
{
temp=(int) listOfPoints.get(i).x;
}
else
{
temp=(int) listOfPoints.get(i).y;
}
if(temp<min)
min=temp;
}
return min;
}