FloatProcessor.convolve()

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

FloatProcessor.convolve()

Kenneth Sloan-2
I'm seeing strange results from:

===================================
FloatProcessor fp;
float[] kernel = {...}
...
fp.convolve(kernel,height,width);
===================================

It produces expected results for (for example):

float[] kernel = {1.0f, 0.0f, -1.0f};

but appears to do nothing at all for:

float[] kernel = {1.0f, -5.0f, 1.0f};

note: i've been thrashing on this, so that might not be *quite* right - but there are kernels which
do absolutely nothing.  Perhaps {0.0f, -5.0f, 0.0f} is a better example.

My first thought is that there is some sort of kernel normalization being done.  Is that correct?  and if so, how do I control it (I'd like to turn it off, completely)

I notice that in the "commands" part of the manual, there is a dialog box which includes a checkbox for "normalize kernel".  But, I don't immediately see where to do this (or, in my case *not* do it) when writing Java code.

I have temporized by using {1.0f, -2.0f, 1.0f} which appears to work as expected.

So...I'm moving ahead - but I'm curious about what I am missing here.  

--
Kenneth Sloan
[hidden email]
Vision is the art of seeing what is invisible to others.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: FloatProcessor.convolve()

John Hayes
Hi Kenneth,

FloatProcessor appears to use the ij.plugin.filter.Convolver class. Indeed, it appears the default attribute for “normalize” is true. Perhaps you should use this class directly in Java on the FloatProcessor? It has a setNormalize member function. See https://imagej.nih.gov/ij/developer/source/ij/plugin/filter/Convolver.java.html and https://imagej.nih.gov/ij/developer/api/ij/plugin/filter/Convolver.html

Best,

John

> On Nov 20, 2020, at 6:15 PM, Kenneth Sloan <[hidden email]> wrote:
>
> I'm seeing strange results from:
>
> ===================================
> FloatProcessor fp;
> float[] kernel = {...}
> ...
> fp.convolve(kernel,height,width);
> ===================================
>
> It produces expected results for (for example):
>
> float[] kernel = {1.0f, 0.0f, -1.0f};
>
> but appears to do nothing at all for:
>
> float[] kernel = {1.0f, -5.0f, 1.0f};
>
> note: i've been thrashing on this, so that might not be *quite* right - but there are kernels which
> do absolutely nothing.  Perhaps {0.0f, -5.0f, 0.0f} is a better example.
>
> My first thought is that there is some sort of kernel normalization being done.  Is that correct?  and if so, how do I control it (I'd like to turn it off, completely)
>
> I notice that in the "commands" part of the manual, there is a dialog box which includes a checkbox for "normalize kernel".  But, I don't immediately see where to do this (or, in my case *not* do it) when writing Java code.
>
> I have temporized by using {1.0f, -2.0f, 1.0f} which appears to work as expected.
>
> So...I'm moving ahead - but I'm curious about what I am missing here.  
>
> --
> Kenneth Sloan
> [hidden email]
> Vision is the art of seeing what is invisible to others.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: FloatProcessor.convolve()

John Hayes
Another quick point, the docs say "If 'normalize' is true (the default), the convolve()...methods divide each kernel coefficient by the sum of the coefficients, preserving image brightness.”

So, that seems to explain why your examples variously work as you do or do not expect. You might be able to use an alternative kernel that sums to zero (like your two good ones below) just as you are currently doing in code.

Best,
John

> On Nov 20, 2020, at 6:27 PM, John A. Hayes <[hidden email]> wrote:
>
> Hi Kenneth,
>
> FloatProcessor appears to use the ij.plugin.filter.Convolver class. Indeed, it appears the default attribute for “normalize” is true. Perhaps you should use this class directly in Java on the FloatProcessor? It has a setNormalize member function. See https://imagej.nih.gov/ij/developer/source/ij/plugin/filter/Convolver.java.html and https://imagej.nih.gov/ij/developer/api/ij/plugin/filter/Convolver.html
>
> Best,
>
> John
>
>> On Nov 20, 2020, at 6:15 PM, Kenneth Sloan <[hidden email]> wrote:
>>
>> I'm seeing strange results from:
>>
>> ===================================
>> FloatProcessor fp;
>> float[] kernel = {...}
>> ...
>> fp.convolve(kernel,height,width);
>> ===================================
>>
>> It produces expected results for (for example):
>>
>> float[] kernel = {1.0f, 0.0f, -1.0f};
>>
>> but appears to do nothing at all for:
>>
>> float[] kernel = {1.0f, -5.0f, 1.0f};
>>
>> note: i've been thrashing on this, so that might not be *quite* right - but there are kernels which
>> do absolutely nothing.  Perhaps {0.0f, -5.0f, 0.0f} is a better example.
>>
>> My first thought is that there is some sort of kernel normalization being done.  Is that correct?  and if so, how do I control it (I'd like to turn it off, completely)
>>
>> I notice that in the "commands" part of the manual, there is a dialog box which includes a checkbox for "normalize kernel".  But, I don't immediately see where to do this (or, in my case *not* do it) when writing Java code.
>>
>> I have temporized by using {1.0f, -2.0f, 1.0f} which appears to work as expected.
>>
>> So...I'm moving ahead - but I'm curious about what I am missing here.  
>>
>> --
>> Kenneth Sloan
>> [hidden email]
>> Vision is the art of seeing what is invisible to others.
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: FloatProcessor.convolve()

Kenneth Sloan-2
In reply to this post by John Hayes
Thank you for the prompt and useful replies.

It took me a long time to figure this out!

Wayne- perhaps it would be useful to have a FloatProcessor.convolve() with a "normalize" parameter?
Or, at least a warning in the documentation that the kernel is normalized by default.
Perhaps the warning is in there, somewhere - I failed to find it.
Ah...as I was typing this, John provided a snippet of the documentation..
A nitpicker might point out that the normalization doesn't happen in the way described when the sum of the kernel elements is zero!  But, I'm not a nitpicker.

If I had my druthers, I suppose I would prefer that "convolve" simply did a convolution.  For those who want normalized kernels, I suggest a "normalizeKernel()" function - something like:

=============================================
float[] normalizeKernel(float[] kernel);
=============================================

But, it may be that it is easier (now) to implement:

=============================================
convolve(float[] kernel, int kernelWidth, int kernelHeight, boolean normalize);
=============================================



--
Kenneth Sloan
[hidden email]
Vision is the art of seeing what is invisible to others.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html