際際滷

際際滷Share a Scribd company logo
ECE 565
Project 1
Weixiong Wang A20332258
Problem 1 Edge detection combined with smoothing and thresholding
(a) (10 points) Write a program to perform spatial filtering of an image. You can fix the size of the
spatial mask at 3 X 3 but the coefficients need to be variables that can be input into your
program.
(b) (10 points) Use the program from Part (a) to filter an image f(x,y)with the Sobel gradient
masks in Fig. 1. Your program should be able to compute the magnitude of the gradient using
M(x,y)   +   and have the option of outputting a binary image by comparing each gradient
point against a specified threshold T
Figure 1. Sobel gradient masks.
(c) (20 points) Processkidney.tiffby combining smoothing with a 3 X 3 mask from Part (a) and
thresholding from Part (b) and produce a binary image that segments the large blood vessel in
the center of the image. This will require repeated trials of smoothing and choices of T. Looking at
the histogram of the gradient image before it is thresholded will help you select a value for T.
(a).
f=imread('image'); % f is the original image
imshow(f);
-1 0 1
-2 0 2
-1 0 1
-1 -2 -1
0 0 0
1 2 1
% we have a1 to a9 as variables
w =[a1 a2 a3;a4 a5 a6;a7 a8 a9] % w is the filter (assumed to be 3x3)
g=zeros(x+2,y+2);% The original image is padded with 0's
for i=1:x
for j=1:y
g(i+1,j+1)=f(i,j);
end
end
%cycle through the array and apply the filter
for i=1:x
for j=1:y
img(i,j)=g(i,j)*w(1,1)+g(i+1,j)*w(2,1)+g(i+2,j)*w(3,1) ... %first column
+ g(i,j+1)*w(1,2)+g(i+1,j+1)*w(2,2)+g(i+2,j+1)*w(3,2)... %second column
+ g(i,j+2)*w(1,3)+g(i+1,j+2)*w(2,3)+g(i+2,j+2)*w(3,3);
end
end
figure
imshow(img,[])
(b).
f=imread('..');
figure
imshow(f); %original figure
w =[a1 a2 a3;a4 a5 a6;a7 a8 a9] %filter from (a)
c = conv2(f,w,'same'); %image after convolution with mask
s1 = [-1 -2 -1;0 0 0;1 2 1]; % sobel operator in horizontal
s2 = [-1 0 1;-2 0 2;-1 0 1]; % sobel operator in vertical
gx = conv2(c,s1,'same');
gy = conv2(c,s2,'same');
M = sqrt(gx.*gx+gy.*gy); % maginitude
figure % image after convolution with horizontal sobel operator
imshow(gx);
figure % image after convolution with vertical sobel operator
imshow(gy)
g = gx+gy; % image with horizontal gx plus vertical gy
figure
imshow(g)
% historgram stem of image
I = gpuArray(imread('E:myimages促kidney.tif'));
[counts,x] = imhist(I);
stem(x,counts);
BW = im2bw(f,T);
figure
imshow(BW)
(c).
Figure before smoothing Figure after smoothing
To produce a binary image thatsegments the large blood vessel in the center of the image.
Convolution with horizontal sobel Convolution with vertical sobel
Below is the horizontal of kidney.tif
0 50 100 150 200 250
0
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
x 10
4
Stem of kindney.tif
Intensity level
Numberofpixels
By looking at the histogram of the gradient image,we find we can set T equal around 7.And get
the binary figure below which show us the blood vessel.
0
0.5
1
1.5
2
2.5
3
x 10
4
Histogram of kindney.tif
Intensity level
Numberofpixels
0 50 100 150 200 250
Code:
f=imread('E:myimages促kidney.tif');
figure
imshow(f); %original figure
w=ones(3)/9; %filter from (a)
c = conv2(f,w,'same'); %image after convolution with mask
s1 = [-1 -2 -1;0 0 0;1 2 1]; % sobel operator in horizontal
s2 = [-1 0 1;-2 0 2;-1 0 1]; % sobel operator in vertical
gx = conv2(c,s1,'same');
gy = conv2(c,s2,'same');
M = sqrt(gx.*gx+gy.*gy); % maginitude
figure % image after convolution with horizontal sobel operator
imshow(gx);
figure % image after convolution with vertical sobel operator
imshow(gy)
g = gx+gy; % image with horizontal gx plus vertical gy
figure
imshow(g)
%historgram stem of image
[counts,x] = imhist(f);
stem(x,counts);
title('Stem of kindney.tif')
xlabel('Intensity level')
ylabel('Number of pixels')
axis([0 256 0 20000])
figure;
imhist(f,64)
title('Histogram of kindney.tif')
xlabel('Intensity level')
ylabel('Number of pixels')
BW = im2bw(f,0.72);
figure
imshow(BW)
Problem 2 Global thresholding Otsus thresholding
Write a global thresholding program in which the threshold is estimated automatically using the
procedure discussed in Section 10.3.2. The output of your program should be a segmented
(binary) image. Use your program to segment noisy_fingerprint.tiff and produce a segmented
image.
Basic idea for designing process as below.
1. input x is a vector. output T is an estimated threshold that groups x
2. into 2 clusters using the algorithm of basic global thresholding
3. procesures:
1) Randomly select an initial estimate for T.
2) Segment the signal using T, which will yield two groups, G1 consisting of all points with
values<=T and G2 consisting of points with value>T.
3) Compute the average distance between points of G1 and T, and points of G2 and T.
4) Compute a new threshold value T=(M1+M2)/2
5) Repeat steps 2 through 4 until the change of T is smaller enough.
Binary Figure
Code:
function level = g_t(I)
I = imread('E:myimages促noisy_fingerprint.tif');
% STEP 1: Compute mean intensity of image from histogram, set T=mean(I)
[counts,N]=imhist(I);
i=1;
mu=cumsum(counts);
T(i)=(sum(N.*counts))/mu(end);
T(i)=round(T(i));
% STEP 2: compute Mean above T (MAT) and Mean below T (MBT) using T from
% step 1
mu2=cumsum(counts(1:T(i)));
MBT=sum(N(1:T(i)).*counts(1:T(i)))/mu2(end);
mu3=cumsum(counts(T(i):end));
MAT=sum(N(T(i):end).*counts(T(i):end))/mu3(end);
i=i+1;
% new T = (MAT+MBT)/2
T(i)=round((MAT+MBT)/2);
% STEP 3 to n: repeat step 2 if T(i)~=T(i-1)
while abs(T(i)-T(i-1))>=1
mu2=cumsum(counts(1:T(i)));
MBT=sum(N(1:T(i)).*counts(1:T(i)))/mu2(end);
mu3=cumsum(counts(T(i):end));
MAT=sum(N(T(i):end).*counts(T(i):end))/mu3(end);
i=i+1;
T(i)=round((MAT+MBT)/2);
Threshold=T(i);
end
% Normalize the threshold to the range [i, 1].
level = (Threshold - 1) / (N(end) - 1);
BW = im2bw(I,level);
imshow(BW)
Problem 3 Otsus thresholding
(a) Implement Otsus optimum thresholding algorithm given in Section 10.3.3. Use your
implementation of Otsus algorithm to segment polymersomes.tiff
(b) Use the global thresholding algorithm from Problem 2 to segment polymersomes.tiff and
compare the result with the segmented image obtained in Part (a).
a)
Original After Otsu
b)
Compare the result for a) and b)
We see the Otsu has a better performance than global,because Otsu is based on the histogram,so
we can get a good threshold value than in global thresholding.In Otsu,We cant change the
distributions, but we can adjust where we separate them (the threshold). As we adjust the threshold
one way, we increase the spread of one and decrease the spread of the other.
Code:
I = imread('E:myimages促polymersomes.tif');
mgk = 0;
mt = 0;
[m,n] = size(I);
h = imhist(I);
pi = h/(m.*n);
for i=1:1:256
if pi(i)~=0
lv=i;
break
end
end
for i=256:-1:1
if pi(i)~=0
hv=i;
break
end
end
lh = hv - lv;
for k = 1:256
p1(k)=sum(pi(1:k));
p2(k)=sum(pi(k+1:256));
end
for k=1:256
m1(k)=sum((k-1)*pi(1:k))/p1(k);
m2(k)=sum((k-1)*pi(k+1:256))/p2(k);
end
for k=1:256
mgk=(k-1)*pi(k)+mgk;
end
for k =1:256
var(k)=p1(k)*(m1(k)-mgk)^2+p2(k)*(m2(k)-mgk)^2;
end
[y,T]=max(var(:));
T=T+lv;
g=I;
g1=find(g>=T);
g(g1)=255;
g2=find(g<T);
g(g2)=0;
imshow(g)

