Giter VIP home page Giter VIP logo

mathias-fuchs / quickconvolution Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 1.91 MB

Single file header library for image convolution, for instance for blurring. Pure C API.

Home Page: https://mathiasfuchs.de/mfem.html

License: Other

C 96.26% CMake 0.04% Makefile 0.03% C++ 3.65% Shell 0.03% Batchfile 0.01%
image-processing gaussian-stencil convolution convolution-filter c computational-geometry fourier-transform fast-fourier-transform single-file single-file-li

quickconvolution's Introduction

Blurring and thresholding for image processing

Quickconvolution

This is a single file C library called blur_plan.h for image blurring using convolution with a Gaussian kernel. It is easy to adapt to other kernels. The complexity is that of the Fast Fourier transform, namely O(n log n) where n is the number of pixels of the image. This is almost the same complexity as that of a single pass over the file. In constrast, the naive implementation of a Gaussian stencil convolution is O(n^2), which corresponds to n passes over the file.

Usage

#define BLUR_PLAN_IMPLEMENTATION
#include "blur_plan.h"

// prepare for blurring images of size 128 x 128
// with a stencil size of 5%, takes a little time to warm up
blur_in_place_plan* bipp = create_blur_plan_quadratic_2d(128, 0.05);

// get the raw pointer to the image data
float* bpd = blur_plan_data(bipp);

while (true) {
// this loop is extremely fast and can be a hot path
// .... fill the image
    bpd[0] = somepixelvalue;
    bpd[1] = anotherpixelvalue;
// ...

// do the blurring
    blur_plan_execute(bipp);

// do something with the blurred image
    float firstpixel = bpd[0];
// ...
}

// free the memory
free_blur_plan(bipp);

As usual with single file header libraries, include the header file blur_plan.h wherever you want to use the declaration of the API functions. Once and only once the symbol

#define BLUR_PLAN_IMPLEMENTATION

needs to be defined in a compilation unit that will provide the definition of the API functions. Make sure your compiler can include and link to fftw3f. On Debian-based Linux distros,

sudo apt install libfftw3-dev

and then link with -lfftw3f. On Windows and with Visual Studio, it is advisable to use CMake as explained below.

Example

This repository contains a simple example in the file blurimage.c. It produces images which concatenate to the gif above.

Building the example blurimage executable with gcc on linux

gcc blurimage.c -lfftw3f -lm -Ofast -o blurimage

Building the example with CMake on Windows

... requires the presence of fftw. Download from ftp://ftp.fftw.org/pub/fftw/fftw-3.3.5-dll64.zip and unpack to a directory of your choice. Open the file CMakeLists.txt with an editor and insert the directory where you placed it to all places that mention fftw. Open the repository folder in Visual Studio and switch to CMake Targets view in the solution explorer. Set the executable blurimage to the startup item and click the green arrow.

API reference

blur_in_place_plan *create_blur_plan_quadratic_2d(int sidelength, float bw);

Create a 2d blur_plan with quadratic sidelength sidelength and bandwidth bw, specificed in fraction of the image size, for instance bw==0.1 leads to a Gaussian stencil size of one tenth the image width._

float* blur_plan_data;

Get the raw data of the image before and after the convolution.

blur_in_place_plan* duplicate_blur_plan_2d(blur_in_place_plan* src);

Deep-copy the blur plan.

blur_in_place_plan *create_blur_plan_1d(int n, float bw);

Create a 1d blur_plan.

void blur_plan_update_bandwidth_1d(blur_in_place_plan* k, float bw);

Change the bandwidth of the 1d blur_plan.

void blur_plan_update_bandwidth_quadratic_2d(blur_in_place_plan* k, float bw);

Change the bandwidth of the 2d blur_plan.

void blur_plan_execute(blur_in_place_plan *bp);

Do it, perform the convolution. A pointer to the raw pixel data of the convolved image can be obtained by blur_plan_data.

void free_blur_plan(blur_in_place_plan *bp)

Clean up to avoid memory leaks.

More info and other projects

https://mathiasfuchs.de/mfem.html

License

A very restrictive GPLv3. Drop me a line for other options.

Emscripten build and inclusion in a website

I have an emscripten build, drop me a line for more info.

quickconvolution's People

Contributors

mathias-fuchs avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

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.