MATLAB GUI设计 图像处理基本操作(六)

一、彩图平滑与锐化

与灰度图原理相似,彩图rgb三通道分别进行锐化或平滑滤波后在合并成一张图像。

%平滑
global s;
a=imread(s);
r=a(:,:,1);
g=a(:,:,2);
b=a(:,:,3);
w=fspecial('average', 9);
fR_filtered=imfilter(r, w, 'replicate');
fG_filtered=imfilter(g, w, 'replicate');
fB_filtered=imfilter(b, w, 'replicate');
fc_filtered = cat(3, fR_filtered, fG_filtered, fB_filtered);
axes(handles.axes2);
imshow(fc_filtered);
setappdata(handles.axes2,'img2',fc_filtered);
global cell;
global pos;
imgpro=getappdata(handles.axes2,'img2');
pos=pos+1;
cell{pos}=imgpro;

%锐化
global s;
a=imread(s);
r=a(:,:,1);
g=a(:,:,2);
b=a(:,:,3);
lapMatrix=[-1 -1 -1;-1 9 -1;-1 -1 -1];
f_R=imfilter(r,lapMatrix,'replicate');
f_G=imfilter(g,lapMatrix,'replicate');
f_B=imfilter(b,lapMatrix,'replicate');
rgb_tmp=cat(3,f_R,f_G,f_B);
axes(handles.axes2);
imshow(rgb_tmp);
setappdata(handles.axes2,'img2',rgb_tmp);
global cell;
global pos;
imgpro=getappdata(handles.axes2,'img2');
pos=pos+1;
cell{pos}=imgpro;

二、rbg图到hsi模式转变后补色

HSI模式包含了色调、饱和度和亮度三种基本特征量,转换遵循以下公式:

 补色如下图:

 

global s;
a=imread(s);
rgb=im2double(a);
R = rgb(:, :, 1);
G = rgb(:, :, 2);
B = rgb(:, :, 3);
% 执行转换方程
a=0.5.*((R-G)+(R-B));
b=sqrt((R-G).^2+(R-B).*(G-B));
c=(a./(b+eps)).*pi/180;
theta=1./(cos(c)+eps);
H=theta;
if B>G
    H=360-H;
end
H=H./360;
c=R+G+B;
d=min(min(R,G),B);
if (c==0)
    c=eps;
end
S=1-3.*(d./c);
I=(R+G+B)./3;
HSI=cat(3,H,S,I);
s1=S;
i1=1-I;
if(H<=0.5)
    h1=0.5+H;
else
    h1=-0.5+H;
end
hsi=cat(3,h1,s1,i1);
figure();
imshow(hsi);
H2=h1*2*pi; 
S2=s1; 
I2=i1; 
R1=zeros(size(hsi,1),size(hsi,2)); 
G1=zeros(size(hsi,1),size(hsi,2)); 
B1=zeros(size(hsi,1),size(hsi,2)); 
if (H2>0) & (H2<=2*pi/3)
    R1=I2.*(1+S2.*cos(H2)./cos(pi/3-H2));
    B1=I2.*(1-S2);
    G1=3*I2-(R1+B1);
elseif (H2>2*pi/3) & (H2<=4*pi/3)
    H2=H2-2*pi/3;
    R1=I2.*(1-S2);
    G1=I2.*(1+S2.*cos(H2)./cos(pi/3-H2));
    B1=3*I2-(R1+G1);
else
    H2=H2-4*pi/3;
    B1=I2.*(1+S2.*cos(H2)./cos(pi/3-H2));
    G1=I2.*(1-S2);     
    R1=3*I2-(G1+B1);
end
rgb=cat(3,R1,G1,B1); 
axes(handles.axes2);
imshow(rgb);
setappdata(handles.axes2,'img2',rgb);
global cell;
global pos;
imgpro=getappdata(handles.axes2,'img2');
pos=pos+1;
cell{pos}=imgpro;

 

 三、彩图彩色变换

即色调与饱和度的变换,亮度不变:

global s;
a=imread(s);
rgb=im2double(a);
R=rgb(:,:,1);
G=rgb(:,:,2);
B=rgb(:,:,3);
a=0.5.*((R-G)+(R-B));
b=sqrt((R-G).^2+(R-B).*(G-B));
c=(a./(b+eps)).*pi/180;
theta=1./(cos(c)+eps);
H=theta;
if B>G
    H=360-H;
