229 | | Mat horizontal = original.clone(); |
230 | | Mat vertical = original.clone(); |
231 | | // Specify size on horizontal axis |
232 | | int horizontal_size = horizontal.cols() / 30; |
233 | | // Create structure element for extracting horizontal lines through morphology operations |
234 | | Mat horizontalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(horizontal_size,1)); |
235 | | // Apply morphology operations |
236 | | Imgproc.erode(horizontal, horizontal, horizontalStructure); |
237 | | Imgproc.dilate(horizontal, horizontal, horizontalStructure); |
238 | | // Show extracted horizontal lines |
239 | | showWaitDestroy("horizontal" , horizontal); |
240 | | // Specify size on vertical axis |
241 | | int vertical_size = vertical.rows() / 30; |
242 | | // Create structure element for extracting vertical lines through morphology operations |
243 | | Mat verticalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size( 1,vertical_size)); |
244 | | // Apply morphology operations |
245 | | Imgproc.erode(vertical, vertical, verticalStructure); |
246 | | Imgproc.dilate(vertical, vertical, verticalStructure); |
247 | | // Show extracted vertical lines |
248 | | showWaitDestroy("vertical", vertical); |
249 | | // Inverse vertical image |
250 | | Core.bitwise_not(vertical, vertical); |
251 | | showWaitDestroy("vertical_bit" , vertical); |
252 | | // Extract edges and smooth image according to the logic |
253 | | // 1. extract edges |
254 | | // 2. dilate(edges) |
255 | | // 3. src.copyTo(smooth) |
256 | | // 4. blur smooth img |
257 | | // 5. smooth.copyTo(src, edges) |
258 | | // Step 1 |
259 | | Mat edges = new Mat(); |
260 | | Imgproc.adaptiveThreshold(vertical, edges, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 3, -2); |
261 | | showWaitDestroy("edges", edges); |
262 | | // Step 2 |
263 | | Mat kernel = Mat.ones(2, 2, CvType.CV_8UC1); |
264 | | Imgproc.dilate(edges, edges, kernel); |
265 | | showWaitDestroy("dilate", edges); |
266 | | // Step 3 |
267 | | Mat smooth = new Mat(); |
268 | | vertical.copyTo(smooth); |
269 | | // Step 4 |
270 | | Imgproc.blur(smooth, smooth, new Size(2, 2)); |
271 | | // Step 5 |
272 | | smooth.copyTo(vertical, edges); |
273 | | // Show final result |
274 | | showWaitDestroy("smooth - final", vertical); |
275 | | System.exit(0); |
| 226 | |
| 227 | //dynamic morphology?? |
| 228 | if(codeVersion == 1) { |
| 229 | int hori = original.width(); |
| 230 | int vert = original.height(); |
| 231 | //Find ratio between 100 and width and 100 and height |
| 232 | int divX = hori/10; |
| 233 | int divY = vert/10; |
| 234 | int sizeX = (hori/divX) * 10; |
| 235 | int sizeY = (vert/divY) * 10; |
| 236 | |
| 237 | Mat test = original.clone(); |
| 238 | showWaitDestroy("Original", test); |
| 239 | |
| 240 | System.out.println("hori: " + hori + '\t' + "vert: " + vert); |
| 241 | System.out.println("sizeX: " + sizeX + '\t' + "sizeY: " + sizeY); |
| 242 | |
| 243 | Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX, (sizeY/100))); |
| 244 | Imgproc.erode(test,test,kernelErode); |
| 245 | showWaitDestroy("01 Erode", test); |
| 246 | |
| 247 | Mat kernelDialate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX,(sizeY/10))); |
| 248 | Imgproc.dilate(test, test, kernelDialate); |
| 249 | showWaitDestroy("02 Dialate", test); |
| 250 | |
| 251 | Mat kernelErodeAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/5))); |
| 252 | Imgproc.erode(test,test,kernelErodeAgain); |
| 253 | showWaitDestroy(" 03 Erode Again", test); |
| 254 | |
| 255 | Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10)*3,(sizeY/10)*3)); |
| 256 | Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelClose); |
| 257 | showWaitDestroy("04 Close", test); |
| 258 | |
| 259 | Imgproc.adaptiveThreshold(test, test,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C); |
| 260 | showWaitDestroy("05 Binarized", test); |
| 261 | |
| 262 | Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/20))); |
| 263 | Imgproc.morphologyEx(test,test,Imgproc.MORPH_OPEN, kernelOpen); |
| 264 | showWaitDestroy(" 06 Open", test); |
| 265 | |
| 266 | Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/5),(sizeY/100))); |
| 267 | Imgproc.dilate(test, test, kernelDialateAgain); |
| 268 | showWaitDestroy("07 Dialate", test); |
| 269 | |
| 270 | |
| 271 | Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/2))); |
| 272 | Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelCloseAgain); |
| 273 | showWaitDestroy(" 08 Close Again (Final)", test); |
| 274 | } |
| 275 | //Successful hardcode for morhpology |
| 276 | if (codeVersion == 2) { |
| 277 | Mat test = original.clone(); |
| 278 | showWaitDestroy("00 Original", test); |
| 279 | |
| 280 | Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,1)); |
| 281 | Imgproc.erode(test,test,kernelErode); |
| 282 | showWaitDestroy("01 Erode", test); |
| 283 | |
| 284 | Mat kernelDialate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,10)); |
| 285 | Imgproc.dilate(test, test, kernelDialate); |
| 286 | showWaitDestroy("02 Dialate", test); |
| 287 | |
| 288 | Mat kernelErodeAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,20)); |
| 289 | Imgproc.erode(test,test,kernelErodeAgain); |
| 290 | showWaitDestroy(" 03 Erode Again", test); |
| 291 | |
| 292 | Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(50,30)); |
| 293 | Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelClose); |
| 294 | showWaitDestroy("04 Close", test); |
| 295 | |
| 296 | Imgproc.adaptiveThreshold(test, test,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C); |
| 297 | showWaitDestroy("05 Binarized", test); |
| 298 | |
| 299 | Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4)); |
| 300 | Imgproc.morphologyEx(test,test,Imgproc.MORPH_OPEN, kernelOpen); |
| 301 | showWaitDestroy(" 06 Open", test); |
| 302 | |
| 303 | // Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,10)); |
| 304 | // Imgproc.dilate(test, test, kernelDialateAgain); |
| 305 | // showWaitDestroy("07 Dialate", test); |
| 306 | |
| 307 | //FIGURE OUT FLOOD FILL!! |
| 308 | Imgproc.floodFill(test,test, new Point(1,1), new Scalar(2)); |
| 309 | |
| 310 | |
| 311 | Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,50)); |
| 312 | Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelCloseAgain); |
| 313 | showWaitDestroy(" 08 Close Again (Final)", test); |
| 314 | |
| 315 | } |
| 316 | //Tutorial/Demo Code |
| 317 | if (codeVersion == 3) { |
| 318 | Mat horizontal = original.clone(); |
| 319 | Mat vertical = original.clone(); |
| 320 | // Specify size on horizontal axis |
| 321 | int horizontal_size = horizontal.cols() / 50; |
| 322 | // Create structure element for extracting horizontal lines through morphology operations |
| 323 | Mat horizontalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(horizontal_size, 2)); |
| 324 | // Apply morphology operations |
| 325 | Imgproc.erode(horizontal, horizontal, horizontalStructure); |
| 326 | Imgproc.dilate(horizontal, horizontal, horizontalStructure); |
| 327 | // Show extracted horizontal lines |
| 328 | showWaitDestroy("horizontal", horizontal); |
| 329 | // Specify size on vertical axis |
| 330 | int vertical_size = vertical.rows() / 30; |
| 331 | // Create structure element for extracting vertical lines through morphology operations |
| 332 | Mat verticalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1, vertical_size)); |
| 333 | // Apply morphology operations |
| 334 | Imgproc.erode(vertical, vertical, verticalStructure); |
| 335 | Imgproc.dilate(vertical, vertical, verticalStructure); |
| 336 | // Show extracted vertical lines |
| 337 | showWaitDestroy("vertical", vertical); |
| 338 | // Inverse vertical image |
| 339 | Core.bitwise_not(vertical, vertical); |
| 340 | showWaitDestroy("vertical_bit", vertical); |
| 341 | // Extract edges and smooth image according to the logic |
| 342 | // 1. extract edges |
| 343 | // 2. dilate(edges) |
| 344 | // 3. src.copyTo(smooth) |
| 345 | // 4. blur smooth img |
| 346 | // 5. smooth.copyTo(src, edges) |
| 347 | // Step 1 |
| 348 | Mat edges = new Mat(); |
| 349 | Imgproc.adaptiveThreshold(vertical, edges, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 3, -2); |
| 350 | showWaitDestroy("edges", edges); |
| 351 | // Step 2 |
| 352 | Mat kernel = Mat.ones(2, 2, CvType.CV_8UC1); |
| 353 | Imgproc.dilate(edges, edges, kernel); |
| 354 | showWaitDestroy("dilate", edges); |
| 355 | // Step 3 |
| 356 | Mat smooth = new Mat(); |
| 357 | vertical.copyTo(smooth); |
| 358 | // Step 4 |
| 359 | Imgproc.blur(smooth, smooth, new Size(2, 2)); |
| 360 | // Step 5 |
| 361 | smooth.copyTo(vertical, edges); |
| 362 | // Show final result |
| 363 | showWaitDestroy("smooth - final", vertical); |
| 364 | System.exit(0); |
| 365 | } |