首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python中的图像配准

python中的图像配准
EN

Stack Overflow用户
提问于 2012-06-19 15:41:19
回答 1查看 12.1K关注 0票数 0

我正在做一个项目,它需要找到一个新的图像已经移动和旋转了多少w.r.t旧图像。我正在尝试用fft来实现它。然而,它在某些情况下有效,而对另一些情况则失败。我遵循的步骤是:

shift

  • transform

  • 对这两幅图像进行canny边缘检测,实现

  • ,利用快速傅立叶变换找到移位点,将图像移除到极坐标区域,找到移位,并将答案转换为弧度

在某些情况下,我得到正确的答案,但在其他情况下,我得到移位为(0,0),旋转为0弧度。请提出可能发生这种情况的原因。

以下是代码:

代码语言:javascript
复制
class Register:
    '''
        Class for registering images based on FFT. The usage is as follows:
        >>> im0 = imread('image0.jpg', flatten = True)
        >>> im1 = imread('image1.jpg', flatten = True)
        >>> reg = Register(im0, im1)
        >>> shift = reg.shift
        >>> rotation = reg.theta

        Note:
            1. This image registration technique is not very reliable and
               is valid only for small rotation
            2. The class is very slow, since it depends on canny edge detection
               module for finding edges.
    '''
    def __init__(self,imin0, imin1, PROCESSED = False):
        '''
            This method is used to execute all the routines required to get the
            shift and the rotation
        '''
        # find edges to remove low frequency signals and suppress information
        if PROCESSED:
            im0 = imin0
            im1 = imin1
        else:
            im0 = Canny(imin0, 0.85, 5).grad
            im1 = Canny(imin1, 0.85, 5).grad

        # A major drawback of this method is that it can operate only on square
        # images. Hence we will make square image of any input image

        im0 = self.createsquareim(self.clearBorder(im0))
        im1 = self.createsquareim(self.clearBorder(im1))

        self.shift = self.findShift(im0,im1)
        imtrans = shift(im1, self.shift)
        # Remove the shift in the image. This is mandatory before we find theta
        impolar0 = self.makePolar(im0)
        impolar1 = self.makePolar(imtrans)
        self.index = self.findShift(impolar0, impolar1)[1]
        self.theta = ((self.index*90.0)/impolar1.shape[0])

    def clearBorder(self,im,width = 50, color = 255):
        '''
            A little house keeping to clear any border noise
        '''
        im[:,:width] = color
        im[:,-width:] = color
        im[:width,:] = color
        im[-width:,:] = color

        return im

    def createsquareim(self, im):
        """
        function createsquareim
        input:numpy ndarray
        output:numpy ndarray

        The function takes in an image array and converts it into square
        image by creating empty columns and rows.
        """
        lenmax = max(im.shape[0],im.shape[1])
        imout = zeros((lenmax,lenmax))
        imout[:,:] = 255
        imout[:im.shape[0],:im.shape[1]] = im
        return imout

    def findShift(self, im0, im1):
        '''
            This method is based on fft method of registering images.
        '''
        IM0 = fft2(im0)
        IM1 = fft2(im1)

        numer = IM0*conj(IM1)
        denom = abs(IM0*IM1)

        pulse_im = ifft2(numer/denom)
        mag = abs(pulse_im)
        x, y = where(mag == mag.max())

        x = array(x.tolist())   # Issues with read only arrays
        y = array(y.tolist())

        X, Y = im0.shape

        if x > X/2:
            x -= X
        if y > Y/2:
            y -= Y

        return [x[0], y[0]]

    def makePolar(self, im):
        '''
            This method will convert the cartesian coordinates image
            to polar coordinates image. The relation between the two
            domains is
              F(r,theta) = f(r*cos(theta),r*sin(theta))
            To make the process fast, we are using map_coordinates function
        '''
        m, n = im.shape
        r_max = hypot(m, n)

        r_mat = zeros_like(im)
        t_mat = zeros_like(im)

        r_mat.T[:] = linspace(0, r_max, m)
        t_mat[:] = linspace(0, pi/2, n)

        x = r_mat*cos(t_mat)
        y = r_mat*sin(t_mat)

        imout = zeros_like(im)
        imout = map_coordinates(im, [x, y], cval = 255)

        return imout
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-06 17:05:43

我已经用SIFT和RANSAC算法编写了一个用于图像配准的python脚本,您可以在http://github.com/vishwa91/pyimreg上找到代码。另外,在http://cyroforge.wordpress.com/2012/07/05/image-registration-using-python/上也可以找到一个类似的小介绍。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11104551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档