More Related Content

What's hot (20)

Computer graphics
Computer graphics Computer graphics
Computer graphics
shafiq sangi
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
Uma mohan
Computer graphics
Computer graphics   Computer graphics
Computer graphics
Prianka Padmanaban
Basics of Computer graphics lab
Basics of Computer graphics labBasics of Computer graphics lab
Basics of Computer graphics lab
Priya Goyal
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
Ankit Kumar
Computer graphics
Computer graphicsComputer graphics
Computer graphics
Nanhen Verma
Cg lab cse-v (1) (1)
Cg lab cse-v (1) (1)Cg lab cse-v (1) (1)
Cg lab cse-v (1) (1)
Surya Sukumaran
Dda algo notes
Dda algo notesDda algo notes
Dda algo notes
shreeja asopa
Cgm Lab Manual
Cgm Lab ManualCgm Lab Manual
Cgm Lab Manual
Oriental College of Technology,Bhopal
Computer graphics lab assignment
Computer graphics lab assignmentComputer graphics lab assignment
Computer graphics lab assignment
Abdullah Al Shiam
Introduction to Data Science With R Lab Record
Introduction to Data Science With R Lab RecordIntroduction to Data Science With R Lab Record
Introduction to Data Science With R Lab Record
Lakshmi Sarvani Videla
Matlab plotting
Matlab plottingMatlab plotting
Matlab plotting
Amr Rashed
Lec02 03 rasterization
Lec02 03 rasterizationLec02 03 rasterization
Lec02 03 rasterization
Maaz Rizwan
Programming Assignment Help
Programming Assignment HelpProgramming Assignment Help
Programming Assignment Help
Programming Homework Help
Matlab plotting
Matlab plottingMatlab plotting
Matlab plotting
shahid sultan
Writeup advanced lane_lines_project
Writeup advanced lane_lines_projectWriteup advanced lane_lines_project
Writeup advanced lane_lines_project
Manish Jauhari
Line drawing algo.
Line drawing algo.Line drawing algo.
Line drawing algo.
Mohd Arif
Introduction to graphics programming in c
Introduction to graphics programming in cIntroduction to graphics programming in c
Introduction to graphics programming in c
baabtra.com - No. 1 supplier of quality freshers
Introduction to curve
Introduction to curveIntroduction to curve
Introduction to curve
Omprakash Chauhan
Fuzzy dm
Fuzzy dmFuzzy dm
Fuzzy dm
Akshay Chaudhari

