Ask Your Question

Revision history [back]

This solution will help you.

    Core.absdiff(firstMat, secondMat, outputImageMat);

    List<MatOfPoint> contours = new ArrayList<>();

    Imgproc.cvtColor(outputImageMat, outputImageMat, Imgproc.COLOR_BGR2GRAY);
    Imgproc.findContours(outputImageMat, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

    for (int i = 0; i < contours.size(); i++) {
        Rect rect = Imgproc.boundingRect(contours.get(i));
        Imgproc.rectangle(secondMat, new Point(rect.x, rect.y), new Point(rect.x + rect.width,
                rect.y + rect.height), new Scalar(100, 0, 0, 255), 6);
    }
    outputImage.setImageBitmap(setMatToBitmap(secondMat));