|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
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);
Code:
ByteBuffer buf = ByteBuffer.allocate(76800); |
|
#2
|
|||
|
|||
|
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
}
}
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;
}
|
|
#3
|
|||
|
|||
|
Re: Get image from USBCamera into an OpenCV Mat
That looks pretty good I'll check it out tonight.
|
|
#4
|
|||
|
|||
|
Re: Get image from USBCamera into an OpenCV Mat
Worked beautifully. Writing a white paper now.
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|