Image Operations - Histogram Analysis - Thresholding
Image Operations - Histogram Analysis - Thresholding
- Image Operations
- Histogram Analysis
- Thresholding
CREATING AN IMAGE:
%program for creating a single color image using MATLAB.
u=.7; % Changing the value of u changes the Red component of the image and it varries from the
range of 0 to 1
v=0.7; % Changing the value of u changes the Blue component of the image and it varries from
the range of 0 to 1
w=0.8; % Changing the value of u changes the Green component of the image and it varries from
the range of 0 to 1
for i=1:6
end
imshow(b);
Why do individual colour matrices of an RGB image
appear in gray colour rather than their respective
colours?
DATA CONVERSIONS AND DATA CLASSES:
To understand how image contents are represented in memory, consider the following
example,
>>im=imread(‘balls3.jpg’);
Also in the ‘Workspace’ area on the right hand side of Matlab, you will notice the
variable ‘im’ and also its value, size, bytes and class.
The class will be listed as uint8.
The class uint8 implies that each pixel in the image takes 8 bits per element of an array
and since there are 3 arrays in a truecolor image, the BitDepth = 8*3=24 i.e. 24 bits
per pixel.
8 bits per element implies that the value of element can vary from 0 to 28-1 (from 0 to
255). The most common data classes for images are as follows:
• uint8: 1 byte per pixel (8-bits per pixel), in the [0, 255] range
• double: 8 bytes per pixel, usually in the [0.0, 1.0] range
• logical: 1 byte per pixel, representing its value as true (1 or white) or false (0 or
black)
ARITHEMATIC OPERATION ON IMAGES:
%code1
I=imread(‘rice.png’);
J=imread(‘cameraman.tif’) %cameraman.tif is a default image.
X=imadd(I,J);
imshow(X);
**Supposing a uint8 image that has a pixel with value 255. Then
even after adding a value of 50 in it, the final value will be 255.
That is after addition of images, the final pixel values still remain
in range [0,255]
%code2
im=imread(‘lenna.tif’);
im1=imadd(im,50);
imshow(im);
figure,imshow(im1);
SUBTRACTION:
Ans:
X = uint8([200 100 100; 0 10 50; 50 250 120])
Y = uint8([100 220 230; 45 95 120; 205 100 0])
Za = imsubtract(X,Y)
Zb = imsubtract(Y,X)
Zc = imabsdiff(Y,X)
MULTIPLICATION AND DIVISION:
Matlab’s IPT has commands immultiply and imdivide for
multiplying images (or constant), or dividing images (or by
constant).
Ans:
X = uint8([200 100 100; 0 10 50; 50 250 120])
Y = uint8([100 220 230; 45 95 120; 205 100 0])
Z = uint8([200 160 130; 145 195 120; 105 240 150])
IM1 = imdivide(imadd(X,imadd(Y,Z)),3)
IM2=imlincomb(1/3,X,1/3,Y,1/3,Z,’uint8’) %Help
im=imread('cameraman.tif');
figure, imshow(im);
s=size(im); % gets the length and breadth of the image
temp=im; % copies the image in a different variable
thresh=128; % the threshold value
figure, imshow(temp);
>>threshold
The following function calculates the center of gravity of the object in the image. This
file is to be saved with the filename same as the name of the function (e.g. center.m).
function [cent]= center(im)
s=size(im);
cx=0; cy=0;
n=1;
for i=1:s(1,1)
for j=1:s(1,2)
if im(i,j)==255 % 1 or 255
n=n+1;
cx=cx+i;
cy=cy+j;
end
end
end
cxavg=cx/n;
cyavg=cy/n;
[cent]=[cyavg,cxavg];
end
GENERATING AN IMAGE:
1. Binary image
An image which contains only black and white colors is called binary image.
>>imbin=[0 1;1 0];
>>figure,imshow(imbin,'InitialMagnification','fit')
Black indicates zero intensity and white indicates maximum intensity.
2. Grayscale image
A grayscale image contains intensities values in a range from zero to maximum including black
and white. Typically this range is 0 to 255. Thus values near to zero are dark shades of gray, while
those near 255 indicate light gray.
>> imgray=[0.0 0.2 0.4 0.8 1.0];
>> figure,imshow(imgray,'InitialMagnification','fit')
or
>>imgray=[0 32 64 128 196 255];
>> figure,imshow(uint8(imgray),'InitialMagnification','fit')
Note:
By default, the range is 0 to 1 (type: double). However you can convert the image values from
double type to unsigned integer(8 bits) type by using
>>imgray=uint8(imgray)