matlab_数字图像处理代码

%%%%%% 求对比度%%%%%%

clear;

clc;

[filename,pathname]=uigetfile({'*jpg;*.bmp;*.jpeg'},'load?image'); fpath=fullfile(pathname,filename);

A=imread(fpath);

figure(1);

imshow(A);

title('原图像')

img=rgb2gray(A);

figure(2);

imshow(img);

title('灰度后的');

img1=imnoise(img,'salt & pepper',0.05); %加入椒盐躁声

figure(3);

imshow(img1);

title('椒盐后的图像');

%%% K近邻域滤椒盐噪声%%% 3*3

%%% 找出与f(i,j)相近的像素值这里利用做差排序找出AAAA1=img1;

AAAA1=double(AAAA1);

%%%得到椒盐噪声图像

[m,n]=size(AAAA1);

for i=2:m-1

for j=2:n-1

ASD1=[AAAA1(i-1,j-1) AAAA1(i-1,j) AAAA1(i-1,j+1) ...

AAAA1(i,j-1) AAAA1(i,j) AAAA1(i,j+1) ...

AAAA1(i+1,j-1) AAAA1(i+1,j) AAAA1(i+1,j+1)];

aa=ASD1; %%随机设定的一个数组

b=AAAA1(i,j); %%取参照值

a=aa(:); %%将给定数组化为一维的

ab=(a(:)-b)'; %%将数组a与b做差abc=abs(ab);

abc=sort(abc); %%差值取绝对值并排序

[as1 as11] =find(abs((a(:)-b))==abc(1,1));

as=[as1 as11];

if ~isempty(abc(abc(:)~=abc(1,1)))

if ~isempty(abc)

abc1=abc(abc(:)~=abc(1,1));

[as2 as22]=find(abs((a(:)-b))==abc1(1,1));

as=[as1 as11;as2 as22];

end

end

if ~isempty(abc1(abc1(:)~=abc1(1,1)))

if ~isempty(abc1)

abc2=abc1(abc1(:)~=abc1(1,1));

[as3 as33]=find(abs((a(:)-b))==abc2(1,1));

as=[as1 as11;as2 as22;as3 as33];

end

end

if ~isempty(abc2(abc2(:)~=abc2(1,1)))

if ~isempty(abc2)

abc3=abc2(abc2(:)~=abc2(1,1));

[as4 as44]=find(abs((a(:)-b))==abc3(1,1));

as=[as1 as11;as2 as22;as3 as33;as4 as44];

end

end

if ~isempty(abc3(abc3(:)~=abc3(1,1)))

if ~isempty(abc3)

abc4=abc3(abc3(:)~=abc3(1,1));

[as5 as55]=find(abs((a(:)-b))==abc4(1,1));

as=[as1 as11;as2 as22;as3 as33;as4 as44;as5 as55];

end

end

if ~isempty(abc4(abc4(:)~=abc4(1,1)))

if ~isempty(abc4)

abc5=abc4(abc4(:)~=abc4(1,1));

[as6 as66]=find(abs((a(:)-b))==abc5(1,1));

as=[as1 as11;as2 as22;as3 as33;as4 as44;as5 as55;as6 as66];

end

end

%%%% as即是与b值相近的值的坐标包括和b相等的(这里是与b 值相差为六种值的情况不一定是六个值可能等于也可能大于%%%% 将以下坐标的值取第二个到第六个即是与b值相近的5个)

result=zeros(1,(length(as)-1));

for VB=2:6

result(VB-1)=a(as(VB,1));

end %%%求前五个的均值

AVERAGE=sum(result(:))/5;

AAAA1(i,j)=AVERAGE;

end

end

figure(9)

imshow(uint8(AAAA1));

title('K近邻域滤椒盐噪声后的图像');

clear;

clc;

I_gray=...

[1 5 255 225 100 200 255 200;...

1 7 254 225 100 10 10 9; ...

3 7 10 100 100 2 9 6;...

3 6 10 10 9 2 8 2;...

2 1 8 8 9

3

4 2;...

1 0 7 8 8 3

2 1;...

1 1 8 8 7

2 2 1;...

2 3 9 8 7 2 2 0]; %%%%%A可以是矩阵也可以是图像

[height,width]=size(I_gray); %测量图像尺寸参数

p=zeros(1,256); %预创建存放灰度出现概率的向量

for i=1:height

for j=1:width

p(I_gray(i,j) + 1) = p(I_gray(i,j) + 1) + 1;

end

end

s=zeros(1,256);

