1

To solve my last question How to do histogram matching with normal distribution as reference? I want to create an image with normal distribution. For that for every pixel of the new image I want to choose a number from 0 to 255 randomly and with normal distribution. I've done this:

normal_image = np.random.normal(0, 1, size = (M,N)) 

But the dtype of this image is float64. So then I did this:

normal_image = np.random.normal(0, 1, size = (M,N)).astype('uint8') 

But I'm not sure if this is a correct approach. Should I choose random numbers from the integers 0 to 255 based on normal distribution?(Which I don't know how to do this!)

Would you please guide me?

2
  • See the answers here stackoverflow.com/questions/37411633/… Commented Mar 31, 2023 at 8:18
  • You don’t need to make this image, if all you will do to it is compute its histogram. Just create a histogram with the bins filled according to your target distribution. Commented Apr 1, 2023 at 15:42

1 Answer 1

2

You can normalize values from normal between 0 and 1 then multiply by 255:

import numpy as np M = 10 N = 10 n = np.random.normal(0, 1, size=(M, N)) s = (n - n.min()) / (n.max() - n.min()) i = (255 * s).astype(np.uint8) 

Output:

>>> i array([[146, 117, 141, 120, 64, 105, 155, 154, 89, 81], [109, 86, 152, 105, 168, 51, 195, 50, 117, 65], [112, 0, 95, 102, 82, 74, 79, 98, 27, 183], [131, 172, 102, 220, 255, 94, 96, 138, 111, 106], [131, 170, 151, 97, 169, 138, 28, 74, 125, 151], [119, 170, 83, 190, 65, 184, 40, 183, 121, 104], [191, 193, 91, 80, 145, 49, 92, 87, 160, 132], [141, 76, 131, 65, 93, 98, 187, 66, 98, 168], [185, 81, 182, 210, 90, 151, 39, 99, 104, 123], [ 98, 109, 154, 215, 130, 93, 146, 156, 121, 37]], dtype=uint8) 

For this array:

import matplotlib.pyplot as plt plt.hist(i.ravel(), bins=10) plt.show() 

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. But this new distribution doesn't have the mean 0 and the sd 1. Right?
How do you want to have the mean is 0 with numbers between 0 and 255? Except if all numbers are 0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.