-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathivp_hw_0_2.m
151 lines (120 loc) · 3.17 KB
/
ivp_hw_0_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
I = imread('rlcinput.png');
%figure('name','Image'),imshow(I);
%figure('name','Histogram'),imhist(I);
level = graythresh(I);
J = im2bw(I,level);
%figure('name','Otsus segmentation'),imshow(J);
dimJ = size(J);
R = zeros(dimJ);
k = 1;
scount = 1;
J(1,:) = 1;
J(:,1) = 1;
for i = 2:dimJ(1)
for j = 2:dimJ(2)
if J(i,j) == 0
if J(i,j-1) == 1 && J(i-1,j) == 1
R(i,j) = k;
k = k + 1;
elseif J(i,j-1) == 1 && J(i-1,j) == 0
R(i,j) = R(i-1,j);
elseif J(i,j-1) == 0 && J(i-1,j) == 1
R(i,j) = R(i,j-1);
elseif J(i,j-1) == 0 && J(i-1,j) == 0
R(i,j) = R(i-1,j);
if R(i,j-1) ~= R(i-1,j)
for l = 1:dimJ(1)
for m = 1:dimJ(2)
if R(l,m) == R(i,j-1)
R(l,m) = R(i-1,j);%tracing back
end
end
end
end
end
end
end
end
figure('name','segmented'),imshow(J);
sign = [1,1,0];
flag = 0;
for i = 2:dimJ(1)
for j = 2:dimJ(1)
if R(i,j) ~= 0
dimS = size(sign);
for k = 1:dimS(1)%checks which region the current pixel belongs to and increments that particular value
if R(i,j) == sign(k,2)
sign(k,3) = sign(k,3) + 1;%gives the total number of pixels in that region
flag = 1;
break
end
end
if flag == 0 %new region
sign = [sign;dimS(1)+1,R(i,j),1];%adding a new roww to the matrix
end
flag = 0;
end
end
end
%identifying the region with maximum pixels%
dimS = size(sign);
mx = max(sign(:,3));
for i = 1:dimS(1)
if sign(i,3) == mx
obj = sign(i,2);
break
end
end
%%modifying the program
%%making the mx go to zero
for i = 1:dimS(1)
if sign(i,3) == mx
sign(i,3)=1;
break
end
end
mx1 = max(sign(:,3));%%second maxima
for i = 1:dimS(1)
if sign(i,3) == mx1
obj1 = sign(i,2);
break
end
end
if mx1<(mx/2)
obj1=obj;
end
for i = 1:dimS(1)
if sign(i,3) == mx1
sign(i,3)=1;
break
end
end
mx2 = max(sign(:,3));%%second maxima
S = zeros(dimJ);
for i = 1:dimJ(1)
for j = 1:dimJ(2)
if R(i,j) == obj||R(i,j)== obj1
S(i,j) = 0;
else
S(i,j) = 1;
end
end
end
figure('name','Object Plot'),imshow(S);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%done obtaining only the
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%circuit%%%%%%%%%%%%%%%%%%%%%%%%%%%%
grad=zeros(dimJ);
for i = 2:dimJ(1)-1
for j = 2:dimJ(2)-1
grad(i,j)=S(i+1,j+1)-S(i,j);%%x gradient
end
end
% figure('name','Object Plot'),imagesc(grad);
grad1=zeros(dimJ);
for i = 2:dimJ(1)-1
for j = 2:dimJ(2)-1
grad1(i,j)=grad(i+1,j+1)-grad(i,j);%%x gradient
end
end
% figure('name','Object Plot'),imagesc(grad1);
%%try to fit a polynomial