Image-feature-detection-using-Phase-Stretch-Transform icon indicating copy to clipboard operation
Image-feature-detection-using-Phase-Stretch-Transform copied to clipboard

Java or C edition?

Open maxbrito opened this issue 9 years ago • 15 comments

Hello,

Thanks for releasing this algorithm. Would by any chance be possible to port into Java or C code that can run without MatLab?

Many thanks in advance, Nuno

maxbrito avatar Feb 11 '16 08:02 maxbrito

Hello @nunobrito,

if you just want to test the code, you can use the freely available Octave (a MATLAB-compatible programming language).

Besides, to whom it may concern, here's a Python (with NumPy) port (almost 1:1) of the code [the cleaning steps for Morph_flag=1 are left out, as they are not easily 1:1 portable]:

def pst(image, lpf=0.5, phase_strength=0.5, warp_strength=0.5, thresh_min=-0.5, thresh_max=0.5, morph_flag=False):
    # port of https://github.com/JalaliLabUCLA/Image-feature-detection-using-Phase-Stretch-Transform
    # restrictions on usage apply, consult the original source!
    import numpy
    from numpy.fft import fft2, ifft2, fftshift

    def cart2pol(x, y):
        return numpy.arctan2(y, x), numpy.hypot(x, y)

    image = image.astype(numpy.float64)

    L = 0.5

    x = numpy.linspace(-L, L, image.shape[1])
    y = numpy.linspace(-L, L, image.shape[0])

    X, Y = numpy.meshgrid(x, y)

    THETA, RHO = cart2pol(X, Y)

    X_step = x[1]-x[0]
    Y_step = y[1]-y[0]

    fx = numpy.linspace(-L/X_step, L/X_step, len(x))
    fy = numpy.linspace(-L/Y_step, L/Y_step, len(y))

    fx_step = fx[1]-fx[0]
    fy_step = fy[1]-fy[0]

    FX, FY = numpy.meshgrid(fx_step, fy_step)

    FTHETA, FRHO = cart2pol(FX, FY)

    # lowpass

    sigma = (lpf ** 2) / numpy.log(2)

    image_f = fft2(image)   
    image_f = image_f * fftshift(numpy.exp(-(RHO / numpy.sqrt(sigma))**2))
    image_filtered = numpy.real(ifft2(image_f))

    # PST kernel construction

    rws = RHO*warp_strength
    pst_kernel = rws * numpy.arctan(rws) - 0.5*numpy.log(1+(rws**2))
    pst_kernel /= pst_kernel.max()
    pst_kernel *= phase_strength

    # application of the kernel, and phase calculation

    image_processed = ifft2(fft2(image_filtered) * fftshift(numpy.exp(-1j * pst_kernel)))

    result = numpy.angle(image_processed)

    if morph_flag == False:
        return result
    else:
        binary = numpy.zeros_like(image, dtype=bool)

        binary[result > thresh_max] = 1
        binary[result < thresh_min] = 1    
        binary[image < image.max() / 20] = 0

        # the matlab version does post-processing (cleaning) here!

        return binary

And thanks to Dr. Asghari and Prof. Jalali for publishing this very interesting algorithm.

Regards, Christian

csachs avatar Feb 12 '16 11:02 csachs

Thank you Christian. Really useful, this Python code is already something easier for me to understand as a non-MATLAB user.

Was looking into the license terms (it is actually what we do at triplecheck) and the code requires licensing, plus it is protected by a US patent. Does this mean that even we'd have a Java version written that it would be under the scope of this patent? It is an interesting algorithm, just not sure if we'd be able of affording it for our (humble) context.

maxbrito avatar Feb 12 '16 11:02 maxbrito

We will release the Java version soon, but we recommend to play with the MATLAB code to understand the technique. The algorithm is patented, so for educational purposes you can use the algorithm but to use it in a commercial application, you need to obtain license from UCLA.

JalaliLabUCLA avatar Feb 12 '16 14:02 JalaliLabUCLA

Ok, thank you. A java edition would certainly be great. Please update this thread once it is ready so that we can get an automatic notification.

maxbrito avatar Feb 12 '16 14:02 maxbrito

I understand that is possible to port MATLAB code to java and c by using Matlab's Compiler SDK. Some adjustments would be required in the code to provide proper outputs.

http://www.mathworks.com/products/matlab-compiler-sdk/?requestedDomain=www.mathworks.com#

roj4s avatar Feb 13 '16 05:02 roj4s

Thank you for contributing. We will release the Java code soon, no need for using MATLAB's Compiler.

JalaliLabUCLA avatar Feb 15 '16 20:02 JalaliLabUCLA

Do you plan a C or C++ version ?

maddin200 avatar Feb 17 '16 11:02 maddin200

If there is a lot of interest, we will make the c++.

JalaliLabUCLA avatar Feb 17 '16 14:02 JalaliLabUCLA

You might want to figure out whether it needs to be C++, or just something a bit more accessible without matlab. I bet that a Python implementation would be easier to create and would likely suffice for most research applications. That being said, the more implementations the better!

Regards, Evan

On Wednesday, February 17, 2016, JalaliLabUCLA [email protected] wrote:

If there is a lot of interest, we will make the c++.

— Reply to this email directly or view it on GitHub https://github.com/JalaliLabUCLA/Image-feature-detection-using-Phase-Stretch-Transform/issues/1#issuecomment-185236502 .

evanjbowling avatar Feb 17 '16 16:02 evanjbowling

The idea here is to make a version using OpenCV.

maddin200 avatar Feb 17 '16 16:02 maddin200

I'm thinking about using this algorithm for image pre-processing to later apply an OCR like Tesseract. The final intention is to be included on an Android app. Actually i'm not too experienced in the topic so i wonder if i'm in the right direction. Any advice is appreciated.

roj4s avatar Feb 19 '16 15:02 roj4s

Using the algorithm in OCR applications is a good idea, please keep us updated on how it goes and let us know if we can help.

JalaliLabUCLA avatar Feb 20 '16 05:02 JalaliLabUCLA

I'm also very interested in a C++ (or C) port.

etale-cohomology avatar Jun 12 '16 18:06 etale-cohomology

I'm also interested in C++ version and I am willing to help. Python is nice too but for this application I think it would be quite slow. A Python wrapper on top of the C++ code would be a better solution.

amirmasoudabdol avatar Jun 13 '16 09:06 amirmasoudabdol

We wanted to inform everyone that the Python code has been released along with the updated Matlab code.

JalaliLabUCLA avatar Apr 09 '18 18:04 JalaliLabUCLA