Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opencv cv2.absdiff(img1, img2).sum() without temporary img

Is it possible to calculate cv2.absdiff(img1, img2).sum() without temporary img ?

I have a video stream and I need some kind of image stabilization on the begining of my processing. The absdiff gives fast and error dependent result on check different placement vectors with two following images, but I have to create, write and read a temporary image which one is used only for calculate the img.sum(). So it would be fine to eliminate these memory allocation, writing and reading steps.

def calcMatch(img1, img2):
    diff = cv2.absdiff(img1, img2)
    return diff.sum()

Solution in python

import cv2
import time

img = cv2.imread('test.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img1 = img[10:330, 10:870]
img2 = img[20:340, 20:880]

start = time.clock()
d = cv2.absdiff(img1, img2)
s = d.sum()
t = time.clock() - start
print 'with absdiff ', t
print s

start = time.clock()
s = cv2.norm(img1, img2, cv2.NORM_L1)
t = time.clock() - start
print 'with norm L1 ',  t
print s

It gives significant speed up as on my laptop with a very stable ratio:
with absdiff 0.00207574457822
4315120
with norm L1 0.000226647018223
4315120.0

like image 602
jshepherd Avatar asked Jul 24 '13 08:07

jshepherd


People also ask

How to add two images in OpenCV using NumPy?

Learn these functions: cv.add (), cv.addWeighted (), etc. You can add two images with the OpenCV function, cv.add (), or simply by the numpy operation res = img1 + img2. Both images should be of same depth and type, or the second image can just be a scalar value. There is a difference between OpenCV addition and Numpy addition.

How many code examples are there for cv2absdiff?

The following are 30 code examples for showing how to use cv2.absdiff (). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

Is there a mask part in OpenCV function?

But OpenCV does not have the mask part in its function. I saw this question but did not work for me. I am trying to multiply the result in the mask so that only the specified region remains. When I try this, I get an error. Not the answer you're looking for? Browse other questions tagged c++ opencv mask mat or ask your own question.

How to get the area of a previous frame in CV2?

You may also want to check out all available functions/classes of the module cv2 , or try the search function . def background_subtraction(previous_frame, frame_resized_grayscale, min_area): """ This function returns 1 for the frames in which the area after subtraction with previous frame is greater than minimum area defined.


1 Answers

Try norm function with NORM_L1 norm. C++ code:

double res = cv::norm(img1, img2, cv::NORM_L1);
like image 59
jet47 Avatar answered Oct 24 '22 08:10

jet47