Want to convert python code to java

Discussion in 'Python' started by Rajvardhan Thakare, Apr 1, 2017.

  1. Rajvardhan Thakare

    Rajvardhan Thakare

    Joined:
    Apr 1, 2017
    Messages:
    1
    Likes Received:
    0
    Hello guys,

    I want to convert Python code to VB.net or Java

    Plz help me

    The code is for calculating Zernike moments
    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')
     
    Rajvardhan Thakare, Apr 1, 2017
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Royan
    Replies:
    8
    Views:
    1,168
    Patricia Shanahan
    Feb 15, 2008
  2. Wind
    Replies:
    0
    Views:
    885
  3. HUNT HUNT
    Replies:
    2
    Views:
    274
    Robert Klemme
    Mar 31, 2010
  4. brewhousepdx

    how to convert wordpress template into Html

    brewhousepdx, Dec 29, 2016, in forum: HTML and CSS
    Replies:
    0
    Views:
    199
    brewhousepdx
    Dec 29, 2016
  5. Amy21
    Replies:
    0
    Views:
    412
    Amy21
    Jan 20, 2017
  6. Rohini
    Replies:
    0
    Views:
    136
    Rohini
    Feb 23, 2017
  7. SUser
    Replies:
    1
    Views:
    151
    kyrericard
    Apr 24, 2017
  8. Kritika Bhardwaj
    Replies:
    0
    Views:
    116
    Kritika Bhardwaj
    Mar 21, 2017
Loading...