Ask Your Question

Revision history [back]

This is were contours may be of use to you.

The comments in the code should be self-explanatory.

Absolute diff Image

The calculated difference between the two

Thresholded Diff

Thresholded Diff

Final Image

Differences shown

Mat first = imread("first.png", IMREAD_GRAYSCALE);
Mat second = imread("second.png", IMREAD_GRAYSCALE);

// inorder to perform the absdiff, the images need to be of equal size
// NOTE: I believe this is the reason why you have a big blue box on the edges
// as well of the output image. This can be fixed by using two equal images in the first place
resize(second, second, first.size());

Mat diff;
absdiff(first, second, diff);

// after getting the difference, we binarize it
Mat thresh;
threshold(diff, thresh, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);

vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
// extract the contours in the threshold image
findContours(thresh, contours, hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE);

// this matrix will be used for drawing purposes
Mat out;
cvtColor(second, out, COLOR_GRAY2BGR);

// for each detected contour, calculate its bouding rectangle and draw it
// now, you can also filter out some noise should you wish by checking if the contour is big
// or small enough along with other contour properties.
// Ref: https://docs.opencv.org/trunk/dd/d49/tutorial_py_contour_features.html
// Ref: https://docs.opencv.org/trunk/d1/d32/tutorial_py_contour_properties.html
for(vector<Point> cont: contours)
{
    Rect box = boundingRect(cont);
    rectangle(out, box, Scalar(255, 0, 0));
}

imshow("FIRST", first);
imshow("SECOND", second);
imshow("ABS-DIFF", diff);
imshow("THRESH", thresh);
imshow("OUTPUT", out);

waitKey();
destroyAllWindows();

This is were contours may be of use to you.

The comments in the code should be self-explanatory.

Absolute diff Image

The calculated difference between the two

Thresholded Diff

Thresholded Diff

Final Image

Differences shown

Mat first = imread("first.png", IMREAD_GRAYSCALE);
Mat second = imread("second.png", IMREAD_GRAYSCALE);

// inorder to perform the absdiff, the images need to be of equal size
// NOTE: I believe this is the reason why you have a big blue box on the edges
// as well of the output image. This can be fixed by using two equal images in the first place
resize(second, second, first.size());

Mat diff;
absdiff(first, second, diff);

// after getting the difference, we binarize it
Mat thresh;
threshold(diff, thresh, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);

vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
// extract the contours in the threshold image
findContours(thresh, contours, hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE);

// this matrix will be used for drawing purposes
Mat out;
cvtColor(second, out, COLOR_GRAY2BGR);

// for For each detected contour, calculate its bouding bounding rectangle and draw it
// now, you You can also filter out some noise should you wish by checking if the contour is big
// or small enough along with other contour properties.
// Ref: https://docs.opencv.org/trunk/dd/d49/tutorial_py_contour_features.html
// Ref: https://docs.opencv.org/trunk/d1/d32/tutorial_py_contour_properties.html
for(vector<Point> cont: contours)
{
    Rect box = boundingRect(cont);
    rectangle(out, box, Scalar(255, 0, 0));
}

imshow("FIRST", first);
imshow("SECOND", second);
imshow("ABS-DIFF", diff);
imshow("THRESH", thresh);
imshow("OUTPUT", out);

waitKey();
destroyAllWindows();