Page 1 of 2

possible long time bug with -fft in IM 6.5.x to 6.6.x

Posted: 2011-01-23T14:58:20-07:00
by fmw42
IM 6.6.7.2 Q16 Mac OSX Tiger

Magick,

I am seeing the same issue as shown in item 3) for IM 6.6.6.2 at viewtopic.php?f=3&t=16365&p=59537&hilit=bug+fft#p59532. The phase image appears to have overshoots and the magnitude image when log enhanced is too dark. However a round trip appears fine, so whatever is going on in with -fft seems to be reversed by the -ift.


input: square31.png
Image


convert square31.png -fft square31_fft_mp_v6672.tif
convert square31_fft_mp_v6672.tif square31_fft_mp_v6672_%d.tif
display square31_fft_mp_v6672_1.tif is the phase image

The current phase image _1 looks like this:
Image
and it should look like this:
Image
(ACTUALLY THIS IS AN OLDER VERSION FROM SEAN's DEMO BEFORE IT BECAME PART OF IM --- IT IS NOT QUITE COORECT EITHER --- BUT SEE THE POST BELOW)



convert square31_fft_mp_v6672_0.tif -evaluate log 10000 square31_fft_mp_v6672_maglog10000.tif

The current spectrum generated from the magnitude (log processed as the magnitude is too dark) looks like this:
Image
but it should look more like this:
Image


This bug may have been fixed in 6.6.2.3 per viewtopic.php?f=3&t=16365&p=59537&hilit=bug+fft#p59549 and viewtopic.php?f=3&t=16365&p=59537&hilit=bug+fft#p59570 though it was too long ago to remember.

I have installed and tested both 6.6.2.10 and 6.5.9.10, but both seem to still have this problem.

If you have a copy of 6.6.2.3 and can either email it to me or upload it where I can download, I will test that version.

Fred

Re: possible long time bug with -fft in IM 6.5.x to 6.6.x

Posted: 2011-01-23T17:19:31-07:00
by magick
Legacy releases of ImageMagick are available here: http://ftp.nluug.nl/ImageMagick/.

Re: possible long time bug with -fft in IM 6.5.x to 6.6.x

Posted: 2011-01-24T11:38:50-07:00
by fmw42
I am not able to find any release that has it correct, even 6.6.2.3 or 6.6.2.4. Though I am sure it was working correctly at one time.

I tried:
ImageMagick-6.5.4-7
ImageMagick-6.5.4-10
ImageMagick-6.5.9-10
ImageMagick-6.5.5-10
ImageMagick-6.6.2-3
ImageMagick-6.6.2-4
ImageMagick-6.6.2-10
ImageMagick-6.6.7-0
ImageMagick-6.6.7-1
ImageMagick-6.6.7-2

So if as in the report mentioned above it was fixed in IM 6.6.2.3 beta. It was lost from the beta to the released version.


Here is the problem as I see it -- the black and white (overshoots) in the phase image.
(The magnitude, real and imaginary components are fine.)


Input:
Image


NON_HDRI:create magnitude and phase

convert square31.png -fft square31_fft_mp_v6672_%d.tif

phase image _1
Image



HDRI:create magnitude and phase
convert square31.png -fft square31_ffthdri_mp_v6672.png

phase image -1
Image

create round trip

convert square31_ffthdri_mp_v6672-0.png square31_ffthdri_mp_v6672-1.png -ift square31_ffthdri_mp_v6672_ift.png
compare -metric rmse square31.png square31_ffthdri_mp_v6672_ift.png null:
47.5618 (0.000725746) (This is reasonable for png intermediate)

For more accuracy use PFM:
convert square31.png -fft square31_ffthdri_mp_v6672.pfm

phase image [1] shows the same results
http://www.fmwconcepts.com/misc_tests/F ... _v6672.pfm

convert square31_ffthdri_mp_v6672.pfm -ift square31_ffthdri_mp_v6672_ift_pfm.png
compare -metric rmse square31.png square31_ffthdri_mp_v6672_ift_pfm.png null:
0 (0)


However, if I create the phase image from the real and imaginary:

create real and imaginary...
convert square31.png +fft square31_ffthdri_ri_v6672.pfm

convert real and imaginary to phase with proper scaling to convert -pi and pi to 0 and 1
convert square31_ffthdri_ri_v6672.pfm -monitor -fx "(atan2(v,u)+pi)/(2*pi)" square31_ffthdri_ri2phase2.png

Image

And there is no sign of the black and white overshoots even with PNG output


My first thought was that you may have lines 287 and 288 reversed from my -fx formula above:


phase_source/=(2.0*MagickPI);
phase_source+=0.5;


Seems to me from my -fx expression above, you need to add 0.5 and then divide by 2*pi.

However, that does not seem to be the case as I get the same good result with

convert square31_ffthdri_ri_v6672.pfm -monitor -fx "atan2(v,u)/(2*pi)+0.5" square31_ffthdri_ri2phase3.png

Image


So I am not sure where the overshoots appear to be coming from. It may be a slight precision issue somewhere.

The strange thing is that even with the overshoots, the round trip produces reasonable results. So this is puzzling to me.

Re: possible long time bug with -fft in IM 6.5.x to 6.6.x

Posted: 2011-01-24T13:23:30-07:00
by magick
There may be more than one problem, but there appears to be a problem with saving grayscale images with PNG. We sent a note to Glenn to investigate.

Re: possible long time bug with -fft in IM 6.5.x to 6.6.x

Posted: 2011-01-24T13:33:11-07:00
by fmw42
It happens with any output format. I have tried tif as you can see above and also pfm, which is linked above, but not displayed. Furthermore, when converting from real/imaginary in HDRI to phase and saving as PNG it looks fine, as shown at the end.

It might just be a clamping issue (need) for the phase only image (w/ or w/out hdri) related to some small overshoot from precision somewhere?

Re: possible long time bug with -fft in IM 6.5.x to 6.6.x

Posted: 2011-02-10T19:12:16-07:00
by rgottlieb
I've tried to use the FFT notch filter scripts, but with no success: the masks all come out useless. I found this forum was discussing the bug.

Since FFT obviously worked at some point, I figured I'd try the earliest versions that had FFT support. However, I get errors during the builds.

I configured them all like this:
./configure --with-jp2=yes --enable-hdri=yes LDFLAGS="-L/usr/lib -L/usr/local/lib"

I have the following libraries installed:
fftw-3.2.2
jpeg-8b
lzma
tiff-3.9.4
zlib-1.2.5

configure and make go OK, but I get this during make install:
In ImageMagick-6.5.4-3 through -6.5.4-5:

/usr/bin/ld: cannot find -lperl

What am I missing to get that error?

By the way, I have built the latest available releases OK, but the FFT problem perrsists, up to ImageMagick-6.6.7-7

Cheers,
Ruven Gottlieb

Re: possible long time bug with -fft in IM 6.5.x to 6.6.x

Posted: 2011-02-10T19:36:15-07:00
by fmw42
Ruven,

What platform are you on? (My problems with FFT above went away on my Mac OSX Tiger G4 PowerPC when I installed everything in /usr/local/bin manually rather than using MacPorts and reverted my .profile back so that it did not reference MacPorts)

I tested the following (IM 6.6.7.6 Q16 non-hdri on my Mac OSX Tiger PowerPC) and got correct results that match what is on my examples page for notch on my web site below. Try with and without HDRI and let me know if it is only a problem with HDRI.

notch -c 10 -d 2 -K twig.jpg twig_notch.jpg

Thresholding Image At 11.3725%

Perhaps the image you are testing with does not conform well to work with what I have programmed for my notch filter?

Have you tried the latest version of IM, but avoid my notch script for now and just run the tests below to verify that FFTW is installed correctly with IM and that -fft works properly?

square31.png
Image

convert square31.png -fft square31_mp.png

then compare the phase image (-1) with the following:
Image



then compute the spectrum from the

convert square31_mp-0.png -auto-level -evaluate log 1000 square31_mp_spec1000.png

and compare to the following:
Image


Then verify the round trip produces a reasonable result

convert square31_mp-0.png square31_mp-1.png -ift square31_rt.png
compare -metric rmse square31.png square31_rt.png null:

the result should be about 47.5618 (0.000725746) or less.

Also verify that FFTW is installed correctly, by

convert -list configure

and looking at the line starting with DELEGATES and see if it lists fftw.

Fred

Re: possible long time bug with -fft in IM 6.5.x to 6.6.x

Posted: 2011-02-11T16:04:30-07:00
by rgottlieb
I'm happy to report that all is well and everything works. I didn't realize you had a real 'notch' script available, and I was using the small scripts from here:
http://www.imagemagick.org/Usage/fourier/#noise_removal
...and doing all the masking by hand. I didn't get anywhere even when I used the images on that page.

Now, using the 'notch' script, I get great results. I am so thrilled to be able to remove noise from images like this. I notched the clown and twigs images successfully with 'notch'.

For the record, I'm running on Debian stable (Squeeze I think), all the tests you mentioned above came out just right, and I have HDRI enabled. So, all's well that ends well.

(Now, my question is: In order to deconvolve an image, how do you calculate the optimum diameter of the kernel image.)

Thanks so much for all your help, and for the work you've put into giving us all this great tool.

Cheers,
Ruven

Re: possible long time bug with -fft in IM 6.5.x to 6.6.x

Posted: 2011-02-11T16:32:15-07:00
by fmw42
(Now, my question is: In order to deconvolve an image, how do you calculate the optimum diameter of the kernel image.)
If the images are blurred by ideal camera defocus (jinc function in frequency domain) or motion blur (sinc function in frequency domain), then my ceptsrum script may allow you to measure that information. However, for other blurs that do not conform in that way, such as Gaussian blur, then there is no really good way to determine that that I know about. You then have to look into Blind Deconvolution techniques which I have not scripted. There may be some Photoshop filters that also allow you to deconvolve such images.

See my docs at http://www.fmwconcepts.com/imagemagick/ ... urier.html

I am really puzzled though that you could not manually create a notch filter and get good results as in my examples in the link above. For notch filtering you don't have to be in HDRI mode, but you do need most likely at least Q16.

Re: possible long time bug with -fft in IM 6.5.x to 6.6.x

Posted: 2011-02-12T14:11:16-07:00
by rgottlieb
I am really puzzled though that you could not manually create a notch filter and get good results as in my examples in the link above. For notch filtering you don't have to be in HDRI mode, but you do need most likely at least Q16.
I was using the example code from the ImageMagick site. See:

http://www.imagemagick.org/Usage/fourier/#noise_removal
The code from the ImageMagick page didn't handle the edited spectrum image with edits in red properly.

Once I used the code from the link you provided, everything worked. I wasn't sure exactly what was supposed to happen until I saw your new code.

I'm now using ImageMagick 6.6.7-6 2011-02-11 Q16 ...
Features: OpenMP HDRI

I'm trying to descreen an image which was a scanned halftone image, so it has moire aliasing all over it in a sort of double-waffle pattern.
Lots of that noise seems to be in the low-frequency part of the image. So far I'm having poor results with the notch filter. When I get the noise out, I've destroyed lots of the low-freq image information as well. I think I need to generate a similar waffle pattern to null out this odd screen effect. Is there a way to do that?

Thanks again,
Ruven

Re: possible long time bug with -fft in IM 6.5.x to 6.6.x

Posted: 2011-02-12T14:32:05-07:00
by fmw42
I wrote the initial page for FFT, and then Anthony made some contributions. But then we deviated and he made his own version with his particular style of examples, which is on the IM web site. I have not tested his examples, so you might point those out to him.

With regard to manually notching your image, I would have to see it and perhaps try it for myself if it looks possible.

With regard to making a screen effect, you could try my screeneffect script at the link below. But I don't know much about dither patterns. But there is a whole page on that at http://www.imagemagick.org/Usage/quantize/ Complex dither patterns can cause lots of "dot" arrays in the frequency domain and it is often hard to tell which dots are from the dither and which are signal. Most should be at the mid and high frequency parts of the spectrum and not much at the low frequencies, however. And there should be some symmetry.

You could look at the dither patterns and how they were removed on my notch filter page and do something similar but manually.

By the way, out of curiosity, what platform and OS are you running? (Linux or Mac)?

Re: possible long time bug with -fft in IM 6.5.x to 6.6.x

Posted: 2011-02-12T15:40:55-07:00
by rgottlieb
By the way, out of curiosity, what platform and OS are you running? (Linux or Mac)?
I'm running on Debian stable (Squeeze I think)

Currently using ImageMagick 6.6.7-6 2011-02-11 Q16, with OpenMP HDRI, but since everything's now working, I'll upgrade to the latest version and see what happens. ;-)

