Fundamental of Image Processing
Fundamental of Image Processing
close all
clear all
a=imread('flower.jpeg');
figure(1);
imshow(a);
title('main rgb image')
Result:
Explanation: Here we used the function “imread” to read the image which gives us the value of rows
and columns and what type of image it is like RGB or GRAY . “imshow ” function is used to display the
image .
# Resizing Image :
close all
clear all
a=imread('flower.jpeg');
figure(1);
subplot (211); imshow(a); title('main rgb image');
i=a(200:600,200:800, 1:3); %resizing
subplot(212), imshow(i); title ('resized image');
Result:
Explanation: Resizing the image telling the parameters that from which pixel to pixel I need
the image. In this case we lose some side of the image or we can say the image was cut from
original shape.
# Resizing Image with Resized function:
Code in MATLAN 2018a :
close all
clear all
a=imread('flower.jpeg');
figure(1);
subplot (211); imshow(a); title('main rgb image');
i=imresize(a, [500,850]); %resizing image using resize tool
subplot(212), imshow(i); title ('resized image with function');
Result:
Explanation: here we used “imresized “ function to resize the image. Inside the function we
just added the variable and give the parameters of what size of the image we need . In this case
we will not lose any side of the image ,the original image became small . The dimension of the
image will be the given dimension ,which in this case will be 500*850.
close all
clear all
a=imread('flower.jpeg');
figure(1);
subplot (211); imshow(a); title('main rgb image');
i_gray=rgb2gray(a);
figure (1),
subplot(212),imshow(i_gray),title('gray scale image ');
Result:
Explanation: For converting into a gray –scale image we have a function named “rgb2gray”
which converts the image to direct a gray –scale image.
#Histogram image:
close all
clear all
a=imread('flower.jpeg');
figure(1);
subplot (211); imshow(a); title('main rgb image');
i_gray=rgb2gray(a);
figure (1),
subplot(212),imshow(i_gray),title('gray scale image ');
figure(2)
imhist(i_gray)
Result:
Explanation: We ploted the histogram image using “imhist” function . To plot the histogram the image
is needed to be converted into gray – scale image. The x axis is the intensity from 0 to 255 and y axis is
the number of pixels. From histogram we can find that which intensity has the high number of pixel.
#spliting RGB image into 3 types of shades:
i=imread('butterfly.jpg');
imshow(i)
allblack=zeros(size(i,1), size(i,2), 'uint8');
ired=cat(3, i(:, :, 1), allblack, allblack);
igreen=cat(3, allblack, i(:, :, 2), allblack);
iblue=cat(3, allblack, allblack, i(:, :, 3));
figure(2)
subplot(324); imshow(i);
subplot(321);imshow(ired);
subplot(322);imshow(igreen);
subplot(323);imshow(iblue);
Result:
Explanation: RGB Image is combination of red , green, blue colors. To split the rgb image into one shade
we need to take the pixel intensity of that particular color and make the rest of the pixel of other shade
to black. To do so we made a matrix of zeros of the same dimension. Then to get the red image we took
all the pixel value of red scale but took zero matrix for the green and blue scale and catinated it using
“cat” function. As e result we got the red shade of the image. For the other types of shade we took value
of pixel that we are interested and set the other pixel to zeo and catinated it.
close all
clear all
x=imread('x-ray.jpg');
x=rgb2gray(x);
xraw=x;
x1=x;
for i=1:628
for j=1:1200
if(x1(i ,j) <140)
x1(i,j)=0;
end
end
end
figure(2)
subplot(211)
imshow(xraw)
subplot(212)
imshow(x1)
Result:
Explanation: Using for loop we can reach every pixel of the image and check for certain condition , can
perform any operation for that particular pixel. Here we used a loop to reach every pixel of the image
and check for the condition that if the intensity of that image is less than 150 , if it is true than we set
the pixel value to zero otherwise we kept the value as it is . So basically we did is that set the pixel to
zero if it was less than 100 that is why areas where the pixel were less than 150 became black.
#Linear Combination In image Processing:
close all
clear all
p=imread('sharingaan1.jpg');
p=imresize(p,[300,350]);
figure (1);
imshow(p) ,title("sharingaan1")
q=imread('sharingaan2.jpg');
q=imresize(q,[300,350]);
figure(2);
imshow(q); title("sharingaan2")
r=imlincomb(3,p,.7,q);
figure(3);
imshow(r); title("mangekyou sharingaan")
Result:
Explanation: If we want to do linear combination then the size of each image should be equal . Then
we can use “imlincomb” function to do so. In the function we have give the variable name and what
percentage of information we need from each image .
close all
clear all
i=imread('shepp loagan.png');
i=rgb2gray(i);
imshow(i)
ang_space=1;
theta =0: ang_space: 180-ang_space;
[R,xp]=radon(i,theta);
figure('color' ,'w');
imagesc(theta,xp,R)
colormap(gray)
colorbar ;
title('projection of the 3 objects');
xlabel('parallel rotation angle -\theta (degrees)');
ylabel('parallel sensor positon')
figure();
R=radon(i,0:175);
i1=iradon(R,0:175);
i2=iradon(R,0:175, 'linear', 'none');
subplot(311), imshow(i);title("Original of 3 objects with sharp edges")
subplot(312), imshow(i1);title("Filter backprojection of 3 objects wtih sharp
edges")
subplot(313), imshow(i2,[]);title("Unfiltered backprojection of 3 objects with
sharp edges")
Result:
Explanation: We are doing radon transformation on the image. For radon transformation we need
angle ,so declared an angle. Then we used the radon transformation function and got the figure 1 in
image-scale . If we have the value of radon transform we can reverse transform it to get back the
original image. Here we used 2 types reconstruction using filter and not using filter. One thing we have
to keep in mind that while doing radon transformation we lose some of the information so while doing
the reverse transformation we will not the exact image.
# Image DFT :
close all
clear all
x=imread('mri.jpg');
%x=rgb2gray(x);
figure()
imshow(x)
X=fft2(x,1024,1024);
Y=fftshift(abs(X));
YR= Y; YR(:, :,[2,3])=0;
YG= Y; YG(:, :, [1,3])=0;
YB= Y; YB(:, :, [1,2])=0;
figure()
imshow(YR*0.0001)
imshow(YG*0.0001)
imshow(YB*0.0001)
Explantation: To do Fourier Transform there is a dedicated function “fft2” .If we use this function and
give the variable and range then it will do the first fourier transformation. In fourier transformation for
RGB image , colours are shifted ,so to hold the position of the color , we used the “fftshhift” ,which holds
the position of color.To split the color we just took the value of that particular color and set the rest of
the color to zero. After the fourier transformation the actual information remains in small area ,so just
to get the desired information we multiply with a small factor to show the image.
# Gray scale FFT in MATLAB :
close all
clear all
x=imread('mri.jpg');
x=rgb2gray(x);
figure()
imshow(x)
X=fft2(x,1024,1024);
Y=fftshift(abs(X));
figure()
imshow(Y*0.0001)
Explanation: To do FFT for gray scale image ,at first we just have to convert the RGB image to GRAY scale
image ,using “rgb2gray” .Then we will use the “fft2” function and “fftshift” to do the fourier
transformation.
# HOG transform :
close all
clear all
x= imread('mri.jpg');
x=x(170:400,100:300);
figure();
imshow(x)
y=255-x;
figure();
imshow(y)
z= edge(y,'canny',0.7);
figure();
imshow(z)
[h,r,t]=hough(z);
figure();
mesh(h)
axis on
xlabel('theta')
ylabel('rho')
figure();
h1=h(end :-1:1,:);
imshow(h1(51:420,:)*0.1)
axis on
Explanation: For Hog transformation there is some limits so at first we resized the image of
which part we are interested in. Then we just reversed the intensity. Then we used the edge
detecting function “canny” to detect the edges and established the variable for hog
transformation. Then we used the function “hough” function on the variable and found 3
parameters. The first parameter is intensity, 2nd one is rho and 3rd one is theta. Then we ploted
the parameters and got our desired image.
# Wavelet Transformation:
close all
clear all
x =imread('flower.jpeg');
%x =rgb2gray(x)
figure; imshow(x);
[xar,shr,xvr,xdr]=dwt2(x(:, :,1),'db2');
[xag,xhgx,xvg,xdg]=dwt2(x(:, :,2),'db2');
[xab,xhb,xvb,xdb]=dwt2(x(:, :,3),'db2');
xa(:, :,1)=xar; xa(:, :,2)=xag ; xa(:, :, 3)=xab;
xh(:, :,1)=xhr; xh(:, :,2)=xhg ; xh(:, :, 3)=xhb;
xv(:, :,1)=xvr; xv(:, :,2)=xvg ; xv(:, :, 3)=xvb;
xd(:, :,1)=xdr; xd(:, :,2)=xdg ; xd(:, :, 3)=xdb;
X1= [xa*0.003 log10(xv)*0.3 ; log10(xh)*0.3 log10(xd)*0.3];
figure; imshow(X1)
[xaar,xhhr,xvvr,xddr]=dwt2(xa(:, :, 1),'db2');
[xaag,xhhg,xvvg,xddg]=dwt2(xa(:, :, 2),'db2');
[xaab,xhhb,xvvb,xddb]=dwt2(xa(:, :, 3),'db2');
xaa(:, :, 1) =xaar; xaa(:,:,2)=xaag;xaa(:,:,3)=xaab;
xhh(:, :, 1) =xhhr; xhh(:,:,2)=xhhg;xhh(:,:,3)=xhhb;
xvv(:, :, 1) =xvvr; xvv(:,:,2)=xvvg;xvv(:,:,3)=xvvb;
xdd(:, :, 1) =xddr; xdd(:,:,2)=xddg;xdd(:,:,3)=xddb;
X11=[xaa*0.001 log10(xvv)*0.3; log10(xhh)*0.3 log10(xdd)*0.3];
figure;imshow(X11)
[r,c,s]=size(xv);
figure; imshow([X11(1:r, 1:c, :) xv*0.05;xh*0.05 xd*(0.05)])