-
Notifications
You must be signed in to change notification settings - Fork 0
/
thesis_process.m
230 lines (185 loc) · 5.86 KB
/
thesis_process.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
function varargout = thesis_process(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @thesis_process_OpeningFcn, ...
'gui_OutputFcn', @thesis_process_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function thesis_process_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = thesis_process_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --- Executes on button press in importImage.
function importImage_Callback(hObject, eventdata, handles)
%Import Image
%error = imread('C:\Users\jesse\Desktop\error.jpg');
[a,b] = uigetfile({'*.png';'*.jpg'},'Pick an Image(.png,.jpg) File');
Original = imread([b a]);
imshow(Original,'Parent',handles.originalImage);
angleFinal = 0;
angleFinalOld = 0;
%Convert Original to Grayscale
Grayscale = rgb2gray(Original);
%Binarization of Image
BW = imbinarize(Grayscale);
%Invert Image
BW2 = imcomplement(BW);
BW2 = bwareaopen(BW2,10);
%Old Algorithm
tic
try
[rows,cols] = size(BW2);
ctr = false;
for col = 1:cols
for row = 1:rows
if BW2(row,col) == 1
if ctr == false
leftmost = [row,col];
ctr = true;
end
end
end
end
ctr = false;
for row = 1:rows
for col = 1:cols
if BW2(row,col) == 1
if ctr == false
topmost = [row,col];
ctr = true;
end
end
end
end
fpX = topmost(2);
fpY = topmost(1);
spX = leftmost(2);
spY = leftmost(1);
halfc = col/2;
if topmost(2) < halfc
rotation = 'cclockwise';
else
rotation = 'clockwise';
end
if rotation == "cclockwise"
slope = (spY - fpY)/(spX - fpX);
angleNew = atan(slope);
angleFinalOld = 90+(angleNew*180)/pi;
angleFinalOlddisp = -angleFinalOld;
elseif rotation == "clockwise"
slope = (spY - fpY)/(spX - fpX);
angleNew = atan(slope);
angleFinalOld = (angleNew*180)/pi;
angleFinalOlddisp = -angleFinalOld;
end
rotatedOld = imrotate(BW2, angleFinalOld,'crop');
catch
end
oldAlgoRt = toc;
tic
try
%New algorithm
%Get centroids
dots = regionprops(BW2, Grayscale, 'all');
numberOfBlobs = size(dots, 1);
%disp(numberOfBlobs);
%Stored centroids of dots
allBlobCentroids = [dots.Centroid];
centroidsX = allBlobCentroids(1:2:end-1);
centroidsY = allBlobCentroids(2:2:end);
dataval=[centroidsX;centroidsY];
%disp(dataval);
%Get convex hull
initHull = convhull(centroidsX,centroidsY);
finalHull = initHull;
lastHull = length(initHull);
finalHull(lastHull) = [];
%disp(finalHull);
%Algorithm Start
ctr = 1;
counter = true;
lengthHull = length(finalHull);
while counter == true
%Initialization of first point
if ctr == 1
first = lengthHull;
else
first = ctr-1;
end
%Initialization of first point
mid = ctr;
%Initialization of last point
if ctr == lengthHull
last = 1;
else
last = ctr+1;
end
%Compute the degree between the three points
if ctr > length(finalHull)
break;
end
midP = [centroidsX(finalHull(mid)), centroidsY(finalHull(mid))];
lastP = [centroidsX(finalHull(last)), centroidsY(finalHull(last))];
firstP = [centroidsX(finalHull(first)), centroidsY(finalHull(first))];
deg = atan2(abs(det([firstP-midP;lastP-midP])),dot(firstP-midP,lastP-midP));
finalDeg = deg * (180/pi);
%disp(finalDeg)
%Condition if the midpoint is to be removed
if finalDeg > 170
finalHull(mid) = [];
lengthHull = length(finalHull);
else
ctr = ctr + 1;
end
if mid == lengthHull+1
counter = false;
end
end
finalHull(lengthHull+1) = finalHull(1);
finalHullCentroidsX = centroidsX(finalHull);
finalHullCentroidsY = centroidsY(finalHull);
ctrArray = 1;
maxPointsInLine = 0;
while ctrArray < length(finalHull)
lineRecX = [finalHullCentroidsX(ctrArray)+20,finalHullCentroidsX(ctrArray)-20,finalHullCentroidsX(ctrArray+1)-50,finalHullCentroidsX(ctrArray+1)+50,finalHullCentroidsX(ctrArray)+50];
lineRecY = [finalHullCentroidsY(ctrArray)+20,finalHullCentroidsY(ctrArray)-20,finalHullCentroidsY(ctrArray+1)-50,finalHullCentroidsY(ctrArray+1)+50,finalHullCentroidsY(ctrArray)+50];
inside = inpolygon(centroidsX,centroidsY,lineRecX,lineRecY);
numOfPoints = sum(inside(:) == 1);
%disp(in);
%disp(numOfPoints);
if numOfPoints >= maxPointsInLine
maxPointsInLine = numOfPoints;
currentBasisLine = [finalHull(ctrArray),finalHull(ctrArray+1)];
end
ctrArray = ctrArray + 1;
end
slope = (centroidsY(currentBasisLine(2)) - centroidsY(currentBasisLine(1))) ./ (centroidsX(currentBasisLine(2)) - centroidsX(currentBasisLine(1)));
angleNew = atan(slope);
angleFinal = (angleNew*180)/pi;
angleFinaldisp = -angleFinal;
rotatedNew = imrotate(BW2, angleFinal,'crop');
catch
%imshow(error,'Parent',handles.new);
end
newAlgoRt = toc;
imshow(rotatedOld,'Parent',handles.old);
imshow(rotatedNew,'Parent',handles.new);
set(handles.angleOld, 'String', angleFinalOlddisp);
set(handles.angleNew, 'String', angleFinaldisp);
set(handles.rtOld, 'String', oldAlgoRt);
set(handles.rtNew, 'String', newAlgoRt);
% --- Executes on button press in reset.
function reset_Callback(hObject, eventdata, handles)
cla(handles.originalImage);
cla(handles.old);
cla(handles.new);