Changeset 33458 for other-projects/is-sheet-music-encore/trunk/image-identification-dev-02/image-identification-development/src/MainMorph.java
- Timestamp:
- 2019-09-07T14:30:32+12: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
r33455 r33458 226 226 //String default_file = hiresDirectory+"/MU/SheetMusic/bc.ark+=13960=t2j72dt1p-8.png"; //Handwritten 227 227 //String default_file = hiresDirectory+"/MU/SheetMusic/bc.ark+=13960=t2j72dt1p-9.png"; //Handwritten 228 String default_file = hiresDirectory+"/MU/SheetMusic/coo.31924062612282-9.png"; //Snippet228 //String default_file = hiresDirectory+"/MU/SheetMusic/coo.31924062612282-9.png"; //Snippet 229 229 //String default_file = hiresDirectory+"/MU/SheetMusic/dul1.ark+=13960=t2x41569k-10.png"; //Generated 230 230 //String default_file = hiresDirectory+"/MU/SheetMusic/dul1.ark+=13960=t2x41569k-7.png"; … … 235 235 //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-2.png"; //large numbers 236 236 //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-3.png"; 237 String default_file = hiresDirectory+"/MU/SheetMusic/mdp.39015080921409-9.png"; 237 238 //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-4.png"; 238 239 //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-5.png"; … … 344 345 imageViewer("04 Close", test); 345 346 346 //Imgproc.threshold(test,test, 127, 255, Imgproc.THRESH_BINARY);347 //imageViewer("05 Binarized", test);347 Imgproc.threshold(test,test, 127, 255, Imgproc.THRESH_BINARY); 348 imageViewer("05 Binarized", test); 348 349 349 350 Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4)); … … 351 352 imageViewer(" 06 Open", test); 352 353 353 // Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,10)); 354 // Imgproc.dilate(test, test, kernelDialateAgain); 355 // imageViewer("07 Dialate", test); 356 357 //FIGURE OUT FLOOD FILL!! 358 Imgproc.floodFill(test,test, new Point(1,1), new Scalar(2)); 359 354 Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,10)); 355 Imgproc.dilate(test, test, kernelDialateAgain); 356 imageViewer("07 Dialate", test); 360 357 361 358 Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,50)); 362 359 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelCloseAgain); 363 360 imageViewer(" 08 Close Again (Final)", test); 361 362 //FIGURE OUT FLOOD FILL!! 363 //Imgproc.floodFill(test,test, new Point(1,1), new Scalar(2)); 364 364 365 365 } … … 857 857 // Mat circleOutput = allContoursFound.clone(); 858 858 // MatOfPoint2f[] contoursPoly = new MatOfPoint2f[largeContours.size()]; 859 // Rect[] boundRect = new Rect[largeContours.size()]; 859 860 // Point[] centers = new Point[largeContours.size()]; 860 861 // float[][] radius = new float[largeContours.size()][1]; … … 864 865 // contoursPoly[i] = new MatOfPoint2f(); 865 866 // Imgproc.approxPolyDP(new MatOfPoint2f(largeContours.get(i).toArray()), contoursPoly[i], 1, true); 867 // boundRect[i] = Imgproc.boundingRect(new MatOfPoint(contoursPoly[i].toArray())); 866 868 // centers[i] = new Point(); 867 869 // Imgproc.minEnclosingCircle(contoursPoly[i], centers[i], radius[i]); … … 870 872 // //Draw circle for each large contour 871 873 // for (int i = 0; i < largeContours.size(); i++) { 874 // Imgproc.rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2); 872 875 // Imgproc.circle(circleOutput, centers[i], (int) radius[i][0],new Scalar(255, 0, 0), 1); 873 876 // } … … 1157 1160 //Display Original 1158 1161 imageViewer("original", original); 1159 Mat base = binarizedOriginal.clone(); 1160 imageViewer("00 Inverse Binarized Original", base); 1162 imageViewer("000 Inverse Binarized Original", binarizedOriginal); 1161 1163 1162 1164 1163 1165 //************************************ 1164 //1. Large object Remover1166 //1. Large Object Remover 1165 1167 //************************************ 1166 1168 Mat srcLOR = binarizedOriginal.clone(); … … 1173 1175 imageViewer("001 De-noise", maskLOR); 1174 1176 1175 // heal the large items1176 Mat healLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size( 10,10));1177 //Heal the large items 1178 Mat healLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(25,25)); 1177 1179 Imgproc.morphologyEx(maskLOR,maskLOR, Imgproc.MORPH_CLOSE, healLOR); 1178 1180 imageViewer("002 heal objects in mask", maskLOR); 1179 1181 1180 1182 //IsolateLarge 1181 Mat isolateLargeLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size( 20,20));1182 Imgproc. erode(maskLOR,maskLOR,isolateLargeLOR);1183 Mat isolateLargeLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(30,30)); 1184 Imgproc.morphologyEx(maskLOR,maskLOR, Imgproc.MORPH_OPEN, isolateLargeLOR); 1183 1185 imageViewer("003 Isolate large", maskLOR); 1184 1186 … … 1187 1189 imageViewer("100 Large Items Removed", dstLOR); 1188 1190 1191 // //CODE FROM PRESENTATION 1192 // Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,1)); 1193 // Imgproc.erode(dstTOR,base,kernelErode); 1194 // imageViewer("201 Erode", base); 1195 // 1196 // Mat kernelDialate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(110,10)); 1197 // Imgproc.dilate(base, base, kernelDialate); 1198 // imageViewer("202 Dialate", base); 1199 // 1200 // Mat kernelErodeAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,20)); 1201 // Imgproc.erode(base,base,kernelErodeAgain); 1202 // imageViewer(" 203 Erode Again", base); 1203 // 1204 // Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(35,20)); 1205 // Imgproc.morphologyEx(base,base,Imgproc.MORPH_CLOSE, kernelClose); 1206 // imageViewer("204 Close", base); 1207 // 1208 // Imgproc.threshold(base,base, 127, 255, Imgproc.THRESH_BINARY); 1209 // imageViewer("205 Binarized", base); 1210 // 1211 // Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4)); 1212 // Imgproc.morphologyEx(base,base,Imgproc.MORPH_OPEN, kernelOpen); 1213 // imageViewer("206 Open", base); 1214 // 1215 // Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,10)); 1216 // Imgproc.dilate(base, base, kernelDialateAgain); 1217 // imageViewer("207 Dialate", base); 1218 // 1219 // Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,50)); 1220 // Imgproc.morphologyEx(base,base,Imgproc.MORPH_CLOSE, kernelCloseAgain); 1221 // imageViewer("208 Close Again (Final)", base); 1222 1189 1223 //*********************************** 1190 //2. Text-sized Object Removal 1224 // //3.1 Remove big outlines 1225 // //*********************************** 1226 // Mat srcTOR = base.clone(); 1227 // Mat maskTOR = new Mat(); 1228 // Mat dstTOR = new Mat(); 1229 // 1230 // //Remove small objects in prep for masking (De-Noise) 1231 // Mat removeSmallTOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5)); 1232 // Imgproc.morphologyEx(srcTOR,maskTOR, Imgproc.MORPH_OPEN, removeSmallTOR); 1233 // imageViewer("2101 Remove Small from mask", maskTOR); 1234 // 1235 // //heal the large items 1236 // Mat healTOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10)); 1237 // Imgproc.morphologyEx(maskTOR,maskTOR, Imgproc.MORPH_CLOSE, healTOR); 1238 // imageViewer("2102 heal objects in mask", maskTOR); 1239 // 1240 // //Highlight black outline 1241 // Mat highlightLargeTOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10)); 1242 // Imgproc.erode(maskTOR,maskTOR,highlightLargeTOR); 1243 // imageViewer("2103 highlight objects", maskTOR); 1244 // 1245 // Core.bitwise_not(maskTOR, maskTOR); 1246 // srcTOR.copyTo(dstTOR, maskTOR); 1247 // imageViewer("2200 Black outline Removed", dstTOR); 1248 1249 1250 1251 1252 1253 1254 1255 //4. Classify like line clusters. 1256 Mat base = binarizedOriginal.clone(); 1191 1257 //*********************************** 1192 1193 Mat srcTOR = dstLOR.clone(); 1194 Mat maskTOR = new Mat(); 1195 Mat dstTOR = new Mat(); 1196 1197 //Remove small objects in prep for masking (De-Noise) 1198 Mat removeSmallTOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5)); 1199 Imgproc.morphologyEx(srcTOR,maskTOR, Imgproc.MORPH_OPEN, removeSmallTOR); 1200 imageViewer("101 Remove Small from mask", maskTOR); 1201 1202 //heal the large items 1203 Mat healTOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10)); 1204 Imgproc.morphologyEx(maskTOR,maskTOR, Imgproc.MORPH_CLOSE, healTOR); 1205 imageViewer("102 heal objects in mask", maskTOR); 1206 1207 //Highlight black outline 1208 Mat highlightLargeTOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10)); 1209 Imgproc.erode(maskTOR,maskTOR,highlightLargeTOR); 1210 imageViewer("103 highlight objects", maskTOR); 1211 1212 Core.bitwise_not(maskTOR, maskTOR); 1213 srcTOR.copyTo(dstTOR, maskTOR); 1214 imageViewer("200 Black outline Removed", dstTOR); 1215 1258 //3. Isolate straight lines 1216 1259 //*********************************** 1217 //3. Standard plan from presentation (TAKEN FROM CODE VERSION 5) 1260 //Heal lines 1261 Mat healISL = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(82,1)); 1262 Imgproc.morphologyEx(dstLOR,base, Imgproc.MORPH_OPEN, healISL); 1263 imageViewer("202 heal objects in mask", base); 1264 1265 //Make White Blobs 1266 1267 //NEED THIS?? 1268 Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,1)); 1269 Imgproc.morphologyEx(base,base,Imgproc.MORPH_OPEN, kernelOpen); 1270 imageViewer("203 Open", base); 1271 //NEED THIS? 1272 1273 Mat kernelDilateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,10)); 1274 Imgproc.dilate(base, base, kernelDilateAgain); 1275 imageViewer("204 Dilate", base); 1276 1277 Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(12,50)); 1278 Imgproc.morphologyEx(base,base,Imgproc.MORPH_CLOSE, kernelCloseAgain); 1279 imageViewer("205 Close Again (Final)", base); 1280 1218 1281 //*********************************** 1219 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(20,2)); 1220 Imgproc.erode(dstTOR,base,kernelErode); 1221 imageViewer("201 Erode plus pre", base); 1222 1223 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(20,2)); 1224 Imgproc.dilate(base,base,kernelDilate); 1225 imageViewer("202 Dilate", base); 1226 1227 Mat kernelClosing = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(20,5)); 1228 Imgproc.morphologyEx(base, base, Imgproc.MORPH_CLOSE, kernelClosing); 1229 imageViewer("203 Close", base); 1230 1231 Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,2)); 1232 Imgproc.erode(base,base,kernelErode02); 1233 imageViewer("204 Erode (Final)", base); 1234 1235 //4. Classify like line clusters. 1282 //4. Find 'Clusters' 1283 // Need to find areas, using bounding boxes 1284 // Find how tall the bounding box is, if its taller than 'x' then classify as Sheet Music 1285 //*********************************** 1286 1287 //***************************************************************** 1288 //4.1 Prep Rectangles on processed images 1289 //***************************************************************** 1290 ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>(); 1291 Mat hierarchy = new Mat(); 1292 Imgproc.findContours(base, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); 1293 //Draw contours and record areas 1294 Mat allContoursFound = Mat.zeros(base.size(), CvType.CV_8UC3); 1295 Mat rectOutput = allContoursFound.clone(); 1296 Imgproc.drawContours(allContoursFound, contours, -1, new Scalar(0, 255, 0), 1); //USES LINE_8 1297 //Init arrays 1298 MatOfPoint2f[] contoursPoly = new MatOfPoint2f[contours.size()]; 1299 Rect[] boundRect = new Rect[contours.size()]; 1300 1301 //Fill arrays 1302 for (int i = 0; i < contours.size(); i++) { 1303 contoursPoly[i] = new MatOfPoint2f(); 1304 Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(i).toArray()), contoursPoly[i], 1, true); 1305 boundRect[i] = Imgproc.boundingRect(new MatOfPoint(contoursPoly[i].toArray())); 1306 } 1307 //Draw circle for each large contour 1308 for (int i = 0; i < contours.size(); i++) { 1309 if(boundRect[i].height > 100){ 1310 System.out.println("i:"+ i + " Height: " + boundRect[i].height); 1311 if(boundRect[i].width > 300){ 1312 System.out.println("i:"+ i + " Width: " + boundRect[i].width); 1313 Imgproc.rectangle(rectOutput, boundRect[i].tl(), boundRect[i].br(), new Scalar(255, 0, 0), 20); 1314 } 1315 } 1316 } 1317 1318 1319 imageViewer("4000 Rect found", rectOutput); 1320 1321 1322 1236 1323 } 1237 //USE stuc element, to rule out large wide and long pieces of black and white. 1238 1324 //ClassfierComparsion - Reset after david chat 1325 if(CODE_VERSION == 91){ 1326 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 1327 Pair returnVariables = new Pair(); 1328 int areaCounter=0; 1329 //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); 1332 try{ 1333 //************************************ 1334 //1. Large Object Remover 1335 //************************************ 1336 Mat srcLOR = binarizedOriginal.clone(); 1337 Mat maskLOR = new Mat(); 1338 Mat dstLOR = new Mat(); 1339 1340 //Remove small objects in prep for masking (De-Noise) 1341 Mat removeSmallLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5)); 1342 Imgproc.morphologyEx(srcLOR,maskLOR, Imgproc.MORPH_OPEN, removeSmallLOR); 1343 1344 //Heal the large items 1345 Mat healLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(25,25)); 1346 Imgproc.morphologyEx(maskLOR,maskLOR, Imgproc.MORPH_CLOSE, healLOR); 1347 1348 //IsolateLarge 1349 Mat isolateLargeLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(30,30)); 1350 Imgproc.morphologyEx(maskLOR,maskLOR, Imgproc.MORPH_OPEN, isolateLargeLOR); 1351 1352 Core.bitwise_not(maskLOR,maskLOR); 1353 srcLOR.copyTo(dstLOR, maskLOR); 1354 1355 Mat base = binarizedOriginal.clone(); 1356 //*********************************** 1357 //3. Isolate straight lines 1358 //*********************************** 1359 //Heal lines 1360 Mat healISL = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(82,1)); 1361 Imgproc.morphologyEx(dstLOR,base, Imgproc.MORPH_OPEN, healISL); 1362 1363 //Make White Blobs 1364 Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,1)); 1365 Imgproc.morphologyEx(base,base,Imgproc.MORPH_OPEN, kernelOpen); 1366 1367 Mat kernelDilateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,10)); 1368 Imgproc.dilate(base, base, kernelDilateAgain); 1369 1370 Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(12,50)); 1371 Imgproc.morphologyEx(base,base,Imgproc.MORPH_CLOSE, kernelCloseAgain); 1372 1373 //*********************************** 1374 //4. Find 'Clusters' 1375 // Need to find areas, using bounding boxes 1376 // Find how tall the bounding box is, if its taller than 'x' then classify as Sheet Music 1377 //*********************************** 1378 1379 //***************************************************************** 1380 //4.1 Prep Rectangles on processed images 1381 //***************************************************************** 1382 ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>(); 1383 Mat hierarchy = new Mat(); 1384 Imgproc.findContours(base, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); 1385 //Draw contours and record areas 1386 Mat allContoursFound = Mat.zeros(base.size(), CvType.CV_8UC3); 1387 Imgproc.drawContours(allContoursFound, contours, -1, new Scalar(0, 255, 0), 1); //USES LINE_8 1388 //Init arrays 1389 MatOfPoint2f[] contoursPoly = new MatOfPoint2f[contours.size()]; 1390 Rect[] boundRect = new Rect[contours.size()]; 1391 1392 //Fill arrays 1393 for (int i = 0; i < contours.size(); i++) { 1394 contoursPoly[i] = new MatOfPoint2f(); 1395 Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(i).toArray()), contoursPoly[i], 1, true); 1396 boundRect[i] = Imgproc.boundingRect(new MatOfPoint(contoursPoly[i].toArray())); 1397 } 1398 //Draw circle for each large contour 1399 //MAKE THE COMPARING VALUES GLOBAL CONSTANTS 1400 for (int i = 0; i < contours.size(); i++) { 1401 if(boundRect[i].height > 100){ 1402 if(boundRect[i].width > 300){ 1403 areaCounter++; 1404 } 1405 } 1406 } 1407 1408 //Classifier Calculation 1409 if(areaCounter >= THRESHOLD_AREA_COUNT){ 1410 returnVariables.setBoolean(true); 1411 returnVariables.setInteger(areaCounter); 1412 } 1413 } 1414 catch(Exception e){ 1415 System.err.println(e); 1416 } 1417 //return returnVariables; 1418 } 1239 1419 1240 1420 //****************MORPHOLOGY****************************************************************************************
Note:
See TracChangeset
for help on using the changeset viewer.