Reputation: 1023
I have opened a .txt
file many times using the C language's file handling facilities. But when I try to open an image file using the same procedure as for text files, I just can't do that.
I even tried this by opening the image file in binary mode "rb"
.
This is the code I'm using:
#include "file.h"
#include "stdio.h"
main()
{
FILE *fp;
char ch;
fp = fopen("D:\\setups\\tcc\\Bluehills.bmp", "rb+");
if(fp == NULL)
{
printf("Error in opening the image");
fclose(fp);
exit(0);
}
printf("Successfully opened the image file");
while((ch = fgetc(fp)) != EOF)
{
printf("%c", ch);
}
printf("\nWriting to o/p completed");
}
What do I need to modify to get the image as it is? As I'm directing the image output to the DOS window at least a monochrome pixel image must come up.
Upvotes: 4
Views: 41188
Reputation: 1212
The Opencv community offers some routines to load image, access the values of the image. In fact, in most of the opencv tutorial docs, we'll find a C version for many functions. You can look up this link C_Api functions.
However, there is one disclaimer: C routines of opencv are fewer compared to the C++/python routines. But if you have no choice, they are always there to fall back on.
Moreover, the Internet community believes that using C(Opencv routines) purposes is rather an outdated method for two reasons:
The available routines are few in number(I have faced this problem).
There is no compatibility between Mat
objects of C++ and IplImage/cvMat
objects of C.
Some sample code(blurring) after including stdio.h, highgui.h, cv.h
libraries:
int main( int argc, char** argv ) {
IplImage* img = 0;
IplImage* out = 0;
if( argc < 2 ) {
printf( "Usage: Accepts one image as argument\n" );
exit( EXIT_SUCCESS );
}
img = cvLoadImage( argv[1] );
if( !img ) {
printf( "Error loading image file %s\n", argv[1]);
exit( EXIT_SUCCESS );
}
out = cvCreateImage( cvGetSize(img), IPL_DEPTH_8U, 3 );
cvSmooth( img, out, CV_GAUSSIAN, 3, 3 );
cvReleaseImage( &img );
cvReleaseImage( &out );
cvDestroyWindow( "Example1" );
cvDestroyWindow( "Output" );
return EXIT_SUCCESS;
}
Upvotes: 5
Reputation: 1
i think the problem is with reading the file,bcoz u r opening the file in binary(bit) orientation and reading in byte orientation. so i think the problem is with the "fgetc" instead try with "fread"
Upvotes: 0
Reputation: 1
I am not very sure about this one, but i feel that your file pointer isn't moving ahead. You need an incremental statement. Each time it checks while((ch = fgetc(fp))!=EOF)
it comes out true. It also goes inside to print out the first pixel and repeats!
Upvotes: 0
Reputation: 7317
As others have mentioned, since images are binary files you have to know how to "interpret" the data in them after reading them. Luckily, for most formats you can find libraries that do it for you like libpng or libjpeg.
Upvotes: 3
Reputation: 40879
Opening files in C is usually (offtopic: how about always? :) done using fopen().
Now, when it comes to reading and assuming you're talking about binary file formats (such as the case with most image file formats), then you have to do some studying first to read in data correctly. If you tell us which file type you're trying to read perhaps you'll get more detailed information.
Upvotes: 2
Reputation: 4232
This really depends on the extension - a BMP is totally different to a TIFF or whatsoever...
You could just get every single pixel within an image and store it. With setpixel() and getpixel() you can store the pixels from the worldview and dmap them to viewport...
Upvotes: 1