Subtract one image from another using openCV

How can I subtract one image from another using openCV?

Ps: I cannot use the python implementation because I have to do it in C ++

+7
c ++ image opencv subtraction
source share
4 answers

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

This link contains some sample code if this helps: http://permalink.gmane.org/gmane.comp.lib.opencv/36167

+5
source share
 #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 are the same size.

+13
source share

use the cv :: subtract () method.

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

Performs element subtraction (img1-img2). you can find more information about this http://docs.opencv.org/modules/core/doc/operations_on_arrays.html

+3
source share

Instead of using diff or just subtracting im1-im2 I would rather suggest the OpenCV 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 @Dat and @ ssh99 will kill all the negative differences. For example, if some pixel of the BMP image has the value [20, 50, 30] for im1 and [70, 80, 90] for im2 , using both im1 - im2 and diff(im1, im2, diff) will result in the value [0,0,0] , since 20-70 = -50 , 50-80 = -30 , 30-90 = -60 , and all negative results will be converted to unsigned value 0 , which in most cases is not what Do you want to. The absdiff method will instead calculate the absolute values ​​of all subtractions, thereby creating more reasonable ones [50,30,60] .

0
source share

All Articles