首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于Zernike矩的mahotas和opencv图像重建

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

Stack Overflow用户
提问于 2015-10-18 02:58:17
回答 2查看 6.1K关注 0票数 5

我听说mahotas跟随教程,希望在python中找到Zernike多项式的一个良好实现。再简单不过了。但是,我需要比较原始图像和由Zernike矩重建的图像之间的欧几里德差。如果可能的话,我已询问 mahotas的作者可以将重构功能添加到他的库中,但是他没有时间构建它。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-26 05:56:40

基于fireant在回答中提到的代码,我开发了以下重建代码。我还发现研究论文[A. Khotanzad和Y. H. Hong,“Zernike矩的不变图像识别”]和[“快速计算Zernike矩的一种新方法”]非常有用。

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

下面是使用此代码进行的示例重构:

输入图像

使用12阶重建图像的实部

代码语言:javascript
复制
'''
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')
票数 9
EN

Stack Overflow用户

发布于 2015-10-21 19:03:15

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

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

https://stackoverflow.com/questions/33193841

复制
相关文章

相似问题

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