您的位置:首页 > 其它

计算两幅图片的farneback 稠密光流,并将结果图显示出来的程序

2014-07-29 10:25 351 查看
import cv2
import numpy as np
import Image
import cv2.cv as cv

def image_joint(image_list,opt):#opt= vertical ,horizon
image_num=len(image_list)
image_size=image_list[0].size
height=image_size[1]
width=image_size[0]

if opt=='vertical':
new_img=Image.new('RGB',(width,image_num*height),255)
else:
new_img=Image.new('RGB',(image_num*width,height),255)
x=y=0
count=0
for img in image_list:

new_img.paste(img,(x,y))
count+=1
if opt=='horizontal':
x+=width
else : y+=height
return new_img

start_img = Image.open("C:/Users/Dell/Desktop/output/0260.bmp")
later_img = Image.open("C:/Users/Dell/Desktop/output/0275.bmp")
print type(start_img)
start_img=np.array(start_img)
later_img=np.array(later_img)
print start_img.shape
prvs = cv2.cvtColor(start_img,cv2.COLOR_BGR2GRAY)
next = cv2.cvtColor(later_img,cv2.COLOR_BGR2GRAY)
print next.shape

flow = cv2.calcOpticalFlowFarneback(prvs,next,0.5, 3, 15,3, 5, 1.2, 0)

hsv = np.zeros_like(start_img)
hsv[...,1] =255
mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
hsv[...,0] = ang*180/np.pi/2
hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
RGB= cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)
GRAY = cv2.cvtColor(RGB,cv2.COLOR_BGR2GRAY)
print type(GRAY)
GRAY=Image.fromarray(GRAY)
start_img=Image.fromarray(start_img)
later_img=Image.fromarray(later_img)

joint_image=image_joint([start_img,later_img,GRAY],'vertical')
joint_image.show()
#GRAY.show()
#cv2.imshow('frame2',RGB)

cv.WaitKey()

#if ord('s'):

#    cv2.imwrite('opticalhsv.png',GRAY)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