Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

I have a binary file containing Bayer image in BGGR 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 int[] array such that every cell contains 10 bit pixel. Now I need a way to convert the Bayer int[] array to a JPG image. For that, I am using OpenCV's Mat object and Imgproc.cvtColor. The code is as follows: byte[] jpgByteArray = baos.toByteArray(); int[] pix=getPixel(jpgByteArray);

    Mat mat1=new Mat();
    mat1.create(1280, 1024, CvType.CV_32S);
    mat1.put(1280, 1024, pix);

    Mat conv1 = new Mat();
    mat1.convertTo(conv1, CvType.CV_16U);  // Imgproc works only on depth == CV_8U || depth == CV_16U  //|| depth == CV_32F.Hence this conversion.   

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

    Imgproc.cvtColor(conv1, mat2, Imgproc.COLOR_BayerBG2BGR);
    int[] data1 = new int[mat2.rows() * mat2.cols() * (int)(mat2.elemSize())];

    Mat conv2 = new Mat();
    mat2.convertTo(conv2, CvType.CV_32S);

    conv2.get(0, 0, data1);
    BufferedImage bim=new BufferedImage(1024, 1280, BufferedImage.TYPE_INT_BGR); 
    bim.getRaster().setDataElements(0, 0,1024,1280,data1);
    try {
        ImageIO.write(bim, "jpg", new File("..output.jpg"));
    } catch (IOException e) {
        e.printStackTrace();
    }

I get no errors but as an output I am only getting a pure BLACK image. Does anyone know where I am wrong ?

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

I have a binary file containing Bayer image in BGGR 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 int[] array such that every cell contains 10 bit pixel. Now I need a way to convert the Bayer int[] array to a JPG image. For that, I am using OpenCV's Mat object and Imgproc.cvtColor. The code is as follows: byte[] jpgByteArray = baos.toByteArray(); int[] pix=getPixel(jpgByteArray);

    Mat mat1=new Mat();
    mat1.create(1280, 1024, CvType.CV_32S);
    mat1.put(1280, 1024, pix);

    Mat conv1 = new Mat();
    mat1.convertTo(conv1, CvType.CV_16U);  // Imgproc works only on depth == CV_8U || depth == CV_16U  //|| depth == CV_32F.Hence this conversion.   

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

    Imgproc.cvtColor(conv1, mat2, Imgproc.COLOR_BayerBG2BGR);
    int[] data1 = new int[mat2.rows() * mat2.cols() * (int)(mat2.elemSize())];

    Mat conv2 = new Mat();
    mat2.convertTo(conv2, CvType.CV_32S);

    conv2.get(0, 0, data1);
    BufferedImage bim=new BufferedImage(1024, 1280, BufferedImage.TYPE_INT_BGR); 
    bim.getRaster().setDataElements(0, 0,1024,1280,data1);
    try {
        ImageIO.write(bim, "jpg", new File("..output.jpg"));
    } catch (IOException e) {
        e.printStackTrace();
    }

I get no errors but as an output I am only getting a pure BLACK image. image.I have tried using TYPE_INT_ARGB and used other image formats like PNG for ImageIO. But I get same results. Does anyone know where I am wrong ?? Any help would be greatly appreciated.

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

I have a binary file containing Bayer image in BGGR 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 int[] array such that every cell contains 10 bit pixel. Now I need a way to convert the Bayer int[] array to a JPG image. For that, I am using OpenCV's Mat object and Imgproc.cvtColor. The code is as follows: byte[] jpgByteArray = baos.toByteArray(); int[] pix=getPixel(jpgByteArray);

    Mat mat1=new Mat();
    mat1.create(1280, 1024, CvType.CV_32S);
    mat1.put(1280, 1024, pix);

    Mat conv1 = new Mat();
    mat1.convertTo(conv1, CvType.CV_16U);  // Imgproc works only on depth == CV_8U || depth == CV_16U  //|| depth == CV_32F.Hence this conversion.   

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

    Imgproc.cvtColor(conv1, mat2, Imgproc.COLOR_BayerBG2BGR);
    int[] data1 = new int[mat2.rows() * mat2.cols() * (int)(mat2.elemSize())];

    Mat conv2 = new Mat();
    mat2.convertTo(conv2, CvType.CV_32S);

    conv2.get(0, 0, data1);
    BufferedImage bim=new BufferedImage(1024, 1280, BufferedImage.TYPE_INT_BGR); 
    bim.getRaster().setDataElements(0, 0,1024,1280,data1);
    try {
        ImageIO.write(bim, "jpg", new File("..output.jpg"));
    } catch (IOException e) {
        e.printStackTrace();
    }

I get no errors but as an output I am only getting a pure BLACK image.I have tried using TYPE_INT_ARGB image. The int[] array data1 seems to be empty and used other image formats like PNG for ImageIO. But I get same results. Does that Imgproc is not doing the desired conversion.Does anyone know where I am wrong ? Any help would be greatly appreciated.

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

I have a binary file containing Bayer image in BGGR 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 int[] short[] array such that every cell contains 10 bit pixel. Now I need a way to convert the Bayer int[] short[] array to a JPG image. For that, I am using OpenCV's Mat object and Imgproc.cvtColor. The code is as follows: byte[] jpgByteArray = baos.toByteArray(); int[] pix=getPixel(jpgByteArray);follows:

    Mat mat1=new Mat();
    mat1.create(1280, 1024, CvType.CV_32S);
CvType.CV_16U);
    mat1.put(1280, 1024, pix);

    Mat conv1 = new Mat();
    mat1.convertTo(conv1, CvType.CV_16U); charpix);                              // Imgproc works only on depth == CV_8U || depth == CV_16U  //|| depth == CV_32F.Hence this conversion.   charpix is short[]

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

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

    Imgproc.cvtColor(conv1, Imgproc.cvtColor(mat1, mat2, Imgproc.COLOR_BayerBG2BGR);
    int[] Imgproc.COLOR_BayerBG2RGB);

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

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

    Mat conv2 = new Mat();
    mat2.convertTo(conv2, CvType.CV_32S);

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

I get no errors but as an output I am only getting a pure BLACK image. The int[] array data1 seems to be empty 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 that Imgproc the rest part of the images is BLACK.Imgproc is not doing the desired conversion.Does conversion.Am I using Imgcodecs the right way? Does anyone know where I am may be wrong ? Any help would be greatly appreciated.

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

I have a binary file containing Bayer image in 2-channel BGGR 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.Imgproc is not doing the desired conversion.Am I using Imgcodecs the right way? Does anyone know where I may be wrong ? Any help would be greatly appreciated.

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

I have a binary file containing Bayer image in 2-channel BGGR format to 3-channel RGB format. Every pixel is 10 bits. For 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 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.Imgproc is not doing the desired conversion.Am I using Imgcodecs the right way? Does anyone know where I may be wrong ? Any 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.