subtract one image from another using openCV

前端 未结 4 1494
不思量自难忘°
不思量自难忘° 2020-12-30 07:50

How can I subtract one image from another using openCV?

Ps.: I coudn\'t use the python implementation because I\'ll have to do it in C++

相关标签:
4条回答
  • 2020-12-30 08:13
    #include <cv.h>
    #include <highgui.h>
    
    using namespace cv;
    
    Mat im = imread("cameraman.tif");
    Mat im2 = imread("lena.tif");
    
    Mat diff_im = im - im2;
    

    Change the image names. Also make sure they have the same size.

    0 讨论(0)
  • 2020-12-30 08:17

    Use LoadImage to load your images into memory, then use the Sub method.

    This link contains some example code, if that will help: http://permalink.gmane.org/gmane.comp.lib.opencv/36167

    0 讨论(0)
  • 2020-12-30 08:18

    Instead of using diff or just plain subtraction im1-im2 I would rather suggest the OpenCV method cv::absdiff

    using namespace cv;
    Mat im1 = imread("image1.jpg");
    Mat im2 = imread("image2.jpg");
    Mat diff;
    absdiff(im1, im2, diff);
    

    Since images are usually stored using unsigned formats, the subtraction methods of @Dat and @ssh99 will kill all the negative differences. For example, if some pixel of a BMP image has value [20, 50, 30] for im1 and [70, 80, 90] for im2, using both im1 - im2 and diff(im1, im2, diff) will produce value [0,0,0], since 20-70 = -50, 50-80 = -30, 30-90 = -60 and all negative results will be converted to unsigned value of 0, which, in most cases, is not what you want. Method absdiff will instead calculate the absolute values of all subtractions, thus producing more reasonable [50,30,60].

    0 讨论(0)
  • 2020-12-30 08:20

    use cv::subtract() method.

    Mat img1=some_img;
    Mat img2=some_img;
    
    Mat dest;
    
    cv::subtract(img1,img2,dest); 
    

    This performs elementwise subtract of (img1-img2). you can find more details about it http://docs.opencv.org/modules/core/doc/operations_on_arrays.html

    0 讨论(0)
提交回复
热议问题