Issue with multiple imwrite(".pgm") format in loop

asked 2015-07-15 07:50:46 -0600

deepthpk gravatar image

updated 2017-09-11 15:18:24 -0600

While saving an image capture as PGM format in a loop, the opencv library crashes on second iteration. The function which crashes is imwrite("out.pgm", gray_image, compression_params). Is there a workaround to solve this?

Thanks Deepth

edit retag flag offensive close merge delete

Comments

Are you saving the image with same name in all iterations? It may cause problems if trying to overwrite an existing file. Moreover, why would you want to overwrite a file inside a loop? You'll end up with only the last image

LorenaGdL gravatar imageLorenaGdL ( 2015-07-15 07:56:26 -0600 )edit

Thank you Lorena for the quick reply. Yes I am overwriting the image in every iteration. It is a limitation with third party application, which access the image with same name and process it.

deepthpk gravatar imagedeepthpk ( 2015-07-15 07:58:59 -0600 )edit

for ( unsigned int i = 0; i < loops; ++i ) bS = cap.read(frame);

if (!bS) //if not success, break loop
{

    return -1;
}

    /*Grayscale converson*/

cvtColor(frame, gray_image, CV_BGR2GRAY);

imwrite("sample.pgm", gray_image, compression_params);

}

deepthpk gravatar imagedeepthpk ( 2015-07-15 08:02:54 -0600 )edit
1

@deepthpk what are you using as compression_params? Have you tried without them? Do you really need them?

LorenaGdL gravatar imageLorenaGdL ( 2015-07-15 08:09:29 -0600 )edit

I tried compression_params.push_back(CV_IMWRITE_PXM_BINARY); compression_params.push_back(1); both 1 and 0. Doesnt make a difference in crash

deepthpk gravatar imagedeepthpk ( 2015-07-15 08:17:36 -0600 )edit

Problem is probably in that "push_back". Why don't you just try imwrite("sample.pgm", gray_image); and see if it works? Then if you really need to specify the flags for any reason, we'll work about it

LorenaGdL gravatar imageLorenaGdL ( 2015-07-15 08:19:05 -0600 )edit

when i removed the compression_params as you mentioned, the program crashed on first iteration itself! imwrite("sample.pgm", gray_image);

opencv_world300.dll!01cf8aa0()  Unknown
deepthpk gravatar imagedeepthpk ( 2015-07-15 08:34:20 -0600 )edit

Interesting... I guess you've manually compile OpenCV with CMake and enabled the world dll. Are you really using it? I don't know a lot about the dll's, maybe someone else can help here

LorenaGdL gravatar imageLorenaGdL ( 2015-07-15 08:51:41 -0600 )edit

No, i just took the library as it is. I didnt compile. The error occurs in the function call to opencv_world300.dll. But thanks a lot for looking into this!

deepthpk gravatar imagedeepthpk ( 2015-07-15 09:56:59 -0600 )edit

But still what do you think the issue can be, if its not the issue about dll. I have extra information: If u save the out file with PNG format,then the crash never happens in any iteration. The issue exists only in PGM format. Unfortunately the thrid party application only takes pgm format.

deepthpk gravatar imagedeepthpk ( 2015-07-15 10:00:56 -0600 )edit