Monochromatic Noise

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
djkprojects
Posts: 21
Joined: 2012-07-06T04:44:48-07:00
Authentication code: 13

Monochromatic Noise

Post by djkprojects »

Hello,

I'm trying to simulate Photoshop monochromatic noise but can't figure out how to do that from a command line. The PS seetings are:

- gaussian
- 5% amount
- monochromatic

The code I have so far is:

Code: Select all

+noise Gaussian -evaluate add 5%
Also, how can I simulate Motion Blur with 45 angle and distance 10 pixels. I'm playing with different radiusxsigma+angle values but not sure if they're correct:

Code: Select all

-motion-blur 4x10-45
Thanks for advice
djkprojects
Posts: 21
Joined: 2012-07-06T04:44:48-07:00
Authentication code: 13

Re: Monochromatic Noise

Post by djkprojects »

OK,

I've figured out how to create a gaussian color noise however there is no option to generate monochromatic one. The only way to do it that I can think of is to create a layer with a color noise on a black background and then merge it with the original image but to get monochromatic noise effect the pixels from the layer need to be added or subtracted (this needs to be random) to/from the corresponding pixels of the original image and the question is now is it at all possible or can you think of any other way to generate monochromatic noise ?

Thanks
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Monochromatic Noise

Post by Bonzo »

A lot of people here do not have photoshop and so have no idea what effect monochromatic noise is so it would be a good idea to post an example.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Monochromatic Noise

Post by fmw42 »

You can create grayscale noise by taking just one output channel of the noise image, then mixing it will your image. If you need b/w noise, then you have to convert to grayscale or take one channel of the noise and then threshold.

In my old PS CS, under add noise, I see a choice of gaussian or uniform which adds color noise. With the monochromatic option, it adds just gray noise. So in IM create your noise image, select one channel or convert to grayscale, then mix that with your (color) image.

This seems to be close, but I used random noise rather than gaussian. IM gaussian noise does not seem to match PS, but that is not surprising as the gaussian arguments may be different. Note I had to use 15% to match PS 5% (see at the bottom for gaussian noise).


Image

convert -size 128x128 xc:"gray(50%)" -seed 1000 +noise random -channel green -separate noise_graylinear_random.png

Image

convert zelda3.png noise_graylinear_random.png -compose dissolve -define compose:args="15" -composite zelda_graylinear_random_dissolve_15pct.png

Image

Note that in recent versions of IM, you can create linear or non-linear gray. Linear by using gray(50%) and non-linear by using gray or gray50. The results are different.

This can be one command:

convert zelda3.png \( -size 128x128 xc:"gray(50%)" -seed 1000 +noise random -channel green -separate +channel \) \
-compose dissolve -define compose:args="15" -composite zelda_graylinear_random_dissolve_15pctb.png

Image

see -seed to control the randomization of the noise, so that if set, you can reproduce the results.
http://www.imagemagick.org/script/comma ... s.php#seed

see (-attenuate)
http://www.imagemagick.org/script/comma ... ddr4#noise


However, the way it is done above is not really the proper way to add noise in a non-HDRI system. What you really want is to do is a 50% biased combination. This can be done using -fx or faster with -compose Mathematics


convert zelda3.png \( -size 128x128 xc:"gray(50%)" -seed 1000 +noise random -channel green -separate +channel \) \
+swap -compose Mathematics -define compose:args="0,1,0.15,-0.075" -composite zelda_graylinear_random_dissolve_15pctc.png

Note the -0.075=0.5*0.15, that is it is a 50% bias of the 15% addition of the noise.

Image


see
http://www.imagemagick.org/Usage/compose/#mathematics
http://www.imagemagick.org/Usage/transf ... h_addition
http://www.imagemagick.org/script/compose.php (near the bottom of the section)


Here is an attempt with gaussian noise, though I had to stretch and clip it some.

convert zelda3.png \( -size 128x128 xc:"gray(50%)" -seed 1000 +noise gaussian \
-contrast-stretch 5% -channel green -separate +channel \) \
-compose dissolve -define compose:args="15" -composite zelda_graylinear_gauss_cs5_dissolve_15pct.png


Image
Last edited by fmw42 on 2012-07-08T10:35:34-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Monochromatic Noise

Post by fmw42 »

See above for answers about noise.

-motion-blur 4x10-45
IM -motion-blur is really an asymmetric gaussian blur effect and not a true motion blur as one might get from a moving camera.

The former is best applied using

-motion-blur 0xsigma-45

IM blurs use radiusxsigma. If you set radius=0, then it will be determined automatically and is typically 3xsigma so that the gaussian rolls off sufficiently towards zero. Alternately you can get a more uniform rather than gaussian motion blur effect by using radiusx65000

