In OpenCV , failed to create an RGB image from a Bayer binary file. [closed]

asked 2016-01-12 11:44:57 -0600

ocv_user gravatar image

updated 2016-01-13 14:21:13 -0600

I have a binary file containing Bayer image in 2-channel BGGR format to 3-channel RGB format. Every pixel is 10 bits.For that I am reading the binary file into byte[] array and then did some bit level manipulation with the byte[] to create an short[] array such that every cell contains 10 bit pixel. Now I need a way to convert the Bayer short[] array to a JPG image. For that, I am using OpenCV's Mat object and Imgproc.cvtColor.The code is as follows:

    Mat mat1=new Mat();
    mat1.create(1280, 1024, CvType.CV_16U);
    mat1.put(1280, 1024, charpix); // charpix is short[]

    Imgcodecs.imwrite("/../codec1.png", mat1); 

    Mat mat2=new Mat(1280,1024,CvType.CV_16U);

    Imgproc.cvtColor(mat1, mat2, Imgproc.COLOR_BayerBG2RGB);

    Imgcodecs.imwrite("/../codec2.png", mat2);

    short[] data1 = new short[mat2.rows() * mat2.cols() * (int)(mat2.elemSize())];

    mat2.get(0, 0, data1);

    BufferedImage bim=new BufferedImage(1024, 1280, BufferedImage.TYPE_USHORT_GRAY); 
    bim.getRaster().setDataElements(0, 0,1024,1280,data1);
    try {
        ImageIO.write(bim, "png", new File("/../new.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }

I get no errors but as an output I am only getting a BLACK image for all the 3 images which I am trying to create. 1/4 of each the 3 images shows some color(either gray/rgb depending on cvType/BufferedImageType I use) and the rest part of the images is BLACK. I see that mat1 is unable to store my short[] in it. Only some mat1 cells have data and rest have all 0s. What is the correct way to store short[] in the array.Any help would be greatly appreciated.

edit retag flag offensive reopen merge delete

Closed for the following reason duplicate question by ocv_user
close date 2016-01-14 03:50:05.152517