Commit | Line | Data |
---|---|---|
c724deb3 E |
1 | import java.util.ArrayList; |
2 | ||
0e9212ae | 3 | import org.opencv.core.Core; |
fb24aa85 | 4 | import org.opencv.core.Mat; |
c724deb3 | 5 | import org.opencv.core.MatOfPoint; |
a5abe89b | 6 | import org.opencv.core.Point; |
0e9212ae | 7 | import org.opencv.core.Scalar; |
a5abe89b | 8 | import org.opencv.core.Size; |
7a245e8b | 9 | import org.opencv.imgcodecs.Imgcodecs; |
a5abe89b | 10 | import org.opencv.imgproc.Imgproc; |
0e9212ae | 11 | |
bf95a16f | 12 | public class IsolateImage { |
0e9212ae | 13 | |
fb24aa85 | 14 | public static Mat matOriginal; |
7a245e8b | 15 | public static Mat filtered; |
a5abe89b | 16 | public static Mat kernel; |
c724deb3 | 17 | public static Mat matHierarchy; |
fb24aa85 | 18 | |
bf95a16f E |
19 | public static final Scalar LOWER_BOUNDS = new Scalar(169, 164, 103); |
20 | public static final Scalar UPPER_BOUNDS = new Scalar(255, 229, 140); | |
c724deb3 | 21 | public static final Scalar BLACK = new Scalar(0, 0, 0); |
bf95a16f | 22 | // public static final Scalar UPPER_BOUNDS = new Scalar(0, 255, 255); |
0e9212ae E |
23 | |
24 | static { | |
25 | System.loadLibrary(Core.NATIVE_LIBRARY_NAME); | |
26 | } | |
27 | ||
0e9212ae | 28 | public static void main(String args[]) throws Exception { |
fb24aa85 E |
29 | |
30 | matOriginal = new Mat(); | |
7a245e8b | 31 | filtered = new Mat(); |
c724deb3 | 32 | matHierarchy = new Mat(); |
fb24aa85 | 33 | |
c724deb3 | 34 | kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5, 5)); |
a5abe89b | 35 | |
8f580bc8 | 36 | processImage(); |
0e9212ae | 37 | } |
7a245e8b E |
38 | |
39 | public static void processImage() { | |
c724deb3 E |
40 | ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>(); |
41 | contours.clear(); | |
7a245e8b | 42 | matOriginal = Imgcodecs.imread("imgs/testpicture.png"); // convert image |
c724deb3 | 43 | // into matrix |
7a245e8b | 44 | |
a5abe89b | 45 | filtered = matOriginal; |
c724deb3 | 46 | Imgproc.GaussianBlur(matOriginal, filtered, new Size(17, 17), Core.BORDER_DEFAULT); |
a5abe89b | 47 | |
8f580bc8 | 48 | Core.inRange(matOriginal, LOWER_BOUNDS, UPPER_BOUNDS, filtered); |
c724deb3 E |
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); | |
53 | ||
54 | // for (MatOfPoint mop : contours) { | |
55 | // Rect rec = Imgproc.boundingRect(mop); | |
56 | // Imgproc.rectangle(matOriginal, rec.br(), rec.tl(), BLACK, 5); | |
57 | // } | |
8f580bc8 E |
58 | Imgcodecs.imwrite("output.png", filtered); |
59 | ||
7a245e8b | 60 | } |
0e9212ae | 61 | } |