Search code examples
image-processingopencvwebcammaskingemgucv

Get a Image Mask of the Differences Between Two Images Emgu CV


I was wondering how to take two different gray images and create a mask of the differences between the two. Any help would be much appreciated.


Solution

  • Assuming the camera is stationary and the images are very well, you can find the differences between the two images using element-wise subtraction. Using OpenCV's C++ API, it would look something like this (note: this code is untested):

    void FindDifference(cv::Mat src1, cv::Mat src2, cv::Mat &dst, int threshold) {
        dst = cv::abs(src2 - src1);
        cv::threshold(dst, dst, threshold, 255, cv::THRESH_BINARY);
    }
    

    You can tweak the value of threshold to reduce false positives caused by noise. If you are still getting too many false positives, you should use look into motion estimation and image registration to align the two images.

    I am not familiar with EmuCV's API, but it should be fairly straightforward to translate the above code into its API calls.