MATLAB对图像进行直方图统计、灰度变换、直方图均衡、平滑去噪(不调用库函数)

MATLAB对图像进行直方图统计、灰度变换、直方图均衡、平滑去噪

本文基于matlab实现直方图统计以及均衡化,并做简单的灰度变换和去噪。所有的实现基于对像素点的操作,并不调用库函数。

线性拉伸代码

a9=imread('D:a2.jpeg'); 
a=rgb2gray(a9); 
a8=mat2gray(a); 
subplot(3,4,1);
 imshow(a); title("原始图像");

b=2*a;
%对比度变换 
subplot(3,4,2); 
imshow(b);
 title('对比度增加后图像');

c=255-a;
%灰度反转
 subplot(3,4,3); 
 imshow(c); 
 title('灰度反转后图像');

d=(log(1+double(a8)));
subplot(3,4,4); 
imshow(d);
 title('对数变换');

e=10.^(double(d))-1;
subplot(3,4,8);
 imshow(e); 
 title('对数变换的逆变换');

f=255-c;%灰度反转 
subplot(3,4,7);
 imshow(f);
title('灰度反转后图像的逆变换');

g=0.5*b;%对比度变换 
subplot(3,4,6);
 imshow(g);
title('对比度增加后图像的逆变换');

i=(double(a8).^0.5);
subplot(3,4,9);
imshow(i); 
title('伽马变换');

h=double(i).^2; 
subplot(3,4,10); 
imshow(h); 
title('伽马变换的逆变换');




%   旋转部分(与本实验无关只是做着玩玩),以下内容借鉴了CSDN博客内容,求出旋转矩阵
A = 30 / 180 * pi;
R = [cos(A), -sin(A); sin(A), cos(A)];
 R = R'; % 求出旋转矩阵的逆矩阵进行逆向查找

% 计算原彩色图大小
sz = size(a9); H = sz(1);
w = sz(2);
ch = sz(3);
c1 = [H; w] / 2;

% 计算显示完整图像需要的画布大小
hh = floor(w*sin(A)+H*cos(A))+1; 
ww = floor(w*cos(A)+H*sin(A))+1;
 c2 = [hh; ww] / 2;

% 初始化目标画布
im2 = uint8(ones(hh, ww, 3)*128); 
for k = 1:ch
for I = 1:hh
for J = 1:ww
p = [I; J];
pp = (R*(p-c2)+c1); mn = floor(pp);
ab = pp - mn; A = ab(1);
B = ab(2);
m = mn(1);
n = mn(2);
% 线性插值方法
if (pp(1) >= 2 && pp(1) <= H-1 && pp(2) >= 2 && pp(2) <= w-1)
im2(I, J, k) = (1-A)*(1-B)*a9(m, n, k) + A*(1-B)*a9(m+1, n, k)...
+ (1-A)*B*a9(m, n, k)	+ A*B*a9(m, n, k);
end
end 
end
end


% 显示图像

subplot(3,4,5) imshow(im2); title('旋转');

直方图代码

img=imread('D:a2.jpeg') ;
 img=rgb2gray(img);
img1 = double(img); 
[r,c]=size(img1);
%获取图像的高r和宽c
%统计图像中每个灰度级出现的次数 count = zeros(1,256);
for i=1:r
for j=1:c
count(img(i,j)+1) = count(img(i,j)+1)+1;
end
end
%统计图像中每个灰度级出现的概率 p = zeros(1,256);
for i=1:256
p(1,i) = count(1,i)/(r*c);
end
img2 = im2uint8(ones(r,c));%创建一个r X c大小的1矩阵


func_T = zeros(1,256);%变换函数 p_sum = 0;
%求直方图均衡化的变换函数
for k = 1:256
p_sum = p_sum + p(k);%求每个灰度级的概率之和 func_T(k) = (256-1)*p_sum;%根据变换函数的公式求和
end

func_T_z =	round(func_T);%对变换函数进行取整
%完成每个像素点的映射 for i = 1:256
findi = find(func_T_z==i);%找到灰度级为i的概率和
len = length(findi); for j=1:len
findj = find(img==(findi(j)-1));%进行对应每个像素点的映射
img2(findj) = i;
end
end




