-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathmain.js
More file actions
324 lines (305 loc) · 10.3 KB
/
Copy pathmain.js
File metadata and controls
324 lines (305 loc) · 10.3 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
const PROCESS_OPERATIONS = require("../../operations");
const ReadImage = require("../operator/basic/ReadImage");
const WriteImage = require("../operator/basic/WriteImage");
const GrayImage = require("../operator/convertions/GrayImage");
const GrayToBinary = require("../operator/convertions/GrayToBinary");
const Blur = require("../operator/blurring/Blur");
const GaussianBlur = require("../operator/blurring/GaussianBlur");
const MedianBlur = require("../operator/blurring/MedianBlur");
const DrawArrowLine = require("../operator/drawing/DrawArrowLine");
const DrawCircle = require("../operator/drawing/DrawCircle");
const DrawEllipse = require("../operator/drawing/DrawEllipse");
const DrawLine = require("../operator/drawing/DrawLine");
const DrawRectangle = require("../operator/drawing/DrawRectangle");
const DrawText = require("../operator/drawing/DrawText");
const CannyEdge = require("../operator/edgeDetection/CannyEdge");
const Laplacian = require("../operator/edgeDetection/Laplacian");
const ScharrDerivative = require("../operator/edgeDetection/ScharrDerivative");
const SobleDerivative = require("../operator/edgeDetection/SobleDerivative");
const BilateralFilter = require("../operator/filtering/BilateralFilter");
const BoxFilter = require("../operator/filtering/BoxFilter");
const Dilation = require("../operator/filtering/Dilation");
const Erosion = require("../operator/filtering/Erosion");
const Morphological = require("../operator/filtering/Morphological");
const PyramidDown = require("../operator/filtering/PyramidDown");
const PyramidUp = require("../operator/filtering/PyramidUp");
const AffineImage = require("../operator/geometric/AffineImage");
const ReflectImage = require("../operator/geometric/ReflectImage");
const RotateImage = require("../operator/geometric/RotateImage");
const ScaleImage = require("../operator/geometric/ScaleImage");
const AdaptiveThreshold = require("../operator/thresholding/AdaptiveThresholding");
const ApplyThreshold = require("../operator/thresholding/ApplyThreshold");
class MainController {
// This private field is used to store the applied operators in the workspace
#appliedOperators;
// This holds the original image added by the user
#originalImage;
//Instead of directly exporting the image, the processed image is stored
#processedImage;
constructor() {
this.#appliedOperators = [];
this.#originalImage = null;
this.#processedImage = null;
}
/**
*
* @param {String} blockId
* This function takes the ID of the block and deletes
* it from the array
*/
deleteBlock(blockId) {
const index = this.#appliedOperators.findIndex(
(item) => item.blockId === blockId
);
this.#appliedOperators.splice(index, 1);
}
/**
* This function is responsible for making the blocks in order
* according to the order in the workspace
* @param {String} parent
* @param {String} child
* @returns none
*/
arrangeBlocks(parent, child) {
const parentIndex = this.#appliedOperators.findIndex(
(item) => item.type === parent
);
const childIndex = this.#appliedOperators.findIndex(
(item) => item.type === child
);
// If the parent is not found then the index will be -1
// If the child is not found then the index will be -1
if (parentIndex === -1 || childIndex === -1) {
return;
}
// If the child is next to the parent then we do need to done anything
if (parentIndex + 1 === childIndex) {
return;
}
const childElement = this.#appliedOperators[childIndex];
this.#appliedOperators.splice(childIndex, 1);
this.#appliedOperators.splice(parentIndex + 1, 0, childElement);
}
/**
* This method set the original image
* @param {Mat Image} image
*/
setOriginalImage(image) {
this.#originalImage = image;
}
/**
* This methods returns the original image
* @returns Image
*/
getOriginalImage() {
return this.#originalImage;
}
/**
*
*/
getProcessedImage() {
return this.#processedImage;
}
/**
* This function generates the operator object accroding to the string passed
* @param {String} operatorType
* @returns
*/
addOperator(operatorType, id) {
switch (operatorType) {
case PROCESS_OPERATIONS.READIMAGE:
this.#appliedOperators.push(
new ReadImage(PROCESS_OPERATIONS.READIMAGE, id)
);
break;
case PROCESS_OPERATIONS.WRITEIMAGE:
this.#appliedOperators.push(
new WriteImage(PROCESS_OPERATIONS.WRITEIMAGE, id)
);
break;
case PROCESS_OPERATIONS.GRAYIMAGE:
this.#appliedOperators.push(
new GrayImage(PROCESS_OPERATIONS.GRAYIMAGE, id)
);
break;
case PROCESS_OPERATIONS.GRAYTOBINARY:
this.#appliedOperators.push(
new GrayToBinary(PROCESS_OPERATIONS.GRAYTOBINARY, id)
);
break;
case PROCESS_OPERATIONS.REFLECTIMAGE:
this.#appliedOperators.push(
new ReflectImage(PROCESS_OPERATIONS.REFLECTIMAGE, id)
);
break;
case PROCESS_OPERATIONS.ROTATEIMAGE:
this.#appliedOperators.push(
new RotateImage(PROCESS_OPERATIONS.ROTATEIMAGE, id)
);
break;
case PROCESS_OPERATIONS.AFFINEIMAGE:
this.#appliedOperators.push(
new AffineImage(PROCESS_OPERATIONS.AFFINEIMAGE, id)
);
break;
case PROCESS_OPERATIONS.SCALEIMAGE:
this.#appliedOperators.push(
new ScaleImage(PROCESS_OPERATIONS.SCALEIMAGE, id)
);
break;
case PROCESS_OPERATIONS.DRAWLINE:
this.#appliedOperators.push(
new DrawLine(PROCESS_OPERATIONS.DRAWLINE, id)
);
break;
case PROCESS_OPERATIONS.DRAWELLIPSE:
this.#appliedOperators.push(
new DrawEllipse(PROCESS_OPERATIONS.DRAWELLIPSE, id)
);
break;
case PROCESS_OPERATIONS.DRAWARROWLINE:
this.#appliedOperators.push(
new DrawArrowLine(PROCESS_OPERATIONS.DRAWARROWLINE, id)
);
break;
case PROCESS_OPERATIONS.DRAWTEXT:
this.#appliedOperators.push(
new DrawText(PROCESS_OPERATIONS.DRAWTEXT, id)
);
break;
case PROCESS_OPERATIONS.DRAWCIRCLE:
this.#appliedOperators.push(
new DrawCircle(PROCESS_OPERATIONS.DRAWCIRCLE, id)
);
break;
case PROCESS_OPERATIONS.DRAWRECTANGLE:
this.#appliedOperators.push(
new DrawRectangle(PROCESS_OPERATIONS.DRAWRECTANGLE, id)
);
break;
case PROCESS_OPERATIONS.BLURIMAGE:
this.#appliedOperators.push(new Blur(PROCESS_OPERATIONS.BLURIMAGE, id));
break;
case PROCESS_OPERATIONS.GAUSSIANBLUR:
this.#appliedOperators.push(
new GaussianBlur(PROCESS_OPERATIONS.GAUSSIANBLUR, id)
);
break;
case PROCESS_OPERATIONS.MEDIANBLUR:
this.#appliedOperators.push(
new MedianBlur(PROCESS_OPERATIONS.MEDIANBLUR, id)
);
break;
case PROCESS_OPERATIONS.BILATERALFILTER:
this.#appliedOperators.push(
new BilateralFilter(PROCESS_OPERATIONS.BILATERALFILTER, id)
);
break;
case PROCESS_OPERATIONS.BOXFILTER:
this.#appliedOperators.push(
new BoxFilter(PROCESS_OPERATIONS.BOXFILTER, id)
);
break;
case PROCESS_OPERATIONS.PYRAMIDUP:
this.#appliedOperators.push(
new PyramidUp(PROCESS_OPERATIONS.PYRAMIDUP, id)
);
break;
case PROCESS_OPERATIONS.PYRAMIDDOWN:
this.#appliedOperators.push(
new PyramidDown(PROCESS_OPERATIONS.PYRAMIDDOWN, id)
);
break;
case PROCESS_OPERATIONS.EROSION:
this.#appliedOperators.push(
new Erosion(PROCESS_OPERATIONS.EROSION, id)
);
break;
case PROCESS_OPERATIONS.DILATION:
this.#appliedOperators.push(
new Dilation(PROCESS_OPERATIONS.DILATION, id)
);
break;
case PROCESS_OPERATIONS.MORPHOLOGICAL:
this.#appliedOperators.push(
new Morphological(PROCESS_OPERATIONS.MORPHOLOGICAL, id)
);
break;
case PROCESS_OPERATIONS.SOBLEDERIVATE:
this.#appliedOperators.push(
new SobleDerivative(PROCESS_OPERATIONS.SOBLEDERIVATE, id)
);
break;
case PROCESS_OPERATIONS.SCHARRDERIVATE:
this.#appliedOperators.push(
new ScharrDerivative(PROCESS_OPERATIONS.SCHARRDERIVATE, id)
);
break;
case PROCESS_OPERATIONS.LAPLACIAN:
this.#appliedOperators.push(
new Laplacian(PROCESS_OPERATIONS.LAPLACIAN, id)
);
break;
case PROCESS_OPERATIONS.CANNYEDGE:
this.#appliedOperators.push(
new CannyEdge(PROCESS_OPERATIONS.CannyEdge, id)
case PROCESS_OPERATIONS.ADAPTIVETHRESHOLDING:
this.#appliedOperators.push(
new AdaptiveThreshold(PROCESS_OPERATIONS.ADAPTIVETHRESHOLDING, id)
case PROCESS_OPERATIONS.SIMPLETHRESHOLDING:
this.#appliedOperators.push(
new ApplyThreshold(PROCESS_OPERATIONS.SIMPLETHRESHOLDING, id)
);
break;
default:
break;
}
}
/**
* This method compute and generate the output of the selected operators
*/
computeAll() {
if (this.#appliedOperators.length === 0) {
throw Error("No operators are added to the workspace");
}
if (this.#appliedOperators[0]?.type !== PROCESS_OPERATIONS.READIMAGE) {
throw Error("Read Image block is not added");
}
if (this.#originalImage === null) {
throw Error("Image is not set");
}
var image = this.#originalImage;
this.#appliedOperators.forEach((item) => {
if (image) {
image = item.compute(image);
if(image) {
this.#processedImage = image;
}
}
});
}
/**
* This function changes the attributes of the
* Operator type
* @param {String} blockType
* @param {String} paramType
* @param {String} value
*/
changeValuesOfBlocks(blockType, paramType, value) {
const block = this.#appliedOperators.find(
(item) => item.type === blockType
);
if (block) {
try {
block.setParams(paramType, value);
} catch (error) {
throw error;
}
}
}
resetTheWorkpace() {
this.#appliedOperators = [];
this.#originalImage = null;
}
}
module.exports = MainController;