import org.opencv.core.*; import org.opencv.core.Point; import org.opencv.highgui.HighGui; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; import static org.opencv.imgcodecs.Imgcodecs.imwrite; import java.awt.image.BufferedImage; import java.awt.image.DataBufferByte; import java.io.File; import java.io.BufferedWriter; import java.io.FileWriter; import javax.imageio.ImageIO; import java.util.logging.Logger; //REFERENCES: //https://docs.opencv.org/3.4.3/d9/db0/tutorial_hough_lines. //https://stackoverflow.com/questions/43443309/count-red-pixel-in-a-given-image //https://www.wikihow.com/Calculate-Percentage-in-Java //https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object //https://stackoverflow.com/questions/15758685/how-to-write-logs-in-text-file-when-using-java-util-logging-logger //https://stackoverflow.com/questions/9961292/write-to-text-file-without-overwriting-in-java //GOAL for 21st //Classifier 01 //Have args so can call "java image-identification-classifier01 XX XX" //args can be parameters in algorthim such as threshold or theta? //Run on 5000 images. //Record success rates //All done with makefile //But first understand houghline transform //Know what the algorithm being used is doing. //MAke constants for this classifier //Make java be able to run on CMD line public class javaImageClassifier{ public static void main(String[] args) { try { if (args.length != 2) { System.out.println("Usage: imageClassifier "); } else { String imageFilename = args[0]; int classifierType = Integer.parseInt(args[1]); String result = null; //Execute classifierType defined from arguement switch(classifierType){ case 1: result = classifier01(imageFilename); break; case 2: System.out.println("unknown"); break; } //Write output to disc File log = new File("log.txt"); FileWriter fileWriter = new FileWriter(log, true); BufferedWriter bw = new BufferedWriter(fileWriter); bw.write(result + classifierType + '\n'); bw.close(); } } catch(Exception e){ System.err.println(e); } } private static String classifier01(String filename){ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Boolean isSheetMusic = null; try{ //Variables Mat edgesDetected = new Mat(); Mat edgesDetectedRGB = new Mat(); Mat edgesDetectedRGBProb; // Load an image Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE); // Edge detection Imgproc.Canny(original, edgesDetected, 50, 200, 3, false); //Copy edges to the images that will display the results in BGR Imgproc.cvtColor(edgesDetected, edgesDetectedRGB, Imgproc.COLOR_GRAY2BGR); // Probabilistic Line Transform Mat linesP = new Mat(); // will hold the results of the detection Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 180, 50, 50, 10); // runs the actual detection // Draw the lines for (int x = 0; x < linesP.rows(); x++) { double[] l = linesP.get(x, 0); Imgproc.line(edgesDetectedRGB, new Point(l[0], l[1]), new Point(l[2], l[3]), new Scalar(0, 0, 255), 3, Imgproc.LINE_AA, 0); } //Convert MAT into a BufferedImage BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB); //Calculate if its sheet music or not isSheetMusic = Classifier(toBeClassifiedImg); //Save Processed Image String processedFile = filename; if (isSheetMusic == true) { processedFile = "proc_T_"+filename; }else { processedFile = "proc_F_"+filename; } imwrite(processedFile, edgesDetectedRGB); //Display Results //HighGui.imshow("Source", original); //HighGui.imshow("Just Edges", justEdges); //TESTING //HighGui.imshow("Detected Lines (in red) - Standard Hough Line Transform", edgesDetectedRGB); //HighGui.imshow("Detected Lines (in red) - Probabilistic Line Transform", edgesDetectedRGBProb); // Wait and Exit //HighGui.waitKey(); //System.exit(0); } catch(Exception e){ System.err.println(e); } if (isSheetMusic == true){ return (1 + "\t" + "Filename: " + filename + " Status: " + isSheetMusic +"\t" );} else{ return (0 + "\t" + "Filename: " + filename + " Status: " + isSheetMusic +"\t" );} } private static boolean Classifier(BufferedImage img){ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); try { //Read file //BufferedImage img = ImageIO.read(new File(processedFile)); int x = img.getWidth(); int y = img.getHeight(); int pixelCount = 0; int redCount = 0; float percentage = 0; //Go Thru every pixel for(int i=0; i < y; i++){ for(int j=0;j < x; j++){ //Get value for current pixels RGB value int currPixelRGB = img.getRGB(j, i); //Check if pixel is red (hex value of red) if(currPixelRGB == 0xFFFF0000){ redCount++; } pixelCount++; } } //Calculate percentage of Red in image percentage = ((float)redCount/(float)pixelCount)*(float)100; //If more than %10 and less than %50 then its sheet music! if(percentage > 10 && percentage < 50){ //MAKE THESE CONSTANTS!! return true;} } catch (Exception e) { System.err.println(e); } return false; } private static BufferedImage toBufferedImage(Mat mat){ //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object try{ int type = BufferedImage.TYPE_3BYTE_BGR; int bufferSize = mat.channels() * mat.cols() * mat.rows(); byte[] b = new byte[bufferSize]; //get all the pixels mat.get(0, 0, b); BufferedImage image = new BufferedImage(mat.cols(), mat.rows(), type); final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); System.arraycopy(b, 0, targetPixels, 0, b.length); return image; } catch(Exception e){ System.err.println(e); } return null; } }