Thread: how to save pixel values of a Image in a matrix?

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    14

    how to save pixel values of a Image in a matrix?

    hello guys.
    I want to have a algo or c-program that can save image pixel values in a matrix similar like we do in MATLAB
    I have a bmp image which black and white only that its pixel values is only 0 or 255 (black or white)
    Now I want to save all that pixel values in a 2D matrix in a c-program
    so, is there any way to this?
    I used Cimg and imagemagick they are very slow.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,983
    BMP files are one of the easiest formats to deal with without having to import some library to do the work for you.
    https://en.wikipedia.org/wiki/BMP_file_format
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,729
    Heh, I remember when I was messing around with bitmaps... Writing code that'd load all the different types( including compression and alternative info-headers ) was far more difficult than I'd anticipated.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Mar 2016
    Posts
    14
    But my cprogram not reading it.. some type of compression or encryption is used i think. i was using FILE pointer in my cprogram but it doesn't help

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    14
    so is there any solution u come on?

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,729
    What are you having problems with? Can you open the file? Can you read its contents? The setup of file headers and such is a little finicky, especially with BMP because its header isn't aligned "correctly".
    Devoted my life to programming...

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,983
    Quote Originally Posted by jimmy2447 View Post
    so is there any solution u come on?
    Yes, you should post your code so we can advise you accordingly.
    Don't forget to mention which OS/Compiler you're using.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Mar 2016
    Posts
    14
    okeh I am using Ubuntu 14 and gcc compiler. Following is the code:
    Code:
    main()
    {
    char ch;
    FILE *f;
    f=fopen("image.bmp","r");
    while((ch=fgetc(f))!=EOF)
    {
    printf("%c",ch);
    }
    fclose(f);
    }

  9. #9
    Registered User
    Join Date
    Mar 2016
    Posts
    14
    following is the image that i am using to read:

    how to save pixel values of a Image in a matrix?-image-bmp

  10. #10
    Registered User
    Join Date
    Mar 2016
    Posts
    14
    This is the output i get:
    BM6($"X ������������������������NNN ������}}}��

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    BMP files are binary, so trying to read and print them as text will not work. I suggest you use "fread()" instead, and for printing, use hexadecimal so you can see the bytes.

    If you read the link Salem posted in post #2, you will notice that there is header information in the file before the pixel data. The header contains important information about the image data.

    I suggest you first try to understand the file format, then plan the variables you need in order to store the header information. Once you get that working, you can then start planning how to store the pixel data.

  12. #12
    Registered User
    Join Date
    Mar 2016
    Posts
    14
    okeh thanks Matticus.. But I still do not understand u.. i dont know how to know or read file headers and then u said use hexadecimal, did u mean by %p ??

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    A binary file is a collection of bytes. What these bytes represent depend upon the definition of the file format. For a BMP file, the first group of bytes (what we call the header) represent information about the image. How many bytes and what they mean are defined as described in the wiki link above.

    Here's an analogy. Let's say you have a series of letters: G, Y, R, G, Y, R, G, Y, R.

    Without any context, these letters have no discernable meaning.

    But if I were to say, "these letters represent the states of a traffic light", then those letters now have meaning (the light starts on Green, goes to Yellow, then to Red, then back to Green, and so on), since a context was defined.

    It's the same with a binary file. What the bytes represent depend upon the context in which they're read.



    "%x" is used for printing hex. This probably isn't necessary, but I personally find it easier to read hex values when working with binary.

    Code:
    #include <stdio.h>
    
    #define BYTES 8
    
    int main(void)
    {
        unsigned char byte[BYTES] = { 0x10, 0x0A, 0xFF, 0x00, 0x35, 0xC8, 0xEF, 0x77 };
        int i;
    
        printf("Hex  Dec\n\n");
    
        for(i = 0; i < BYTES; i++)
            printf("%02X = %3d\n", byte[i], byte[i]);
    
        return 0;
    }

  14. #14
    Registered User
    Join Date
    Mar 2016
    Posts
    14
    Thanks Mr. Matticus you are very helpful to me.
    Do you know any better explanation of file headers of different images other than wikipedia. It is very difficult to understand on wikipedia.

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    It takes some deliberate study to understand some file formats. I wrote a simple BMP "library" once for fun, and had to take a fair bit of notes before I got my head around the file format and how the data worked together. That is programming though - breaking down a problem into smaller pieces that are easy to understand (be it code or concepts).

    You can try doing a search for "bmp tutorial", but be warned: a lot of the examples I found were pretty bad. But maybe you can find explanations that will help you understand the file format better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copying the particular pixel in an image
    By prathiksa in forum C Programming
    Replies: 14
    Last Post: 10-23-2012, 02:22 PM
  2. How to get the rgb pixel values from a bmp image
    By anan in forum C Programming
    Replies: 3
    Last Post: 03-16-2011, 05:21 PM
  3. getting pixel data from image using libpng
    By mamacken in forum C Programming
    Replies: 5
    Last Post: 01-11-2010, 11:59 AM
  4. reading gif image pixel Information-help
    By kapil1089thekin in forum C Programming
    Replies: 2
    Last Post: 05-17-2008, 01:13 AM
  5. Calculate the pixel intensities in an image...
    By Sharmontime in forum C Programming
    Replies: 10
    Last Post: 06-08-2006, 01:19 AM