convert pgm to pbm format?

asked 2016-06-10 09:02:55 -0600

yagami_md gravatar image

hey friends I need to convert pgm format to pbm format , I need to read the image in binary just 0 or 1 black or white , I tried to convert in C visual studio 2010

edit retag flag offensive close merge delete

Comments

"I tried to convert in C" -- let's see it !

berak gravatar imageberak ( 2016-06-10 09:36:45 -0600 )edit

include <stdio.h>

#include <io.h>
#include <ctype.h>

// ...

FILE *pFile = fopen("result.pbm", "rb");
// NOTE: With Microsoft compilers these functions might require a 
//  leading under score (_filelength, _fileno)
long file_len = filelength(fileno(pFile));
unsigned char *buffer = (unsigned char *)malloc(file_len);
fread(buffer, 1, file_len, pFile);
fclose(pFile);

const char *header = (const char *)buffer;

// Skip magic number "P4"
while (isalnum(*header)) header++;
// Skip white spaces
while (isspace(*header)) header++;
// Get width
int width = atoi(header);
// Skip width
while (isdigit(*header)) header++;
// Skip whitespace
while (isspace(*header)) header++;
int height = atoi(header);
// Skip height
while (isdigit(*header)) header++;
// Skip single whitespace
header++;

// Point
yagami_md gravatar imageyagami_md ( 2016-06-10 18:57:03 -0600 )edit

please answer me ?,

yagami_md gravatar imageyagami_md ( 2016-06-11 07:36:45 -0600 )edit

please, put the code into your question , not a comment (it's incomplete now) then - * do you get any errors ? * does it fail ? how so ?

last, none of your problems are related to opencv, so it's all kinda off-topic (though i'd personally go the same way for a simple job like this)

berak gravatar imageberak ( 2016-06-11 07:41:10 -0600 )edit

did you try like

Mat src =imread("filename.pgm");
imwrite("filename.pbm",src);
sturkmen gravatar imagesturkmen ( 2016-06-17 09:17:40 -0600 )edit