-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetectContours.java
112 lines (69 loc) · 3.33 KB
/
detectContours.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// ********************************************************
// Example : computer the difference between a pair of images,
// detect the resulting contours and display them
// Author : Toby Breckon, [email protected]
// Copyright (c) 2015 Durham University
// License : LGPL - http://www.gnu.org/licenses/lgpl.html
// ********************************************************
// import required Java components
import java.util.List;
import java.util.Vector;
//import required OpenCV components
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Size;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
// ********************************************************
public class detectContours {
public static void main(String[] args) throws InterruptedException {
// load the Core OpenCV library by name
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// load two image files
Mat img1 = Highgui.imread("files/cctv_example1.png");
Mat img2 = Highgui.imread("files/cctv_example2.png");
// create a new image object to store image difference
Mat diff_img = new Mat();
// compute the difference between the images
Core.absdiff(img1, img2, diff_img);
// now convert it to grey and threshold it
Mat grey = new Mat();
Imgproc.cvtColor(diff_img, grey, Imgproc.COLOR_BGR2GRAY);
Imgproc.adaptiveThreshold(grey, diff_img, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C,
Imgproc.THRESH_BINARY_INV, 7, 10);
// now clean it up using some morphological operations
Size ksize = new Size(15,15);
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, ksize);
Imgproc.morphologyEx(diff_img, diff_img, Imgproc.MORPH_CLOSE, kernel);
// find the all the contours from the binary image using the edge to contour
// stuff we looked at in lectures
List<MatOfPoint> contours = new Vector<MatOfPoint>();
Imgproc.findContours(diff_img, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
// draw the contours on image 2 in red
Imgproc.drawContours(img2, contours, -1, new Scalar(0,0,255));
// find the largest contour by area
double maxArea = 0;
int maxAreaIndex = 0;
for (int i = 0; i < contours.size(); i++) {
double area = Imgproc.contourArea(contours.get(i), false);
if ( area > maxArea )
{
maxArea = area;
maxAreaIndex = i;
}
}
// draw the largest contour in red
Imgproc.drawContours(img2, contours, maxAreaIndex, new Scalar(0,255,0));
// create a new window objects
Imshow ims1 = new Imshow("Image 1");
Imshow ims2 = new Imshow("Image 2");
Imshow ims_diff = new Imshow("Difference");
// display images
ims1.showImage(img1);
ims2.showImage(img2);
ims_diff.showImage(diff_img);
}
}
// ********************************************************