View Single Post
  #1   Spotlight this post!  
Unread 28-01-2015, 20:21
JML's Avatar
JML JML is offline
Jlyon
AKA: Jlyon
FRC #3044 (0xBe4)
Team Role: Programmer
 
Join Date: Oct 2012
Rookie Year: 2012
Location: 3044
Posts: 17
JML is an unknown quantity at this point
OpenCV USBCamera convert to Mat

Our team is trying to do Driver Station image processing from a Microsoft lifecam HD-3000 HD USB camera which is plugged into the robot. We began working with the camera connected to the laptop, converting to and from a BufferedImage with

Code:
public static Mat bufferedImageToMat(BufferedImage i){
    byte[] pixels = ((DataBufferByte) i.getRaster().getDataBuffer()).getData();
    Mat img = new Mat(i.getWidth(),i.getHeight(), CvType.CV_8UC3);
    img.put(0, 0, pixels);

    return img;
}
public static BufferedImage matToBufferedImage(Mat matrix) {  
    int cols = matrix.cols();  
    int rows = matrix.rows();  
    int elemSize = (int)matrix.elemSize(); 

    byte[] data = new byte[cols * rows * elemSize];  
    int type;  
    matrix.get(0, 0, data);  

    type = BufferedImage.TYPE_3BYTE_BGR;  
    // bgr to rgb  
    byte b;  

    for(int i=0; i<data.length; i=i+3) {  

        if(i < data.length - 3){
            b = data[i];  
            data[i] = data[i+2];
            data[i+2] = b;  
        }

    }  

    BufferedImage image2 = new BufferedImage(cols, rows, type); 
    try{
    image2.getRaster().setDataElements(0, 0, cols, rows, data);  
    }catch(Exception e){

    }
    return image2;      
}
The BufferedImage is of type BufferedImage.TYPE_3BYTE_BGR and the code works perfectly when the camera is plugged into the laptop. The problem arises when we are using the camera plugged into the robot. The image is acquired using a modified version of the Smart Dashboard USB Camera Viewer Class included in the java wpilib. When we try to convert the buffered image to a mat, and back to a buffered image (to simulate "Processing") the image has lines throughout it, and is distorted and unusable.

Any assistance would be greatly appreciated.