Guide to Image Processing with C [1]

Guide to Image Processing with C [1]

-without the use of any external libraries

🔵Introduction

🔹Soo, Yup! Recently I was assigned a project on the topic "Image processing with C", and I was clueless. I was like, "What! C? How can someone process an image with C?". We've always seen C as the most basic language. So I went on the “internet”. It took painstaking hours to discover something useful. By doing trial-and-error, I finally arrived at some conclusions. I finally managed to do the task. Also, I learned that all of the resources are available on the internet, but they are dispersed.

🔹As a result, this series of blog posts has been designed to provide the majority of the necessary material in a single sitting, and you can begin with Image Processing with C. 😊

The concepts discussed in this Blog Series are not meant to be applied in production, especially when dealing with efficiency and performance. Also, I am not a professional when comes to image processing, so please correct me if I made a mistake anywhere. 😊

🔵What is Image-Processing

🔹Image processing is the manipulation and analysis of digital images using mathematical algorithms. It is a crucial field in computer science, with applications in a wide range of areas such as medical imaging, security, and robotics.

🔵Why should I process images with C

🔹Well, there are several reasons why you might choose to process images with C:

  1. Speed: C is a compiled language, which means that it is converted into machine code before being executed. This allows for faster execution times compared to interpreted languages like Python. This is especially important if you are working on real-time image processing applications where speed is crucial.

  2. Low-level access: C gives you direct access to the underlying hardware, allowing for fine-grained control over how the image is processed. This makes it possible to optimize your code for specific hardware architectures, resulting in better performance.

  3. Portability: C code can be easily ported across different platforms and architectures, making it a versatile choice for image processing. This allows you to write code once and run it on different systems without modification.

  4. Large Community and support: C has a large community and a lot of resources and libraries available, making it easy to find solutions to common image processing problems. This can save you time and effort in developing your own algorithms.

  5. Popularity in Embedded Systems: C is widely used in embedded systems and real-time systems, where image processing is often required. This means that many libraries, tools and resources are already available for C and image processing.

  6. Modularity and Reusability: C allows for the creation of separate modules or functions that can be reused across different projects, making it easier to maintain and update code.

🔹Overall, C is a powerful and efficient language, it's a great choice for image-processing projects that require speed, low-level access, and portability.

🔵PPM image

🔹PPM (Portable Pixmap) is a simple image file format that stores the image data in ASCII or binary format. It is a lossless format, which means that it preserves all the data of the original image. It does not include any compression, which makes it easy to work with and understand the image data. PPM is one of the oldest image formats and it is widely supported by many image-processing libraries and tools.

🔹A PPM image file typically has the file extension of .ppm. It starts with a header that includes information about the image, such as its dimensions and the maximum value for its colour channels. The header is followed by the image data, which is a stream of binary values representing the colour of each pixel in the image. PPM supports only 8-bit RGB colour images.

🔵Why should I use ppm format for image processing

🔹The PPM (Portable Pixmap) format is a simple and widely supported format for image processing. There are several reasons why you might choose to use PPM for image processing:

  1. Simplicity: PPM is a very simple format that stores the image data in ASCII or binary format, which makes it easy to read and write using C file I/O. It does not include any compression, which makes it easy to work with and understand the image data.

  2. Wide support: PPM is widely supported by many image-processing libraries and tools. This makes it easy to find libraries, tools and resources that can help you work with PPM images.

  3. Lossless: PPM is a lossless format, which means that it preserves all the data of the original image. This is important for image processing applications where the original image quality needs to be preserved.

  4. Uncompressed format: PPM stores the image data without any compression, this makes it easy to work with and understand the image data, but also means that the file size can be quite large, especially for large images.

  5. PPM format is good for testing and prototyping: Since it is a simple format, it is good for testing and prototyping image processing algorithms, it can be used to quickly verify the results of your image processing code.

🔹Overall, PPM is a simple and widely supported format that is well-suited for image processing. Its lossless, uncompressed nature and wide support make it a good choice for tasks that require preservation of the original image quality and easy to work with the image data.

🔵How to import ppm image in C

🔹To import a PPM image in C, you will need to use file I/O functions to read the image data from a file. Here are the general steps you would need to follow:

  1. Open the image file using the fopen() function. This function takes the file name and the mode (e.g. "r" for reading) as input and returns a pointer to the file.

  2. Read the header information from the file. The header contains information such as the dimensions of the image and the maximum value for the colour channels. You can use the fscanf() function to read this information from the file.

  3. Allocate memory for the image data. Once you know the dimensions of the image, you can use the malloc() function to allocate memory for the image data.

  4. Read the image data from the file. The image data is a stream of binary values representing the colour of each pixel in the image. You can use the fread() function to read the data into the memory you allocated in the previous step.

  5. Close the file using the fclose() function. This function takes the file pointer returned by fopen() as input and releases any resources associated with the file.

Code:

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

int main() {

    // Open the image file for reading.
    FILE* fp = fopen("image.ppm", "r");

    // Read the header Information.
    char header[3];
    int width, height, max_color;
    fscanf(fp, "%s", header);
    fscanf(fp, "%d %d %d", &width, &height, &max_color);
    if (header[0] != 'P' || header[1] != '3') {
        printf("Invalid file format.\n");
        return 1;
    }

    int pixels[height][width][3];
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            for (int k = 0; k < 3; k++) {
                fscanf(fp, "%d", &pixels[i][j][k]);
            }
        }
    }
    // Allocate memory for the image data
    int size = width * height * 3;
    unsigned char *data = (unsigned char *) malloc(size * sizeof(unsigned char));

    // Read the image data
    fread(data, sizeof(unsigned char), size, fp);

    // Close the file
    fclose(fp);

    // Do the Processing
    // ...

    // Free the memory
    free(data);

    return 0;
}

🔹It's important to note that this is a simple example, and the actual implementation will depend on the structure of the file and the way you want to store the image data, but the idea is the same, opening the file, reading the header, allocating memory, reading the image data and finally closing the file.

This is all you need to know, for now, to start processing your PPM image in C.

Thank You Soo Much for your valuable time.😊🥳👋

➲Mithin Dev

I would love to connect and collaborate.

🔹GitHub: github.com/mithindev

🔹Twitter: twitter.com/MithinDev

🔹LinkedIn: linkedin.com/in/mithin-dev-a-397983247