Image Acquisition
Take any picture of anything you like by using a digital camera and transfer the picture from your camera to your computer. The image should be able to view it by standard image viewing software such as Windows Picture and Fax Viewer).
Read the image into MATLAB
>> x = imread (‘obama.png’);
Display the Information
>> whos x
Name Size Bytes Class Attributes
x 278x200x3 166800 uint8
The class should be “unit8” and the 3rd dimension of size of x should be 3 which denote it is a colour image which correspond the three components which are Red, Green and Blue channels.
Red x(:,:,1)
Green x(:,:,2)
Blue x(:,:,1)
The first colon denotes all rows, the second colon all columns, and the third index denotes the color (1 for red, 2 for green and 3 for blue).
Display the image
>> imshow(x)
Convert Color Image to Grayscale
>>x_g = rgb2gray(x);
>>figure, imshow(x_g)
rgb2gray command converts the truecolor image RGB to the grayscale intensity image. rgb2gray converts RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance.
Display R, G or B Channel of the Image
There are many ways to display an image in certain channel (red, green or blue channel). You can try either:
- clear or set the other channels to 0
- using colormap
For the first option, we need to assign a different variable for the image so that we do not overwrite the original image. As for the example below, we assigned image x to three different variables (x_green, x_red and x_blue for green, red and blue channel respectively).
Red
>>x_red = x;
>>x_red(:,:,2) = 0;
>>x_red(:,:,3) = 0;
|
Green
>>x_green = x;
>>x_green(:,:,1) = 0;
>>x_green(:,:,3) = 0;
|
Blue
>>x_blue = x;
>>x_blue(:,:,1) = 0;
>>x_blue(:,:,2) = 0;
|
Take note that (:,:,1) represent red channel, (:,:,2) represents green channel and (:,:,3) represents blue channel . So, if we want to display the red channel, we must set the green and blue channel to 0 and vice versa.
For the second option, we need to extract each of these layers by using their respective indices (Red = 1, Green = 2, Blue = 3).
>>img_r = I(:,:,1);
>>image(img_r), colorbar;
The image above shows red channel of image x. By default, the colorbar has a scale of 0 to 64. It is difficult to use this rainbow-colored scale. The color intensity for full-color images ranges is from 0 to 255. Thus, we need to modify the scale by using colormap. This can be done as below.
Red
>>R = x(:,:,1);
>>figure;
>>image(R),colormap([[0:1/255:1]',zeros(256,1),zeros(256,1)]), colorbar;
Green
>>G = x(:,:,2);
>>figure;
>>image(G),colormap([zeros(256,1),[0:1/255:1]',zeros(256,1)]), colorbar;
Blue
>>B = x(:,:,3);
>>figure;
>>image(B),colormap([zeros(256,1),zeros(256,1),[0:1/255:1]']),colorbar;
Transpose an Image
To transpose the image, use imshow command followed by the variable name with apostrophe ( ‘ ).
>>imshow(x_g');
Crop an Image
To crop an image, use imcrop command followed by the variable name and the size of the crop rectangle, rect. rect is a four-element position vector [xmin ymin width height] that specifies the size and position of the crop rectangle.
>> cropImg = imcrop(x_g,[1 1 100 100]);
>> figure, imshow (cropImg);
Flip an Image
To flip an image, use flipdim command followed by the variable and number of 1 (vertically) or 2 (horizontally)
>>upsidedown=flipdim(x_g,1);
>>figure,imshow(upsidedown);
>>left2right=flipdim(x_g,2);
>>figure,imshow(left2right);
.