Viewers also liked (17)

3. 亳从亳 于亠亠仍亳从亳
3. 亳从亳 于亠亠仍亳从亳3. 亳从亳 于亠亠仍亳从亳
3. 亳从亳 于亠亠仍亳从亳
弌亠亞亶 仍亳亳仆
Resume Feb 2016
Resume Feb 2016Resume Feb 2016
Resume Feb 2016
Zachary Schweissguth
Los dinosauriosLos dinosaurios
Los dinosaurios
Maribelkoplo
Sticky Note Draft #7
Sticky Note Draft #7Sticky Note Draft #7
Sticky Note Draft #7
maryashtona2
After pcl-reconstruction - POSTSURGICAL PCL REHABILITATION PROTOCOL
After pcl-reconstruction - POSTSURGICAL PCL REHABILITATION PROTOCOLAfter pcl-reconstruction - POSTSURGICAL PCL REHABILITATION PROTOCOL
After pcl-reconstruction - POSTSURGICAL PCL REHABILITATION PROTOCOL
priyaakumarr
Arany J叩nos Secondary School, Sz叩zhalombatta
Arany J叩nos Secondary School, Sz叩zhalombattaArany J叩nos Secondary School, Sz叩zhalombatta
Arany J叩nos Secondary School, Sz叩zhalombatta
read in europe
StreetSafe-report_9-16-10
StreetSafe-report_9-16-10StreetSafe-report_9-16-10
StreetSafe-report_9-16-10
C. Daniel Bowes
HT16 - DA156A - Introduktion till anv辰ndbarhet
HT16 - DA156A - Introduktion till anv辰ndbarhetHT16 - DA156A - Introduktion till anv辰ndbarhet
HT16 - DA156A - Introduktion till anv辰ndbarhet
Anton Tibblin
Steps for avoiding pitfalls
Steps for avoiding pitfallsSteps for avoiding pitfalls
Steps for avoiding pitfalls
KUN CAPITAL, ASHOK LEYLAND AS A INTERN
Happy tanksgiving day Aurora 2 B tur
Happy tanksgiving day Aurora 2 B turHappy tanksgiving day Aurora 2 B tur
Happy tanksgiving day Aurora 2 B tur
secondatur
丐 "从舒弍仂仆 - 丿仆亞亳 舒亠仍亳"
 丐 "从舒弍仂仆 - 丿仆亞亳 舒亠仍亳" 丐 "从舒弍仂仆 - 丿仆亞亳 舒亠仍亳"
