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++
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++
use cv::subtract() method.
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
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
Instead of using
diff
or just plain subtractionim1-im2
I would rather suggest the OpenCV methodcv::absdiff
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]
forim1
and[70, 80, 90]
forim2
, using bothim1 - im2
anddiff(im1, im2, diff)
will produce value[0,0,0]
, since20-70 = -50
,50-80 = -30
,30-90 = -60
and all negative results will be converted to unsigned value of0
, which, in most cases, is not what you want. Methodabsdiff
will instead calculate the absolute values of all subtractions, thus producing more reasonable[50,30,60]
.Change the image names. Also make sure they have the same size.