1 import java
.util
.ArrayList
;
2 import java
.util
.Iterator
;
4 import org
.opencv
.core
.Core
;
5 import org
.opencv
.core
.Mat
;
6 import org
.opencv
.core
.MatOfPoint
;
7 import org
.opencv
.core
.Point
;
8 import org
.opencv
.core
.Rect
;
9 import org
.opencv
.core
.Scalar
;
10 import org
.opencv
.core
.Size
;
11 import org
.opencv
.imgcodecs
.Imgcodecs
;
12 import org
.opencv
.imgproc
.Imgproc
;
14 public class IsolateImage
{
16 public static Mat matOriginal
;
17 public static Mat filtered
;
18 public static Mat kernel
;
19 public static Mat matHierarchy
;
21 public static final Scalar LOWER_BOUNDS
= new Scalar(80, 80, 80);
22 public static final Scalar UPPER_BOUNDS
= new Scalar(140, 180, 140);
23 public static final Scalar BLACK
= new Scalar(0, 0, 0);
24 public static final Scalar GRAY
= new Scalar(100, 100, 100);
27 System
.loadLibrary(Core
.NATIVE_LIBRARY_NAME
);
30 public static void main(String args
[]) throws Exception
{
32 matOriginal
= new Mat();
34 matHierarchy
= new Mat();
36 kernel
= Imgproc
.getStructuringElement(Imgproc
.MORPH_RECT
, new Size(3, 3));
41 public static void processImage() {
43 ArrayList
<MatOfPoint
> contours
= new ArrayList
<MatOfPoint
>();
45 int THE_BEST_MAGIC_NUMBER
= 300;
46 while (frameCount
< 1) {
48 matOriginal
= Imgcodecs
.imread("imgs/goal.png"); // convert image
51 filtered
= matOriginal
;
53 Imgproc
.GaussianBlur(matOriginal
, filtered
, new Size(25, 25), Core
.BORDER_DEFAULT
);
55 Core
.inRange(matOriginal
, LOWER_BOUNDS
, UPPER_BOUNDS
, filtered
);
56 Imgproc
.erode(matOriginal
, filtered
, kernel
, new Point(-1, -1), 5);
57 Imgproc
.dilate(matOriginal
, filtered
, kernel
, new Point(-1, -1), 0);
58 Imgproc
.findContours(filtered
, contours
, matHierarchy
, Imgproc
.RETR_EXTERNAL
, Imgproc
.CHAIN_APPROX_SIMPLE
);
60 for (Iterator
<MatOfPoint
> iterator
= contours
.iterator(); iterator
.hasNext();) {
61 MatOfPoint matOfPoint
= iterator
.next();
62 Rect rec
= Imgproc
.boundingRect(matOfPoint
);
63 if (rec
.height
< THE_BEST_MAGIC_NUMBER
|| rec
.width
< THE_BEST_MAGIC_NUMBER
) {
64 System
.out
.println(rec
.toString());
70 for (MatOfPoint mop
: contours
) {
71 Rect rec
= Imgproc
.boundingRect(mop
);
72 Imgproc
.rectangle(matOriginal
, rec
.br(), rec
.tl(), GRAY
, 1);
78 Imgcodecs
.imwrite("output.png", filtered
);
79 System
.out
.println("done");
85 * a nonnormalized angle
88 public static double normalize360(double angle
) {
89 while (angle
>= 360.0) {