0

I am trying to build an application, which get data from webcam or external device, saves Video Frames into text file, then read frames from created text file.

I don't know whether it is a good idea to save to text file, I'm open suggestions.

So far I've done to saving to a text file.

My problem is reading from text file. Basically I read text line by line, but I don't know how to convert this text into Mat object.

So far my code is:

ifstream read_storage(new_vid_frm_path);
if(!read_storage.is_open()) {
    perror("\n\n\n\t\t\t(-)FAIL : Can't Open SavedVideoFrames.txt\n\n\n\t\t\t");
    return -1;
}

VideoWriter *vid = new VideoWriter(new_vid_frm_path,CV_FOURCC('P', 'I', 'M', '1'),30,Size(vc.get(CV_CAP_PROP_FRAME_WIDTH),vc.get(CV_CAP_PROP_FRAME_HEIGHT)));
Mat line;
vector<Mat> vid_frms;
while ( getline (read_storage,line) ) {
    cout << line << '\n';

}

read_storage.close();

if(vid_frms.size() == 0){
    printf("\n\n\n\t\t\t(-)FAIL: Error In Frame\n\n\n\t\t\t");
    return -1;
}

for(size_t i = 0; i<vid_frms.size(); i++)
    (*vid).write(vid_frms[i]);

printf("\n\n\n\t\t\t(+)SUCCESS: Video Processing Complete \n\n\n\t\t\t ");

Do you have ay suggestions how can I cast or convert this line string to Mat obejct?

while ( getline (read_storage,line) ) {
    cout << line << '\n';

}

Thanks.

By the way, I looked at this solution, but I couldn't understand. Convert a string of bytes to cv::mat

I couldn't find the byte type in c++ and I think there might be a direct conversion between String to Mat object.

Community
  • 1
  • 1
Ahmet
  • 7,527
  • 3
  • 23
  • 47

1 Answers1

2

You can save anything (just about) in OpenCV to a .xml or .yml text file and then read it back in using the OpenCV XML/YAML FileStorage methods.

I highly recomend this over using native C++ methods for file stuff.

It's specifically designed to handle all this legwork for you.

Jeremy Gamet
  • 165
  • 7
  • Instead of saving to the text, xml/yaml file , How about saving as image .png format? Then later it can be processed as video input frames. However if you try to create video during runtime, it slows down whole process. – Ahmet Jul 20 '16 at 09:36
  • I agree with @Ahmet Tavli and actually most of the time it's a lot faster not to save stuff this way, however within the spirit of the question itself, if there is some good reason you needed the text, there's no reason not to use the tools available. – Jeremy Gamet Jul 20 '16 at 18:41