warp.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:inverse-compositional-STN 作者: ericlin79119 项目源码 文件源码
def transformImage(opt,image,pMtrx):
    with tf.name_scope("transformImage"):
        refMtrx = tf.tile(tf.expand_dims(opt.refMtrx,axis=0),[opt.batchSize,1,1])
        transMtrx = tf.matmul(refMtrx,pMtrx)
        # warp the canonical coordinates
        X,Y = np.meshgrid(np.linspace(-1,1,opt.W),np.linspace(-1,1,opt.H))
        X,Y = X.flatten(),Y.flatten()
        XYhom = np.stack([X,Y,np.ones_like(X)],axis=1).T
        XYhom = np.tile(XYhom,[opt.batchSize,1,1]).astype(np.float32)
        XYwarpHom = tf.matmul(transMtrx,XYhom)
        XwarpHom,YwarpHom,ZwarpHom = tf.unstack(XYwarpHom,axis=1)
        Xwarp = tf.reshape(XwarpHom/(ZwarpHom+1e-8),[opt.batchSize,opt.H,opt.W])
        Ywarp = tf.reshape(YwarpHom/(ZwarpHom+1e-8),[opt.batchSize,opt.H,opt.W])
        # get the integer sampling coordinates
        Xfloor,Xceil = tf.floor(Xwarp),tf.ceil(Xwarp)
        Yfloor,Yceil = tf.floor(Ywarp),tf.ceil(Ywarp)
        XfloorInt,XceilInt = tf.to_int32(Xfloor),tf.to_int32(Xceil)
        YfloorInt,YceilInt = tf.to_int32(Yfloor),tf.to_int32(Yceil)
        imageIdx = np.tile(np.arange(opt.batchSize).reshape([opt.batchSize,1,1]),[1,opt.H,opt.W])
        imageVec = tf.reshape(image,[-1,int(image.shape[-1])])
        imageVecOut = tf.concat([imageVec,tf.zeros([1,int(image.shape[-1])])],axis=0)
        idxUL = (imageIdx*opt.H+YfloorInt)*opt.W+XfloorInt
        idxUR = (imageIdx*opt.H+YfloorInt)*opt.W+XceilInt
        idxBL = (imageIdx*opt.H+YceilInt)*opt.W+XfloorInt
        idxBR = (imageIdx*opt.H+YceilInt)*opt.W+XceilInt
        idxOutside = tf.fill([opt.batchSize,opt.H,opt.W],opt.batchSize*opt.H*opt.W)
        def insideImage(Xint,Yint):
            return (Xint>=0)&(Xint<opt.W)&(Yint>=0)&(Yint<opt.H)
        idxUL = tf.where(insideImage(XfloorInt,YfloorInt),idxUL,idxOutside)
        idxUR = tf.where(insideImage(XceilInt,YfloorInt),idxUR,idxOutside)
        idxBL = tf.where(insideImage(XfloorInt,YceilInt),idxBL,idxOutside)
        idxBR = tf.where(insideImage(XceilInt,YceilInt),idxBR,idxOutside)
        # bilinear interpolation
        Xratio = tf.reshape(Xwarp-Xfloor,[opt.batchSize,opt.H,opt.W,1])
        Yratio = tf.reshape(Ywarp-Yfloor,[opt.batchSize,opt.H,opt.W,1])
        imageUL = tf.to_float(tf.gather(imageVecOut,idxUL))*(1-Xratio)*(1-Yratio)
        imageUR = tf.to_float(tf.gather(imageVecOut,idxUR))*(Xratio)*(1-Yratio)
        imageBL = tf.to_float(tf.gather(imageVecOut,idxBL))*(1-Xratio)*(Yratio)
        imageBR = tf.to_float(tf.gather(imageVecOut,idxBR))*(Xratio)*(Yratio)
        imageWarp = imageUL+imageUR+imageBL+imageBR
    return imageWarp
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号