Skip to main content

random - Speed up adding Poisson noise to an image


I'm simulating some images corrupted with Poisson noise, but I'm encountering a few problems with performance. According to the documentation on ImageEffect, one can add Poisson noise according to the following rule:



enter image description here


Loading up an example image, and running the code:


man256 = ImageAdjust[ImageResize[ExampleData[{"TestImage", "Man"}], {256, 256}]];
ImageAdjust[ImageEffect[man256, {"PoissonNoise", 0.8}]] // AbsoluteTiming

(* 10.839 seconds *)

Subsequently running it again in the same kernel, it only takes 0.196 seconds. Meanwhile, the Gaussian noise effect:


ImageAdjust[ImageEffect[man256, {"GaussianNoise", 4.}]] // AbsoluteTiming


(* 0.014 seconds *)

And then running it again once more, it only takes 0.006 seconds.


I tried defining my own functions for adding Poisson and Gaussian noise for comparison. Note that I define the amount of Poisson noise slightly differently to the Mathematica method (in a way that actually makes more sense to me).


addPoissonNoise[x_, peak_] :=
Block[{imagedata},
imagedata = peak*ImageData[x];
Map[If[# == 0., 0., RandomVariate[PoissonDistribution[#]]] &, imagedata, {2}]
];


addGaussianNoise[x_, sig_] :=
Block[{imagedata},
imagedata = ImageData[x];
Map[# + RandomVariate[NormalDistribution[0., sig]] &, imagedata, {2}]
];

ImageAdjust[Image[addPoissonNoise[man256, 40.]]] // AbsoluteTiming
(* 6.495 seconds *)

ImageAdjust[Image[addGaussianNoise[man256, 4.]]] // AbsoluteTiming

(* 0.007 seconds *)

So on an initial run, my code is faster than the built-in code in both cases. But on subsequent runs, it's slower than the built-in code. Considerably slower in the case of Poisson noise.


The Gaussian example is only for comparison - it's the Poisson noise I'm more interested in, and speeding up the initial run of the code, as ~10 seconds is considerably slower than I'd like, and in reality my images are bigger than 256x256 pixels.


Is there any way to speed-up the corruption of an image with Poisson noise?


Update #1


Replacing Map with ParallelMap speeds up my Poisson noise function, but it's still quite slow (taking about 1.89 seconds on my machine).


Avoiding multiple calls to RandomVariate is hard to avoid, I think. It's a shame PoissonDistribution can't be compiled (Which Distributions can be Compiled using RandomVariate).


Update #2


There's this question: Why is Poisson Random Deviate Generation so slow?



(For info, this is with v10 on Win8.1)



Answer



Simon Woods points out in a comment that:



In fact the delay on the initial run is caused by compiling code to provide the Poisson distribution :-) You can look at Image`ColorOperationsDump`iImageEffectPoissonNoise to see how it works internally.



Now, although PoissonDistribution can't be compiled, there's nothing stopping the use of my own C++ function and calling it with LibraryLink.


So that's what I tried! Working with the code by Arnoud Buzing from Minimal effort method for integrating C++ functions into Mathematica, I wrote some C++ code (given at the bottom of this answer) that utilises the Poisson distribution built into C++.


My Mathematica code is then:


Needs["CCompilerDriver`"]

poissonNoiseLib = CreateLibrary[
{ToString[NotebookDirectory[]] <> "/poissonnoise.cpp"},
"poissonNoiseLib",
"Debug" -> False
];
poissonNoise = LibraryFunctionLoad[
poissonNoiseLib,
"poissonNoise",
{{Real, 2}},
{Real, 2}

];
(* Takes 5.18 seconds to do all this *)

So let's reimport the image, but make it much bigger this time.


man2048 = ImageAdjust[ImageResize[ExampleData[{"TestImage", "Man"}], {2048, 2048}]];
img1 = ImageAdjust[ImageEffect[man2048, {"PoissonNoise", 0.8}]]; // AbsoluteTiming
(* Takes 21.64 seconds first time round *)
(* Subsequently takes 11.45 seconds even after the function has been compiled *)

poissonpeak = 50.;

man2048data = poissonpeak * ImageData[man2048];
img2 = ImageAdjust[Image[poissonNoise[man2048data]]]; // AbsoluteTiming
(* Takes 0.98 seconds *)

enter image description here


If you include the time it takes to compile my function using LibraryLink, I have a first-time run of 6.16 seconds, and subsequent runs of 0.98 seconds, for an image 2048x2048 pixels. This is compared to 11.45 seconds for the built-in function. I could even look into parallelising the C++ code below, for a further speed-up.


A 10x speed-up isn't bad, plus I learned how to use LibraryLink along the way :-)




C++ code


#include "WolframLibrary.h"

#include "stdio.h"
#include "stdlib.h"
#include
#include
EXTERN_C DLLEXPORT mint WolframLibrary_getVersion(){return WolframLibraryVersion;}
EXTERN_C DLLEXPORT int WolframLibrary_initialize( WolframLibraryData libData) {return 0;}
EXTERN_C DLLEXPORT void WolframLibrary_uninitialize( WolframLibraryData libData) {}

EXTERN_C DLLEXPORT int poissonNoise(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res){


int err; // error code

MTensor m1; // input tensor
MTensor m2; // output tensor

mint const* dims; // dimensions of the tensor

double* data1; // actual data of the input tensor
double* data2; // data for the output tensor


mint i; // bean counters
mint j;

m1 = MArgument_getMTensor(Args[0]);
dims = libData->MTensor_getDimensions(m1);
err = libData->MTensor_new(MType_Real, 2, dims,&m2);
data1 = libData->MTensor_getRealData(m1);
data2 = libData->MTensor_getRealData(m2);

unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();

std::default_random_engine generator (seed); // RNG

for(i=0;i for(j=0;j if(data1[i*dims[1]+j]==0.)
{
data2[i*dims[1]+j] = 0.; // If pixel value is 0, return 0
}
else
{

std::poisson_distribution distribution(data1[i*dims[1]+j]); // Poisson distribution
data2[i*dims[1]+j] = distribution(generator); // If pixel value is >0, sample from the distribution
}
}
}

MArgument_setMTensor(Res, m2);
return LIBRARY_NO_ERROR;
}

Comments

Popular posts from this blog

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...

equation solving - Invert and fit implicitly defined curve

I need to fit an implicitly defined curve. I thought I could get some data out of Solve , and then using FindFit . Therefore, I would like to find the relation the parametric curve defined by $F(x,y)=0$: Solve[-(1/2) + 1/2 (0.41202 BesselK[0, 0.1 Sqrt[x^2 + y^2]] + (0.101483 x BesselK[1, 0.1 Sqrt[x^2 + y^2]])/Sqrt[x^2 + y^2]) == 0, y] But I can't get an output: Solve was unable to solve the system with inexact coefficients or the system obtained by direct rationalization of inexact numbers present in the system. Since many of the methods used by Solve require exact input, providing Solve with an exact version of the system may help. >> Edit: In particular, I would like to fit the data coming from the curve with the expression of another curve, and not with a function $f(x)$. In particular, since this clearly looks like a cardioid , I would like it to fit to something like it. What other strategies could I try?

dynamic - How can I make a clickable ArrayPlot that returns input?

I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use? Answer ArrayPlot is much more than just a simple array like Grid : it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange . These features make it quite complicated to reproduce the same layout and order with Grid . Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled , HighlightCoordinates , HighlightStyle and HighlightElementFunction . data = {{Missing["HasSomeMoreData"], GrayLevel[ 1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]}, RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel...