How to resolve the algorithm Bitmap/Histogram step by step in the C programming language
How to resolve the algorithm Bitmap/Histogram step by step in the C programming language
Table of Contents
Problem Statement
Extend the basic bitmap storage defined on this page to support dealing with image histograms. The image histogram contains for each luminance the count of image pixels having this luminance. Choosing a histogram representation take care about the data type used for the counts. It must have range of at least 0..NxM, where N is the image width and M is the image height. Test task Histogram is useful for many image processing operations. As an example, use it to convert an image into black and white art. The method works as follows: Use read/write ppm file, and grayscale image solutions.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Bitmap/Histogram step by step in the C programming language
The provided C code is designed to perform image processing tasks, specifically histogram computation and median thresholding. Here's a step-by-step explanation of the code:
-
Type Definitions:
histogram_t
andhistogram
are typedefs used to represent a histogram. A histogram is an array of 256 elements, where each element counts the occurrences of a specific intensity value in an image.
-
Image Structure:
- The code assumes the existence of an
image
structure, which likely represents an image in memory with attributes such as width, height, and pixel data. grayimage
is a pointer to animage
structure that stores grayscale images.
- The code assumes the existence of an
-
GET_LUM
Macro:- This macro is used to access the luminance (or intensity) value of a pixel at the specified coordinates (
X
andY
) within the image pointed to byIMG
.
- This macro is used to access the luminance (or intensity) value of a pixel at the specified coordinates (
-
get_histogram
Function:- This function takes a grayscale image (
im
) as input and returns a histogram representing the distribution of intensity values in the image. - It initializes a histogram
t
with 256 elements, all set to 0. - It iterates through all the pixels in the input image and increments the corresponding element in
t
based on the pixel's luminance value. - The result is a histogram where each element represents the number of pixels with that specific intensity value in the image.
- This function takes a grayscale image (
-
luminance histogram_median
Function:- This function takes a histogram
h
as input and computes the median luminance value using a binary search approach. - It iterates until the left and right cumulative sums of the histogram become equal.
- The returned value is the median luminance value, which divides the histogram into two equal halves.
- This function takes a histogram
-
Usage Example (the
main
Function):- This function demonstrates the functionality of the previous functions by reading an image file, converting it to grayscale, computing its histogram, finding the median luminance, and applying a threshold to the image based on the median.
- If a pixel's luminance is less than the median, it's set to black; otherwise, it's set to white.
- The processed image is then output as a PPM file.
In summary, this code performs image processing tasks including histogram computation and median thresholding to create a binary image where pixels below the median luminance are black and those above are white.
Source code in the c programming language
typedef unsigned int histogram_t;
typedef histogram_t *histogram;
#define GET_LUM(IMG, X, Y) ( (IMG)->buf[ (Y) * (IMG)->width + (X)][0] )
histogram get_histogram(grayimage im);
luminance histogram_median(histogram h);
histogram get_histogram(grayimage im)
{
histogram t;
unsigned int x, y;
if ( im == NULL ) return NULL;
t = malloc( sizeof(histogram_t)*256 );
memset(t, 0, sizeof(histogram_t)*256 );
if (t!=NULL)
{
for(x=0; x < im->width; x++ )
{
for(y=0; y < im->height; y++ )
{
t[ GET_LUM(im, x, y) ]++;
}
}
}
return t;
}
luminance histogram_median(histogram h)
{
luminance From, To;
unsigned int Left, Right;
From = 0; To = (1 << (8*sizeof(luminance)))-1;
Left = h[From]; Right = h[To];
while( From != To )
{
if ( Left < Right )
{
From++; Left += h[From];
} else {
To--; Right += h[To];
}
}
return From;
}
#include <stdio.h>
#include <stdlib.h>
#include "imglib.h"
/* usage example */
#define BLACK 0,0,0
#define WHITE 255,255,255
int main(int argc, char **argv)
{
image color_img;
grayimage g_img;
histogram h;
luminance T;
unsigned int x, y;
if ( argc < 2 )
{
fprintf(stderr, "histogram FILE\n");
exit(1);
}
color_img = read_image(argv[1]);
if ( color_img == NULL ) exit(1);
g_img = tograyscale(color_img);
h = get_histogram(g_img);
if ( h != NULL )
{
T = histogram_median(h);
for(x=0; x < g_img->width; x++)
{
for(y=0; y < g_img->height; y++)
{
if ( GET_LUM(g_img,x,y) < T )
{
put_pixel_unsafe(color_img, x, y, BLACK);
} else {
put_pixel_unsafe(color_img, x, y, WHITE);
}
}
}
output_ppm(stdout, color_img);
/* print_jpg(color_img, 90); */
free(h);
}
free_img((image)g_img);
free_img(color_img);
}
You may also check:How to resolve the algorithm Sum of squares step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Mastermind step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Matrix multiplication step by step in the OCaml programming language
You may also check:How to resolve the algorithm Factorial primes step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Sorting algorithms/Stooge sort step by step in the True BASIC programming language