0% found this document useful (0 votes)
63 views20 pages

Image Operations - Histogram Analysis - Thresholding

The document discusses various image processing operations in MATLAB including: - Creating images of different colors by defining red, green, and blue channel values - Performing arithmetic operations like addition and subtraction on images - Converting between image types like RGB to grayscale - Resizing and rotating images - Applying thresholding to convert a grayscale image to binary - Calculating the center of a binary object in an image

Uploaded by

Surabhi Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views20 pages

Image Operations - Histogram Analysis - Thresholding

The document discusses various image processing operations in MATLAB including: - Creating images of different colors by defining red, green, and blue channel values - Performing arithmetic operations like addition and subtraction on images - Converting between image types like RGB to grayscale - Resizing and rotating images - Applying thresholding to convert a grayscale image to binary - Calculating the center of a binary object in an image

Uploaded by

Surabhi Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Day-2

- 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

u=[u u; u u]; %creates the matrix for the values of u


v=[v v ; v v];%creates the matrix for the values of v
w=[w w ; w w] %creates the matrix for the values of w

end

b(:,:,1)=u % Maps the Red component of image into the variable b


b(:,:,2)=v % Maps the Blue component of image into the variable b
b(:,:,3)=w % Maps the Green component of image into the variable b

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:

Arithmetic operation is performed on two images to form a new


image.
X operation Y = Z
where operation can be (+, -, X, /) operators.
The operation is performed on individual values of the pixels of the
images to construct a new image.

IMAGE-1 IMAGE-2 FINAL IMAGE AFTER OPERATIONS


ADDITION OPERATIONS:
Addition operation is performed to create a new pixel value by
adding pixel values of two or more images, or to add a constant
value to the pixel value.
Above is an example of adding two monochrome images,
thereby blending the pixel content.

ORIGINAL IMAGE BRIGHTER VERSION: ORIGINAL IMAGE +75


Matlab IPT has a command called imadd that is used to add two
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:

Matlab’s IPT has built in command imsubtract that subtracts


pixel values of two images.
Ques1. Two matrices X and Y are given, convert them to class uint8,
and perform the following operations: a) X-Y b) Y-X c) |X-Y| (modulus)
Where X is:
200 100 100
0 10 50
50 250 120
And Y is:
100 220 230
45 95 120
205 100 0

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).

Multiplying an image by a number greater than one increases


the pixel value hence whitening it (value increases thus shifting it
towards whiter side), and vice versa. Multiplying by value less
than one, decreases the pixel values hence darkening the image.
Same goes for division also.
Ques2. There are 3 matrices. Convert them to uint8 and a) imadd and
imdivide by 3 b) imlincomb
X=[200 100 100; 0 10 50; 50 250 120]
Y=[100 220 230; 45 95 120; 205 100 0]
Z=[200 160 130; 145 195 120; 105 240 150]

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

The IPT offers a built-in function to perform a linear combination of


two or more images: imlincomb. This function computes each element
of the output individually, in double-precision floating point. If the
output is an integer array, imlincomb truncates elements that exceed
the range of the integer type and rounds off fractional values.
Write a MATLAB function to perform brightness correction on
monochrome images. It should take as arguments a
monochrome image, a number between 0 and 100 (amount of
brightness correction, expressed in percentage terms), and a
third parameter indicating whether the correction is intended to
brighten or darken the image.
CONVERTING IMAGES:
For convert RGB images to grayscale, the command used is
rgb2gray
im=imread(‘balls3.jpg’);
im_new=rgb2gray(im);
imshow(im_new);
RESIZING IMAGES
%%Change the size of an image
I = imread(‘balls3.jpg’);
J = imresize(I, 1.25);
K = imresize(I, [100 150]);
figure, imshow(J)
figure, imshow(K)
ROTATING IMAGES
%%Rotate an image by an angle in degrees
I = imread(‘ball3.jpg’);
J = imrotate(I, 35, ‘bilinear’);
imshow(I)
figure, imshow(J)
%% We are using a combination of commands to convert a grayscale image into binary. All the
pixel values that are greater than a specified threshold value are changed to 255, and the pixel
values less than the threshold values are changes to 0.

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

for i=1:s(1,1) % iterates through the number of rows


for j=1:s(1,2) %iterates through the number of columns and thus using both for loops we can
% iterate through all the pixels in the image
if temp(i,j)<thresh % if the pixel value at position (i,j) is less than thresh
temp(i,j)=0; % make the value zero (black)
else % if the value is more than thresh
temp(i,j)=255; % make it 255 (white)
end
end
end

figure, imshow(temp);

%%Save this file as “threshold.m” in the work directory and type

>>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)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy