Go to Post I think it's important to keep FIRST's most important award as the top point getter [in Fantasy FIRST]. It's help reinforce that the Chairman's award, Engineering Inspiration and Woodie Flowers award are actually important and FIRST is about more than just the robots. - Koko Ed [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 03-02-2016, 21:24
rpappa rpappa is offline
Registered User
FRC #0340
 
Join Date: Feb 2016
Location: Rochester
Posts: 9
rpappa is an unknown quantity at this point
Get image from USBCamera into an OpenCV Mat

I've successfully put opencv on the Roborio and have tested to make sure it works (will do a write up on that soon™). However, I have run into the roadblock of the fact that the USBCamera class has no apparent way to get a straight up Java Image or BufferedImage or something easy to pass to Mat.put().

Here's my code which crashes:
Code:
        USBCamera cam = new USBCamera("cam0");
        cam.openCamera();
    	cam.startCapture();
    	ByteBuffer buf = ByteBuffer.allocate(76800);
    	cam.getImageData(buf);
    	matOriginal = new Mat();
    	matOriginal.put(0, 0, buf.array());
    	Imgcodecs.imwrite("output.png", matOriginal);
The line that crashes it is probably
Code:
ByteBuffer buf = ByteBuffer.allocate(76800);
But I'm not sure if that matters as I don't believe I am approaching this problem the right way. Any suggestions?
Reply With Quote
  #2   Spotlight this post!  
Unread 04-02-2016, 05:56
pblankenbaker pblankenbaker is offline
Registered User
FRC #0868
 
Join Date: Feb 2012
Location: Carmel, IN, USA
Posts: 108
pblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of light
Re: Get image from USBCamera into an OpenCV Mat

Have you treid using the VideoCapture class from the OpenCV Java libs to open the USB camera instead of the WPI USBCamera class?

Maybe something like:

Code:
VideoCapture vc = new VideoCapture();
vc.open(0); // Assuming USB camera is /dev/video0
if (vc.isOpened()) {
    vc.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, width);
    vc.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, height);
}

If you are able to open the camera this way, you should be able to grab your OpenCV Mat objects via:

Code:
if (vc.grab()) {
    Mat img = new Mat();
    if (vc.retrieve(img)) {
        // Do what you want with the image data in the img Mat object
    }
}
This is basically how we grab our OpenCV images on the driver station side (except we open the IP camera URL instead of the USB camera). If you compiled the OpenCV streaming support in your roboRIO build, I'm assuming it should work there as well.

If you then need to take the Mat and go back to a BufferedImage, here is a snippet of what we use:

Code:
	public BufferedImage toBufferedImage(Mat img) {
		if (img.type() == CvType.CV_8UC1) {
			return Conversion.cvGrayToBufferedImage(img);
		} else if (img.type() == CvType.CV_8UC3) {
			return Conversion.cvRgbToBufferedImage(img);
		}
                return null;
	}
Reply With Quote
  #3   Spotlight this post!  
Unread 04-02-2016, 06:28
rpappa rpappa is offline
Registered User
FRC #0340
 
Join Date: Feb 2016
Location: Rochester
Posts: 9
rpappa is an unknown quantity at this point
Re: Get image from USBCamera into an OpenCV Mat

That looks pretty good I'll check it out tonight.
Reply With Quote
  #4   Spotlight this post!  
Unread 04-02-2016, 20:12
rpappa rpappa is offline
Registered User
FRC #0340
 
Join Date: Feb 2016
Location: Rochester
Posts: 9
rpappa is an unknown quantity at this point
Re: Get image from USBCamera into an OpenCV Mat

Worked beautifully. Writing a white paper now.
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 12:37.

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