3

I'm trying to do background subtraction using OpenCV trying to subtract two images from each other. One of the images is the background image and is fed to the program statically while the second image is taken from the webcam connected to the Raspberry Pi (the same webcam is used to capture the background image and both images have the same resolution). However I get the error:

error: /build/opencv-VF5Hiu/opencv-2.4.9.1+dfsg/modules/core/src/arithm.cpp:1287: error: (-209) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function arithm_op

my code is the following:

import cv2

Original2 = cv2.imread("Original.png")
Original2 = cv2.resize(Original2, (960, 720))

cap = cv2.VideoCapture(0)

cap.set(3, 960)
cap.set(4, 720)

ret,tespi=cap.read()
cap.release()
cv2.imwrite('tespi.png',tespi)

Edited2= cv2.imread('tespi.png')

diffadd = cv2.subtract(Edited2, Original2)
diffsub = cv2.subtract(Original2, Edited2)

I tried resizing both pictures before subtracting them, tried enforcing the desired resolution, almost tried anything I could think of which is not that so many things since I'm just starting to learn programming in python.

Can you please help me fix this issue ?

  • 1
    It would be quite helpful if you didn't cut off the part of the error message which tells us where the error occurred... – Dan Mašek Sep 09 '18 at 18:44
  • ... although the only two possibilities I see are the two calls to `cv2.subtract`... meaning `Original2` and `Edited2` are not the same shape -- look into that. | Furthermore, please turn that code sample into a proper [mcve] -- right now 5 of the 6 imports are useless, and so is the code involving variables `Original` and `Edited`. – Dan Mašek Sep 09 '18 at 19:41
  • Before `diffadd = cv2.subtract(Edited2, Original2)` line try: `print Edited2.shape, Edited2.dtype` and `print Original2.shape, Original2.dtype`, to confirm if both the images have same shape and data type. – ZdaR Sep 10 '18 at 04:33
  • I edited the post, you can now see the full error message above, sorry guys – Hatef Abdollahi Sep 11 '18 at 08:21
  • I also tried to make the code minimal, complete, and verifiable as much as I could. Hope it's more professional now! thanks for the advice though. – Hatef Abdollahi Sep 11 '18 at 08:26
  • @ZdaR I did as you asked and got this values which confirm that both images have the same dimensions and types: -(480,640,3) uint8 -(480,640,3) uint8 – Hatef Abdollahi Sep 11 '18 at 09:51

0 Answers0