1 import java
.util
.ArrayList
;
3 import org
.opencv
.core
.Core
;
4 import org
.opencv
.core
.Mat
;
5 import org
.opencv
.core
.MatOfPoint
;
6 import org
.opencv
.core
.Point
;
7 import org
.opencv
.core
.Scalar
;
8 import org
.opencv
.core
.Size
;
9 import org
.opencv
.imgcodecs
.Imgcodecs
;
10 import org
.opencv
.imgproc
.Imgproc
;
12 public class IsolateImage
{
14 public static Mat matOriginal
;
15 public static Mat filtered
;
16 public static Mat kernel
;
17 public static Mat matHierarchy
;
19 public static final Scalar LOWER_BOUNDS
= new Scalar(169, 164, 103);
20 public static final Scalar UPPER_BOUNDS
= new Scalar(255, 229, 140);
21 public static final Scalar BLACK
= new Scalar(0, 0, 0);
22 // public static final Scalar UPPER_BOUNDS = new Scalar(0, 255, 255);
25 System
.loadLibrary(Core
.NATIVE_LIBRARY_NAME
);
28 public static void main(String args
[]) throws Exception
{
30 matOriginal
= new Mat();
32 matHierarchy
= new Mat();
34 kernel
= Imgproc
.getStructuringElement(Imgproc
.MORPH_RECT
, new Size(5, 5));
39 public static void processImage() {
40 ArrayList
<MatOfPoint
> contours
= new ArrayList
<MatOfPoint
>();
42 matOriginal
= Imgcodecs
.imread("imgs/testpicture.png"); // convert image
45 filtered
= matOriginal
;
46 Imgproc
.GaussianBlur(matOriginal
, filtered
, new Size(17, 17), Core
.BORDER_DEFAULT
);
48 Core
.inRange(matOriginal
, LOWER_BOUNDS
, UPPER_BOUNDS
, filtered
);
49 // Imgproc.erode(matOriginal, filtered, kernel, new Point(-1, -1), 4);
50 Imgproc
.dilate(matOriginal
, filtered
, kernel
, new Point(-1, -1), 4);
51 // Imgproc.findContours(filtered, contours, matHierarchy,
52 // Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
54 // for (MatOfPoint mop : contours) {
55 // Rect rec = Imgproc.boundingRect(mop);
56 // Imgproc.rectangle(matOriginal, rec.br(), rec.tl(), BLACK, 5);
58 Imgcodecs
.imwrite("output.png", filtered
);