Mathematical path-python computing practice (7)-machine vision-Image Generation addition, zero mean Gaussian noise,-python mean

Source: Internet
Author: User

Mathematical path-python computing practice (7)-machine vision-Image Generation addition, zero mean Gaussian noise,-python mean

The image produces zero-mean Gaussian noise and adds noise to the grayscale image. The noise is calculated by adding a gray value of each vertex to the noise value, the Box-Muller algorithm generates Gaussian noise.

In computer simulation, it is often necessary to generate a value with a normal distribution. The most basic method is to use the inverse function of the standard normal cumulative distribution function. There are other more efficient methods. Box-Muller transformation is one of them. Another quicker method is the ziggurat algorithm. The following two methods are described. A simple, feasible, and easy-to-program method is to calculate the sum of 12 evenly distributed on (0, 1), and then subtract 6 (half of 12 ). This method can be used in many applications. The sum of the 12 numbers is Irwin-Hall distribution, and a variance of 12 is selected. The result of this derivation is limited to (-6, 6), and the density is 12. The normal distribution is estimated using 11 polynomials.

The Box-Muller method uses two independent random numbers U and V, which are evenly distributed on, use U and V to generate two sets of independent Standard Normal Distribution Random Variables X and Y:

.

This equation is proposed because the square distribution of two degrees of freedom is easily generated by exponential random variables (lnU in the equation. Therefore, using the random variable V, you can select an even angle around the circle, and use the exponential distribution to select the radius and then convert it to the (Normal Distribution) x and y coordinates.


Box-Muller is a method for generating random numbers. The implicit principle of the Box-Muller algorithm is very profound, but the result is quite simple. Generally, it is used to obtain a random number that follows a normal distribution. The basic idea is to first obtain a random number that follows a uniform distribution and then convert the random number that follows a uniform distribution to a normal distribution.


#-*-Coding: UTF-8-*-# Addition of zero mean Gaussian noise # code: myhaspl@myhaspl.comimport cv2import numpy as npfn = "test2.jpg" myimg = cv2.imread (fn) img = cv2.cvtColor (myimg, cv2.COLOR _ BGR2GRAY) param = 30 # grayscale range grayscale = 256 w = img. shape [1] h = img. shape [0] newimg = np. zeros (h, w), np. uint8) for x in xrange (0, h): for y in xrange (0, w, 2): r1 = np. random. random_sample () r2 = np. random. random_sample () z1 = param * np. cos (2 * np. pi * r2) * np. sqrt (-2) * np. log (r1) z2 = param * np. sin (2 * np. pi * r2) * np. sqrt (-2) * np. log (r1) fxy = int (img [x, y] + z1) fxy1 = int (img [x, y + 1] + z2) # f (x, y) if fxy <0: fxy_val = 0 elif fxy> grayscale-1: fxy_val = grayscale-1 else: fxy_val = fxy # f (x, y + 1) if fxy1 <0: fxy1_val = 0 elif fxy1> grayscale-1: fxy1_val = grayscale-1 else: fxy1_val = fxy1 newimg [x, y] = fxy_val newimg [x, y + 1] = fxy1_val cv2.imshow ('preview ', newimg) cv2.waitKey () cv2.destroyAllWindows ()


All the content in this blog is original. If you repost it, please indicate the source: http://blog.csdn.net/myhaspl/the lower part of the code is the noise of the colored image.

#-*-Coding: UTF-8-*-# Addition of zero mean Gaussian noise # code: myhaspl@myhaspl.comimport cv2import numpy as npfn = "test2.jpg" myimg = cv2.imread (fn) img = myimgparam = 30 # grayscale range grayscale = 256 w = img. shape [1] h = img. shape [0] newimg = np. zeros (h, w, 3), np. uint8) for x in xrange (0, h): for y in xrange (0, w, 2): r1 = np. random. random_sample () r2 = np. random. random_sample () z1 = param * np. cos (2 * np. pi * r2) * np. sqrt (-2) * np. log (r1) z2 = param * np. sin (2 * np. pi * r2) * np. sqrt (-2) * np. log (r1 )).................. newimg [x, y, 0] = fxy_val_0 newimg [x, y, 1] = fxy_val_1 newimg [x, y, 2] = fxy_val_2 newimg [x, y +] = fxy1_val_0 newimg [x, y +] = fxy1_val_1 newimg [x, y +] = values ('preview', newimg) cv2.waitKey () cv2.destroyAllWindows ()





In matlab, how can we add Gaussian noise to this image? The mean is 0, and the variance is 0008.

Clc
Clear all
Close all
A = imread ('cameraman. tif '); % read image
Imshow (A); title ('original ');
V = 0.008;
Noisy = imnoise (A, 'gaussian ', 0, V );
Figure1;
Imshow (Noisy); % add Gaussian noise with mean value 0 and variance 0.008
Y_mask = [-1-1-1; 0 0 0; 1 1 1]; % create a template in the Y direction
X_mask = y_mask '; % create a template in the X direction
I = im2double (A); % convert image data to Double Precision
Dx = imfilter (I, x_mask); % calculate the gradient component in the X direction
Dy = imfilter (I, y_mask); % calculate the gradient component in the Y direction
Grad = sqrt (dx. * dx + dy. * dy); % calculate the gradient
Grad = mat2gray (grad); % convert the gradient matrix to a grayscale image
Level = graythresh (grad); % calculate the grayscale threshold
BW = im2bw (grad, level); % use threshold value to separate gradient Images
Figure, imshow (BW); % display the split image, that is, the edge image
Title ('prewitt ')

That's it.

Use matlab to create an x (k) = sin (2 π k) image, add Gaussian white noise with zero mean variance of 1, and display

% Set the sampling interval
K = (0: 300) '/100;
% Calculate the sample value
X = sin (2 * pi * k );
% Apply Gaussian white noise
Y = awgn (x, 0 );
Figure (1 );
% Set the drawing position. The lower left corner is 200 pixels away from the screen, the lower part is 200 pixels, the width is 800 pixels, and the height is 300 pixels.
Set (gcf, 'position', [200,200,800,300]);
% Drawing Grid 1*2, drawing original signal on the left, drawing noise signal on the right
Subplot (1, 2), plot (k, x );
Subplot (1, 2), plot (k, y );




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.