丐 "从舒弍仂仆 - 丿仆亞亳 舒亠仍亳"
KristinaDovbysh
Modelos Julho
Modelos JulhoModelos Julho
Modelos Julho
Eduardo Abbas
Modelling of air temperature using ann and remote sensing
Modelling of air temperature using ann and remote sensingModelling of air temperature using ann and remote sensing
Modelling of air temperature using ann and remote sensing
mehmet ahin
After cartilage-repair - POSTSURGICAL KNEE CARTILAGE REPAIR REHABILITATION PR...
After cartilage-repair - POSTSURGICAL KNEE CARTILAGE REPAIR REHABILITATION PR...After cartilage-repair - POSTSURGICAL KNEE CARTILAGE REPAIR REHABILITATION PR...
After cartilage-repair - POSTSURGICAL KNEE CARTILAGE REPAIR REHABILITATION PR...
priyaakumarr
Conclusiones gastritisConclusiones gastritis
Conclusiones gastritis
aletixilima
Proyecto de-informatica grupo. 2Proyecto de-informatica grupo. 2
Proyecto de-informatica grupo. 2
Eripam26
永姻艶壊艶稼岳温界庄坦稼2
永姻艶壊艶稼岳温界庄坦稼2永姻艶壊艶稼岳温界庄坦稼2
永姻艶壊艶稼岳温界庄坦稼2
ivalma05
Los dinosauriosLos dinosaurios
Los dinosaurios
Maribelkoplo
Sticky Note Draft #7
Sticky Note Draft #7Sticky Note Draft #7
Sticky Note Draft #7
maryashtona2
After pcl-reconstruction - POSTSURGICAL PCL REHABILITATION PROTOCOL
After pcl-reconstruction - POSTSURGICAL PCL REHABILITATION PROTOCOLAfter pcl-reconstruction - POSTSURGICAL PCL REHABILITATION PROTOCOL
After pcl-reconstruction - POSTSURGICAL PCL REHABILITATION PROTOCOL
priyaakumarr
Arany J叩nos Secondary School, Sz叩zhalombatta
Arany J叩nos Secondary School, Sz叩zhalombattaArany J叩nos Secondary School, Sz叩zhalombatta
Arany J叩nos Secondary School, Sz叩zhalombatta
read in europe
StreetSafe-report_9-16-10
StreetSafe-report_9-16-10StreetSafe-report_9-16-10
StreetSafe-report_9-16-10
C. Daniel Bowes
HT16 - DA156A - Introduktion till anv辰ndbarhet
HT16 - DA156A - Introduktion till anv辰ndbarhetHT16 - DA156A - Introduktion till anv辰ndbarhet
HT16 - DA156A - Introduktion till anv辰ndbarhet
Anton Tibblin
Happy tanksgiving day Aurora 2 B tur
Happy tanksgiving day Aurora 2 B turHappy tanksgiving day Aurora 2 B tur
Happy tanksgiving day Aurora 2 B tur
secondatur
丐 "从舒弍仂仆 - 丿仆亞亳 舒亠仍亳"
 丐 "从舒弍仂仆 - 丿仆亞亳 舒亠仍亳" 丐 "从舒弍仂仆 - 丿仆亞亳 舒亠仍亳"
