Code Monkey home page Code Monkey logo

mf2d's Introduction

2D Median Filter for Floating-Point Data

Alpha version, bugs are possible, use at your own risk.

News

Usage

As a command-line tool

You can use "mf2d" to process 32-bit and 64-bit FITS images, both 1-dimensional and 2-dimensional.

Usage:

bin/mf2d radius input output-median output-difference

Example:

bin/mf2d 15 example/test-1.fits a.fits b.fits

"Radius" is the window radius in pixels. For example, a radius of 15 in a 2D image means a window of 31x31 pixels in total.

"Input" is a FITS file, 1 or 2 dimensions, 32-bit or 64-bit floating point values.

"Output-median" will be a FITS file that contains the result of median filtering.

"Output-difference" will be another FITS file that contains the difference between "input" and "output-median".

All file names are given in a format supported by the cfitsio library. In particular, prefix the output file names with an exclamation mark if you want to overwrite existing files:

bin/mf2d 15 example/test-1.fits '!a.fits' '!b.fits'

You can also filter just a small part of a large image:

bin/mf2d 15 'example/test-1.fits[1:100,1:200]' a.fits b.fits

As a subroutine

You can use this software as a subroutine to filter 1-dimensional and 2-dimensional arrays of floating point values.

You will only need filter.cc and filter.h; you do not need any additional libraries.

See filter.h for the interface, and mf2d-unittest.cc for simple examples.

Performance

Some examples of typical running times on my Macbook Air laptop (1.7 GHz Intel Haswell, 2 cores, 4 threads):

  • less than 0.2s in total:

    bin/mf2d 10 example/test-1.fits '!a.fits' '!b.fits'
    
  • less than 0.4s in total:

    bin/mf2d 40 example/test-1.fits '!a.fits' '!b.fits'
    
  • less than 0.6s in total:

    bin/mf2d 80 example/test-1.fits '!a.fits' '!b.fits'
    
  • less than 0.8s in total:

    bin/mf2d 120 example/test-1.fits '!a.fits' '!b.fits'
    

The sample file is 1024x1024 pixels, 32-bit floats.

Details

Missing values

Missing values (NaNs) are treated as missing values. For example, the median of [x, y, NaN, z] is the same as the median of [x, y, z]. In the output, there is a NaN if and only if the entire window is empty (only NaNs).

Boundaries

Boundaries are handled by clipping the sliding window to image boundaries. For example, while middle parts of the output will be medians of (2r+1) x (2r+1) boxes, the corners of the output will be medians of (r+1) x (r+1) boxes.

In the middle parts, the window always contains an odd number of pixels, and hence the median is unique. Near the boundaries we may have an even number of pixels in the window; in those cases we will output the average of the two middle values.

Limits

The total image size has to be less than 2^31 pixels.

Compiling

Install cfitsio first (see below for details). Then compile mf2d as follows, depending on your platform.

Linux and GCC

Compile:

compile/gcc-linux.sh

Test:

test/test.sh

Linux and ICC

Compile:

compile/gcc-linux.sh

Test:

test/test.sh

OS X and GCC

You can get GCC 4.9 from Homebrew:

brew install gcc

Set CXX to point to the right compiler:

export CXX=g++-4.9

Compile:

compile/gcc-osx.sh

Test:

test/test.sh

OS X and Clang

(Not recommended: slow, will not use OpenMP.)

Compile:

compile/clang-osx.sh

Test:

test/test.sh

Meson and Ninja

See util/build-setup and util/build-all.

Installing cfitsio

To compile the command-line tool, you will need cfitsio. On OS X, you can use Homebrew:

brew install cfitsio

On Ubuntu Linux, you can try:

apt-get install libcfitsio3-dev

Alternatively, you can download the source from http://heasarc.gsfc.nasa.gov/fitsio/fitsio.html and compile and install it e.g. as follows:

./configure --prefix=$HOME/opt
make
make install

If you use a nonstandard location, set the paths accordingly so that the compiler and linker can find it:

export CPATH=$HOME/opt/include
export LIBRARY_PATH=$HOME/opt/lib

Platforms and versions

Tested on the following platforms:

  • OS X 10.10
  • Ubuntu 12.04
  • Ubuntu 14.04

With e.g. the following compilers:

  • GCC 4.4, 4.6, 4.7, 4.8, 4.9
  • ICC 13.0, 14.0, 15.0
  • Apple LLVM version 6.0

Using the following libraries:

  • cfitsio 3.370

License

Copyright (c) 2014, Jukka Suomela.

You can distribute and use this software under the MIT license: http://opensource.org/licenses/MIT

To contact the author, see https://jukkasuomela.fi/

Acknowledgements

Test data contributed by Jean-Eric Campagne.

mf2d's People

Contributors

suomela avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

mf2d's Issues

iOS version

Is the current code build for the iOS platform ?

Different result than ImageJ

Dear all,

i am writing this more of a warning to fellow googlers who find themselfs here; there is really not too many float32 / uint16 libraries that could do median sorting with large window (like 9x9 or 21x21 - as required on certain standard).

This library seemed very fine, and during course tests it seemd to provide correct results. When studying with detail due some other issue, i did notice bit weird output. As attachement is the processed image from the examples compared to ImageJ "median filter" output of radius=2.0.

The m2fd output has to my untrained eye kind of "liney artefacts" that would seem wrong to my intuition. I have not done pen&paper verification to this.

image
On left is the m2fd output, on right is the ImageJ median filtered and on the right is the un-altered data. The gray-levels have been adjusted to same on each image.

When doing image subtraction (m2fd output image - imagej median filter r=2 image) and compare the results are following:
image
Left: The m2fd output and its histogram (to give some idea of the leves) and on right the difference image (and its histogram for the levels).

The command to obtain the result image was

./bin/mf2d 2 ./example/test-1.fits /tmp/out.fits /tmp/out-diff.fits

The platform is linux x86_64 (kubuntu 20.04)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.