imwrite doesn't output correct PBM file format in the header.

asked 2013-10-28 20:12:31 -0500

tomriddle gravatar image

I need opencv to write PBM file which the header should be 'P4' described in this web site.

However I checked OpenCV 2.4.6.1's source code grfmt_pxm.cpp

sprintf( buffer, "P%c\n%d %d\n%d\n",
             '2' + (channels > 1 ? 1 : 0) + (isBinary ? 3 : 0),
             width, height, (1 << depth) - 1 );

So I can see it can never be '4' after 'P'. So I'm asking here whether OpenCV support PBM file format, how should I write the file.

Currently I write my file like this,

Mat outputmask(mask.size(), CV_8UC1, Scalar::all(1)) ;
outputmask = outputmask-mask ;
imwrite( pathAndName + "_mask" + ".pbm", outputmask , write_params );

outputmask is all 0 or 1, but it's CV_8UC1 datatype, since OpenCV doesn't support binary. write_params is a vector only contains CV_IMWRITE_PXM_BINARY. This block outputs 'P5' in the PBM file's header which is wrong, because 'P5' is PGM file's header.

edit retag flag offensive close merge delete