%显示图像
figure('NumberTitle', 'off', 'Name', '实验三直方图');

subplot(2,3,1);
 imshow(img); title('原始图像');

subplot(2,3,3); 
imshow(img2); title('均衡化后图像');


subplot(2,3,4); 
imhist(img); 
xlim([0 255]);
title('原始图像的直方图');


subplot(2,3,5);
 plot(1:256,func_T);
 xlim([0 255]);
ylim([0, 255]);
title('变换函数');
subplot(2,3,6);
 imhist(img2);
 xlim([0 255]);
title('均衡化后图像的直方图');

均值滤波代码

% 自编写均值滤波函数
im = imread('D:a2.jpeg'); im = rgb2gray(im);
im_noise_jiaoyan = imnoise(im,'salt & pepper'); % 加入椒盐噪声
im_noise_gaosi=imnoise(im,'gaussian'); % 加入高斯噪声

n=1; m=2*n+1;
H = ones(m,m)/(m*m); % 3×3矩阵 n=2:5×5 [h,l,c] = size(im_noise_jiaoyan); [w,z,v] = size(im_noise_gaosi);
x1 = double(im_noise_jiaoyan); x3 = double(im_noise_gaosi);
x2 = x1;
for i=n+1:h-n
for j=n+1:l-n
% 去除x1中从(i,j)开始的n行n列元素与模板相乘
c = x1(i-n:i+n,j-n:j+n).*H; s = sum(c(:));
x2(i,j) = s;
end
end
for i=n+1:w-n
for j=n+1:l-n
% 去除x1中从(i,j)开始的n行n列元素与模板相乘
v = x1(i-n:i+n,j-n:j+n).*H; s = sum(v(:));
x4(i,j) = s;
end
end
im_filtered2 = uint8(x2); im_filtered3 = uint8(x4);
subplot(2,3,1),imshow(im);title('原图');
subplot(2,3,2),imshow(im_noise_jiaoyan);title('加入椒盐噪声后的图像'); subplot(2,3,3),imshow(im_filtered2);title('椒盐噪声图像进行均值滤波后的图像'); subplot(2,3,4),imshow(im);title('原图'); subplot(2,3,5),imshow(im_noise_gaosi);title('加入高斯噪声后的图像');

中值滤波代码

im = imread('D:a2.jpeg'); im = rgb2gray(im);
im_noise_salt = imnoise(im,'salt & pepper'); % 加入椒盐噪声
im_noise_gaosi = imnoise(im,'gaussian'); % 加入高斯噪声
% % 定义邻域尺寸
n1 = 2; m1 = 2*n1+1;
n2 = 2; m2 = 2*n2% 自编写中值滤波函数
+1;

k = floor(m1*m2/2)+1;

[h,l,c] = size(im_noise_salt); [w,z,v] = size(im_noise_gaosi); H = zeros(h,l);
W = zeros(w,z);
t1 = zeros(n1,n2); t2 = zeros(n1,n2);

for i=n1+1:h-n1
for j=n2+1:l-n2
Neiborhood = im_noise_salt(i-n1:i+n1,j-n2:j+n2); % 得到3×3邻域
t1 = Neiborhood(:); s = sort(t1);
H(i,j) = s(k);
end
end
for i=n1+1:w-n1
for j=n2+1:l-n2
Neiborhood = im_noise_gaosi(i-n1:i+n1,j-n2:j+n2); % 得到3×3邻域
t2 = Neiborhood(:); s = sort(t2);
W(i,j) = s(k);
end
end
im_filtered2 = uint8(H); im_filtered3 = uint8(W);
subplot(2,3,1),imshow(im);title('原图');
subplot(2,3,2),imshow(im_noise_salt);title('加入椒盐噪声后的图像'); subplot(2,3,3),imshow(im_filtered2);title('椒盐噪声图像进行中值滤波后的图像'); subplot(2,3,4),imshow(im);title('原图'); subplot(2,3,5),imshow(im_noise_gaosi);title('加入高斯噪声后的图像'); subplot(2,3,6),imshow(im_filtered3);title('高斯噪声图像进行中值滤波后的图像');
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值