Changeset 33589 for other-projects/is-sheet-music-encore/trunk/image-identification-dev-02/image-identification-development/src/MainMorph.java
- Timestamp:
- 2019-10-21T21:45:10+13:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
other-projects/is-sheet-music-encore/trunk/image-identification-dev-02/image-identification-development/src/MainMorph.java
r33458 r33589 66 66 67 67 import static org.opencv.core.Core.FILLED; 68 import static org.opencv.core.Core.calcCovarMatrix; 68 69 import static org.opencv.core.CvType.CV_8UC3; 69 70 import static org.opencv.highgui.HighGui.createJFrame; … … 193 194 //Variables 194 195 System.out.println("Running Code version: " + CODE_VERSION + " Image Version: " +IMAGE_VERSION); 195 Mat mid = new Mat();196 196 Mat edgesDetectedRGB = new Mat(); 197 197 Mat clustersFoundRGB = new Mat(); … … 209 209 //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t2q53nq6w-6.png"; 210 210 //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t9z03w65z-4.png"; 211 //String default_file =hiresDirectory+"BK/NotSheetMusic/loc.ark+=13960=t50g4443d-5.png"; 211 212 //String default_file =hiresDirectory+"MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png"; 212 213 //String default_file =hiresDirectory+"MU/SheetMusic/emu.010001066823-9.png"; … … 234 235 //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png"; //Image evaluation 235 236 //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-2.png"; //large numbers 237 //String default_file = hiresDirectory+ "MU/SheetMusic/mdp.39015096400919-9.png"; 236 238 //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-3.png"; 237 String default_file = hiresDirectory+"/MU/SheetMusic/mdp.39015080921409-9.png";239 //String default_file = hiresDirectory+"/MU/SheetMusic/mdp.39015080921409-9.png"; 238 240 //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-4.png"; 239 241 //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-5.png"; … … 242 244 //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-8.png"; 243 245 //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-9.png"; 246 String default_file = hiresDirectory+"/MU/SheetMusic/mdp.39015096398899-4.png"; 244 247 245 248 … … 260 263 Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE); 261 264 System.out.println("Width: " + original.width() + " Height: " + original.height()); 262 Mat binarized Original= original.clone();265 Mat binarized = original.clone(); 263 266 264 Imgproc.adaptiveThreshold(original, binarizedOriginal,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);267 //Imgproc.adaptiveThreshold(original, binarized,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C); 265 268 //TEST PARAMETERSImgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 531,1); 266 269 //Imgproc.threshold(original,original, 127, 255, Imgproc.THRESH_BINARY); 267 270 //************************ 271 //Argument Setup 272 //************************ 273 int tempvariable = 0; 274 if (tempvariable ==1){ 275 Imgproc.adaptiveThreshold(original, binarized, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY_INV, 1001, 5); 276 } 277 else { 278 279 int borderIgnoreAmount = 0; 280 double divisor; 281 switch (borderIgnoreAmount) { 282 case 5: 283 divisor = 40; 284 break; 285 case 10: 286 divisor = 20; 287 break; 288 case 20: 289 divisor = 10; 290 break; 291 case 40: 292 divisor = 5; 293 break; 294 case 0: 295 divisor = -1; 296 break; 297 default: 298 divisor = -1; 299 } 300 if (divisor != -1) { 301 Mat mid = new Mat(); 302 Imgproc.adaptiveThreshold(original, mid, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C); 303 double maxX = mid.size().width; 304 double maxY = mid.size().height; 305 Point cp1 = new Point(maxX / divisor, maxY / divisor); 306 Point cp2 = new Point(maxX - cp1.x, maxY - cp1.y); 307 Rect rectCrop = new Rect(cp1, cp2); 308 binarized = mid.submat(rectCrop); 309 } else { 310 Imgproc.adaptiveThreshold(original, binarized, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C); 311 } 312 313 System.out.println("Width: " + binarized.width() + " Height: " + binarized.height()); 314 } 268 315 269 316 //****************MORPHOLOGY**************************************************************************************** … … 276 323 //dynamic morphology?? 277 324 if(CODE_VERSION == 1) { 278 int hori = binarized Original.width();279 int vert = binarized Original.height();325 int hori = binarized.width(); 326 int vert = binarized.height(); 280 327 //Find ratio between 100 and width and 100 and height 281 328 int divX = hori/10; … … 284 331 int sizeY = (vert/divY) * 10; 285 332 286 Mat test = binarized Original.clone();333 Mat test = binarized.clone(); 287 334 imageViewer("Original", test); 288 335 … … 326 373 327 374 //MAKE SURE BLACK & WHITE 328 Mat test = binarized Original.clone();375 Mat test = binarized.clone(); 329 376 imageViewer("00 Binarized Original", test); 330 377 … … 366 413 //Tutorial/Demo Code 367 414 if (CODE_VERSION == 3) { 368 Mat horizontal = binarized Original.clone();369 Mat vertical = binarized Original.clone();415 Mat horizontal = binarized.clone(); 416 Mat vertical = binarized.clone(); 370 417 // Specify size on horizontal axis 371 418 int horizontal_size = horizontal.cols() / 50; … … 420 467 imageViewer("original", original); 421 468 422 Mat test = binarized Original.clone();423 Mat pre = binarized Original.clone();469 Mat test = binarized.clone(); 470 Mat pre = binarized.clone(); 424 471 Mat dst = new Mat(); 425 472 … … 534 581 //Better morphology attempt - dynamic 535 582 if(CODE_VERSION == 5) { 536 int hori = binarized Original.width();537 int vert = binarized Original.height();583 int hori = binarized.width(); 584 int vert = binarized.height(); 538 585 //Find ratio between 100 and width and 100 and height 539 586 int sizeX100 = 10 * (hori/68); … … 551 598 imageViewer("original", original); 552 599 553 Mat test = binarized Original.clone();600 Mat test = binarized.clone(); 554 601 imageViewer("00 Inverse Binarized Original", test); 555 602 … … 643 690 imageViewer("original", original); 644 691 645 Mat src = binarized Original.clone();646 Mat test = binarized Original.clone();692 Mat src = binarized.clone(); 693 Mat test = binarized.clone(); 647 694 Mat mask = new Mat(); 648 695 Mat dst = new Mat(); … … 729 776 imageViewer("original", original); 730 777 731 Mat test = binarized Original.clone();778 Mat test = binarized.clone(); 732 779 733 780 … … 738 785 //Large Object Removal 739 786 //************************************ 740 Mat srcLOR = binarized Original.clone();787 Mat srcLOR = binarized.clone(); 741 788 Mat maskLOR = new Mat(); 742 789 Mat dstLOR = new Mat(); … … 968 1015 //Imgproc.adaptiveThreshold(original1, original,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C); 969 1016 970 Mat test = binarized Original.clone();1017 Mat test = binarized.clone(); 971 1018 //************************************ 972 1019 //Large Object Removal 973 1020 //************************************ 974 Mat srcLOR = binarized Original.clone();1021 Mat srcLOR = binarized.clone(); 975 1022 Mat maskLOR = new Mat(); 976 1023 Mat dstLOR = new Mat(); … … 1158 1205 //Reset after david chat 1159 1206 if(CODE_VERSION == 9){ 1207 int areaCounter=0; 1160 1208 //Display Original 1161 1209 imageViewer("original", original); 1162 imageViewer("000 Inverse Binarized Original", binarized Original);1210 imageViewer("000 Inverse Binarized Original", binarized); 1163 1211 1164 1212 … … 1166 1214 //1. Large Object Remover 1167 1215 //************************************ 1168 Mat srcLOR = binarized Original.clone();1216 Mat srcLOR = binarized.clone(); 1169 1217 Mat maskLOR = new Mat(); 1170 1218 Mat dstLOR = new Mat(); … … 1176 1224 1177 1225 //Heal the large items 1178 Mat healLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(25,25)); 1226 Mat healLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(25,25)); //5,5 1179 1227 Imgproc.morphologyEx(maskLOR,maskLOR, Imgproc.MORPH_CLOSE, healLOR); 1180 1228 imageViewer("002 heal objects in mask", maskLOR); 1181 1229 1182 1230 //IsolateLarge 1183 Mat isolateLargeLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(30,30)); 1231 Mat isolateLargeLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(30,30)); //8,8 1184 1232 Imgproc.morphologyEx(maskLOR,maskLOR, Imgproc.MORPH_OPEN, isolateLargeLOR); 1185 1233 imageViewer("003 Isolate large", maskLOR); … … 1254 1302 1255 1303 //4. Classify like line clusters. 1256 Mat base = binarized Original.clone();1304 Mat base = binarized.clone(); 1257 1305 //*********************************** 1258 1306 //3. Isolate straight lines 1259 1307 //*********************************** 1260 1308 //Heal lines 1261 Mat healISL = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8 2,1));1309 Mat healISL = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(86,1)); 1262 1310 Imgproc.morphologyEx(dstLOR,base, Imgproc.MORPH_OPEN, healISL); 1263 1311 imageViewer("202 heal objects in mask", base); … … 1310 1358 System.out.println("i:"+ i + " Height: " + boundRect[i].height); 1311 1359 if(boundRect[i].width > 300){ 1360 areaCounter++; 1312 1361 System.out.println("i:"+ i + " Width: " + boundRect[i].width); 1313 1362 Imgproc.rectangle(rectOutput, boundRect[i].tl(), boundRect[i].br(), new Scalar(255, 0, 0), 20); … … 1318 1367 1319 1368 imageViewer("4000 Rect found", rectOutput); 1369 System.out.println(areaCounter); 1320 1370 1321 1371 … … 1327 1377 Pair returnVariables = new Pair(); 1328 1378 int areaCounter=0; 1379 1380 1329 1381 //Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE); 1330 //Mat binarizedOriginal = original.clone(); 1331 //Imgproc.adaptiveThreshold(original, binarizedOriginal,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C); 1382 //Mat binarized = original.clone(); 1383 //Mat mid = new Mat(); 1384 //if(borderCrop > 0){ 1385 //Imgproc.adaptiveThreshold(original, mid,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, 2); 1386 //double maxX = mid.size().width; 1387 //double maxY = mid.size().height; 1388 //Point cp1 = new Point(maxX/10, maxY/10); 1389 //Point cp2 = new Point(maxX - cp1.x, maxY -cp1.y); 1390 //Rect rectCrop = new Rect(cp1, cp2); 1391 //binarized = mid.submat(rectCrop); 1392 //} 1393 //else{ 1394 //Imgproc.adaptiveThreshold(original, binarized,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C); 1395 //} 1396 1397 1332 1398 try{ 1333 1399 //************************************ 1334 1400 //1. Large Object Remover 1335 1401 //************************************ 1336 Mat srcLOR = binarized Original.clone();1402 Mat srcLOR = binarized.clone(); 1337 1403 Mat maskLOR = new Mat(); 1338 1404 Mat dstLOR = new Mat(); … … 1353 1419 srcLOR.copyTo(dstLOR, maskLOR); 1354 1420 1355 Mat base = binarized Original.clone();1421 Mat base = binarized.clone(); 1356 1422 //*********************************** 1357 1423 //3. Isolate straight lines
Note:
See TracChangeset
for help on using the changeset viewer.