image processing using opencv - how to draw on the video

hello, how can I draw on the video in the smart dashboard ? I know how to draw with opencv (imgproc.line , for example) but I dont know how to draw on the video in the smart dashboard… (to draw, or to show the hsv filter, or any other thing)

how can I do that ?
Thanks in advance

Have you looked at the Intermediate Vision sample in the example projects? It does exactly that (though it draws a rectangle instead).

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



	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



 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



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?

You’re not releasing the temporary HSV and thresh Mats after you’re done with them. They use native memory that the JVM doesn’t know about, so they don’t get garbage collected like normal “pure” Java objects

I wrote it (I wrote 879 instead od just 89 (typo))

oh ok thanks, so how can I release the memory ?

Call the release() method on the OpenCV objects after you’re done with them. You may also be able to declare them before the loop and re-use them later.

You’re also creating new Points and Scalars when you don’t need to. They also use native memory. But since they never change, you can declare them before the loop and avoid memory problems