Reputation: 445
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()
Upvotes: 0
Views: 2262
Reputation: 13651
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()
Upvotes: 1