图像灰度处理

rgb2gray的方法:

灰度化处理的方法主要有如下3种:
最大值法:使R,G,B的值等于3值中最大的一个,R=G=B=max(R,G,B)。
平均值法:是R,G,B的值求出平均值,R=G=B=(R+G+B)/3。
加权平均值法:根据重要性或其他指标给R,G,B赋予不同的权值,并使R,G,B的值加权平均,R=G=B=WR+VG+UB,W,V,U分别表示权重。

close all;
clear all;
clc;
image = imread('flower800x480_24b.bmp');
gray_img = rgb2gray(image);
bw_img = gray_img >95;
figure,
subplot(231),imshow(image);
subplot(232),imshow(gray_img);
subplot(233),imshow(gray_img,[95,255]);

I = imread('flower800x480_24b.bmp');
[R,C,G] = size(I);%得到原来图像的矩阵的参数  
y1 = zeros(R , C);%创建一个全零的矩阵
y1 = uint8(y1);%将创建的全零矩阵转化为uint8格式,因为用上面的语句创建之后图像是double型的  
 
for i = 1:R  
    for j = 1:C  
            y1(i,j) =(double(I(i , j , 1))+double(I(i , j , 2))+double(I(i , j , 3)))/ 3; %平均法
            y2(i,j)  = max(I(i , j , 1),max(I(i , j , 2),I(i , j , 3)));%最大值法
            y3(i,j) = 0.229*I(i,j ,1)+0.578*I(i,j ,2)+0.114*I(i,j ,3);%加权平均法
    end  
end 
subplot(234),imshow(y1);
subplot(235),imshow(y2);
subplot(236),imshow(y3);

python如何利用平滑函数来平滑灰度直方图 平滑处理灰度校正_灰度


不同方法所得到的灰度图像。

灰度图像处理

1、分段灰度处理
2、线性灰度处理
3、灰度图像直方图的获取

clear all;
close all;
clc;
I=imread('flower800x480_24b.bmp');
Y=rgb2gray(I);
Y1=double(Y);
[M,N]=size(Y);
%线性灰度处理
for x = 1:M
    for y = 1:N
        if(Y(x,y)<=35)
            H1(x,y)=Y(x,y) +10; 
        elseif(Y(x,y)>35 & Y(x,y)<=75)
            H1(x,y)=(10/7)*[Y(x,y)-5] +50; 
        else( Y(x,y)> 100);
            H1(x,y)=(105/180)*[Y(x,y)-100] +150; 
        end
    end
end

H2=(log(Y1+1))/10;%非线性灰度处理
figure,subplot(231),imshow(Y);
subplot(232),imshow(H1);
subplot(233),imshow(H2);

%灰度图像直方图
row=size(Y,1);%行
column=size(Y,2);%列
N=zeros(1,256);
for i= 1:row
    for j=1:column
        k=Y(i,j);
        N(k+1)=N(k+1)+1;
    end
end
subplot(234),bar(N);      
%imhist()函数直接绘制直方图
subplot(235),imhist(Y);


%通过函数imadjust(),调整灰度范围
H3=imadjust(Y,[0.3,0.8],[0,1],0.4);
H4=imadjust(Y,[0.3,0.8],[0,1],4);
%LOW_IN, HIGH_IN, LOW_OUT and HIGH_OUT must be in the range [0.0, 1.0].
%subplot(235),imshow(H3);   
%subplot(236),imshow(H4);

%通过函数brighten(),(-1~1)之间调整灰度范围

%strechlim()和函数imadjust()进行增强
M=stretchlim(Y);
H5=imadjust(Y,M,[]);

%imcomplement()灰度的反转

%rgb2hsv()色度亮度饱和度

%histeq()直方图均衡化

滤波增强(空间域)

图像平滑(去噪声)&&图像锐化(突出轮廓)

领域处理法(模板,&即卷积处理)
1.线性平滑
2.非线性平滑
3.自适应平滑
领域平均法:损失了高频信息,造成了图像模糊
领域加权平均法:
中值滤波:即消除噪声,又保留细节,可以消除孤立的噪声

clear all;
close all;
clc;
I=imread('coins.png');
I=im2double(I);
I=rgb2gray(I);
J=imnoise(I,'salt & pepper',0.02);
%J=imnoise(I,'gaussian',0,0.01);%参数为均值和方差
h=ones(3,3)/5;
h(1,1)=0;h(1,3)=0;h(3,1)=0;h(3,3)=0;%设置模板,全1/9
K=imfilter(J,h);%卷积计算
%K=conv2(J,h);
figure,subplot(231),imshow(I);
subplot(232),imshow(J);
subplot(233),imshow(K);

%二维线性滤波filter2()
h1=fspecial('average',3);%建立3*3的模板
h2=fspecial('average',5);%建立5*5的模板
K1=filter2(h2,J);
subplot(234),imshow(K1);

%中值滤波medfilt2(),排序3*3
K2=medfilt2(J);
subplot(235),imshow(K2);

%排序滤波ordfilt2(I,n,true(5))n表示大小排序后显示哪个位置的数

%通过拉布拉斯算子进行锐化
h3=[0,1,0;1,-4,1;0,1,0];%拉布拉斯算子
J1= conv2(I,h3,'same');%卷积锐化滤波
K3=J1+I;
subplot(236),imshow(K3);

python如何利用平滑函数来平滑灰度直方图 平滑处理灰度校正_边缘检测_02


整体效果为中值滤波运算后效果最好。

边缘检测

微分运算 梯度锐化 边缘检测 三种方法
边缘检测算子: robeter交叉微分算子
sobel算子
Prewitt算子
高斯-拉普拉斯算子
二阶微分

clear all;
close all;
clc;
I=imread('coins.png');
I=im2double(I);
I1=rgb2gray(I);
h1=[-1,-1,-1;2,2,2;-1,-1,-1];%水平
h2=[-1,-1,2;-1,2,-1;2,-1,-1];%+45
h3=[-1,2,-1;-1,2,-1;-1,2,-1];%垂直
h4=[2,-1,-1;-1,2,-1;-1,-1,2];%-45
J1=imfilter(I1,h1);
J2=imfilter(I1,h2);
J3=imfilter(I1,h3);
J4=imfilter(I1,h4);
J=J1+J2+J3+J4;
figure,subplot(231),imshow(J),title('梯度算法');

[K,thresh]=edge(I1,'roberts',10/255);
subplot(232),imshow(K),title('roberts');

[K1,thresh]=edge(I1,'prewitt',[],'both');%[]表示自动计算阈值进行二值化处理,
subplot(233),imshow(K1),title('prewitt');%horisontal vertional,both

[K2,thresh]=edge(I1,'sobel',[],'both');%[]表示自动计算阈值进行二值化处理,
subplot(234),imshow(K2),title('sobel');%horizontal vertional,both

[K4,thresh]=edge(I1,'canny');
subplot(236),imshow(K),title('canny');

%利用函数fspecial()产生预定义模板
hsobel=fspecial('sobel');
hprewitt=fspecial('prewitt');
hlaplacian=fspecial('laplacian');
hlog=fspecial('log',3);%log算子
format short;
H1=imfilter(I,hsobel);
subplot(235),imshow(H1),title('sobel');

K3=im2bw(H1,80/255);
subplot(235),imshow(K3),title('12');