The purpose of this series is to get right down in the mud
and to learn how to implement many of the algorithms that are commonly used to
create special effects with images by working directly with the pixels.
Manipulate pixels directly
and individually
In this lesson, I will provide and
explain a program that makes it easy to:
- Manipulate and modify the pixels that belong to an image
- Display the modified image along with the original image for easy comparison in a before and after sense
A framework or driver
program
This program is designed to be used
as a framework or driver that controls the execution of another program that
actually processes the pixels.
By using this program as a driver,
you can concentrate on writing and executing image-processing algorithms
without having to worry about many of the details involving image files, image
display, etc.
A simple image-processing
program
Also in this lesson, I will provide
and explain the first of several image-processing programs designed to teach
you how to modify an image by directly modifying the pixels that represent the
image.
The image-processing program
provided in this lesson will be relatively simple with the intent being to get
you started but not necessarily to produce a modified image that is especially
interesting.
More interesting imaging
processing programs
Future lessons will show you how to write
image-processing programs that implement many common special effects as
well as a few that aren't so common. This will include programs to do the
following:
- Highlight a particular area in an image.
- Blur all or part of an image.
- Sharpen all or part of an image.
- Perform edge detection on an image.
- Apply color filtering to an image.
- Apply color inversion to an image.
- Morphing one image into another image.
- Rotating an image.
- Squeezing part of an image into a smaller size.
- Controlling the brightness of an image using linear and non-linear algorithms.
- Other special effects that I may dream up or discover while doing the background research for the lessons in this series.
Before getting into the programming details, it will be useful to review the concept of how images are constructed, stored, transported, and rendered in Java (and in most modern computers for that matter).
An array of colored dots - pixels
A modern computer image, at the point where it is presented (rendered) for human consumption, usually consists of a rectangular array of closely spaced colored dots. Ideally, the dots are so small and so close together that the human eye cannot distinguish them individually. This causes them to run together and appear to represent continuous color.
Image files
The pixels are typically stored and transported in files, and are then extracted from the files and displayed on a computer screen or sheet of paper for human consumption.
There are a fairly large number of formats for storing the pixels in a file. Different file formats have advantages and disadvantages in terms of compression, size, reproduction quality, etc.
Not interested in file formats
This series of lessons will not be concerned about file formats. We will be concerned with what to do with the pixels once they have been extracted from the file. The driver program that I will provide can read gif files and jpg files, and possibly other file formats as well.
Will pick up at the extraction point for raw pixels
We will initially become interested in the pixels at the point where they have been extracted from the file and exist in the form of a one-dimensional array of type int. We will convert that array into a three-dimensional array that is better suited for processing. Once we understand the conversion process, our attention will shift to the three-dimensional array containing pixel data. The image-processing programs that we will write will receive raw pixel data in the form of a three-dimensional array.
A grid of colored pixels
Each three-dimensional array object will represent one image consisting of a grid of colored pixels. When rendered, the pixels in the grid will be arranged in rows and columns. One of the dimensions of the array will represent rows. A second dimension will represent columns. The third dimension will represent the color (and transparency) of the pixel.
Now back to the fundamentals
A pixel in a modern computer image is represented by four unsigned 8-bit bytes of data. Three of those four bytes represent the colors red, green, and blue. The fourth byte, often referred to as the alpha byte, represents transparency. I will have more to say about the alpha byte and transparency later.
Mixing the primary colors red, green, and blue
Specific colors are created by mixing different amount of red, green, and blue. That is to say, when the program needs to cause the color orange to be displayed on the screen, it mixes together the correct amounts of red, green, and blue to produce orange.
The range of a color
Each unsigned eight-bit color byte can contain 256 different values ranging from 0 to 255 inclusive. If the value of the red byte is 0, for example, no red color is added into the mix to produce the overall color for that pixel. If the value of the red byte is 255, the maximum possible amount of red is added into the mix to produce the overall color for that pixel. The same is true for blue and green as well.
Black and white pixels
If all three of the color pixels have a value of 0, the color of that pixel is black. If all three of the color pixels have a value of 255, the color of that pixel is white. If all three of the pixels have the same value somewhere between 0 and 255, the color of the pixel is some shade of gray.
No comments:
Post a Comment