0

I have a array double dc[][] and want to convert this as to a IplImage* image and further to a video frame. What I had to do was I was given a video and I extracted out some features and then make a new video of the extracted features. My approach was I divided the video into frames extracted the features from each frame then did the updation like this and in each iteration of frame I get a new dc

double dc[48][44];
for(int i=0;i<48;i++)
{
  for(int j=0;j<44;j++)
  {
     dc[i][j]=max1[i][j]/(1+max2[i][j]);
  }
}

Now I need to save this dc in such a way that I can reconstruct the video.Anybody help me with this. Thanks in advance

2
  • 1
    If you're using the C++ opencv interface why not use Mat instead of IplImage? Commented May 15, 2013 at 21:39
  • I am fine with using Mat as well but I have not been able to convert this double to Mat and furthermore when saving into video I only know how to save a lpllmage and not Mat. Commented May 15, 2013 at 21:41

1 Answer 1

1

If you're okay with using Mat, then you can make a Mat for existing user-allocated memory. One of the Mat constructors has the signature:

Mat::Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP)

where the parameters are:

rows: the memory height, 
cols: the width, 
type: one of the OpenCV data types (e.g. CV_8UC3), 
data: pointer to your data, 
step: (optional) stride of your data

I'd encourage you to take a look at the documentation for Mat here

EDIT: Just to make things more concrete, here's an example of making a Mat from some user-allocated data

int main()
{
    //allocate and initialize your user-allocated memory
    const int nrows = 10;
    const int ncols = 10;
    double data[nrows][ncols];
    int vals = 0;
    for (int i = 0; i < nrows; i++)
    {
        for (int j = 0; j < ncols; j++)
        {
            data[i][j] = vals++;
        }
    }
    //make the Mat from the data (with default stride)
    cv::Mat cv_data(nrows, ncols, CV_64FC1, data);
    //print the Mat to see for yourself
    std::cout << cv_data << std::endl;
} 

You can save a Mat to a video file via the OpenCV VideoWriter class. You just need to create a VideoWriter, open a video file, and write your frames (as Mat). You can see an example of using VideoWriter here

Here's a short example of using the VideoWriter class:

//fill-in a name for your video 
const std::string filename = "...";
const double FPS = 30;
VideoWriter outputVideo;
//opens the output video file using an MPEG-1 codec, 30 frames per second, of size height x width and in color 
outputVideo.open(filename, CV_FOURCC('P','I','M,'1'), FPS, Size(height, width));

Mat frame;
//do things with the frame
// ...

//writes the frame out to the video file
outputVideo.write(frame);

The tricky part of the VideoWriter is the opening of the file, as you have a lot of options. You can see the names for different codecs here

Sign up to request clarification or add additional context in comments.

7 Comments

Actually the problem is I have a video I need to take out RBG do some feature extraction and make a video back of extracted features. Now can u help if I have obtained this cv::Mat how can Use this to form a image or a video.
@0908 I added in some code to my answer for using VideoWriter. I'd really advise for you to read the documentation for the class though (as well as the sample application), as there are a lot of parameters that will be specific to your application. Links to those are in the answer
once you've created and opened your VideoWriter object (e.g. if it's named out_vid, then you just do out_vid.write(cv_data);). Writing the Mat out to the video is the easy part, the hard(er) part is opening the video file
Use the isOpened() member function of VideoWriter to check if it opened correctly prior to writing. e.g. if(!out_vid.isOpened()) std::cerr << "File Not Opened" << std::endl;
OpenCV Error: Assertion failed (image->depth == 8) in unknown function, file ..\ ..\..\modules\highgui\src\cap_ffmpeg.cpp, line 230
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.