public static List<Element> detectChanges(Mat base, Mat mChanged) { int PIXEL_DIFF_THRESHOLD = 3; int IMAGE_DIFF_THRESHOLD = 5; Mat mBaseGray = Element.getNewMat(); Mat mChangedGray = Element.getNewMat(); Mat mDiffAbs = Element.getNewMat(); Mat mDiffTresh = Element.getNewMat(); Mat mChanges = Element.getNewMat(); List<Element> rectangles = new ArrayList<>(); Imgproc.cvtColor(base, mBaseGray, toGray); Imgproc.cvtColor(mChanged, mChangedGray, toGray); Core.absdiff(mBaseGray, mChangedGray, mDiffAbs); Imgproc.threshold(mDiffAbs, mDiffTresh, PIXEL_DIFF_THRESHOLD, 0.0, Imgproc.THRESH_TOZERO); if (Core.countNonZero(mDiffTresh) > IMAGE_DIFF_THRESHOLD) { Imgproc.threshold(mDiffAbs, mDiffAbs, PIXEL_DIFF_THRESHOLD, 255, Imgproc.THRESH_BINARY); Imgproc.dilate(mDiffAbs, mDiffAbs, Element.getNewMat()); Mat se = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5, 5)); Imgproc.morphologyEx(mDiffAbs, mDiffAbs, Imgproc.MORPH_CLOSE, se); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Mat mHierarchy = Element.getNewMat(); Imgproc.findContours(mDiffAbs, contours, mHierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); rectangles = contoursToRectangle(contours); Core.subtract(mDiffAbs, mDiffAbs, mChanges); Imgproc.drawContours(mChanges, contours, -1, new Scalar(255)); //logShow(mDiffAbs); } return rectangles; }
public Mat open(Mat input, int elementSize, int elementShape) { Mat outputImage = new Mat(); Mat element = getKernelFromShape(elementSize, elementShape); Imgproc.morphologyEx(input,outputImage, Imgproc.MORPH_OPEN, element); return outputImage; }
public Mat close(Mat input, int elementSize, int elementShape) { Mat outputImage = new Mat(); Mat element = getKernelFromShape(elementSize, elementShape); Imgproc.morphologyEx(input,outputImage, Imgproc.MORPH_CLOSE, element); return outputImage; }
public Mat open(Mat input, int elementSize, int elementShape) { Mat outputImage = new Mat(); Mat element = getKernelFromShape(elementSize, elementShape); Imgproc.morphologyEx(input,outputImage, Imgproc.MORPH_OPEN, element); return outputImage; }
public Mat close(Mat input, int elementSize, int elementShape) { Mat outputImage = new Mat(); Mat element = getKernelFromShape(elementSize, elementShape); Imgproc.morphologyEx(input,outputImage, Imgproc.MORPH_CLOSE, element); return outputImage; }
public Mat close(Mat input, int elementSize, int elementShape) { Mat outputImage = new Mat(); Mat element = getKernelFromShape(elementSize, elementShape); Imgproc.morphologyEx(input,outputImage, Imgproc.MORPH_CLOSE, element); return outputImage; }
public Mat open(Mat input, int elementSize, int elementShape) { Mat outputImage = new Mat(); Mat element = getKernelFromShape(elementSize, elementShape); Imgproc.morphologyEx(input,outputImage, Imgproc.MORPH_OPEN, element); return outputImage; }
public Mat close(Mat input, int elementSize, int elementShape) { Mat outputImage = new Mat(); Mat element = getKernelFromShape(elementSize, elementShape); Imgproc.morphologyEx(input,outputImage, Imgproc.MORPH_CLOSE, element); return outputImage; }
public Mat open(Mat input, int elementSize, int elementShape) { Mat outputImage = new Mat(); Mat element = getKernelFromShape(elementSize, elementShape); Imgproc.morphologyEx(input,outputImage, Imgproc.MORPH_OPEN, element); return outputImage; }
public boolean hasChanges(Mat current) { int PIXEL_DIFF_THRESHOLD = 5; int IMAGE_DIFF_THRESHOLD = 5; Mat bg = new Mat(); Mat cg = new Mat(); Mat diff = new Mat(); Mat tdiff = new Mat(); Imgproc.cvtColor(base, bg, Imgproc.COLOR_BGR2GRAY); Imgproc.cvtColor(current, cg, Imgproc.COLOR_BGR2GRAY); Core.absdiff(bg, cg, diff); Imgproc.threshold(diff, tdiff, PIXEL_DIFF_THRESHOLD, 0.0, Imgproc.THRESH_TOZERO); if (Core.countNonZero(tdiff) <= IMAGE_DIFF_THRESHOLD) { return false; } Imgproc.threshold(diff, diff, PIXEL_DIFF_THRESHOLD, 255, Imgproc.THRESH_BINARY); Imgproc.dilate(diff, diff, new Mat()); Mat se = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5,5)); Imgproc.morphologyEx(diff, diff, Imgproc.MORPH_CLOSE, se); List<MatOfPoint> points = new ArrayList<MatOfPoint>(); Mat contours = new Mat(); Imgproc.findContours(diff, points, contours, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); int n = 0; for (Mat pm: points) { log(lvl, "(%d) %s", n++, pm); printMatI(pm); } log(lvl, "contours: %s", contours); printMatI(contours); return true; }