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

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)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值