B Adding random noise to a gradient

AI Thread Summary
The discussion revolves around adding random noise to a quilt design made of 15x20 squares in six shades of blue. The user is seeking a mathematical approach to introduce variability in the gradient, as Photoshop struggles with limited palettes. Suggestions include using a Gaussian distribution to generate random values for each cell, with a formula that adjusts the average color based on these random numbers. The conversation also highlights the importance of ensuring the random values remain within the range of 1 to 6 to avoid artifacts in the design. Ultimately, the participants share techniques for achieving a visually appealing, randomized gradient in the quilt pattern.
DaveC426913
Gold Member
Messages
23,886
Reaction score
7,915
TL;DR Summary
I have a grid of tiles filled with exactly 6 colours in a gradient from 1 to 6. I want to add "noise" to it using some sort of randomization technique.
I am designing the pattern of a quilt my wife is making. The quilt is made of 15x20 squares of exactly six shades of blue - dark at one end to light at the other end.

The gradient will be "noisy". I want to experiment with that noise.

I am exploring Photoshop to do this visually, but it doesn't deal well with limited palettes. In particular any filters such as "add noise" are not available to an image with an indexed (i.e. less than 16-bit) palette.

Left: no noise:
Right: noise done manually:

1607815172250.png
1607814751742.png


I am thinking of some sort of mathematical procedure that spits out a value from 1 to 6 into every cell of a 15x20 table.

There are a few ways I can see this varying, but mostly it involves the "spread" - whether colour "4" is smeared across only bands with colours "3" and "5", or whether it is smeared all the way from, say band 2 to 6. That would presumably affect the parameters of the randomizer.

I am looking for suggestions on how to randomize this gradient above and beyond my fruitless attempts in Photoshop.
 
Last edited:
Mathematics news on Phys.org
I would define a function f(x,y) for the average color at place x,y, then add a random number taken from e.g. a Gaussian, then round to the nearest integer. The width of the Gaussian is a free parameter.

f(x,y) is not limited to the range from 1 to 6. Values outside that range will give most/nearly all tiles the color 1 or 6.
 
If you're able to play with the pixels why not just do swapping over a line crossing thru the gradients.

11111222223333344445555566666777778888899999
11112122232333434445455565666767778788898999

1121122122334334434455655665667787788788...

in my case I swapped digits across the borders 1111122222 1111212222...

you could make the distance between the two pixel swap within a range of 1-5 pixels and then choose a distance randomly.
 
Quickly made in a spreadsheet with a width of 1:

colors.png


The first one looks more chaotic than most other things I got, not sure why. Maybe a typo in the width.
 
  • Like
Likes jim mcnamara
jedishrfu said:
If you're able to play with the pixels why not just do swapping over a line crossing thru the gradients.

11111222223333344445555566666777778888899999
11112122232333434445455565666767778788898999

1121122122334334434455655665667787788788...

in my case I swapped digits across the borders 1111122222 1111212222...

you could make the distance between the two pixel swap within a range of 1-5 pixels and then choose a distance randomly.
On this scale, any non-randomness shows up as an obvious artifact.
I've tried some manual swapping but - as I have learned in several different modalities recently (online article leading to book purchase), humans are super-bad at randomness.

(Had my wife pretend to toss a coin 100 times, while I did it for real and compared the difference. Fascinating.)
 
  • Like
Likes jedishrfu
mfb said:
Quickly made in a spreadsheet with a width of 1:

View attachment 274192

The first one looks more chaotic than most other things I got, not sure why. Maybe a typo in the width.
That's very cool and exactly what I'm looking for!