Other motion blur like effects can be done using -morphology. see http://www.imagemagick.org/Usage/convolve/#blur and http://www.imagemagick.org/Usage/convolve/#comet

If you want a true camera motion blur, then the best way is to do that in the frequency domain via FFT. See http://www.fmwconcepts.com/imagemagick/ ... otion_blur and my script, camerablur.

If you want an approximation, you can do that in the spatial domain, by creating a simple white straight line of the appropriate length (and angle) on a black background and using -convolve. (Or half straight line from center to corner)
stupid
Posts: 51
Joined: 2010-02-12T07:30:34-07:00
Authentication code: 8675308

Re: Monochromatic Noise

Post by stupid »

?

+noise Random -modulate 100,0

(If you're generating noise for a simulatation of film grain it shoulld be +noise Poisson instaead of +noise Random.)


s.
djkprojects
Posts: 21
Joined: 2012-07-06T04:44:48-07:00
Authentication code: 13

Re: Monochromatic Noise

Post by djkprojects »

Hello all,

Thanks for the replies. I'm going to have a look at your suggestions and see what results I'm getting.

As for the monochromatic noise I've created a function in JS that simulate this action pretty well for the uniform type and the logic is very simple:

It's called for every pixel:

Code: Select all

function noise(r, g, b, lvl, mono) {
	
	if (lvl) {
				
		if (mono) {

			var s1 = s2 = s3 = Math.round(Math.random())==1?1:-1;
		}
		else {

			s1 = Math.round(Math.random())==1?1:-1;
			s2 = Math.round(Math.random())==1?1:-1;
			s3 = Math.round(Math.random())==1?1:-1;
		}

		r += Math.random() * 255 * lvl / 100 * s1;
		g += Math.random() * 255 * lvl / 100 * s2;
		b += Math.random() * 255 * lvl / 100 * s3;
	}
				
	return [r, g, b];			
}
where

Code: Select all

r,g,b - channel values
lvl - intensity of the noise from 0 to any positive value
mono - monochromatic on/off (if off then generate color noise)
s1, s2, s3 - +/- operators
This of course is in 8bit quantum range but would work with 16bit as well I guess

The

Code: Select all

Math.random() * 255 * lvl / 100
is just a random value from 0 to 255 controlled by lvl so if it's equal 10 then the random value would be in 0 - 26 (25.5 rounded up) and for 100 it would be 0 to 255. If the the final value exceeds quantum range then of course it's rounded up to the min=0 (if r < 0) or max=255 (if r > 255)

As you can see from the above the only difference between mono and color is the +/- operator where for mono it's the same for each channel and random for color noise.

I'm not sure what algorithm PS uses but the results with this function are I would say identical for each level of intensity

BTW - how do I set email notifications on this forum please ? I can't see any option to do it :lol:

Thanks guys
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Monochromatic Noise

Post by fmw42 »

stupid wrote:?

+noise Random -modulate 100,0

(If you're generating noise for a simulatation of film grain it shoulld be +noise Poisson instaead of +noise Random.)


s.
This is not the same as choosing one channel. In IM, each channel gets a different seed so that it is colored noise. So by either using -modulate 100,0 or -colorspace gray, you are combining the channels into one by different combinations of the channels. That is not the same as choosing one channel.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Monochromatic Noise

Post by fmw42 »

As for the monochromatic noise I've created a function in JS that simulate this action pretty well for the uniform type and the logic is very simple:
Applying some of the noise models as above (such as multiplicative and possibly poisson) may not conform to additive combinations as in my earlier post. Thus you may be better off applying them directly with

convert image -seed XX -attenuate YY +noise result.

But there are problems. I don't know how to make it generate grayscale noise rather than color noise

For example:

convert zelda3.png -seed 1000 -attenuate 0.5 +noise gaussian zelda_gaussian_0p5.png

Image


convert zelda3.png -seed 1000 -attenuate 1 +noise gaussian zelda_gaussian_1.png

Image


convert zelda3.png -seed 1000 -attenuate 2 +noise gaussian zelda_gaussian_2.png

Image
djkprojects
Posts: 21
Joined: 2012-07-06T04:44:48-07:00
Authentication code: 13

Re: Monochromatic Noise

Post by djkprojects »

Hello fmw42,

As for the color noise:

Code: Select all

convert input -attenuate XX + noise uniform/gaussian ouput
fits perfectly my purpose - the only thing is that when using 0 - 400 (intensity range) with gaussian type XX needs to be divided by 10. Also to make results closer to PS I add 0.2 * Intensity to Intensity

Still struggling with monochromatic noise Intensity controlled - I tried your suggestion and it works alright for small levels - is there any way I can control it ?

As for Motion Blur PS seems to be just shifting image left/right + angle with 50% opacity starting from left shift - the pixel distance is a number of shifts e.g.

For pixel distance = 2:
1. shift copy of an image left by one pixel and merge with original with opacity 50% (result image here is one pixel wider than original)
2. copy result image and shift it to the right by one pixel then merge (the result is now 2 pixels wider than original)

I tried:

Code: Select all

\( +clone -fill white -colorize 100% \) -define filter:filter=Box  -define filter:support=0.25 -define compose:args="80x0-45" -compose Blur -composite
which is pretty close to PS version (visually of course :)) but it's a bit slow for larger radius values but:
If you want an approximation, you can do that in the spatial domain, by creating a simple white straight line of the appropriate length (and angle) on a black background and using -convolve. (Or half straight line from center to corner)
and I thought that that might be the way to go so could you please tell me how this could be implemented? I understand that the line length would then work as radius in the example above.

Thank you

EDIT: as for monochromatic noise I think this could be done using color noise - if you look at my function you 'll notice that both mono and color versions are using randomly generated values but to get mono these need to be all added or subtracted from the original image pixel by pixel. The syntax would be something like:

Code: Select all

\( +clone 0  -attenuate XX +noise uniform \) -compose +/- -composite
The question is how to randomize +/- ?? :?

I don't really want to use -fx as it's pretty slow
Last edited by djkprojects on 2012-07-08T11:09:43-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Monochromatic Noise

Post by fmw42 »

fits perfectly my purpose - the only thing is that when using 0 - 400 (intensity range) with gaussian type XX needs to be divided by 10. Also to make results closer to PS I add 0.2 * Intensity to Intensity
What do you mean by 0-400 intensity range? What intensity -- of the image? Please clarify.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Monochromatic Noise

Post by fmw42 »

and I thought that that might be the way to go so could you please tell me how this could be implemented? I understand that the line length would then work as radius in the example above.
Note sure if this would be radius 2
filt="\
1 0 0 0 0 \
0 1 0 0 0 \
0 0 1 0 0 \
0 0 0 1 0 \
0 0 0 0 1 "

or (one sided) as

filt="\
0 0 0 0 0 \
0 0 0 0 0 \
0 0 1 0 0 \
0 0 0 1 0 \
0 0 0 0 1 "


convert image -convolve $filt result

You can do similarly with -morphology convolve/correlate
djkprojects
Posts: 21
Joined: 2012-07-06T04:44:48-07:00
Authentication code: 13

Re: Monochromatic Noise

Post by djkprojects »

fmw42 wrote:
fits perfectly my purpose - the only thing is that when using 0 - 400 (intensity range) with gaussian type XX needs to be divided by 10. Also to make results closer to PS I add 0.2 * Intensity to Intensity
What do you mean by 0-400 intensity range? What intensity -- of the image? Please clarify.
By Intensity I basically mean how much should be added/subtracted to/from each pixel in % where 100% equals 0 to QuantumRange values
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Monochromatic Noise

Post by fmw42 »

I don't think there is an easy answer that I know to convert from your intensity in percent to the -attenuate factor. It depends upon what strength of the noise is internally generated within IM. If anyone knows more, I would like to hear.

The reason was that I had assumed in my last example with -gaussian noise that I could apply -attenuate 0.05 to get some thing like PS 5% was generating, but was surprised when I had to use -attenuate 1. I don't know if PS is doing something odd or there is just some different scaling between PS and IM noise models.

P.S. You could always look at the code in IM. I was told it was the code from Graphic Gems regarding noise models. See

gem.c:592: % GenerateDifferentialNoise() generates differentual noise.
djkprojects
Posts: 21
Joined: 2012-07-06T04:44:48-07:00
Authentication code: 13

Re: Monochromatic Noise

Post by djkprojects »

fmw42 wrote:I don't think there is an easy answer that I know to convert from your intensity in percent to the -attenuate factor. It depends upon what strength of the noise is internally generated within IM. If anyone knows more, I would like to hear.

The reason was that I had assumed in my last example with -gaussian noise that I could apply -attenuate 0.05 to get some thing like PS 5% was generating, but was surprised when I had to use -attenuate 1. I don't know if PS is doing something odd or there is just some different scaling between PS and IM noise models.

P.S. You could always look at the code in IM. I was told it was the code from Graphic Gems regarding noise models. See

gem.c:592: % GenerateDifferentialNoise() generates differentual noise.
Hello fmw42,

My post re Intensity was actually a statement rather than question (sorry for confusion) - it actually works pretty well e.g.

Code: Select all

-attenuate 100 +noise uniform
will give similar results to 100% in PS but for gaussian type equivalent of 100% would be 10:

Code: Select all

-attenuate 10 +noise gaussian
Of course I add 0.2 * 100 to get more similar result to PS. I have PS CS5
Post Reply