s(1)=p(1);

for i=2:256

s(i)=p(i) + s(i-1); %统计图像中<每个灰度级像素的累积个数,s(i):0,1,```,i-1

end

for i=1:256

s(i) = s(i)*256/(width*height); %求灰度映射函数

if s(i) > 256

s(i) = 256;

end

end

%图像均衡化

I_equal = I_gray;

for i=1:height

for j=1:width

I_equal(i,j) = s( I_gray(i,j) + 1);

end

end %显示均衡化后的图像

Dui_Bi_Du2=Qiu_Dui_Bi_Du(I_equal) %%%%%均衡化后的矩阵Dui_Bi_Du1=Qiu_Dui_Bi_Du(I_gray)

clear;

clc;

[filename,pathname]=uigetfile({'*jpg;*.bmp;*.jpeg'},'load?image'); fpath=fullfile(pathname,filename);

A=imread(fpath);

img=rgb2gray(A);

figure(1)

imshow(img);

Jiao_Du=input('请输入旋转角度'); %%%此旋转为逆时针方向

theta=Jiao_Du/180*pi;

img=double(img);

[m,n]=size(img);

mm=zeros();

nn=zeros();

tic %%%计时开始

%%%%--求变换后的坐标值---%%%

for i=1:m

for j=1:n

mm(i,j)=round(i*cos(theta)-j*sin(theta));

nn(i,j)=round(i*sin(theta)+j*cos(theta));

end

end

%%%----------------------%%%

Cha_Zhi_1=floor(abs(max(mm(:))-min(mm(:)))); %%横坐标最大差值//注意as=max(mm(:)) as=max(mm)的区别

Cha_Zhi_2=floor(abs(max(nn(:))-min(nn(:)))); %%纵坐标最大差值//此处用as=max(mm(:))

%%%由于上面求出的mm和nn可能会有0,这在数组中是不允许的故要找出0,统一加一定数值,是其全部为正值

mm_min=min(mm(:));

nn_min=min(nn(:)); %%找出行列中最小的负值

%%%下面实现对mm nn的值统一变为大于0的整数

[a,b]=size(mm);

[aa,bb]=size(nn);

for i=1:a

for j=1:b

mm(i,j)=mm(i,j)+abs(mm_min)+1;

end

end

for i=1:aa

for j=1:bb

nn(i,j)=nn(i,j)+abs(nn_min)+1;

end

end

%%%%%%%-------------------------------%%%

new_img=zeros(Cha_Zhi_1,Cha_Zhi_2); %%旋转后的新图像的画布大小

%%%% 直接放大%%%%

for i=1:m

for j=1:n

new_img(mm(i,j),nn(i,j))=img(i,j);

end

end

%%%% 开始邻近插值%%%%

for i=2:Cha_Zhi_1-1

for j=2:Cha_Zhi_2-1

if new_img(i,j)==0

new_img(i,j)=round((new_img(i-1,j)+new_img(i+1,j)+new_img(i,j-1)+new _img(i,j+1)))/4;

end

end

end

toc

%%%计时结束

figure(2);

imshow(uint8(new_img));

%%%%%----不等宽度、高度放大----%%%%%

clear;

clc;

[filename,pathname]=uigetfile({'*jpg;*.bmp;*.jpeg'},'load?image'); fpath=fullfile(pathname,filename);

A=imread(fpath);

img=rgb2gray(A);

figure(1)

imshow(img);

title('原图像');

%%%%----width,height为原图像宽度和高度-----%%%% [Width,Height]=size(img);

%%%%-----输入放大倍数的相关参数-----%%%%%

Width_BeiShu=input('请输入宽度上的放大倍数:');

Height_BeiShu=input('请输入高度上的放大倍数:');

%%%%-----新图像画布大小-----%%%%%

new_Width=round(Width_BeiShu*Width);

new_Height=round(Height_BeiShu*Height);

%%%%-----申请新画布------%%%%

new_img=zeros(new_Width,new_Height);

%%%%-----开始放大--------%%%%

%%% 双线性差值算法中的四个顶点%%%

Q12=0;

Q11=0;

Q21=0;

Q22=0;

%%% 开始差值%%%

%%% 差值过程中要注意放大倍数不是整数时对坐标的取整%%% for i=1:Width-1

for j=1:Height-1

Q12=img(i,j+1);

Q11=img(i,j);

Q22=img(i+1,j+1);

Q21=img(i+1,j);

for

a=(round((i-1)*Width_BeiShu)+1):round((i)*Width_BeiShu)

for

b=(round((j-1)*Height_BeiShu)+1):round((j)*Height_BeiShu)

%%%双线性差值中的三个特征像素点%%%

new_img((round((i-1)*Width_BeiShu)+1),b)=(i*Width_BeiShu-a)/(round( i*Width_BeiShu)-i)*Q11+(a-i)/(round(i*Width_BeiShu)-i)*Q12;

%%%F(R1)

new_img(round(i*Width_BeiShu),b)=(round(i*Width_BeiShu)-a)/(round( i*Width_BeiShu)-i)*Q12+(a-i)/(round(i*Width_BeiShu)-i)*Q22;

%%%F(R2)

new_img(a,b)=(round(j*Height_BeiShu)-b)/(round(j*Height_BeiShu)-j)*n ew_img(i,b)+(b-j)/(round(j*Height_BeiShu)-j)*new_img(round(i*Width_ BeiShu),b); %%%F(P);

end

end

end

end

figure(2);

imshow(uint8(new_img));

title('放大后的图像');

clear all;

close all;

clc;

jiaodu=input('请输入角度:'); %要旋转的角度,旋转方向为顺时针

img=imread('E:\国创相关\国创\bmp图片\8位图像\017.bmp'); %这里v为原图像的高度,u为原图像的宽度

imshow(img); %这里y为变换后图像的高度,x 为变换后图像的宽度

[h w]=size(img);

theta=jiaodu/180*pi;

rot=[cos(theta) -sin(theta) 0;sin(theta) cos(theta) 0;0 0 1];

pix1=[1 1 1]*rot; %变换后图像左上点的坐标

pix2=[1 w 1]*rot; %变换后图像右上点的坐标

pix3=[h 1 1]*rot; %变换后图像左下点的坐标

pix4=[h w 1]*rot; %变换后图像右下点的坐标

height=round(max([abs(pix1(1)-pix4(1))+0.5

abs(pix2(1)-pix3(1))+0.5])); %变换后图像的高度

width=round(max([abs(pix1(2)-pix4(2))+0.5

abs(pix2(2)-pix3(2))+0.5])); %变换后图像的宽度

imgn=zeros(height,width);

delta_y=abs(min([pix1(1) pix2(1) pix3(1) pix4(1)])); %取得y方向的负轴超出的偏移量

delta_x=abs(min([pix1(2) pix2(2) pix3(2) pix4(2)])); %取得x方向的负轴超出的偏移量

for i=1-delta_y:height-delta_y

for j=1-delta_x:width-delta_x

pix=[i j 1]/rot; %用变换后图像的点的坐标去寻找原图像点的坐标,

%否则有些变换后的图像的像素点无法完全填充

float_Y=pix(1)-floor(pix(1));

float_X=pix(2)-floor(pix(2));

if pix(1)>=1 && pix(2)>=1 && pix(1) <= h && pix(2) <= w

pix_up_left=[floor(pix(1)) floor(pix(2))]; %四个相邻的点

pix_up_right=[floor(pix(1)) ceil(pix(2))];

pix_down_left=[ceil(pix(1)) floor(pix(2))];

pix_down_right=[ceil(pix(1)) ceil(pix(2))];

value_up_left=(1-float_X)*(1-float_Y); %计算临近四个点的权重

value_up_right=float_X*(1-float_Y);

value_down_left=(1-float_X)*float_Y;

value_down_right=float_X*float_Y;

imgn(i+delta_y,j+delta_x)=value_up_left*img(pix_up_left(1),pix_up_left( 2))+ ...

value_up_right*img(pix_up_right(1),pix_up_right(2))+ ...

value_down_left*img(pix_down_left(1),pix_down_left(2))+ ...

value_down_right*img(pix_down_right(1),pix_down_right(2));

end

end

end

figure,imshow(uint8(imgn))

clear;

clc;

[filename,pathname]=uigetfile({'*jpg;*.bmp;*.jpeg'},'load?image'); fpath=fullfile(pathname,filename);

image1=imread(fpath);

%%%参数调整Fa Fb Ga Gb

image=image1(:,:,1);

figure(1);

imshow(uint8(image));

title('原图')

[m,n]=size(image);

newimage=zeros(m,n);

km1=input('输入原图像灰度值一:');

km2=input('输入原图像灰度值二:');

kn1=input('展宽后的灰度值一:');

kn2=input('展宽后的灰度值二:');

for i=1:m

for j=1:n

if image(i,j)

newimage(i,j)=kn1*image(i,j)/km1;

elseif image(i,j)

newimage(i,j)=(kn2-kn1)/(km2-km1)*(image(i,j)-km1)+kn1;

else

newimage(i,j)=(255-kn2)/(255-km2)*(image(i,j)-km2)+kn2;

end

end

end

figure(2);

imshow(uint8(newimage));

title('处理后的')

clear;

clc;

[filename,pathname]=uigetfile({'*jpg;*.bmp;*.jpeg'},'load?image');

fpath=fullfile(pathname,filename);

A=imread(fpath);

img=rgb2gray(A);

figure(1)

imshow(img);

Jiao_Du=input('请输入旋转角度'); %%%此旋转为逆时针方向

theta=Jiao_Du/180*pi;

img=double(img);

[m,n]=size(img);

mm=zeros();

nn=zeros();

tic %%%计时开始

%%%%--求变换后的坐标值---%%%

for i=1:m

for j=1:n

mm(i,j)=round(i*cos(theta)-j*sin(theta));

nn(i,j)=round(i*sin(theta)+j*cos(theta));

end

end

%%%----------------------%%%

Cha_Zhi_1=floor(abs(max(mm(:))-min(mm(:)))); %%横坐标最大差值//注意as=max(mm(:)) as=max(mm)的区别

Cha_Zhi_2=floor(abs(max(nn(:))-min(nn(:)))); %%纵坐标最大差值//此处用as=max(mm(:))

%%%由于上面求出的mm和nn可能会有0,这在数组中是不允许的故要找出0,统一加一定数值,是其全部为正值

mm_min=min(mm(:));

nn_min=min(nn(:)); %%找出行列中最小的负值

%%%下面实现对mm nn的值统一变为大于0的整数

[a,b]=size(mm);

[aa,bb]=size(nn);

for i=1:a

for j=1:b

mm(i,j)=mm(i,j)+abs(mm_min)+1;

end

end

for i=1:aa

for j=1:bb

nn(i,j)=nn(i,j)+abs(nn_min)+1;

end

end

%%%%%%%-------------------------------%%%

new_img=zeros(Cha_Zhi_1,Cha_Zhi_2); %%旋转后的新图像的画布大小

%%%还没有差值的

for i=1:m

for j=1:n

new_img(mm(i,j),nn(i,j))=img(i,j);

end

end

toc

%%%计时结束

figure(2);

imshow(uint8(new_img));

%%%%%% 求对比度%%%%%%

function ZhongZhi_filter()

clear;

clc;

[filename,pathname]=uigetfile({'*jpg;*.bmp;*.jpeg'},'load?image');

fpath=fullfile(pathname,filename);

A=imread(fpath);

figure(1);

imshow(A);

title('原图像')

img=rgb2gray(A);

figure(2);

imshow(img);

title('灰度后的');

img1=imnoise(img,'salt & pepper',0.05); %加入椒盐躁声

figure(3);

imshow(img1);

title('椒盐后的图像');

img2=imnoise(img,'gaussian',0.05); %加入高斯躁声

figure(4);

imshow(img2);

title('高斯噪声后的图像');

%%% 以下是中值滤波%%%

%%% 中值滤波椒盐噪声%%%

AAA=img1; %%%得到椒盐噪声图像

[m,n]=size(AAA);

for i=2:m-1

for j=2:n-1

ASD=[AAA(i-1,j-1) AAA(i-1,j) AAA(i-1,j+1) AAA(i,j-1) AAA(i,j) AAA(i,j+1) AAA(i+1,j-1) AAA(i+1,j) AAA(i+1,j)+1];

Pai_Xu=sort(ASD);

Length=length(Pai_Xu);

AAA(i,j)=Pai_Xu((Length+1)/2);

end

end

figure(7)

imshow(AAA);

title('中值滤椒盐噪声后的图像');

AAA1=img2; %%%得到椒盐噪声图像

[m,n]=size(AAA1);

for i=2:m-1

for j=2:n-1

ASD=[AAA1(i-1,j-1) AAA1(i-1,j) AAA1(i-1,j+1) AAA1(i,j-1) AAA1(i,j) AAA1(i,j+1) AAA1(i+1,j-1) AAA1(i+1,j) AAA1(i+1,j+1)];

Pai_Xu=sort(ASD); %%%排序

Length=length(Pai_Xu); %%%求数组长度

AAA1(i,j)=Pai_Xu((Length+1)/2); %%%取最中间的end

end

figure(8)

imshow(AAA1);

title('中值滤高斯噪声后的图像');

相关推荐
相关主题
热门推荐