andreas-aeschlimann/gabor

How to replicate filter result from web

Opened this issue · 4 comments

Hi, nice work.

Your repository includes code both in MATLAB and C, I'm wondering which functions and how to call them to replicate the filter result on the cat.jpg image.

In C, is it using normalizedFilter2 or fgc2? When calling gaborConvolution2.js is f the convoluted image? If so, how can I read and split the cat image into y1 and yConvSum so that I can call fgc2?

For MATLAB, is it using the filter2 or normalizedFilter2 or fgc2 function? I guess my question is the same as above.

Thanks..!!

Hello, thanks for checking out the repository.

I implemented the same functionality in multiple languages (MATLAB, Python, JavaScript and C). I started with MATLAB and then tried Python, because I was curious to see a comparison of performance.

After that, I wanted to create the web application and the obvious implementation was in JavaScript. However, the calculations were too heavy for JavaScript back then. Images greater than 500x500 processed quite slow (around 30s). I read about WebAssembly and that you can natively execute C code from JavaScript in the browser.

Here you can see how I call the C code from JavaScript:

// Call c code
try {
const buffer1 = Module._malloc(f.length * f.BYTES_PER_ELEMENT);
const buffer2 = Module._malloc(fConv.length * fConv.BYTES_PER_ELEMENT);
Module.HEAPF32.set(f, buffer1 >> 2);
Module.HEAPF32.set(fConv, buffer2 >> 2);
Module.ccall(
"fgc2",
null,
["number", "number", "number", "number", "number", "number", "number", "number"],
[buffer1, buffer2, n, xi, sigma, lambda, theta, amount]
);
fConv = new Float32Array(Module.HEAPF32.buffer, buffer2, fConv.length);
Module._free(buffer1);
Module._free(buffer2);
} catch (e) {
console.error(e);
}

As you can see, I call fgc2 to calculate the Gabor transform.

In Matlab, fgc2 performs the Gabor transform. But before you can do it, you need to calculate the Gabor filter with normalizedFilter2. The 2 is for 2D because I also implemented it in 1D.

Thanks. In the above code, what do f and fconv represent? I understand f refers to the image.

If I were to call fgc2 directly from C, how should I initialize f and fconv?

Thanks. In the above code, what do f and fconv represent? I understand f refers to the image.

If I were to call fgc2 directly from C, how should I initialize f and fconv?

Please refer to the code/documentation in the repository to see how they are initialized. f should be a matrix (2d array) by the grayscale value of the image.

Thanks. In the above code, what do f and fconv represent? I understand f refers to the image.

If I were to call fgc2 directly from C, how should I initialize f and fconv?

fConv is the resulting data. It should be an empty matrix (2d array) of the same size as f. I had to pass it to the C function because I had no other way to get the return value from there. fConv is basically the return value of the function, but passed as parameter.