Go to Post I don't want a carbon copy of your wheels.. I want to improve upon your design. - Tom Bottiglieri [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 29-01-2017, 05:39
Noam Noam is offline
Registered User
None #3034 (galileo)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2015
Location: Israel
Posts: 14
Noam is an unknown quantity at this point
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
Reply With Quote
  #2   Spotlight this post!  
Unread 29-01-2017, 07:58
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

Have you looked at the Intermediate Vision sample in the example projects? It does exactly that (though it draws a rectangle instead).
Reply With Quote
  #3   Spotlight this post!  
Unread 30-01-2017, 01:36
Noam Noam is offline
Registered User
None #3034 (galileo)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2015
Location: Israel
Posts: 14
Noam is an unknown quantity at this point
Re: image processing using opencv - how to draw on the video

Quote:
Originally Posted by YairZiv View Post
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

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..
Reply With Quote
  #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
  #5   Spotlight this post!  
Unread 30-01-2017, 07:17
SamCarlberg's Avatar
SamCarlberg SamCarlberg is offline
GRIP, WPILib. 2084 alum
FRC #2084
Team Role: Mentor
 
Join Date: Nov 2015
Rookie Year: 2009
Location: MA
Posts: 161
SamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to behold
Re: image processing using opencv - how to draw on the video

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
__________________
WPILib
GRIP, RobotBuilder
Reply With Quote
  #6   Spotlight this post!  
Unread 30-01-2017, 09:14
Noam Noam is offline
Registered User
None #3034 (galileo)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2015
Location: Israel
Posts: 14
Noam is an unknown quantity at this point
Re: image processing using opencv - how to draw on the video

Quote:
Originally Posted by YairZiv View Post
What does line 89 in your Robot.java code?
I wrote it (I wrote 879 instead od just 89 (typo))

Quote:
Originally Posted by SamCarlberg View Post
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
oh ok thanks, so how can I release the memory ?
Reply With Quote
  #7   Spotlight this post!  
Unread 30-01-2017, 09:43
SamCarlberg's Avatar
SamCarlberg SamCarlberg is offline
GRIP, WPILib. 2084 alum
FRC #2084
Team Role: Mentor
 
Join Date: Nov 2015
Rookie Year: 2009
Location: MA
Posts: 161
SamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to behold
Re: image processing using opencv - how to draw on the video

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
__________________
WPILib
GRIP, RobotBuilder
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 13:16.

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