Sie sind auf Seite 1von 3

ECE220: Computer Systems and Programming HCMUT

Machine Problem 2 December 2015

Image Processing and Convolution

Your task this week is to implement several image processing techniques. The most complex of these is
the Gaussian blur technique used for blurring images through a convolution operation. Gaussian blur is a
type of image blurring that uses a Gaussian function as the filter applied to each pixel in the image.
The objective for this week is for you to gain some experience with arrays and pointers, to implement code
using subroutines, and to solve a problem using programming knowledge gained from the class.

Background
An image is composed of an
array of pixels. Each pixel has
four bytes of information in the
RGBA format. These bytes
represent the red, blue, green,
and alpha (transparency) levels
of the pixel. The values for each
pixel range from 0 to 255. The
alpha channel represents
transparency information and
may not be present in all images.
Images can be modified by a
process called filtering. A filter
is a two-dimensional array that is
applied to the entire image, as
illustrated to the right.

In order to blur the image, one overlays the filter over a source pixel and multiplies each overlapping value
in the filter by the corresponding value in the image, then sums all of these values, as shown in the upper
right of the figure. This process of executing this operation for each pixel in the image is called convolution.
The sum of products calculated for a pixel is used for the corresponding pixel in the resulting image, so it
must not be allowed to go out of bounds (that is, below 0 or above 255). If the sum is out of bounds, you
should clamp it into the desired range. In other words, replace values below 0 with 0, and replace values
above 255 with 255.
The convolution process is performed for each pixel of each color array (red, green, and blue). The alpha
channel may or may not be included in the process and is dependent on the type of image processing done.
For this assignment, you should simply copy the alpha channel from the source to the final image (do not
convolve it with the filter).
Notice that if the filter is placed over a pixel at the edge of the image, it will not have enough information
to process. Therefore, you can consider the pixels outside of the image to be zero-valued pixels
(equivalently, ignore them). In other words, you will apply the filter only to the pixels in the image bounds.
Do not access values beyond the array bounds!
In order to generate the filter for Gaussian blur, a Gaussian function is used. The equation of the Gaussian
function in two dimensions is as follows:
1
, exp
2 2
where x is the distance from the origin in the horizontal axis, y is the distance from the origin in the vertical
axis, and σ is the standard deviation of the Gaussian distribution.
The Gaussian distribution is infinite in extent, meaning that its value at any finite distance is non-zero. In
theory, we need to include the entire image in the sum for each pixel. In practice, however, we can
approximate the Gaussian and choose a finite radius beyond which G(x,y) is negligibly small. How quickly
the Gaussian falls off away from the origin depends on the parameter σ. In this assignment, you must
calculate
ceil 3
The x and y values in the filter then range from –radius to radius. For example, if σ = 2.5, we can calculate
radius = ceil (3σ) = ceil (7.5) = 8. The filter is thus 17×17 pixels, with indices from 0 to 16 in the filter
applied from -8 to 8 relative to the original pixel in the image. Note that sqrt, exp, and ceil are available
in the math library for the C language.

Understanding Arrays
Your functions will be given arrays for each of the four channels in the image: red, green, blue, and alpha.
These arrays will be passed as pointers to 8-bit pixels (that is, as uint8_t*). However, they represent two-
dimensional arrays of specified height and width (these values are also provided to your functions). To
make use of the arrays, you will have to calculate the appropriate offset into the one-dimensional C arrays
based on the two indices of the pixel that you want to access. Recall from class that you should make use
of the expression x + y * width to access the pixel at (x,y). For example, given an array uint8_t* red
for the red channel, you can access the (x,y) pixel in the array with the expression red[x + y * width].

Pieces
There is more code in the package this time, but you still need to look at only two files:
mp2.h This header file provides function declarations and descriptions of the functions
that you must write for this assignment.
mp2.c The main source file for your code. Function headers for all functions are
provided to help you get started.
Other files provided to you include
main.c A source file that interprets commands and calls your functions.
Makefile A file that describes how to build your program, allowing you to type “make”
instead of re-typing the full compiler command every time.
imageData.h Some utility functions written by a former TA for the class.
imageData.c
lodepng.h A package for loading and storing PNG images.
lodepng.c
You need not read any of these, although you are welcome to do so.
Details
You should read the descriptions of the functions in the header file and peruse the function headers in the
source file before you begin coding. Here are the tasks that you need to complete for this assignment:
1. Create a Gaussian filter for a specific value of σ.
2. Convolve a filter with an image to obtain the Gaussian blur effect.
3. Compute a greyscale image for a given image.
4. Invert the colors in an image.
Each of these tasks corresponds to implementation of a single function in the mp2.c file.
For computing the greyscale image, you are given a 3-element array to be used as multipliers for the R, G,
and B channels (in that order). For each pixel, you should multiply the R, G, and B by the corresponding
multipliers, calculate the sum of the three values, and clamp it into the range 0 to 255. The alpha channel
should not be changed.
For color inversion, each channel (except alpha) for a pixel should be subtracted from 255 to obtain the
channel value in the inverted image. The alpha value should not be changed (but must be copied, as with
the other functions).

The Interface
When you have completed one or more of the functions, you can type “make” (no quotes) to compile your
code into an “mp2” executable program. The make program shows you the compiler commands that it
executes on your behalf, so you can see what is happening and will receive any syntax errors or other errors
or warnings from the compiler. Fix any warnings!
Once you have compiled your program, you can use the interface built into main.c to execute your
functions. A set of images has been provided to you in the Images subdirectory along with several copies
processed by a correct version of the program (take a look).
The mp2 program takes three or four command line arguments:
./mp2 <input file> <output file> <command #> [<sigma>]
The commands are 1 for a Gaussian blur (for which you need to include a sigma; values over 5-10 will run
quite slowly!), 2 for a greyscale image, and 3 for a color inversion.

Specifics
 Your code must be written in C and must be contained in the mp2.c file provided to you. We will
NOT grade files with any other name.
 You must implement the gaussianFilter, convolveImage, greyscale, and invertColors
functions correctly.
 Your routines return values and outputs must be correct.
 Your code must be well-commented. Follow the commenting style of the code examples provided
in class and in the textbook.

Grading Rubric
Functionality (100%)
 25% - gaussianFilter works correctly.
 35% - convolveImage works correctly.
 20% - greyscale works correctly.
 20% - invertColors works correctly.

Das könnte Ihnen auch gefallen