0

I intend to show the optical flow result and difference image between every pair of images, but the image window always display the result from the last pair even I can see the images have been written correctly.
cv2.imshow('flow',bgr) cv2.imshow('diff',diff) Could someone shed a light on this, thanks!

import numpy as np   
import cv2


dirOC = 'ImgDir_OriC\\'
dirTC = 'ImgDir_TransC\\'
suffix = '.bmp'
fCount = 11
count = 0
MAX = 10
while(count < MAX ):
    fNameO = dirOC + str(fCount) + suffix
    fNameT = dirTC + str(fCount) + suffix
    print fNameO
    print fNameT

    imgO = cv2.imread(fNameO, cv2.IMREAD_COLOR)
    prvs = cv2.cvtColor(imgO,cv2.COLOR_BGR2GRAY)

    imgT = cv2.imread(fNameT, cv2.IMREAD_COLOR)
    next = cv2.cvtColor(imgT,cv2.COLOR_BGR2GRAY)   

    flow = cv2.calcOpticalFlowFarneback(prvs,next, 0.5,3,15,15,3,5,1)
    mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])    

    hsv = np.zeros_like(imgO)    
    hsv[...,1] = 255
    hsv[...,0] = ang*180/np.pi/2     
    hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)           
    bgr = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)
    cv2.imshow('flow',bgr)

    diff = np.zeros_like(prvs)    
    cv2.subtract(prvs,next,diff)
    cv2.imshow('diff',diff)

    cv2.imwrite('flow'+str(fCount) + '.png',bgr)    
    cv2.imwrite('diff'+str(fCount) + '.png',diff)

    fCount = fCount +1
    count = count +1    


cv2.waitKey(0)
cv2.destroyAllWindows()

1 Answer 1

1

You need to put the waitKey() inside the while loop:

import numpy as np   
import cv2

dirOC = 'ImgDir_OriC\\'
dirTC = 'ImgDir_TransC\\'
suffix = '.bmp'
fCount = 11
count = 0
MAX = 10
while(count < MAX ):
    fNameO = dirOC + str(fCount) + suffix
    fNameT = dirTC + str(fCount) + suffix
    print fNameO
    print fNameT

    imgO = cv2.imread(fNameO, cv2.IMREAD_COLOR)
    prvs = cv2.cvtColor(imgO,cv2.COLOR_BGR2GRAY)

    imgT = cv2.imread(fNameT, cv2.IMREAD_COLOR)
    next = cv2.cvtColor(imgT,cv2.COLOR_BGR2GRAY)   

    flow = cv2.calcOpticalFlowFarneback(prvs,next, 0.5,3,15,15,3,5,1)
    mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])    

    hsv = np.zeros_like(imgO)    
    hsv[...,1] = 255
    hsv[...,0] = ang*180/np.pi/2     
    hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)           
    bgr = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)
    cv2.imshow('flow',bgr)

    diff = np.zeros_like(prvs)    
    cv2.subtract(prvs,next,diff)
    cv2.imshow('diff',diff)

    cv2.imwrite('flow'+str(fCount) + '.png',bgr)    
    cv2.imwrite('diff'+str(fCount) + '.png',diff)

    fCount = fCount +1
    count = count +1    

    cv2.waitKey(0) # <<<<<<< inside the while loop

cv2.destroyAllWindows()
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, you are right. Forgot to add that line of code. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.