![]() |
|
#1
|
|||
|
|||
BMP image on matrix loadHello.I am working with visual c++ compiler 7.0.I have write down a c++ program to load a 256 color bitmap image (load on the same folder of .cpp program) into a 800x600 integer matrix.Everything seems to go ok but I would like a check.If some one can help me.Thanks a lot in advance.This is the code:
CPP / C++ / C Code:
Last edited by LuciWiz : 12-Sep-2007 at 11:05.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|||
|
#2
|
|||
|
|||
Re: bmp image on matrix load helpQuote:
1. It appears to me that you have hard-coded a number of values so that the program doesn't have a chance of working with anything other than an 800x600 24-bit color bitmap file, but you don't test any of the header parameters to make sure that's what you actually have. You also don't test to see how many bytes you actually read from the file. None of these gives me any confidence in the program as written. For example you allocate an array size of 3*800*600=1,440,000 chars to hold the values from the file, and you try to read that many bitmap values after the two headers. That's how a 24-bit color bitmap file would be organized and that's the number of bytes that would be in the bitmap portion. A 256 color bitmap file has a (usually) 1024-byte color palette after the two headers (before the bitmap pixel data), then one byte per pixel for the rest of the file. 2. Then you copy the first (800*600) bytes from your 1,440,000 array of bytes from the file into your 2-D array. If there was a palette in the file (that is, if it really is a 256-color bitmap), you are reading the palette into the first 1024 members of the 2-D array. If it is really a 24-bit color file, you are reading the Red, Green, Blue color values of the first 160,000 pixels into your 2-D array. I don't know what you expect, but neither gives me anything useful. Regards, Dave Last edited by davekw7x : 12-Sep-2007 at 12:08.
|
|
#3
|
|||
|
|||
Re: bmp image on matrix load helpthanks for explenation but how Can I find where effectively my bitmap starts?
I have tried to obytain it cheking the values of fp,BITMAPINFOHEADER and BITMAPFILEHEADER pointers but they do not correspond.Can you help me? |
|
#4
|
|||
|
|||
Re: bmp image on matrix load helpQuote:
The first header (BITMAPFILEHEADER in Microsoft's definition) is 14 bytes long The second header (BITMAPINFOHEADER) is 40 bytes long The field bfOffBits of the BITMAPFILEHEADER tells the start of the bitmap data. . You indicated that your program is supposed to load a 256-color bitmap file. I suggest that you print out all of the information that you can get from the headers before trying to do anything with the data: CPP / C++ / C Code:
For a 256 color file (8-bit color) you should see something like: Code:
Key elements: biBitCount = 8 for a 256 color file, The image is represented by 800*600 pixel values. (This file, for some reason has an extra two bytes at the end. That's not a problem.) The bitmap data starts 1078 bytes from the beginning of the file, as indicated by the bfOffBits value. (54 bytes for the headers plus 1024 bytes for the palette.) For a 24-bit color file you might see something like Code:
The bitmap data starts 54 bytes from the beginning of the file (54 bytes for the two headers). The bitmap data occupies 800*600*3 bytes, since each pixel is represented by three bytes. (Again, a couple of extra bytes at the end.) So my questions are: 1. Where did the bitmap file come from? 2. Where did the program come from? Who said that it is going to read a 256 color file? 3. What are you going to do with whatever data you get it into the 2-Dimension array of chars? If it is a 8-bit color file (256-colors), then just storing the bitmap data bytes is useless, since you can't possibly know what colors they represent without referring to the palette. If it is a 24-bit color file (millions of colors), then each pixel is represented by three bytes. Storing one byte per pixel in your array of chars gives nothing useful. (At least that's the way that I see it.) Regards, Dave Footnote: Bitmap files are not usually compressed, I think, but provision is made in the file format definition for indicating a compression scheme in the info header. You should always make sure that the biCompression value is zero, otherwise you have some more work to do. |
|
#5
|
|||
|
|||
Re: bmp image on matrix load helpOh thanks really.You are very prepared.So I would like to ask you how to do.
Image come from a paint where I have saved a image captured by a camera. I have saved the image on paint like 256 color bitmap.Now I would like to convert it in a grey level image because I have to use Sobel gradient operator and I need a matrix 800x600 where each grey level pixel value is stored.Do you think is it possible?is better using 24 bitmap image and make the average for every 3 byte (RGB)?I hope you'll reply trying to help me thanks for your attention and in advance for this post. Bye. |
|
#6
|
|||
|
|||
Re: bmp image on matrix load helpQuote:
Quote:
Using an 8-bit (256-color) file would require you to use the color table (palette) to convert each 8-bit pixel value to three color values and average them. That's not too difficult, I'm thinking. Some 8-bit gray scale bitmap files have a color table that is a "gray-scale ramp" with the final result that a 0x00 pixel value gives black, a 0xff pixel value gives white. But some have different values in the color table with the effect of adjusting brightness and contrast without actually changing bitmap pixel values in the file. Some rendering programs ignore the "color table" with the result that you don't always see what was written. Some application programs don't bother to put in the color table for eight-bit grayscale files. That's why programs that handle all kinds of bitmap files can get pretty complicated. It seems to me that using a 24-bit color file is more straightforward, since there is no palette, and simply taking the rgb average might be a good starting point for converting to gray scale. Generally speaking, if visual consistency is desired, experimenting with weighted averages might give more pleasing subjective (aesthetic) results, but for edge detection, a straight average will probably be sufficient. I think that it will be easiest to make a program that always works for 24-bit color files. I believe that your example already reads that kind of file, and you need only to take the rgb average from each 3 bytes of each pixel to write into your 2-D array. If you want to write the results to a bitmap file after the edge detection, you can just create rgb values equal to the pixel value in the 800x600 array and write a 24-bit color file (actually a grayscale). Or you can write an 8-bit grayscale bitmap if you would rather have the output in that format. Regards, Dave |
|
#7
|
|||
|
|||
Re: bmp image on matrix load helpReally thanks you have been extremely clear and precise.I hope You will forgive my ignorance but making the average of the RGB bytes is not enough to obtain the grey scale level image?And really last question (sorry to disturb you) how can I visualize the obtained matrix with grey scale level values like an image?
Really thanks for your help. |
|
#8
|
|||
|
|||
Re: bmp image on matrix load helpQuote:
Quote:
For example. Suppose you had the following 24-bit color bitmap file Code:
You create an internal matrix of gray scale values: [code] G1G2G3... (total of rows*cols gray values for the pixels) [code] Where G1 = (r1+g1+b1)/3 G2 = (r2+g2+b2)/3 . . . You store the G values in your 2-Dimension matrix, so now you have a 2-D matrix of gray values. Then perform the Sobel (or whatever) transformation) on the 2-D matrix. Now you want to see the results, right? You could create a brand new 256-color grayscale bit map, (with or without a palette), using the G values from your transformed matrix Or you could just use the G values to create a 24-bit color (gray scale) bitmap file. The output file will be like this: Code:
In other words, the output file will be exactly the same size and type as the input file. Each set of "rgb" pixel values will be the same three bytes repeated (equal parts of red, greeen, blue give a gray value. Once you have something like this that works and for which you can see the edge-detection at work, it may occur to you to handle some other type if input and output for more efficiency or whatever. My point is that you almost have the first part in place: namely you can (almost) read a 24-bit color file with width = 800 and height = 600 into the 3x800x600 1-Dimension array, and I think that's a reasonable start. Regards, Dave |
|
#9
|
|||
|
|||
Re: bmp image on matrix load helpHello to everybody!Sorry but I have still a problem.I have loaded a 24 bit image.Transformed into a greyscale image and loaded on a 800x600 matrix.Now I have to apply sobel gradient tecnique.It simply consists of applying two rotated mask 3x3 at each point of the matrix.This leads to two matrix one for the x gradient and another one for y gradient copmponent.I need to work separately with both components then calculate module of x and y gradient components.P{roblem is that I can only treat one for cycle in my program.I mean I cannot create two for cycle one to calculate x component and another one for y component and the another for cycle for their module.IBecause comnputer give me an error and ask me to send an e mail to microsoft.It is obviously an error in my code may be inside matrix range or definition but I still cannot find it I hope someone will help me.Thanks in advance I post the code'
//program to load a 24 color bitmap on a matrix and visualize its content like grey level image and make //sobel gradient analysis #include <stdio.h> #include <math.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <windows.h> void load_bitmap(char *,int,int,void *); void main(int argc, char *argv[]) { int i=0,j=0,w=0,h=0,t=0; unsigned char *buf = (unsigned char *)malloc(sizeof(unsigned char)*800*600*3); unsigned char buff[800][600]; // unsigned char sobelx[800][600]; //this is the matrix in which I would like to load the x component of gradient unsigned char sobel[800][600]; //this id the matrix where I would like to load the module of x and y gradient components // unsigned char sobely[800][600]; //this is the matrix in which I woulod like to load the y component of gradient double teta[800][600]; //here I load arctang values of gradient x and y components char *name = "polyhedron10.bmp"; //this is the name of the image I want to load (it is present on .cpp file folder) int n=0; load_bitmap(name,w,h,buf); //member function to open the bitmap image for(i=0; i<800; i++) { for(j=0; j<600; j++){ //double for cycle to load the image buff[i][j]=(int)((*(buf + t) + *(buf + t + 1) + *(buf + t + 2))/3); t=t+3; } } for(i=1; i<799; i++) { for(j=1; j<599; j++){ // sobelx[i][j]=(buff[i-1][j-1])*(-1)+(buff[i-1][j+1])-2*(buff[i][j-1])+2*(buff[i][j+1])-1*(buff[i+1][j-1])+(buff[i+1][j+1]); // printf(" %d ",sobelx[i][j]); sobel[i][j]=sqrt((pow((buff[i-1][j-1])*(-1)-2*(buff[i-1][j])-1*(buff[i-1][j+1])+1*(buff[i+1][j-1])+2*(buff[i+1][j])+(buff[i+1][j+1]),2)+pow((buff[i-1][j-1])*(-1)+(buff[i-1][j+1])-2*(buff[i][j-1])+2*(buff[i][j+1])-1*(buff[i+1][j-1])+(buff[i+1][j+1]),2))); // in the upper line I load module of x and y gradient into the sobel matrix the problem is that I cannot treat them separately I mean I cannot make two for cycles for // x and y components load them into sobelx and sobely matrices and then load them into sobel matrix I do not know why // but I can write down only one for cycle It is obviously impossible but I cannot find the error.I nead to treat x and y component of gradient separately printf(" %d ",sobel[i][j]); // teta[i][j]=atan2((buff[i-1][j-1])*(-1)-2*(buff[i-1][j])-1*(buff[i-1][j+1])+1*(buff[i+1][j-1])+2*(buff[i+1][j])+(buff[i+1][j+1]),(buff[i-1][j-1])*(-1)+(buff[i-1][j+1])-2*(buff[i][j-1])+2*(buff[i][j+1])-1*(buff[i+1][j-1])+(buff[i+1][j+1])); } } for(i=1; i<799; i++) { for(j=1; j<599; j++){ teta[i][j]=atan2((buff[i-1][j-1])*(-1)-2*(buff[i-1][j])-1*(buff[i-1][j+1])+1*(buff[i+1][j-1])+2*(buff[i+1][j])+(buff[i+1][j+1]),(buff[i-1][j-1])*(-1)+(buff[i-1][j+1])-2*(buff[i][j-1])+2*(buff[i][j+1])-1*(buff[i+1][j-1])+(buff[i+1][j+1])); } } } void load_bitmap(char* filename,int width, int height, void* buffer) { FILE* fp; if((fp = fopen(filename, "rb")) == NULL) //check it is a bitmap image { printf("it is not a bitmap”sB\n"); return; } fread(buffer, sizeof(BYTE), 800*600*3, fp); // save the image from buffer pointer fclose(fp); } |
|
#10
|
|||
|
|||
Re: bmp image on matrix load helpQuote:
Please paste the exact program that you are using into your post. There is no way (no way) that I am going to try to extract the usable part from the mess that you showed us. All of the extra color and comment parts that you put in to the source make it unreadable and uncompileable. Here's the drill: 1. Ask your question or state your problem 2. Paste the program into yout post. 3. Put [c] before the first line of code. 4. Put [/c] after the last line of code. Regards, Dave |
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| i need help in C++ PLZ | its_me | C++ Forum | 3 | 04-Dec-2006 22:51 |
| Load image problem, need help!!! | Skynet159 | Java Forum | 6 | 14-Aug-2006 10:21 |
| Combining Vectors and References | Frankg | C++ Forum | 7 | 14-Jan-2006 07:17 |
| GIM gidedit - a fltk fluid resize project | cable_guy_67 | FLTK Forum | 2 | 01-Jun-2005 16:00 |
| Checking source codes of image, audio and video files | onauc | C Programming Language | 5 | 26-Feb-2005 22:47 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The