convert an image (black & white) to a file (zero & one) in the VC++
-
Hello everyone I have a graduation project and I need to know how to convert an image (black & white) to a file (zero & one) in the VC++ and OpenCv ? Or in other words how can Read image as a File ?? Can anyone help me :/
You really need to give more input than that. What is the format of your 'image'? Is that a HBITMAP, or some other data structure? What format should the output file have? Raw/jpg/bmp/gif?
Shatha88 wrote:
Can anyone help me :/
Probably not, as it is right now.
-
Hello everyone I have a graduation project and I need to know how to convert an image (black & white) to a file (zero & one) in the VC++ and OpenCv ? Or in other words how can Read image as a File ?? Can anyone help me :/
Shatha88 wrote:
to a file (zero & one)
How do you think your image is stored on the hard disk ? You really think that on your hard disk there's a small image drawn there somewhere ? Your image is stored as any other file: a bunch of bits. Could you please elaborate your question ?
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
You really need to give more input than that. What is the format of your 'image'? Is that a HBITMAP, or some other data structure? What format should the output file have? Raw/jpg/bmp/gif?
Shatha88 wrote:
Can anyone help me :/
Probably not, as it is right now.
Well I want to convert the (JPG )image to (0`s & 1`s)text file and I used this code from internet and gave me this file, but not 0 and 1 also does not draw my picture :(
#include<iostream>
using namespace std;
#include<stdio.h>
#include<fstream>
#include <cv.h>
#include <highgui.h>
/*
Function takes OpenCV image as input and dumps its pixels to a file specified by filename in function arguments.
*/
int WritePixelsToFile(IplImage *img,const char *filename)
{
/*open a file in text mode with write permissions.*/
//FILE *file = fopen(filename, "wt");
ofstream file;
file.open (filename, ios::out);if(file==NULL)
{
/*If unable to open the specified file display error and return.*/
perror("Unable to open specified file");
return -1;
}
int i,j;
CvScalar s;
CvSize size=cvGetSize(img);
for(i=0;i< size.height;i++)
{
for(j=0;j< size.width;j++)
{
s=cvGet2D(img,i,j); // get the (i,j) pixel value
// fprintf (file,"%f\t",s.val[0]); // dump the (i,j) pixel value in file
file << s.val[0];
}
//fprintf(file,"\n");//write new line in file to seperate rows.
file <<"\n";
}
/*release the file pointer.*/
file.close();return 1;
}int main ()
{
//ofstream o("C:\\file1.txt");
char name []= "C:\\file1.txt";
IplImage *imgI = cvLoadImage("C:\\org.jpg");
IplImage *dst = cvCreateImage( cvSize( 30, 30 ), IPL_DEPTH_8U, 1 );
/* CV_RGB2GRAY: convert RGB image to grayscale */
cvCvtColor( imgI, dst, CV_RGB2GRAY );
IplImage *binary= cvCreateImage( cvSize( 30, 30 ), IPL_DEPTH_8U, 1 );
cvThreshold(dst, binary, 150, 255, CV_THRESH_BINARY);
cvSaveImage("C:\\ooo.jpg",binary);WritePixelsToFile(binary,name ); return 0;
}
the output will be " 255252253245254252254255241248255255255255255254255249255252255231254255246246255248254255 25323024725523525525522725524921014791656572100127220253217255230252255243242251253232 25524425525124725124925516365042480701035151232251254237255255244255243 25225524423825524321369067000451410200033186251247253255247254255 2532382452532322065221326102167185181182169831667141146255253226255237255 24825525524621000110121233255244249255247246255254142230016126255235249252243 2552552522191653325233244255255255254252252241255250236222681030187254243255251 25524425452218292482422542532402392552552542552462472522552307341217232255240252 24625515831102212