filename problem with imwrite() function

asked 2018-03-18 01:46:40 -0500

Hello guys, I have a small problem with imwrite(). I am processing each frame of a video. And I wish to store the frame once I press S key. Here is my code for generating the string of fileName. But I can't save the image.

 char c = waitKey(1000 / 1);
 if (c == 's') {
    ostringstream out;
    out << "img" << cap.get(CAP_PROP_POS_FRAMES) << ".jpg" << endl;
    string fileName(out.str());
    cout << fileName << endl;
    imwrite(fileName, diffFrame);
}

But if I directly replace imwrite() with imwrite("img1.jpg", diffFrame), it works. I don't know why it happens. Is there anyone who knows? Thanks you.

edit retag flag offensive close merge delete

Comments

what is the exact problem ? `i can't is not precise enough.

did you check, what cap.get(CAP_PROP_POS_FRAMES) returns ? (is it supported for your device ?)

berak gravatar imageberak ( 2018-03-18 01:57:17 -0500 )edit
1

remove endl in out << "img" << cap.get(CAP_PROP_POS_FRAMES) << ".jpg" << endl;

jpg\n is not a valid extension file : nothing is saved

LBerger gravatar imageLBerger ( 2018-03-18 03:35:51 -0500 )edit

Oh, yes. I haven't noticed that endl. Thank you!!

mihawk2010 gravatar imagemihawk2010 ( 2018-03-18 03:39:18 -0500 )edit

@berak, sorry I didn't explain my problem clearly. Actually i passed in an invalid fileName to imwrite() function as @LBerger explains. Thank you all.

mihawk2010 gravatar imagemihawk2010 ( 2018-03-18 03:43:22 -0500 )edit