user3419556
user3419556

Reputation:

Arithmetic Operation on images - OpenCV Python exercise

I am trying to do the exercise associated with the "Arithmetic Operations on Images" chapter of OpencV Python tutorials (http://docs.opencv.org/trunk/doc/py_tutorials/py_core/py_image_arithmetics/py_image_arithmetics.html#image-arithmetics).

1. Create a slide show of images in a folder with smooth transition between images using cv2.addWeighted function

Here is my code :

import cv2
import numpy as np
import time

img1 = cv2.imread('add1.jpg')
img2 = cv2.imread('add2.jpg')
res = img1
i = 10
inc = 1
cv2.imshow('res',res)
while(1):
    if(cv2.waitKey(1) & 0xFF == ord('q')):
        break
    else:
        newimg = np.zeros((640,480))
        res = cv2.addWeighted(img1,0.1*i,img2,0.1*(10-i),0)
        if i == 10 | i == 0:
            inc = -inc
        i = i + inc
        cv2.imshow('res',res)
        time.sleep(0.2)

Here, I am simply trying to go back and forth constantly between two selected images (which are both 640x480) but instead of that my code seems to sum the newly generated images with the older images. After a few seconds I end up with a mostly black and red image because of that. I tried other solutions as well (such as setting res to 0 before assigning it the real value of the image I want to generate) but it doesn't change anything.

I looked for answers to that exercise but didn't find any.

Could you help me please ?

Upvotes: 2

Views: 1757

Answers (1)

berak
berak

Reputation: 39796

here's the culprit:

if i == 10 | i == 0:

that should be :

if i == 10 or i == 0:

(a single | is a binary or, not the logical one)

(let me guess, you originally tried i == 10 || i == 0, (like in c or java) and found out it is not valid python).

Upvotes: 3

Related Questions