View Single Post
  #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: 103
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