Hello again, Actually i am comparing two images, to find new object introduced into second image, which is not there in the first image. I have got the working code here, as an answer, when i posted my question; But clearing the concepts, then programming by own style, is what i believe.
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(void)
{
// Reading color images ( including alpha channel )into Mat
Mat first_rgb = imread("./road.jpg", IMREAD_UNCHANGED);
Mat second_rgb = imread("./road_changed.jpg", IMREAD_UNCHANGED);
// Check for invalid input
if( (! first_rgb.data) || (! second_rgb.data) )
{
cout << "Could not open or find color image or images...\n";
return -1;
}
// Resizing both images, so that number of pixels become same for finding negation / difference
resize(second_rgb, second_rgb, first_rgb.size());
// Converting both images to Gray and load them into new matrices
Mat first_gray, second_gray;
cvtColor(first_rgb, first_gray, COLOR_RGB2GRAY);
cvtColor(second_rgb, second_gray, COLOR_RGB2GRAY);
// Now Get absolute difference into matrix
Mat diff;
absdiff(first_gray, second_gray, diff);
// binarizing the difference, making all zero keeping non-zero / difference intact.
Mat bin = diff > 70;
// Find non-black points
vector<Point> points;
findNonZero(bin, points);
// Get bounding rect
Rect box = boundingRect(points);
float area = box.width * box.height;
cout<<"area in pixels = "<<area<<"\n";
// rectangle the new object in second colorful image
rectangle(second_rgb, box, Scalar(0,255,0), 3);
// Show images
imshow("Result", second_rgb);
imshow("ABS-DIFF", diff);
imshow("BIN", bin);
waitKey(0);
destroyAllWindows();
return 0;
}
It is giving me new object found, the difference, in rectangled so nicely. Now what i want to know is, the "area" i have calculated is correct way? and can i use it for comparison? I mean if the new found object area is more than 120 pixels then it must be the real object there.
Its the output :
Apples-MacBook:object_area rushi$ g++ area.cpp -o area pkg-config --cflags --libs opencv
Apples-MacBook:object_area rushi$ ./area
area in pixels = 120
road.jpg file
road_changed.jpg file
output file with change rectangled :
Thanking you so much. rushikant Pawar