clc,clear,close all warning off feature jit off im = imread('coloredChips.png'); Z0 = imnoise_B(im,size(im,1),size(im,2),0.5); Z1 = im2uint8(Z0); % 类型转换 figure('color',[1,1,1]), im(:,:,1) = im(:,:,1) + Z1; % R im(:,:,2) = im(:,:,2) + Z1; % G im(:,:,3) = im(:,:,3) + Z1; % B subplot(121); imshow(im);title('加二项式分布噪声图像') subplot(122); imhist(Z1); title('加二项式分布噪声图像直方图')
function R = imnoise_B(im,M, N, b) % input: % 二项式B分布,噪声的类型; % M,N:输出噪声图像矩阵的大小 % a,b:各种噪声的分布参数 % output: % R: 输出的噪声图像矩阵,数据类型为double型 % 设定默认值 % 考虑第floor(a/2)次命中 if nargin < 4 b = 0.5; end % 产生二项式分布噪声 for i = 1:M for j=1:N a = double( floor(im(i,j)/30)+1 ); R(i,j) = nchoosek(a,floor(a/2)) * b.^(floor(a/2)) .* (1-b).^(a- floor(a/2)); end end end
nchoosek Binomial coefficient or all combinations
Syntax
C = nchoosek(n,k)
C = nchoosek(v,k)
Description
C = nchoosek(n,k) where n and k are nonnegative integers, returns n!/((n–k)! k!).
This is the number of combinations of n things taken k at a time.
C = nchoosek(v,k), where v is a row vector of length n, creates a matrix whose rows consist of all possible combinations of the n elements of v taken k at a time.
Matrix C contains n!/((n–k)! k!) rows and k columns.
Inputs n, k, and v support classes of float double and float single.
Examples
The command nchoosek(2:2:10,4) returns the even numbers from two to ten, taken four at a time:
2 4 6 8
2 4 6 10
2 4 8 10
2 6 8 10
4 6 8 10