Thursday, June 22, 2017

Separating Color Layers in a Digital Image

I'm learning to use Matlab to do image analysis this summer at Northwest Nazarene University (Nampa, ID). The task before me is to learn how to use images taken from drones to count the number of blooms or fruits on a tree. All digital images are three dimensional arrays. The first two dimensions are easy to understand, they're the height and width of the image. The third dimension is color and there are three layers there. So a digital image might be a 2,000 by 3,000 by 3 array. And arrays are an easy mathematical structure to analyze in Matlab.

To mathematically manipulate an image using Matlab, one must first load the image and then split it into three color layers. In Matlab, this is done with the following script.

image = imread('IMGT1818.bmp');  %load the image
imshow(image)                                  %show  the color image
imageRed = image(:,:,1);                  %separate out the first layer, red
imageGreen = image(:,:,2);               %separate out the second layer, green
imageBlue = image(:,:,3);                 %separate out the third layer, blue
figure,imshow(imageRed)                %show the red layer
figure,imshow(imageGreen)             %show the green layer
figure,imshow(imageBlue)               %show the blue layer

A couple of notes here.

First, Matlab is case sensitive. There's a huge difference between the variable f and F.

Second, digital images loaded into Matlab must be enclosed with hyphens because the name of a file is not a variable

Third, the semicolon (;) suppresses output. Without it, the Command Window in Matlab fills with the decimal values of each pixel as an array is loaded or mathematically manipulated.

And fourth, the percent sign (%) signifies a comment. Any text after it is ignored in the script.

So what is the final result of this script? Below is a screen shot of Matlab after splitting a thermal image taken during descent at GPSL 2017.

From left to right, color image, red layer, green layer, and blue layer
Now that the layers have been split apart, further analysis can be done, like making a histogram of each color or finding edges in the image. My goal is to segment images or break them into two portions, those of the things I want to see and then the background. So there's a lot to learn and accomplish yet. I'll post more about my summer research as I learn more.

Meanwhile, readers can try to do this separation of color layers themselves, and without using Matlab (a very expensive matrix mathematics program). The Freeware program, Octave should be nearly identical to Matlab. So you might want to install this program and try out the script I give above.

Many successful image splittings  

No comments:

Post a Comment