Date post: | 30-Jul-2015 |
Category: |
Education |
Upload: | rsenthil1976 |
View: | 513 times |
Download: | 3 times |
Digital Image Processing using Scilab
R.Senthilkumar,Assistant Professor,
Department of Electronics and Communication Engineering,
Institute of Road and Transport Technology,Erode – 638 316
[1]. Image Arithmetic//Prog1.Image Arithmetic - To learn to use arithmetic operations to combine images. [Program]
clc;
clear;
close;
I = imread('C:\Users\Senthil\Desktop\DigitalImageProcessing\cameraman.jpeg'); //SIVP toolbox
J = imread('C:\Users\Senthil\Desktop\DigitalImageProcessing\rice.png');//SIVP toolbox
//Addition of two images
IMA = imadd(I,J); //SIVP toolbox
figure
ShowImage(IMA,'Image Addition')//IPD toolbox
//Difference between two images
IMS = imabsdiff(I,J);//SIVP toolbox
figure
ShowImage(IMS,'Image Subtraction');//IPD toolbox
//Division of two images
IMD = imdivide(I,J);//SIVP toolbox
IMD = imdivide(IMD,0.01);//SIVP toolbox
figure
ShowImage(uint8(IMD),'Image Division');//IPD toolbox
//Multiplication of two images
IMM = immultiply(I,I);//SIVP toolbox
figure
ShowImage(uint8(IMM),'Image Multiply');//IPD toolbox
[2]. Image Format Conversion (RGB to Gray and Gray to Binary)
//Program: Image Format Conversion [Program]//[1].RGB to Gray
//[2]. Gray to Binary
clc;
clear;
close;
I = imread('C:\Users\Senthil\Desktop\DigitalImageProcessing\redrose.jpg');
ShowColorImage(I,'Red Rose Color Image')
//RGB to Gray Image
I_gray = rgb2gray(I);
figure
ShowImage(I_gray,'Red Rose Gray Image')
Imean = mean2(I_gray);
Ithreshold = double(Imean)/double(max(I_gray(:)));
I_bw = im2bw(I,Ithreshold);
figure
ShowImage(I_bw,'Red Rose Binary Image')
[3]. Image Interpolation//To learn the role of interpolation operation
// i) Bi-linear ii) Bi-cubic iii) nearest neighbor [Program]
clc;
clear;
close;
I = imread('C:\Users\Senthil\Desktop\DigitalImageProcessing\lenna512.png');//size 512x512
ShowColorImage(I,'Lenna512 Colour Image')
title('Lenna512 Colour Image')
I = rgb2gray(I);
[m,n] = size(I);
I_nearest = imresize(I,[256,256]); //'nearest' - nearest-neigbor interpolation -SIVP Atom
I_bilinear = imresize(I,[256,256],'bilinear');// 'bilinear' - bilinear interpolation
I_bicubic = imresize(I,[256,256],'bicubic');//'bicubic' - bicubic interpolation
figure
ShowImage(uint8(I_nearest),'nearest-neigbor interpolation');
title('nearest-neigbor interpolation')
figure
ShowImage(uint8(I_bilinear),'bilinear - bilinear interpolation');
title('bilinear - bilinear interpolation')
figure
ShowImage(uint8(I_bicubic),'bicubic - bicubic interpolation');
title('bicubic - bicubic interpolation')
[4]. Point Operations- Image Enhancement
//Point Operations - To learn image enhancement through point transformation [Program]
//i)Linear transformation ii) Non-linear transformation
clc;
clear;
close;
I = imread('C:\Users\Senthil\Desktop\DigitalImageProcessing\rice.png');
//(i). Linear Transformation
//IMAGE NEGATIVE
I = double(I);
J = 255-I;
figure
ShowImage(I,'Original Image')
title('Original Image')
figure
ShowImage(J,'Linear Transformation-IMAGE NEGATIVE')
title('Linear Transformation-IMAGE NEGATIVE')
//(ii) Non-linear transformation
//GAMMA TRANSFORMATION
GAMMA = 0.9;
K = I.^GAMMA;
figure
ShowImage(K,'Non-linear transformation-GAMMA TRANSFORMATION')
title('Non-linear transformation-GAMMA TRANSFORMATION')
[5]. Image Histogram Plot
//Caption: Histogram Plot [Program]
clc;
clear;
close;
a=imread('C:\Users\Senthil\Desktop\DigitalImageProcessing\pout.jpg')
[m n]=size(a);
for i=1:256
b(i)=length(find(a==(i-1)));
end
figure,
ShowImage(a,'Original Image')//IPD toolbox
title('Original Image')
figure
plot2d3('gnn',[0:255],b)
title('Histogram of the Image')
//using SIVP Atom
[count,cells]=imhist(a);
figure
plot2d3('gnn',cells,count)
title('Histogram plot using SIVP atom')
//using IPD Atom
Histogram = CreateHistogram(a,cells);
figure
plot2d3('gnn',cells,Histogram)
title('Histogram plot using IPD atom')
[6]. Histogram Equalization
//Image Histogram Equalisation[Program]
clc;
close;
f=ReadImage('C:\Users\Senthil\Desktop\DigitalImageProcessing\pout.jpg');// Get image (Keep image file in same folder)
OrigSize=size(f);//Get size of original image
OrigRow=OrigSize(1);//Get number of rows in original image
OrigCol=OrigSize(2);//Get number of colomns in original image
figure
ShowImage(f,'Original Image Pout')
WriteImage(f,'Exp6OriginalImagePout.jpg') //Comment out if you do not want o/p file to be written on disk
OrigHist=CreateHistogram(uint8(f));//Create Histogram
figure
plot2d3(OrigHist)//Show Histogram
title('Original Image Histogram')
P=OrigHist/(OrigRow*OrigCol);//get average pixel value
C=zeros(1,256);
for i=2:256
C(1,i)=C(1,i-1)+P(1,i);
end
●
Cdash = round(C*255);
EqImage=[]//Empty matrix for output image
for i=1:OrigRow
for j=1:OrigCol
EqImage(i,j)=Cdash(1,f(i,j)+1);
end
end
figure
ShowImage(uint8(EqImage),'Image Histogram Equalisation')
WriteImage(uint8(EqImage),'Exp6ImageHistogramEqualisation.jpg') //Comment out if you do not want o/p file to be written on disk
EqHist=CreateHistogram(uint8(EqImage));//Create Histogram
figure
plot2d3(EqHist)//Show Histogram
title('Histogram Equalized Image')
[7]. Morphological Operations: Dilation and Erosion [Program]
clc;
clear;
close;
Image = imread('C:\Users\Senthil\Desktop\DigitalImageProcessing\tire.jpeg');
StructureElement = CreateStructureElement('square',3); // generate structuring element IPD atom
ResultImage1 = ErodeImage(Image,StructureElement); //IPD Atom
ResultImage2 = DilateImage(Image,StructureElement); //IPD Atom
ResultImage3 = BottomHat(Image,StructureElement); //IPD Atom
ResultImage4 = TopHat(Image,StructureElement); //IPD Atom
figure
ShowImage(Image,'Original Image')
title('Original tire.jpg Image')
figure
ShowImage(ResultImage1,'Eroded Image')
title('Eroded Image')
figure
ShowImage(ResultImage2,'Dilated Image')
title("Dilated Image")
figure
ShowImage(ResultImage3,'bottom hat filtered image')
title('Botton hat filtered Image')
figure
ShowImage(ResultImage4,'top hat filtered image')
title('Top hat Filtered Image')
ResultImage5 = imadd(ResultImage3,ResultImage4);
figure
ShowImage(ResultImage4,'top hat filtered image+bottom hat filtered image')
title('Top hat filtered image+ Bottom hat Filtered Image')
[8]. Colour Image Manipulations [Program]
clc
clear
close
//Showing RGB components of a color RGB image.
//Splitting the color image (RGB Image) into three planes
a=imread('C:\Users\Senthil\Desktop\DigitalImageProcessing\peppers.png'); //this image is 348x512x3 size
figure
ar=a(:,:,1);
ShowImage(ar,'RED Matrix')
figure
ag=a(:,:,2);
ShowImage(ag,'GREEN Matrix')
figure
ab=a(:,:,3);
ShowImage(ab,'BLUE Matrix')
RGB = imread('C:\Users\Senthil\Desktop\DigitalImageProcessing\peppers.png');//SIVP toolbox
RGB_128 = RGB/2;
RGB_128 = round(RGB_128)
[X,map] = RGB2Ind(RGB_128);
figure
ShowImage(X,'Indexed Image',map)
figure
ShowColorImage(RGB,'RGB Color Image')
YIQ = rgb2ntsc(RGB);
figure
ShowColorImage(YIQ,'NTSC image YIQ')
RGB = ntsc2rgb(YIQ);
YCC = rgb2ycbcr(RGB);
figure
ShowColorImage(YCC,'equivalent HSV image YCbCr')
RGB = ycbcr2rgb(YCC);
HSV = rgb2hsv(RGB);
figure
ShowColorImage(HSV,'equivalent HSV image')
RGB = hsv2rgb(HSV);
[9]. Fourier Transform of 2D Gray Image [Program]
//Caption: 2D-Discrete Fourier Transform using inbuilt fast fourier transform
//function fft2()
clc;
clear;
close;
a = ReadImage('C:\Users\Senthil\Desktop\DigitalImageProcessing\lenna.jpg');
[m,n]= size(a);
ShowImage(a,'Original lenna Image');
title('Original lenna Image')
//2D-DFT using FFT
A = fft2(double(a));
figure(1)
ShowImage(abs(A),'2D-Discrete Fourier Transform of Lena Image');
title('2D-Discrete Fourier Transform of Lena Image')
//fftshifted image
B = fftshift(A)
figure(2)
ShowImage(abs(B),'2D-Discrete Fourier Transform of Lena Image-fftshifted');
title('2D-Discrete Fourier Transform of Lena Image-fftshifted')
//2D-IDFT using FFT
a_inv = fft2(A')
a_inv = a_inv'/(m*n);
figure(3)
ShowImage(uint8(abs(a_inv)),'2D-Inverse Discrete Fourier Transform ');
title('2D-Inverse Discrete Fourier Transform')
[10]. Discrete Wavelet Transform and Inverse Discrete Wavelet Transform [Program]
//Caption: Scilab code to implement Discrete Wavelet Transform
clc;
clear;
close;
//Original Image
img = imread('C:\Users\Senthil\Desktop\DigitalImageProcessing\cameraman.jpeg');
figure(1)
ShowImage(img,'Original Image')
title('Original Image');
[p q] = size(img);
[CA,CH,CV,CD] = dwt2(double(img),'db1');//Compute 2D wavelet transform -daubcheis wavelet
figure(2)
ShowImage(uint8(CA),'LPF-LPF output')
title('LPF-LPF output')
figure(3)
ShowImage(uint8(CH),'LPF-HPF output')
title('LPF-HPF output')
figure(4)
ShowImage(uint8(CV),'HPF-LPF output')
title('HPF-LPF output')
figure(5)
ShowImage(uint8(CD),'HPF-HPF output')
title('HPF-HPF output')
img_inv = idwt2(CA,CH,CV,CD,'db1');
img_inv = uint8(img_inv);
figure(6)
ShowImage(img_inv,'Reconstructed Image')
title('Reconstructed Image')
[11]. Edge Detection [Program]
//Caption: Scilab code for Edge Detection using Different Edge detectors
//[1]. Sobel [2].Prewitt
close;
a = imread('C:\Users\Senthil\Desktop\DigitalImageProcessing\lenna.jpg');
c = edge(a,'sobel');
d = edge(a,'prewitt');
ShowImage(a,'Original Image')
title('Original Image')
figure
ShowImage(c,'Edge Detected Image-Sobel')
title('Edge Detected Image -Sobel')
figure
ShowImage(d,'Edge Detected Image-Prewitt')
title('Edge Detected Image-Prewitt')
//Caption: Scilab code for Edge Detection using Different Edge detectors [Program]
//[1]LoG [2] DoG
close;
a = imread('C:\Users\Senthil\Desktop\DigitalImageProcessing\lenna.jpg');
//LAPLACIAN OF GAUSSIAN////////////////////////////////////////////////
[c,thresh] = edge(a,'log',0.2,'both',2);
//d = edge(a,'prewitt');
ShowImage(a,'Original Image')
title('Original Image')
figure
ShowImage(c,'Edge Detected Image-LoG')
//DERIVATIVE OF GAUSSIAN FUNCTION
function [mask]=DoG(op1,op2)
//If op1 and op2 are not specified -Default size 3x3
if isempty(op1) then
siz = [3,3];
else
if length(op1)==1 then
siz = [op1, op1];
elseif length(op1)==2 then
siz = op1;
else
error("The second argument should have 1 or 2 elements for gaussian filter");
end
end
//set std for the filter
if isempty(op2) then
g_std = 0.5;
else
if length(op2)>1 then
error("The third argument should have only 1 element for gaussian filter");
else
g_std = op2;
end
end
sizx = (siz(2)-1)/2;
sizy = (siz(1)-1)/2;
x2 = ones(siz(1),1) * ([-sizx:sizx]^2);
y2 = ([-sizy:sizy]^2)' * ones(1, siz(2));
r = sqrt(x2+y2);
sigma = g_std;
F = (1/(sigma^2))*(((r.*r)/sigma^2)-1).*exp(-r.*r/2*sigma^2);
F(F<%eps*max(F)) = 0;
sumF=sum(F);
if sumF~=0 then
F = F / sum(F);
end
mask = F;
endfunction
mask = DoG([ ],[ ]);
mx=filter2(mask,a);
my=filter2(mask',a);
border=sqrt(mx.*mx + my.*my);
if thresh >=0 then
scale_thresh = min(border) * (1-thresh) + max(border)*thresh;
border=border > scale_thresh;
end
d = border;
figure
ShowImage(d,'Edge Detected Image-DoG')
Image Enhancement Methods
[13].Brightness enhancement
[14]. Brightness suppression
[15].Contrast Manipulation
[16]. Image Negative
[17]. Threshold Operation on Gray Image
[18]. Gray level slicing without background
[19]. Image Cropping, Image Complement and Linear Combination of Images
//Image Arithmetic: Image Complement, Image //Cropping and Linear Combination of Images [Program]
clc;
clear;
close;
I = imread('C:\Users\senthilkumar\Desktop\Gautam_PAL_Lab\DIP_Lab2\cameraman.jpeg');
J = imread('C:\Users\senthilkumar\Desktop\Gautam_PAL_Lab\DIP_Lab2\lenna.jpg');
K = imabsdiff(I,J);
ShowImage(I,'Cameraman Image')
title('Cameraman Image')
figure
ShowImage(J,'Lenna Image')
title("Lenna Image")
figure
ShowImage(K,'Absolute Difference Between cameraman and Lenna Image')
title('Absolute Difference Between cameraman and Lenna Image')
L = imcomplement(K);
figure
ShowImage(L,'Complement of difference Image K ')
title('Complement of difference Image ')
rect = [20,30,200,200];
I_subimage = imcrop(I,rect);
J_subimage = imcrop(J,rect);
figure
ShowImage(I_subimage,'Sub Image of Cameraman Image')
title('Sub Image of Cameraman Image')
figure
ShowImage(J_subimage,'Sub Image of Lenna Image')
title('Sub Image of Lenna Image')
a=2;
b =0.5;
M = imlincomb(a,I,b,J);
figure
ShowImage(M,'Linear Combination of cameraman and Lenna Image')
title('Linear Combination of cameraman and Lenna Image')
N= imlincomb(b,I,a,J);
figure
ShowImage(N,'Linear Combination of cameraman and Lenna Image')
title('Linear Combination of cameraman and Lenna Image')
[20]. Image Filtering and De-noising
//Neighborhood Operations - To learn about neighborhood operations and use them for
//i) Linear filtering ii) Non-linear filtering [Program]
clc;
clear;
close;
I = imread('C:\Users\Senthil\Desktop\DigitalImageProcessing\lenna.jpg');
I_noise = imnoise(I,'salt & pepper');
figure
ShowImage(I,'Original Lenna Image')
title('Original Lenna Image')
figure
ShowImage(I_noise,'Noisy Lenna Image')
title('Noisy Lenna Image')
//Case 1: Linear Filtering
//(i).Linear Filtering -Example 1: Average Filter
F_linear1 = 1/25*ones(5,5);//5x5 mask
I_linear1 = imfilter(I_noise,F_linear1);//linear filtering -Average Filter
figure
ShowImage(I_linear1,'Linear Average Filtered Noisy Lenna Image')
title('Linear Average Filtered Noisy Lenna Image')
//(ii).Linear Filtering - Example 2: Gaussing filter
hsize = [5,5];
sigma = 1;
F_linear2 = fspecial('gaussian', hsize, sigma); //Linear filtering- gaussian Filter
I_linear2 = imfilter(I_noise,F_linear2);
figure
ShowImage(I_linear2,'Linear Gaussian Filtered Noisy Lenna Image')
title('Linear Gaussian Filtered Noisy Lenna Image')
//Case 2: Non-Linear Filtering (i).Median Filtering
F_NonLinear = [3,3];
I_NonLinear = MedianFilter(I_noise,F_NonLinear);//Median Filter 3x3
figure
ShowImage(I_NonLinear,'Median Filtered(Non-Linear) Noisy Lenna Image')
title('Median Filtered(Non-Linear) Noisy Lenna Image')
Video Processing -Introduction
Scilab Image and Video Processing [Program]
//Program Simple video reading and writing .avi formats and manipulation of video frames.
//Note 1: Install xvid codec for read and write video files from
//http://www.xvid.org/Downloads.15.0.html
//Note 2: very large video can not be read by scilab
//Note 3: shuttle.avi is a large file more 100 frames. use shuttlenew.avi file
//for video processing applications
//Using SIVP Atom
//Software version
//OS Windows7 ,8
//Scilab5.4.1 and above
//Image Processing Design Toolbox 8.3.1-1
//Scilab Image and Video Proccessing toolbox 0.5.3.1-2
clear;
clc;
close;
n = aviopen('C:\Users\Senthil\Desktop\DigitalImageProcessing\shuttle_Xvid.avi');
im = avireadframe(n); //get a frame
imshow(im);
avilistopened()
aviclose(n);
Image Processing Design Atom is a better tool for Scilab video Processing [Program]
clear;
clc;
close;
VideoPath = 'C:\Users\Senthil\Desktop\DigitalImageProcessing\shuttle_Xvid.avi';
VideoInfo = GetVideoStruct('C:\Users\Senthil\Desktop\DigitalImageProcessing\shuttle_Xvid.avi');
VideoFilePointer = OpenVideoFile('C:\Users\Senthil\Desktop\DigitalImageProcessing\shuttle_Xvid.avi');
figure();
for n = 1 : VideoInfo.NumberOfFrames
RGB = ReadImage(VideoFilePointer);
ShowColorImage(RGB, VideoPath);
end;
CloseVideoFile(VideoFilePointer);