Can you walk me through it again? (don't know how to do that in Excel OO-Calc) (Also, is the colour rendered formulaically, or added?)

I'm not sure what you mean by "width of 1". There's nine columns, so are each ... 1/9th?
The quilt is actually 15 tiles wide.
 
Last edited:
Steps in LibreOffice:

Select an area as large as the quilt. In my sheet it's A3 to O22.
Sheet -> Fill Cells -> Random Number, distribution -> normal, mean -> 0, standard deviation -> personal preference. This is the width I mentioned. Making the random numbers wider creates patterns with more contrast. Now the area is filled with random numbers.

Somewhere else prepare the left column from 1 to 20 or whatever, in my sheet it's Q3 to Q22. Next to that (R3) I used the formula =MIN(MAX(ROUND($Q3/3+A3,0),1),6). This can be adjusted, of course, to make the overall gradient faster/slower or to shift it.
This formula can be filled out to an area as large as the random number field (e.g. by dragging the little box in the lower right corner), here until AF22.

Mark the area that's now filled with numbers from 1 to 6.
Format -> Conditional Formatting -> Color Scale
Color Scale (2 entries)
Define starting color (1) and final color (6).
You can also define 6 individual colors by adding more conditions if you prefer that.

Dislike this particular pattern? Just mark the first area and repeat the first step.
 
mfb said:
Steps in LibreOffice:
:cry:
...sigh... What we do for science...
 
You can probably do something like

=ROUNDUP(Row()/3,0)+ROUND((Rand()-0.5)*1.3,0)

And tweak that 1.3 to be whatever you want. This might not quite compile as written.
 
  • #10
mfb said:
Somewhere else prepare the left column from 1 to 20 or whatever, in my sheet it's Q3 to Q22. Next to that (R3) ...
Sorry, I don't know what "prepare" means here.
Your next instruction involves column R, so what did you do to column Q?
mfb said:
This can be adjusted, of course, to make the overall gradient faster/slower or to shift it.
Sorry, there is no gradient in my numbers. It is simply a random grid of 1 to 6.
I obviously missed a step.
 
  • #11
DaveC426913 said:
:cry:
...sigh... What we do for science...
OpenOffice should be similar but I don't use it, in Excel I would have to look up how to produce the normal distribution but apart from that it's the same.
DaveC426913 said:
Sorry, I don't know what "prepare" means here.
Just write in the numbers. You can also put in 1 and 2 in the first two cells, mark them, and then drag down. Or put =Q3+1 in Q4 and drag that formula down.
 
  • #12
Sorry, I m totally not getting the expected results.

For one, there's no gradient. (Nor can I see anything in your instructions that I might have missed would create a gradient).

For two, they're mostly 1's. (Perhaps because the random numbers are often negative - which rounds to 1?)
1607830614464.png
 
Last edited:
  • #13
DaveC426913 said:
On this scale, any non-randomness shows up as an obvious artifact.
I've tried some manual swapping but - as I have learned in several different modalities recently (online article leading to book purchase), humans are super-bad at randomness.

(Had my wife pretend to toss a coin 100 times, while I did it for real and compared the difference. Fascinating.)
I didn’t mean you select it. I meant write a program to select the random distance and then do the swap. Your pixels would be encoded in an array that you’d walk thru row by row swapping columns rather like how card decks are shuffled.
 
  • #14
DaveC426913 said:
Sorry, I m totally not getting the expected results.
You missed this step:
mfb said:
Somewhere else prepare the left column from 1 to 20 or whatever, in my sheet it's Q3 to Q22.
It's the leftmost column in my examples in post 4.

Adding Q[something]/3 creates the gradient.
 
  • #15
mfb said:
You missed this step:
It's the leftmost column in my examples in post 4.
Again, I don't know what you mean by "prepare". You said "write in the numbers". What numbers?
I wrote in all 1's.

mfb said:
Adding Q[something]/3 creates the gradient.
Not in my spreadsheet. I don't know what [something] should be.

I do not know what you're doing well enough to be able to interpolate my actions from your instructions. I have to blindly follow your directions.
 
  • #16
If I'm understanding correctly, he's putting the numbers 0-20 in a column. Then, to make the unrandomised quilt he's taking the value from that column, dividing by 3, and rounding down. That gives him rows of numbers in the range 0-6, which he's using to set the colour.

To make the randomised quilt he's adding a random number before rounding down. Then he's just catching any numbers outside the range 0-6.

So cell Q1 needs to be zero.
Q2 needs to be =Q1+1
Copy/paste Q2 into Q3, Q4,..., Q21.
Set R1 to =min(6,max(0,floor($Q1/3+A3)))
Copy/paste R1 down to R21 and as wide as your quilt.
 
  • Like
Likes jim mcnamara
  • #17
Ibix said:
So cell Q1 needs to be zero.
Q2 needs to be =Q1+1
Copy/paste Q2 into Q3, Q4,..., Q21.
Set R1 to =min(6,max(0,floor($Q1/3+A3)))
Copy/paste R1 down to R21 and as wide as your quilt.
OK, well, I've done everything you said. Still no joy.

1607895294748.png
 
  • #18
Quick test: replace all your random numbers with zero. Do you get the simple gradient?

I'd say you have the standard deviation of your Gaussian a bit high. Your random numbers are quite often 8 or 9. You just want them to be typically in the range ##\pm 1##, so that they occasionally kick a colour 4 into a colour 5. I'd suggest regenerating them with a standard deviation of 1.
 
  • #19
OK, I started from scratch and tried to actually understand what the formulae were doing rather than copy it rote. Getting useful results now.

Whatever the problem was, it was my problem.

1607897551759.png
 
  • #20
There's also a typo - that should be $Q4 in your formula.
 
  • #21
Ibix said:
There's also a typo - that should be $Q4 in your formula.
Well that would sure mess it up all right!
 
  • #22
Thanks everyone, especially @Ibix and @mfb, for your patience.
 
  • Like
Likes jedishrfu and Ibix
  • #23
$Q4/3 gives you a number in the range 0 to 6+2/3. FLOOR() rounds down, so FLOOR($Q4/3) gives you an integer in the range 0-6. That should be enough to get you the un-randomised quilt.

The random numbers are used to push colour 4 to colour 3 or colour 5 occasionally. They are just added to the "base" number before it's rounded. So FLOOR($Q4/3+A4) should usually give you the same as FLOOR($Q4/3), but sometimes push it up ir down one. So you don't want the random numbers to be large.

Finally, pushing the numbers up and down can cause them to be out of range. If we get a negative number, we want it to be treated as zero. MAX(0,FLOOR(...)) does that. Similarly a number larger than 6 we want treated as 6. MIN(6,...) does that. So the final expression is a FLOOR inside a MAX inside a MIN. Which is ugly...
 

Similar threads

Replies
39
Views
7K
Replies
1
Views
12K
Replies
8
Views
5K
Back
Top