-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFinal_project_script_2.m
107 lines (91 loc) · 3.8 KB
/
Final_project_script_2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
% Define the filter size we will use in step 2:
filtsize = 85;
% Creating test image 'im' by splicing together two built in images.
% Also zero-padding (adding zeros around the border) with half the
% filter size (filtsize) we will use so that the filter could be
% centered on any actual image pixel, including those near the border.
% 'coins.png' contains bright nickels and dimes on a dark background
% 'eight.tif' contains dark quarters on a bright background, so we invert it
% to match 'coins.png'
im1 = imread('coins.png');
[r,c] = size(im1);
im2 = imread('eight.tif');
[r2,c2] = size(im2);
filtsizeh = floor(filtsize/2);
im = zeros(r+r2+filtsize,c+filtsize);
im(filtsizeh+1:filtsizeh+r+r2,filtsizeh+1:filtsizeh+c) = [im1;255-im2(:,1:c)];
[r,c] = size(im);
imagesc(im);colormap(gray);title('test image');axis equal;
% Initializing assessed/displayed variables as empty so that code is executable
msk=[]; msk_dil=[]; msk_dil_erd=[]; centroid=[]; component_size=[];
%%%%% 1. Localize the centroid of each coin
% Otsu threshold
msk = OtsuThreshold(im);
figure; imagesc(msk); colormap(gray); title('Otsu'); axis equal;
% Dilate 9x9
msk_dil = imdilate(msk,ones(9,9));
figure; imagesc(msk_dil); colormap(gray); title('Dilated'); axis equal;
% Erode 23x23
msk_dil_erd = imerode(msk_dil,ones(23,23));
figure; imagesc(msk_dil_erd); colormap(gray); title('Eroded'); axis equal;
% Connected components to get centroids of coins:
cc = bwconncomp(msk_dil_erd);
props_struct = regionprops(cc);
centroid = zeros(length(props_struct),2);
component_size = zeros(length(props_struct),1);
for i=1:length(props_struct)
centroid(i,:) = round(props_struct(i).Centroid);
component_size(i) = props_struct(i).Area;
end
%%%%% 2. Measure features for each coin using a bank of matching filters
% make matching filters to create features
% Define diameters to use for filters
dimediameter = 31;
quarterdiameter = 51;
nickeldiameter = 41;
% Initialize assessed variables
D=[]; nickelfilter = []; dimefilter = []; quarterfilter = [];
% Use the MakeCircleMatchingFilter function to create matching filters for dimes, nickels, and quarters
% (This is in a separate Matlab grader problem. Save your work,
% complete the corresponding grader problem and embed the solution
% in the helper function list below.)
dimefilter = MakeCircleMatchingFilter(dimediameter,filtsize);
nickelfilter = MakeCircleMatchingFilter(nickeldiameter,filtsize);
quarterfilter = MakeCircleMatchingFilter(quarterdiameter,filtsize);
figure;
subplot(1,3,1); imagesc(dimefilter); colormap(gray); title('dime filter'); axis tight equal;
subplot(1,3,2); imagesc(nickelfilter); colormap(gray); title('nickel filter'); axis tight equal;
subplot(1,3,3); imagesc(quarterfilter); colormap(gray); title('quarter filter'); axis tight equal;
% Evaluate each of the 3 matching filters on each coin to serve as 3 feature measurements
D=zeros(length(centroid),3);
for i=1:length(centroid)
sliced=msk_dil_erd(centroid(i,2)-filtsizeh:centroid(i,2)+filtsizeh,centroid(i,1)-filtsizeh:centroid(i,1)+filtsizeh);
sliced=sliced(:);
D(i,1)=corr(dimefilter(:),sliced);
D(i,2)=corr(nickelfilter(:),sliced);
D(i,3)=corr(quarterfilter(:),sliced);
end
D
%%%%%%%%%%%%%%%%%%%% Helper Functions %%%%%%%%%%%%%%%%%%%%%
function [msk,thrsh] = OtsuThreshold(im)
hst = imhist(im);
res = otsuthresh(hst);
thrsh = res*255;
msk = im>thrsh;
end
function [filter,xc,yc] = MakeCircleMatchingFilter(diameter,W)
% initialize filter
filter = zeros([W W]);
% define coordinates for the center of the WxW filter
xc=(W+1)/2;
yc=(W+1)/2;
r = diameter/2;
% Use double-for loops to check if each pixel lies in the foreground of the circle
for i = 1:size(filter)
for j = 1:size(filter)
if double((i-yc).^2 + (j-xc).^2)<= r.^2
filter(i,j)=1;
end
end
end
end