丐 "从舒弍仂仆 - 丿仆亞亳 舒亠仍亳"
KristinaDovbysh
Modelling of air temperature using ann and remote sensing
Modelling of air temperature using ann and remote sensingModelling of air temperature using ann and remote sensing
Modelling of air temperature using ann and remote sensing
mehmet ahin
After cartilage-repair - POSTSURGICAL KNEE CARTILAGE REPAIR REHABILITATION PR...
After cartilage-repair - POSTSURGICAL KNEE CARTILAGE REPAIR REHABILITATION PR...After cartilage-repair - POSTSURGICAL KNEE CARTILAGE REPAIR REHABILITATION PR...
After cartilage-repair - POSTSURGICAL KNEE CARTILAGE REPAIR REHABILITATION PR...
priyaakumarr
Conclusiones gastritisConclusiones gastritis
Conclusiones gastritis
aletixilima
Proyecto de-informatica grupo. 2Proyecto de-informatica grupo. 2
Proyecto de-informatica grupo. 2
Eripam26
永姻艶壊艶稼岳温界庄坦稼2
永姻艶壊艶稼岳温界庄坦稼2永姻艶壊艶稼岳温界庄坦稼2
永姻艶壊艶稼岳温界庄坦稼2
ivalma05

Similar to ECE 565 Project1 (20)

