While Finding A Difference Between 2 Pictures Opencv Difference Is Bigger Than It Is Supposed To Be
I'm working with OpenCV (in Android NDK) and I have a problem. I want to find differences between two pictures, and than cut the difference. However, following output made differen
Solution 1:
My result in python:
# 2017.12.22 15:48:03 CST# 2017.12.22 16:12:26 CST
import cv2
import numpy as np
img1 = cv2.imread("img1.png")
img2 = cv2.imread("img2.png")
diff = cv2.absdiff(img1, img2)
gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
## find the nozero regions in the gray
imask = gray>0
## create a Mat like img2
canvas = np.zeros_like(img2, np.uint8)
## set mask
canvas[imask] = img2[imask]
cv2.imwrite("result.png", canvas)
Update with c++
//! 2017.12.22 17:05:18 CST//! 2017.12.22 17:22:32 CST#include<opencv2/opencv.hpp>#include<iostream>usingnamespace std;
usingnamespace cv;
intmain(){
Mat img1 = imread("img1.png");
Mat img2 = imread("img2.png");
// calc the difference
Mat diff;
absdiff(img1, img2, diff);
// Get the mask if difference greater than thint th = 10; // 0Mat mask(img1.size(), CV_8UC1);
for(int j=0; j<diff.rows; ++j) {
for(int i=0; i<diff.cols; ++i){
cv::Vec3b pix = diff.at<cv::Vec3b>(j,i);
int val = (pix[0] + pix[1] + pix[2]);
if(val>th){
mask.at<unsignedchar>(j,i) = 255;
}
}
}
// get the foreground
Mat res;
bitwise_and(img2, img2, res, mask);
// displayimshow("res", res);
waitKey();
return0;
}
Similar answers:
Solution 2:
From here: CV - Extract differences between two images
cv::Mat diffImage;
cv::absdiff(backgroundImage, currentImage, diffImage);
cv::Mat foregroundMask = cv::Mat::zeros(diffImage.rows, diffImage.cols, CV_8UC1);
float threshold = 30.0f;
float dist;
for(int j=0; j<diffImage.rows; ++j)
for(int i=0; i<diffImage.cols; ++i)
{
cv::Vec3b pix = diffImage.at<cv::Vec3b>(j,i);
dist = (pix[0]*pix[0] + pix[1]*pix[1] + pix[2]*pix[2]);
dist = sqrt(dist);
if(dist>threshold)
{
foregroundMask.at<unsignedchar>(j,i) = 255;
}
}
Then make background subtraction.
Solution 3:
I did it like this with a help of @Silencer as a concequence I got the right result. Hope this will help if anyone have a similar problem
Mat& backgroundImage = *(Mat*) addrRgba;
Mat& currentImage = *(Mat*) addrRgba2;
Mat HSV_currentImage;
Mat HSVbackgroundImagebg;
Mat diffImage;
cvtColor(backgroundImage, HSVbackgroundImagebg, CV_BGR2HSV);
cvtColor(currentImage, HSV_currentImage, CV_BGR2HSV);
absdiff(HSVbackgroundImagebg, HSV_currentImage, diffImage);
Mat mask(diffImage.size(), CV_8UC1);
float threshold = 30.0f;
float dist;
for(int j=0; j<diffImage.rows; ++j)
for(int i=0; i<diffImage.cols; ++i)
{
Vec3b pix = diffImage.at<cv::Vec3b>(j,i);
dist = (pix[0]*pix[0] + pix[1]*pix[1] + pix[2]*pix[2]);
dist = sqrt(dist);
if(dist>threshold){
mask.at<unsignedchar>(j,i) = 255;
}
}
Mat res;
bitwise_and(currentImage, currentImage, res, mask);
currentImage=res;
Post a Comment for "While Finding A Difference Between 2 Pictures Opencv Difference Is Bigger Than It Is Supposed To Be"