I want to compare the two images below, but none of the codes I have tried produce any meaningful output. Here are the pictures :
Orginal
Changed
Desired Result
First, the sizes of the pictures are different and there is also color difference. That’s why no method I tried could give me the results I wanted. Here are the methods I tried and their results :
The Code :
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
if (argc != 3)
{
cout << "Usage: <Program Name> <Image1> <Image2>" << endl;
return -1;
}
Mat img1 = imread(argv[1], IMREAD_COLOR);
Mat img2 = imread(argv[2], IMREAD_COLOR);
if (img1.empty() || img2.empty())
{
cout << "Could not open or find the image" << endl;
return -1;
}
// Resize img2 to match the size of img1
resize(img2, img2, img1.size());
Mat diff, diffGray, thresh;
absdiff(img1, img2, diff);
cvtColor(diff, diffGray, COLOR_BGR2GRAY);
threshold(diffGray, thresh, 30, 255, THRESH_BINARY);
// Create a mask with red color where differences are found
Mat redMask = Mat::zeros(img1.size(), img1.type());
redMask.setTo(Scalar(0, 0, 255), thresh);
// Apply the red mask to the original image
img1.setTo(Scalar(0, 0, 255), thresh);
// Save the result
imwrite("differences.png", img1);
namedWindow("Differences", WINDOW_NORMAL);
imshow("Differences", img1);
waitKey(0);
return 0;
}
The Result
I also tried the methods below, but the result was still the same :
Detect and visualize differences between two images with OpenCV Python
Checking images for similarity with OpenCV
Comparing two images/pictures, and mark the difference
Crazy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1