View Single Post
  #4   Spotlight this post!  
Unread 30-01-2017, 07:07
YairZiv's Avatar
YairZiv YairZiv is offline
Registered User
FRC #5951 (Makers Assemble)
Team Role: Programmer
 
Join Date: Oct 2016
Rookie Year: 2016
Location: Tel Aviv, Israel
Posts: 41
YairZiv is an unknown quantity at this point
Re: image processing using opencv - how to draw on the video

Quote:
Originally Posted by Noam View Post
I didnt saw this example, thanks

but if im trying to do more things - and not just a rectangle, so the robot crash after a few seconds (about 2-3 seconds) and the program starts again, and crash..

this is the code

Code:
	Thread visionThread;

	@Override
	public void robotInit() {
		visionThread = new Thread(() -> {
			UsbCamera camera = CameraServer.getInstance().startAutomaticCapture();
			camera.setResolution(640, 480);
			CvSink cvSink = CameraServer.getInstance().getVideo();
			CvSource outputStream = CameraServer.getInstance().putVideo("Rectangle", 640, 480);
			Mat mat = new Mat();
			while (!Thread.interrupted()) {
				if (cvSink.grabFrame(mat) == 0) {
					outputStream.notifyError(cvSink.getError());
					continue;
				}
				Mat hsv = new Mat();
				Mat thres = new Mat();
				Imgproc.cvtColor(mat, hsv, Imgproc.COLOR_RGB2HSV);
				Core.inRange(hsv,
						new Scalar(0, 0, 240),
						new Scalar(255, 255, 255),
						thres);
				Imgproc.rectangle(mat, new Point(100, 100), new Point(400, 400),
						new Scalar(255, 255, 255), 5);
				// outputStream.putFrame(mat);
				outputStream.putFrame(thres);
			}
		});
		visionThread.setDaemon(true);
		visionThread.start();
    }

and this is the error

Code:
 OpenCV Error: Insufficient memory (Failed to allocate 921600 bytes) in OutOfMemoryError, file /var/lib/jenkins/workspace/OpenCV-roborio/modules/core/src/alloc.cpp, line 52 
 OpenCV Error: Assertion failed (u != 0) in create, file /var/lib/jenkins/workspace/OpenCV-roborio/modules/core/src/matrix.cpp, line 424 
 Exception in thread "Thread-0" CvException [org.opencv.core.CvException: cv::Exception: /var/lib/jenkins/workspace/OpenCV-roborio/modules/core/src/matrix.cpp:424: error: (-215) u != 0 in function create 
 ] 
 	at org.opencv.imgproc.Imgproc.cvtColor_1(Native Method) 
 	at org.opencv.imgproc.Imgproc.cvtColor(Unknown Source) 
 	at org.usfirst.frc.team3034.robot.Robot.lambda$robotInit$0(Robot.java:89) 
 	at org.usfirst.frc.team3034.robot.Robot$$Lambda$1/13402185.run(Unknown Source) 
 	at java.lang.Thread.run(Thread.java:745) 
 terminate called after throwing an instance of 'std::system_error' 
   what():  Resource temporarily unavailable 
 ➔ Launching «'/usr/local/frc/JRE/bin/java' '-Djava.library.path=/usr/local/frc/lib/' '-jar' '/home/lvuser/FRCUserProgram.jar'» 
 ********** Robot program starting ********** 
 NT: server: client CONNECTED: 10.30.34.17 port 55027 
 Default IterativeRobot.disabledInit() method... Overload me! 
Warning  44003  FRC:  No robot code is currently running.  Driver Station 
 Default IterativeRobot.disabledPeriodic() method... Overload me! 
 Default IterativeRobot.robotPeriodic() method... Overload me! 
 CS: USB Camera 0: Connecting to USB camera on /dev/video0 
 CS: USB Camera 0: set format 1 res 160x120 
 CS: USB Camera 0: Connecting to USB camera on /dev/video0 
 CS: USB Camera 0: set format 1 res 640x480 
 CS: USB Camera 0: set FPS to 7

and line 879 is this

Code:
Imgproc.cvtColor(mat, hsv, Imgproc.COLOR_RGB2HSV);

you know why its happening and how to fix that ?
I thought that it maybe because its "too much" for the roborio, so the code is crashing (and starts again) - because thats happened to me a few days ago, when i tried to take a lot of photos, but i dont think that its the problem now..
What does line 89 in your Robot.java code?
Reply With Quote