1 | import org.opencv.core.*;
|
---|
2 | import org.opencv.core.Point;
|
---|
3 | import org.opencv.highgui.HighGui;
|
---|
4 | import org.opencv.imgcodecs.Imgcodecs;
|
---|
5 | import org.opencv.imgproc.Imgproc;
|
---|
6 | import static org.opencv.imgcodecs.Imgcodecs.imwrite;
|
---|
7 |
|
---|
8 |
|
---|
9 | //REFERENCES:
|
---|
10 | //https://docs.opencv.org/3.4.3/d9/db0/tutorial_hough_lines.html
|
---|
11 |
|
---|
12 |
|
---|
13 | public class Main {
|
---|
14 |
|
---|
15 | private Scalar meanOfProcessed;
|
---|
16 |
|
---|
17 | public static void main(String[] args) {
|
---|
18 | System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
---|
19 | /*
|
---|
20 | // write your code here
|
---|
21 | System.out.println("Welcome to OpenCV " + Core.VERSION);
|
---|
22 | Mat m = new Mat(5, 10, CvType.CV_8UC1, new Scalar(0));
|
---|
23 | System.out.println("OpenCV Mat: " + m);
|
---|
24 | Mat mr1 = m.row(1);
|
---|
25 | mr1.setTo(new Scalar(1));
|
---|
26 | Mat mc5 = m.col(0);
|
---|
27 | mc5.setTo(new Scalar(8));
|
---|
28 | System.out.println("OpenCV Mat data:\n" + m.dump());
|
---|
29 | */
|
---|
30 | try {
|
---|
31 | //Variables
|
---|
32 | Mat edgesDetected = new Mat();
|
---|
33 | Mat edgesDetectedRGB = new Mat();
|
---|
34 | Mat edgesDetectedRGBProb;
|
---|
35 | Mat justEdges;
|
---|
36 |
|
---|
37 |
|
---|
38 |
|
---|
39 | String default_file = "Test.png";
|
---|
40 | String filename = ((args.length > 0) ? args[0] : default_file);
|
---|
41 |
|
---|
42 | // Load an image
|
---|
43 | Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
|
---|
44 | // Edge detection
|
---|
45 | Imgproc.Canny(original, edgesDetected, 50, 200, 3, false);
|
---|
46 | //Copy edges to the images that will display the results in BGR
|
---|
47 | Imgproc.cvtColor(edgesDetected, edgesDetectedRGB, Imgproc.COLOR_GRAY2BGR);
|
---|
48 | edgesDetectedRGBProb = edgesDetectedRGB.clone();
|
---|
49 |
|
---|
50 | justEdges = edgesDetectedRGBProb.clone();
|
---|
51 |
|
---|
52 |
|
---|
53 |
|
---|
54 | // Standard Hough Line Transform
|
---|
55 | Mat lines = new Mat(); // will hold the results of the detection
|
---|
56 | Imgproc.HoughLines(edgesDetected, lines, 1, Math.PI / 180, 150); // runs the actual detection
|
---|
57 | // Draw the lines
|
---|
58 | for (int x = 0; x < lines.rows(); x++) {
|
---|
59 | double rho = lines.get(x, 0)[0],
|
---|
60 | theta = lines.get(x, 0)[1];
|
---|
61 | double a = Math.cos(theta), b = Math.sin(theta);
|
---|
62 | double x0 = a * rho, y0 = b * rho;
|
---|
63 | Point pt1 = new Point(Math.round(x0 + 1000 * (-b)), Math.round(y0 + 1000 * (a)));
|
---|
64 | Point pt2 = new Point(Math.round(x0 - 1000 * (-b)), Math.round(y0 - 1000 * (a)));
|
---|
65 | Imgproc.line(edgesDetectedRGB, pt1, pt2, new Scalar(0, 0, 255), 3, Imgproc.LINE_AA, 0);
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 |
|
---|
70 | // Probabilistic Line Transform
|
---|
71 | Mat linesP = new Mat(); // will hold the results of the detection
|
---|
72 | Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 180, 50, 50, 10); // runs the actual detection
|
---|
73 | // Draw the lines
|
---|
74 | for (int x = 0; x < linesP.rows(); x++) {
|
---|
75 | double[] l = linesP.get(x, 0);
|
---|
76 | Imgproc.line(edgesDetectedRGBProb, new Point(l[0], l[1]), new Point(l[2], l[3]), new Scalar(0, 0, 255), 3, Imgproc.LINE_AA, 0);
|
---|
77 | }
|
---|
78 |
|
---|
79 | //save output
|
---|
80 | imwrite(filename+"_HoughLineP", edgesDetectedRGBProb);
|
---|
81 | String processedFile = filename+"_HoughLineP";
|
---|
82 | //Load an image
|
---|
83 | Mat processed = Imgcodecs.imread(processedFile, Imgcodecs.IMREAD_COLOR);
|
---|
84 | System.out.println(Core.mean(processed));
|
---|
85 | String meanOfProcessed = (Core.mean(processed).toString());
|
---|
86 | //OUTPUTS [ BLUE , GREEN , RED, ALPHA?
|
---|
87 | //Core.mean GETS scalar of image (array of BGR) and calculates the average for each
|
---|
88 | //https://docs.opencv.org/java/2.4.2/org/opencv/core/Core.html#mean(org.opencv.core.Mat)
|
---|
89 |
|
---|
90 | //With this logic, then if can say if the RED in the array is between 'x' and 'y' then its sheet music
|
---|
91 | System.out.println(meanOfProcessed); //PRINTS AS EXPECTED
|
---|
92 |
|
---|
93 | //CONVERT STRING TO ARRAY OR FIND ANOTHER WAY OF GETTING TO RED VALUE
|
---|
94 | //SAVE AS VARIABLE : RedValue.
|
---|
95 | //IF redValue > 'x' and redValue < 'y' THEN it is sheet music
|
---|
96 |
|
---|
97 |
|
---|
98 |
|
---|
99 | // Show results
|
---|
100 | //HighGui.imshow("Source", original);
|
---|
101 | //HighGui.imshow("Detected Lines (in red) - Standard Hough Line Transform", edgesDetectedRGB);
|
---|
102 | HighGui.imshow("Detected Lines (in red) - Probabilistic Line Transform", edgesDetectedRGBProb);
|
---|
103 | //HighGui.imshow("Just Edges", justEdges);
|
---|
104 | // Wait and Exit
|
---|
105 | HighGui.waitKey();
|
---|
106 | System.exit(0);
|
---|
107 | }
|
---|
108 | catch(Exception e){
|
---|
109 | System.err.println(e);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|