Creating Images from .raw files

Ask software engineering and SDK questions for developers working on Mac OS X, Windows or Linux.
  • Author
  • Message
Offline

Nirmal Ramadoss

  • Posts: 2
  • Joined: Thu Mar 30, 2017 3:55 pm

Creating Images from .raw files

PostWed Apr 26, 2017 3:19 pm

Dear all,

I am using Intensity Pro 4k card to capture frames from Lumix GH4 camera. I get a raw file when I use the Linux/Samples/bin/x86_64/Capture executable. I use the following code to convert into a image. The code and corresponding image generated is given below. I honestly don't understand what mistake I am making.

SDK version: 10.8.5

Code: Select all
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <fstream>
#include <cstring>

class IMG_MAT : public cv::Mat{
   public:
      IMG_MAT() : cv::Mat(2160, 3840, CV_8UC3) {}
};

std::vector<IMG_MAT> yuv2rgb(std::vector<float>& yuv_values, int no_of_frames){
   std::vector<IMG_MAT> images;
   uint loop_variable = 0;
   for(uint i=0; i<no_of_frames; i++){
      cv::Vec3b yuv;
      IMG_MAT image;
      for(uint y=0; y<2160; y++){
         for(uint x=0; x<3840; x++){
            if (x%2 == 0)
               yuv.val[0] = yuv_values[loop_variable+1];
            else
               yuv.val[0] = yuv_values[loop_variable+3];
            yuv.val[1] = yuv_values[loop_variable];
            yuv.val[2] = yuv_values[loop_variable+2];

            float b = (1.164f*(yuv.val[0]-16) + (2.018f * (yuv.val[1] - 128)));
            float g = (1.164f*(yuv.val[0]-16) - (0.813f * (yuv.val[2] - 128)) - (0.391f * (yuv.val[1] - 128)));
             float r = (1.164f*(yuv.val[0]-16) + (1.596f * (yuv.val[2] - 128)));
            
            //float r = yuv.val[0] + 1.13983*yuv.val[2];
            //float g = yuv.val[0] - 0.39465*yuv.val[1] - 0.5806*yuv.val[2];
            //float b = yuv.val[0] - 0.03211*yuv.val[1];

            //std::cout << r << " " << g << " " << b << std::endl;
   
            image.at<cv::Vec3b>(cv::Point(x, y)) = cv::Vec3b(int(std::min(255.f, std::max(b, 0.f))),   
                                             int(std::min(255.f, std::max(g, 0.f))),   
                                             int(std::min(255.f, std::max(r, 0.f))));
            if (x%2 == 0)
               loop_variable += 4;
         }
      }
      images.push_back(image);
   }
   return images;
}


int main(int argc, char** argv) {
   //code for reading the raw file
   std::ifstream video_file("video.raw", std::ios::binary);
   if(!video_file.is_open()){
      std::cout << "Error Opening file" << std::endl;
      return 1;
   }
   std::vector<char> yuv_byte_values((std::istreambuf_iterator<char>(video_file)),
                        (std::istreambuf_iterator<char>()));
   std::vector<float> yuv_values;
   for (auto& _byte : yuv_byte_values){
      char char_to_float[4];
      float yuv;
      for (uint i=0; i<4; i++)
         char_to_float[i] = _byte;
      std::memcpy(&yuv, &char_to_float, sizeof(yuv));
      yuv_values.push_back(yuv);
   }
   //for (auto& yuv : yuv_values)
   //   std::cout << yuv << std::endl;
   std::cout << yuv_values.size() << std::endl;
   int no_of_frames = yuv_values.size() / (2*2160*3840);

   std::vector<IMG_MAT> images = yuv2rgb(yuv_values, no_of_frames);
   std::cout << "yuv2rgb method executed successfully" << std::endl;
   std::cout << "no of images read:" << images.size() << std::endl;
   for (uint i=0; i<images.size(); i++){
      std::string image_name = "hello_" + std::to_string(i) + ".jpg";
      cv::imwrite(image_name, images[i]);
   }
   return 0;
}


The image is given below

Can someone please help me ?
Attachments
hello_0.png
hello_0.png (535.4 KiB) Viewed 404 times

Return to Software Developers

Who is online

Users browsing this forum: No registered users and 5 guests