I'll give your suggestions a try. Thanks once again for your thorough and thoughtful reply.

Cheers,
Ruven

Re: possible long time bug with -fft in IM 6.5.x to 6.6.x

Posted: 2011-02-12T17:16:02-07:00
by fmw42
Ruven,

I tested Anthony's notch filtering of the clown from his page at http://www.imagemagick.org/Usage/fourier/#noise_removal.

I copied his commands and edited them to remove the indents, so they looked as follows:

convert clown.jpg -fft \
\( +clone -write clown_phase.png +delete \) +delete \
-write clown_magnitude.png -colorspace gray \
-auto-level -evaluate log 100000 clown_spectrum.png

convert clown_spectrum_edited.png clown_spectrum.png \
-compose difference -composite \
-threshold 0 -negate clown_spectrum_mask.png

convert clown_magnitude.png clown_spectrum_mask.png \
-compose multiply -composite \
clown_phase.png -ift clown_filtered.png


Then I downloaded his clown_spectrum_edited.png and ran all 3 commands and got the very same results that he presented.

Therefore I am puzzled why you could not reproduce it, unless it was due to the indents that one gets from copying his commands and running those with the indents or possibly line ending issues

Fred

Re: possible long time bug with -fft in IM 6.5.x to 6.6.x

Posted: 2011-02-12T19:39:09-07:00
by rgottlieb
Beats me. I still can't do it that way, and I just copied your code into a script: I even removed the backslashes and joined the lines for each command, and still no luck:

**********************************************************************************
the lines wrap below, but in the script all lines not separated by a blank line are contiguous.
**********************************************************************************

convert clown.jpg -fft \( +clone -write clown_phase.png +delete \) +delete -write clown_magnitude.png -colorspace gray -auto-level -evaluate log 100000 clown_spectrum.png

convert clown_spectrum_edited.png clown_spectrum.png -compose difference -composite -threshold 0 -negate clown_spectrum_mask.png

convert clown_magnitude.png clown_spectrum_mask.png -compose multiply -composite clown_phase.png -ift clown_filtered.png

The magnitude, phase, and spectrum images seem OK, but the mask is always almost black. I have no idea why this is so. If I adjusted the threshold, I got less blackness, but the red bits were just added to the spectrum image and the whole thing gets thresholded.

However the spectrum and notch scripts work just fine. They do thresholding slightly differently than the script above, but you can still get results, while I can't. If I'm doing something wrong, at least I'm doing it consistently. :-)

Cheers,
Ruven

Re: possible long time bug with -fft in IM 6.5.x to 6.6.x

Posted: 2011-02-12T19:55:22-07:00
by rgottlieb
Is there some test suite I can run to see if my setup has some flaw in it? I checked the file for any line-ending problems and don't see any.