matlab.docx
matlab.docxmatlab.docx
matlab.docx
AraniNavaratnarajah2
Dct compressionfactor8.doc
Dct compressionfactor8.docDct compressionfactor8.doc
Dct compressionfactor8.doc
MITTU1
Solutions for Image Processing for Engineers by Yagle and Ulaby
Solutions for Image Processing for Engineers by Yagle and UlabySolutions for Image Processing for Engineers by Yagle and Ulaby
Solutions for Image Processing for Engineers by Yagle and Ulaby
j93965567
LAB05ex1.mfunction LAB05ex1m = 1; .docx
LAB05ex1.mfunction LAB05ex1m = 1;                          .docxLAB05ex1.mfunction LAB05ex1m = 1;                          .docx
LAB05ex1.mfunction LAB05ex1m = 1; .docx
smile790243
E251014
E251014E251014
E251014
jcbp_peru
Notes on image processing
Notes on image processingNotes on image processing
Notes on image processing
Mohammed Kamel
Computer graphics
Computer graphics   Computer graphics
Computer graphics
Prianka Padmanaban
Introduction to Artificial Neural Networks
Introduction to Artificial Neural NetworksIntroduction to Artificial Neural Networks
Introduction to Artificial Neural Networks
Stratio
Solution Manual Image Processing for Engineers by Yagle and Ulaby
Solution Manual Image Processing for Engineers by Yagle and UlabySolution Manual Image Processing for Engineers by Yagle and Ulaby
Solution Manual Image Processing for Engineers by Yagle and Ulaby
spaceradar35
Programming with OpenGL
Programming with OpenGLProgramming with OpenGL
Programming with OpenGL
Syed Zaid Irshad
Computer Graphics Unit 1
Computer Graphics Unit 1Computer Graphics Unit 1
Computer Graphics Unit 1
aravindangc
Test
TestTest
Test
Kinni MEW
Test
TestTest
Test
Kinni MEW
2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph
kinan keshkeh
Otsu
OtsuOtsu
Otsu
Niloufar Azmi
Computer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1bComputer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1b
Philip Schwarz
ISI MSQE Entrance Question Paper (2010)
ISI MSQE Entrance Question Paper (2010)ISI MSQE Entrance Question Paper (2010)
ISI MSQE Entrance Question Paper (2010)
CrackDSE
Gonzalez, rafael,c.digitalimageprocessingusing matlab
Gonzalez, rafael,c.digitalimageprocessingusing matlabGonzalez, rafael,c.digitalimageprocessingusing matlab
Gonzalez, rafael,c.digitalimageprocessingusing matlab
urmia university of technology
Visualizing and solving Complex Integrals.pptx
Visualizing and solving Complex Integrals.pptxVisualizing and solving Complex Integrals.pptx
Visualizing and solving Complex Integrals.pptx
MATLAB Homework Helper
Plotting position and velocity
Plotting position and velocityPlotting position and velocity
Plotting position and velocity
abidraza88
Dct compressionfactor8.doc
Dct compressionfactor8.docDct compressionfactor8.doc
Dct compressionfactor8.doc
MITTU1
Solutions for Image Processing for Engineers by Yagle and Ulaby
Solutions for Image Processing for Engineers by Yagle and UlabySolutions for Image Processing for Engineers by Yagle and Ulaby
Solutions for Image Processing for Engineers by Yagle and Ulaby
j93965567
LAB05ex1.mfunction LAB05ex1m = 1; .docx
LAB05ex1.mfunction LAB05ex1m = 1;                          .docxLAB05ex1.mfunction LAB05ex1m = 1;                          .docx
LAB05ex1.mfunction LAB05ex1m = 1; .docx
smile790243
Notes on image processing
Notes on image processingNotes on image processing
Notes on image processing
Mohammed Kamel
Introduction to Artificial Neural Networks
Introduction to Artificial Neural NetworksIntroduction to Artificial Neural Networks
Introduction to Artificial Neural Networks
Stratio
Solution Manual Image Processing for Engineers by Yagle and Ulaby
Solution Manual Image Processing for Engineers by Yagle and UlabySolution Manual Image Processing for Engineers by Yagle and Ulaby
Solution Manual Image Processing for Engineers by Yagle and Ulaby
spaceradar35
Programming with OpenGL
Programming with OpenGLProgramming with OpenGL
Programming with OpenGL
Syed Zaid Irshad
Computer Graphics Unit 1
Computer Graphics Unit 1Computer Graphics Unit 1
Computer Graphics Unit 1
aravindangc
2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph
kinan keshkeh
Computer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1bComputer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1b
Philip Schwarz
ISI MSQE Entrance Question Paper (2010)
ISI MSQE Entrance Question Paper (2010)ISI MSQE Entrance Question Paper (2010)
ISI MSQE Entrance Question Paper (2010)
CrackDSE
Gonzalez, rafael,c.digitalimageprocessingusing matlab
Gonzalez, rafael,c.digitalimageprocessingusing matlabGonzalez, rafael,c.digitalimageprocessingusing matlab
Gonzalez, rafael,c.digitalimageprocessingusing matlab
urmia university of technology
Visualizing and solving Complex Integrals.pptx
Visualizing and solving Complex Integrals.pptxVisualizing and solving Complex Integrals.pptx
Visualizing and solving Complex Integrals.pptx
MATLAB Homework Helper
Plotting position and velocity
Plotting position and velocityPlotting position and velocity
Plotting position and velocity
abidraza88

