使用mahotas和opencv基于Zernike矩的图像重建

Image reconstruction based on Zernike moments using mahotas and opencv

我听说 mahotas 跟随 this tutorial in the hope of finding a good implementation of Zernike polynomials in python. It couldn't be easier. However, I need to compare the Euclidean difference between the original image and the one reconstructed from the Zernike moments. I asked mahotas 的作者,如果他可以将重建功能添加到他的库中,但他没有时间构建它。

如何使用 mahotas 提供的 Zernike 矩在 OpenCV 中重建图像?

没那么难,我想你可以自己编码。首先,请记住每个 moment/matrix 又名基础图像的逆是该矩阵的转置,因为它们是正交的。然后查看该库的作者用来测试该功能的 code 。这比库中的代码更简单,因此您可以阅读并理解它是如何工作的(当然也慢得多)。您需要为每个时刻获取这些矩阵,这些矩阵是基础图像。您可以修改 _slow_znl 以获取在主循环 for x,y,p in zip(X,Y,P): 内计算的 x,y 的值,并将其存储在与输入图像大小相同的矩阵中。将白色图像传递给 _slow_zernike 并获取所有力矩矩阵,直到您想要的径向度数。要使用系数重建图像,只需像使用 Haar 变换一样使用这些矩阵的转置。

基于code that fireant mentioned in his answer I developed the following code for reconstruction. I also found the research papers [A. Khotanzad and Y. H. Hong, “Invariant image recognition by Zernike moments”] and [S.-K. Hwang and W.-Y. Kim, “A novel approach to the fast computation of Zernike moments”]非常有用。

函数_slow_zernike_poly构造二维泽尼克基函数。在 zernike_reconstruct 函数中,我们将图像投影到 _slow_zernike_poly 返回的基函数上并计算矩。然后我们使用重构公式。

下面是使用此代码完成的示例重建:

输入图片

使用 12 阶重建图像的实部

'''
Copyright (c) 2015
Dhanushka Dangampola <dhanushkald@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''

import numpy as np
from math import atan2
from numpy import cos, sin, conjugate, sqrt

def _slow_zernike_poly(Y,X,n,l):
    def _polar(r,theta):
        x = r * cos(theta)
        y = r * sin(theta)
        return 1.*x+1.j*y

    def _factorial(n):
        if n == 0: return 1.
        return n * _factorial(n - 1)
    y,x = Y[0],X[0]
    vxy = np.zeros(Y.size, dtype=complex)
    index = 0
    for x,y in zip(X,Y):
        Vnl = 0.
        for m in range( int( (n-l)//2 ) + 1 ):
            Vnl += (-1.)**m * _factorial(n-m) /  \
                ( _factorial(m) * _factorial((n - 2*m + l) // 2) * _factorial((n - 2*m - l) // 2) ) * \
                ( sqrt(x*x + y*y)**(n - 2*m) * _polar(1.0, l*atan2(y,x)) )
        vxy[index] = Vnl
        index = index + 1

    return vxy

def zernike_reconstruct(img, radius, D, cof):

    idx = np.ones(img.shape)

    cofy,cofx = cof
    cofy = float(cofy)
    cofx = float(cofx)
    radius = float(radius)    

    Y,X = np.where(idx > 0)
    P = img[Y,X].ravel()
    Yn = ( (Y -cofy)/radius).ravel()
    Xn = ( (X -cofx)/radius).ravel()

    k = (np.sqrt(Xn**2 + Yn**2) <= 1.)
    frac_center = np.array(P[k], np.double)
    Yn = Yn[k]
    Xn = Xn[k]
    frac_center = frac_center.ravel()

    # in the discrete case, the normalization factor is not pi but the number of pixels within the unit disk
    npix = float(frac_center.size)

    reconstr = np.zeros(img.size, dtype=complex)
    accum = np.zeros(Yn.size, dtype=complex)

    for n in range(D+1):
        for l in range(n+1):
            if (n-l)%2 == 0:
                # get the zernike polynomial
                vxy = _slow_zernike_poly(Yn, Xn, float(n), float(l))
                # project the image onto the polynomial and calculate the moment
                a = sum(frac_center * conjugate(vxy)) * (n + 1)/npix
                # reconstruct
                accum += a * vxy
    reconstr[k] = accum
    return reconstr

if __name__ == '__main__':

    import cv2
    import pylab as pl
    from matplotlib import cm

    D = 12

    img = cv2.imread('fl.bmp', 0)

    rows, cols = img.shape
    radius = cols//2 if rows > cols else rows//2

    reconst = zernike_reconstruct(img, radius, D, (rows/2., cols/2.))

    reconst = reconst.reshape(img.shape)

    pl.figure(1)
    pl.imshow(img, cmap=cm.jet, origin = 'upper')
    pl.figure(2)    
    pl.imshow(reconst.real, cmap=cm.jet, origin = 'upper')