-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsw_M11_B.m
More file actions
76 lines (76 loc) · 1.92 KB
/
csw_M11_B.m
File metadata and controls
76 lines (76 loc) · 1.92 KB
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
%M11B: Finding Pennies in the Image
clc;clear;
filename = input('Enter image name: ','s');
img = imread(filename);
[h,w,c] = size(img);
binary = zeros(h,w);
rows = [0 0 0];
cols = [0 0 0];
count = 1;
pennies = 0;
for ii=1:1:h
for jj=1:1:w
if (img(ii,jj,1)>1.5*img(ii,jj,3) && img(ii,jj,1)>80) || (img(ii,jj,1)>1.3*img(ii,jj,3) && img(ii,jj,1)>190)
binary(ii,jj) = 1;
rows(count)=ii;
cols(count)=jj;
count = count+1;
end
end
end
iishift=0;
jjshift=0;
for ii=1:1:h
if iishift>0
iishift = iishift-1;
continue;
end
for jj=1:1:w
if jjshift>0
jjshift = jjshift-1;
continue;
end
if binary(ii,jj)==1
areaup = 0;
for aa=ii:-1:1
if binary(aa,jj)==1
areaup = areaup + 1;
else
break;
end
end
areadown = 0;
for aa=ii:1:h
if binary(aa,jj)==1
areadown = areadown + 1;
else
break;
end
end
arealeft = 0;
for bb=jj:-1:1
if binary(ii,bb)==1
arealeft = arealeft + 1;
else
break;
end
end
arearight = 0;
for bb=jj:1:w
if binary(ii,bb)==1
arearight = arearight + 1;
else
break;
end
end
area = areaup+areadown+arealeft+arearight;
if area>w/8
pennies = pennies+1;
end
iishift=aa-ii;
jjshift=bb-jj;
end
end
end
% imshowpair(img,binary,'montage')
fprintf('Number of pennies: %g',pennies)