end
H=H./360;
c=R+G+B;
d=min(min(R,G),B);
if (c==0)
    c=eps;
end
S=1-3.*(d./c);
I=(R+G+B)./3;
s1=S;
i1=0.5*I;
h1=H;
hsi=cat(3,h1,s1,i1);
H2=h1*2*pi; 
S2=s1; 
I2=i1; 
R1=zeros(size(hsi,1),size(hsi,2)); 
G1=zeros(size(hsi,1),size(hsi,2)); 
B1=zeros(size(hsi,1),size(hsi,2)); 
if (H2>0) & (H2<=2*pi/3)
    R1=I2.*(1+S2.*cos(H2)./cos(pi/3-H2));
    B1=I2.*(1-S2);
    G1=3*I2-(R1+B1);
elseif (H2>2*pi/3) & (H2<=4*pi/3)
    H2=H2-2*pi/3;
    R1=I2.*(1-S2);
    G1=I2.*(1+S2.*cos(H2)./cos(pi/3-H2));
    B1=3*I2-(R1+G1);
else
    H2=H2-4*pi/3;
    B1=I2.*(1+S2.*cos(H2)./cos(pi/3-H2));
    G1=I2.*(1-S2);     
    R1=3*I2-(G1+B1);
end
rgb=cat(3,R1,G1,B1); 
axes(handles.axes2);
imshow(rgb);
figure();
imshow(hsi);
setappdata(handles.axes2,'img2',rgb);
global cell;
global pos;
imgpro=getappdata(handles.axes2,'img2');
pos=pos+1;
cell{pos}=imgpro;

四、图像分割

1.Laplacian of Gaussian (LoG)

global s;
a=imread(s);
axes(handles.axes2);
if ndims(a)==3
    a=rgb2gray(a);
end
h=[0 0 -1 0 0;0 -1 -2 -1 0;-1 -2 16 -2 -1;0 -1 -2 -1 0;0 0 -1 0 0];
I=imfilter(a,h,'replicate');  %图像的外部边界通过镜像反射其内部边界来扩展,relicate复制内部边界值
h1=[0 1 2;-1 0 1;-2 -1 0];
h2=[-2 -1 0;-1 0 1 ;0 1 3];
I1=imfilter(I,h1,'replicate'); 
I2=imfilter(I,h2,'replicate'); 
IMG=I1+I2;
img=im2bw(IMG);
imshow(img);
setappdata(handles.axes2,'img2',img);
global cell;
global pos;
imgpro=getappdata(handles.axes2,'img2');
pos=pos+1;
cell{pos}=imgpro;

2.阈值分割

global s;
a=imread(s);
if ndims(a)==3
    a=rgb2gray(a);
end
axes(handles.axes2);
t=mean2(a);
done=false;
i=0;
while ~done
    r1=find(a<=t);
    r2=find(a>t);
    newt=(mean(a(r1))+mean(a(r2)))/2;
    done=abs(newt-t)<1;%判断是否收敛
    t=newt;%不收敛,继续循环
    i=i+1;
end
a(r1)=0;
a(r2)=1;
imshow(a,[]);
setappdata(handles.axes2,'img2',a);
global cell;
global pos;
imgpro=getappdata(handles.axes2,'img2');
pos=pos+1;
cell{pos}=imgpro;

3.彩图分割

global s;
a=imread(s);
axes(handles.axes2);
t=rgb2gray(a);
[m,n]=size(t);
mm=floor(m/2);
nn=floor(n/2);
r=mean2(a((mm-110):(mm+110),(nn-110):(nn+110),1));%选择想要分割的颜色像素位置
g=mean2(a((mm-110):(mm+110),(nn-110):(nn+110),2));
b=mean2(a((mm-110):(mm+110),(nn-110):(nn+110),3));
t=zeros(m,n);
for i=1:m
    for j=1:n
        h1=a(i,j,1)-r;
        h2=a(i,j,2)-g;
        h3=a(i,j,3)-b;
        if h1^2+h2^2+h3^2<255
            t(i,j)=1;
        else
            t(i,j)=0;
        end
    end
end
imshow(t);
setappdata(handles.axes2,'img2',t);
global cell;
global pos;
imgpro=getappdata(handles.axes2,'img2');
pos=pos+1;
cell{pos}=imgpro;

  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值