Go to Post Re: New to programming, any tips? Join the mechanical team :) - IndySam [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 04-02-2017, 08:45
mrada6 mrada6 is offline
Registered User
FRC #5684
 
Join Date: Jan 2017
Location: New Jersey
Posts: 3
mrada6 is an unknown quantity at this point
OpenCV in java

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"

Code:
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();
Code:
	/**
	 * 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;
	}
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 09:44.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi