Tech »

Image Processing

Introduction

The images in the digital form are made up of pixels. A pixel is the smallest item of information in the image. Depending on the kind of image, each of its pixels have a different kind of value. For grey-scale images, the pixels hold just an intensity value which in 8-bit images goes from 0 (black) to 255 (white). For colour images there are a lot of formats for representing the colours, but the most common (and simplest) is the RGB format. In this, each pixel consists of 3 values - one for the Red component, one for Green and one for Blue. With various values of these three components a pixel can contain various colours. How many colours can a pixel hold? You may have heard about the term bit per pixel (bpp). As the name suggests, this defines how many bits are used to encode the information for a single pixel. (For more info on this, you can have a look at the Wikipedia article on Color depth). I'll be using the 24 bpp format for the colour images in this article, in which each component (R, G, B) is an 8-bit value. Together they can represent 28 × 28 × 28 = 16777216 (> 16.7 million) colours.

Programming

I will be using OpenCV for this tutorial. It's a C library which provides a lot of functions (necessary as well as convenient) for image processing.

First, a sample program is presented which just creates a colour image. A step-by-step explanation of the code follows.

#include <cv.h>

#define IMAGEWIDTH  (640)
#define IMAGEHEIGHT (480)

int main(void)
{
    IplImage *img;
    unsigned int x, y, channels, imgstep;
    unsigned char *imgData;

    img = cvCreateImage(cvSize(IMAGEWIDTH, IMAGEHEIGHT), IPL_DEPTH_8U, 3);

    channels = img->nChannels;
    imgstep  = img->widthStep / sizeof (unsigned char); // Values per row
    imgData  = img->imageData;

    for (y = 0; y < (img->height); y++)
    {
        for (x = 0; x < (img->width); x++)
        {
            imgData[(y * imgstep) + (x * channels) + 0] = 255;  // Blue
            imgData[(y * imgstep) + (x * channels) + 1] = 128;  // Green
            imgData[(y * imgstep) + (x * channels) + 2] = 0;    // Red
        }
    }

    return 0;
}

In OpenCV, an image is represented as an IplImage structure. To create a new image, we use the function

IplImage* cvCreateImage( CvSize size, int depth, int channels );

Here, we create a 640 × 480 image with 8 bit unsigned value per element and 3 channels (RGB), making it a 24bpp image. The above function stores the image data and attributes in the object named img. The image data is stored in a 1D array (not a W×H 2D array) with the rows of the image one after the other. Inside each row, the channels are interleaved and the data order is B G R, thus the data layout of the created image will be B0,0 G0,0 R0,0 B1,0 G1,0 R1,0 ... B639,0 G639,0 R639,0 B0,1 G0,1 R0,1 ... ... B639,479 G639,479 R639,479

This color data is stored in a member array imageData. There are other member variables which hold other important attributes about the image and thus making pixel referencing easier.

img->nChannels  // Number of channels (3 in our case)
img->width      // Image width (IMAGEWIDTH in our case)
img->height     // Image height  (IMAGEHEIGHT in our case)
img->widthStep  // Values per row (nChannels*IMAGEWIDTH in our case)
img->imageData  // Image data containing the pixel values

The usage of above mentioned attributes is shown in the snippet below depicting editing of the individual pixels.

for (y = 0; y < (img->height); y++)
{
    for (x = 0; x < (img->width); x++)
    {
        imgData[(y * imgstep) + (x * channels) + 0] = 255;  // Blue
        imgData[(y * imgstep) + (x * channels) + 1] = 128;  // Green
        imgData[(y * imgstep) + (x * channels) + 2] = 0;    // Red
    }
}

The image being created in the above example is a 24bpp 640 × 480 image with all its pixels holding the same color [ 00 80 FF ] requiring 900KB (640 × 480 × 3 bytes) of memory for the pixel data. A PNG format of the above image is shown linking to the 24-bit BMP format image.

Sample Image 1

Links

Page last modified on Thu Sep 13 12:25:37 2012 - Powered by PmWiki

^