Object counter in C [closed]

-1

I have a work in C language structure to do that consists of the following: Create a program that opens a bmp file, reads pixel by pixel, counts the objects in that file and plots a copy "binarized" (with "0" for white pixel and "1" for colored pixel) of the image on the console. Can someone give me a light on how to get started?

How do I read a pixel of a BMP image?

Unfortunately the teacher will not teach us anything and we will have to learn everything on our own, so I need an initial push. I thank those who try to help.

    
asked by anonymous 07.08.2018 / 00:44

1 answer

1

A read on wikipedia will also help

You can start by compiling and viewing as a .bmp file, follow the example:

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    char tag[2];
    unsigned int size;
    char reserved[4];
    unsigned int pointer;
} Header;

typedef struct
{
    unsigned int size;
    int width;
    int height;
    short numPlanes;
    short bitsPerPixel;
    int compression;
    unsigned int arraySize;
} Dib;

void readAtPos(char *buffer, int x, int y, int width, int height, int bytesPerPixel)
{
    int realY = height - y - 1;
    unsigned char *pixel = (unsigned char *)(buffer + realY*width*bytesPerPixel + x*bytesPerPixel);
    printf("Na pos (%d,%d) temos RGB: %d %d %d\n", x, y, *(pixel+2), *(pixel+1), *pixel);
}

int main()
{
    FILE *file = fopen("imagem.bmp", "rb");

    Header header = {};
    fread(&header.tag, 2, 1, file);
    fread(&header.size, 4, 1, file);
    fread(&header.reserved, 4, 1, file);
    fread(&header.pointer, 4, 1, file);

    Dib dib = {};
    fread(&dib.size, 4, 1, file);
    fread(&dib.width, 4, 1, file);
    fread(&dib.height, 4, 1, file);
    fread(&dib.numPlanes, 2, 1, file);
    fread(&dib.bitsPerPixel, 2, 1, file);
    fread(&dib.compression, 4, 1, file);
    fread(&dib.arraySize, 4, 1, file);

    printf("Tamanho do ficheiro: %d\n", header.size);
    printf("Largura da imagem: %d\n", dib.width);
    printf("Altura da imagem: %d\n", dib.height);
    printf("Bits por pixel: %d\n", dib.bitsPerPixel);

    fseek(file, header.pointer, SEEK_SET);
    int size = dib.bitsPerPixel/8 * dib.arraySize;
    void *buffer = malloc(size);
    fread(buffer, size, 1, file);
    readAtPos(buffer, 0, 0, dib.width, dib.height, dib.bitsPerPixel/8);
    readAtPos(buffer, 0, 532, dib.width, dib.height, dib.bitsPerPixel/8);
    readAtPos(buffer, 443, 408, dib.width, dib.height, dib.bitsPerPixel/8);

    return 0;
}
    
07.08.2018 / 01:01