Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hey friends
I get start working with opencv using c programming
-firstly ,i convert image format greyscale pgm to binary lack and white
-i want getting only the data of image without the header? without changing the proprieties of the image
-if opencv have a function for that is good ;
but if it don't have so i think use 2 buffer one for header and the 2 for data after i save each one for a file (file from header and one for data)
but the probem here is after how i can return to the initial image (it means when i have file for data and file for data how i can additionnel to be i can open again the image)
nota : the code is only for how i convert greyscale format image to binary i wil after update it to be read ony the data

What I have tried:

C++
#include "stdafx.h"
#include <iostream>
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <stdio.h>
#include</iostream>


int _tmain(int argc, _TCHAR* argv[])
{
	//load image
	IplImage* im_gray = cvLoadImage("lena512.pgm",CV_LOAD_IMAGE_GRAYSCALE);
	//convert to grey
		//convert to binary 
	IplImage* im_bw = cvCreateImage(cvGetSize(im_gray),IPL_DEPTH_8U,1);
	cvThreshold(im_gray, im_bw, 128, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
	//save to disk
	cvSaveImage("image_bw.pbm",im_bw);
Posted
Updated 4-Jul-16 23:24pm
v2
Comments
yagami_md 13-Jun-16 6:16am    
you can show the header of the imageand the data here :
// show_pixel_image.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <string>
#include <conio.h>


void func()
{
FILE *pFile=fopen("image_bw.pbm","rb");
int c;
//int a=0;
do {
c = fgetc (pFile);
printf("int -- %d \n",c);
//printf ("int -- %d ",a);
//a++;
} while (c != EOF);
//printf ("a is ===%d",a);
fclose(pFile);
}


int _tmain(int argc, _TCHAR* argv[])
{
func();
getch();


}
yagami_md 13-Jun-16 8:35am    
what I need to do for save header alone and data alone with this programming?
// image_to_buffer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include < stdio.h>
#include <stdlib.h>
#include<conio.h>



int _tmain(int argc, _TCHAR* argv[])
{

FILE * pFile;
long lSize;
char * buffer;
size_t result;

pFile = fopen ("image_bw.pbm","r+b" );
if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
/*char c;
do {
c = fgetc (pFile);
printf ("%c",c);
} while (c != EOF);*/

// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);

// allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}

// copy the file into the buffer:
result = fread (buffer,1,lSize,pFile);
if (result != lSize) {fputs ("Reading error",stderr); exit (3);}

/* the whole file is now loaded in the memory buffer. */

// terminate
int i=0;


while (i < lSize)
{
printf("%02X",(int)buffer[i]);
i++;
}

fclose (pFile);
free (buffer);
getch();
}
yagami_md 14-Jun-16 7:18am    
answer please

1 solution

If you Google you would find this useful information about pgm headers. It says roughly that the pgm files starting with a format identifier aka "magic number" (P1-P7) which telling you the format type and after it some further details which is new line or white space delimited.

Normally the separation of header and data isnt needed. If you want to convert the image you should look for some library.

The link to some C# code in this Q&A might inspire you to your next steps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900