gpt4 book ai didi

python - 如何在python openCV的窗口上显示图像结果

转载 作者:行者123 更新时间:2023-12-02 17:44:54 26 4
gpt4 key购买 nike

我打算显示每对图像之间的光流结果和差异图像,但是即使我看到图像已正确写入,图像窗口也始终显示最后一对的结果。
cv2.imshow('flow',bgr)
cv2.imshow('diff',diff)
有人可以阐明这一点,谢谢!

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()

最佳答案

您需要将waitKey()放入while循环中:

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()

关于python - 如何在python openCV的窗口上显示图像结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37896999/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com