ECE 565 Project1

  • 1. ECE 565 Project 1 Weixiong Wang A20332258 Problem 1 Edge detection combined with smoothing and thresholding (a) (10 points) Write a program to perform spatial filtering of an image. You can fix the size of the spatial mask at 3 X 3 but the coefficients need to be variables that can be input into your program. (b) (10 points) Use the program from Part (a) to filter an image f(x,y)with the Sobel gradient masks in Fig. 1. Your program should be able to compute the magnitude of the gradient using M(x,y) + and have the option of outputting a binary image by comparing each gradient point against a specified threshold T Figure 1. Sobel gradient masks. (c) (20 points) Processkidney.tiffby combining smoothing with a 3 X 3 mask from Part (a) and thresholding from Part (b) and produce a binary image that segments the large blood vessel in the center of the image. This will require repeated trials of smoothing and choices of T. Looking at the histogram of the gradient image before it is thresholded will help you select a value for T. (a). f=imread('image'); % f is the original image imshow(f); -1 0 1 -2 0 2 -1 0 1 -1 -2 -1 0 0 0 1 2 1
  • 2. % we have a1 to a9 as variables w =[a1 a2 a3;a4 a5 a6;a7 a8 a9] % w is the filter (assumed to be 3x3) g=zeros(x+2,y+2);% The original image is padded with 0's for i=1:x for j=1:y g(i+1,j+1)=f(i,j); end end %cycle through the array and apply the filter for i=1:x for j=1:y img(i,j)=g(i,j)*w(1,1)+g(i+1,j)*w(2,1)+g(i+2,j)*w(3,1) ... %first column + g(i,j+1)*w(1,2)+g(i+1,j+1)*w(2,2)+g(i+2,j+1)*w(3,2)... %second column + g(i,j+2)*w(1,3)+g(i+1,j+2)*w(2,3)+g(i+2,j+2)*w(3,3); end end figure imshow(img,[]) (b). f=imread('..'); figure imshow(f); %original figure w =[a1 a2 a3;a4 a5 a6;a7 a8 a9] %filter from (a) c = conv2(f,w,'same'); %image after convolution with mask s1 = [-1 -2 -1;0 0 0;1 2 1]; % sobel operator in horizontal s2 = [-1 0 1;-2 0 2;-1 0 1]; % sobel operator in vertical gx = conv2(c,s1,'same'); gy = conv2(c,s2,'same'); M = sqrt(gx.*gx+gy.*gy); % maginitude figure % image after convolution with horizontal sobel operator imshow(gx); figure % image after convolution with vertical sobel operator imshow(gy) g = gx+gy; % image with horizontal gx plus vertical gy figure
  • 3. imshow(g) % historgram stem of image I = gpuArray(imread('E:myimages促kidney.tif')); [counts,x] = imhist(I); stem(x,counts); BW = im2bw(f,T); figure imshow(BW) (c). Figure before smoothing Figure after smoothing To produce a binary image thatsegments the large blood vessel in the center of the image.
  • 4. Convolution with horizontal sobel Convolution with vertical sobel Below is the horizontal of kidney.tif 0 50 100 150 200 250 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 x 10 4 Stem of kindney.tif Intensity level Numberofpixels
  • 5. By looking at the histogram of the gradient image,we find we can set T equal around 7.And get the binary figure below which show us the blood vessel. 0 0.5 1 1.5 2 2.5 3 x 10 4 Histogram of kindney.tif Intensity level Numberofpixels 0 50 100 150 200 250
  • 6. Code: f=imread('E:myimages促kidney.tif'); figure imshow(f); %original figure w=ones(3)/9; %filter from (a) c = conv2(f,w,'same'); %image after convolution with mask s1 = [-1 -2 -1;0 0 0;1 2 1]; % sobel operator in horizontal s2 = [-1 0 1;-2 0 2;-1 0 1]; % sobel operator in vertical gx = conv2(c,s1,'same'); gy = conv2(c,s2,'same'); M = sqrt(gx.*gx+gy.*gy); % maginitude figure % image after convolution with horizontal sobel operator imshow(gx); figure % image after convolution with vertical sobel operator imshow(gy) g = gx+gy; % image with horizontal gx plus vertical gy figure imshow(g) %historgram stem of image [counts,x] = imhist(f); stem(x,counts); title('Stem of kindney.tif') xlabel('Intensity level') ylabel('Number of pixels') axis([0 256 0 20000]) figure; imhist(f,64) title('Histogram of kindney.tif') xlabel('Intensity level') ylabel('Number of pixels') BW = im2bw(f,0.72); figure imshow(BW)
  • 7. Problem 2 Global thresholding Otsus thresholding Write a global thresholding program in which the threshold is estimated automatically using the procedure discussed in Section 10.3.2. The output of your program should be a segmented (binary) image. Use your program to segment noisy_fingerprint.tiff and produce a segmented image. Basic idea for designing process as below. 1. input x is a vector. output T is an estimated threshold that groups x 2. into 2 clusters using the algorithm of basic global thresholding 3. procesures: 1) Randomly select an initial estimate for T. 2) Segment the signal using T, which will yield two groups, G1 consisting of all points with values<=T and G2 consisting of points with value>T. 3) Compute the average distance between points of G1 and T, and points of G2 and T. 4) Compute a new threshold value T=(M1+M2)/2 5) Repeat steps 2 through 4 until the change of T is smaller enough. Binary Figure Code:
  • 8. function level = g_t(I) I = imread('E:myimages促noisy_fingerprint.tif'); % STEP 1: Compute mean intensity of image from histogram, set T=mean(I) [counts,N]=imhist(I); i=1; mu=cumsum(counts); T(i)=(sum(N.*counts))/mu(end); T(i)=round(T(i)); % STEP 2: compute Mean above T (MAT) and Mean below T (MBT) using T from % step 1 mu2=cumsum(counts(1:T(i))); MBT=sum(N(1:T(i)).*counts(1:T(i)))/mu2(end); mu3=cumsum(counts(T(i):end)); MAT=sum(N(T(i):end).*counts(T(i):end))/mu3(end); i=i+1; % new T = (MAT+MBT)/2 T(i)=round((MAT+MBT)/2); % STEP 3 to n: repeat step 2 if T(i)~=T(i-1) while abs(T(i)-T(i-1))>=1 mu2=cumsum(counts(1:T(i))); MBT=sum(N(1:T(i)).*counts(1:T(i)))/mu2(end); mu3=cumsum(counts(T(i):end)); MAT=sum(N(T(i):end).*counts(T(i):end))/mu3(end); i=i+1; T(i)=round((MAT+MBT)/2); Threshold=T(i); end % Normalize the threshold to the range [i, 1]. level = (Threshold - 1) / (N(end) - 1); BW = im2bw(I,level); imshow(BW)
  • 9. Problem 3 Otsus thresholding (a) Implement Otsus optimum thresholding algorithm given in Section 10.3.3. Use your implementation of Otsus algorithm to segment polymersomes.tiff (b) Use the global thresholding algorithm from Problem 2 to segment polymersomes.tiff and compare the result with the segmented image obtained in Part (a). a) Original After Otsu b)
  • 10. Compare the result for a) and b) We see the Otsu has a better performance than global,because Otsu is based on the histogram,so we can get a good threshold value than in global thresholding.In Otsu,We cant change the distributions, but we can adjust where we separate them (the threshold). As we adjust the threshold one way, we increase the spread of one and decrease the spread of the other. Code: I = imread('E:myimages促polymersomes.tif'); mgk = 0; mt = 0; [m,n] = size(I); h = imhist(I); pi = h/(m.*n); for i=1:1:256 if pi(i)~=0 lv=i; break end end for i=256:-1:1 if pi(i)~=0 hv=i; break end end lh = hv - lv; for k = 1:256 p1(k)=sum(pi(1:k)); p2(k)=sum(pi(k+1:256)); end for k=1:256 m1(k)=sum((k-1)*pi(1:k))/p1(k); m2(k)=sum((k-1)*pi(k+1:256))/p2(k); end for k=1:256 mgk=(k-1)*pi(k)+mgk; end for k =1:256 var(k)=p1(k)*(m1(k)-mgk)^2+p2(k)*(m2(k)-mgk)^2; end [y,T]=max(var(:)); T=T+lv;