G=imread ('cameraman.tif');%读取图像
figure(1),imhist(G);%灰度直方图
M =size(G,1);%获得矩阵的维度,行数
N =size(G,2);%获得矩阵的维度,列数
G=double(G);%变换图像存储格式
for i=1:M
for j=1:N
ifG(i,j)>=65G(i,j)=1;elseG(i,j)=0;
end
end
end
figure(1),imshow(G);
灰度图像边缘提取
edge 函数
I =imread('cameraman.tif');
BW1 =edge(I,'sobel');figure(3),imshow(BW1);
BW2=edge(I,'roberts');figure(4),imshow(BW2);
BW3 =edge(I,'log');figure(5),imshow(BW3);
自定义绘制灰度直方图
G=imread('cameraman.tif');
M=size(G,1);%行数
N=size(G,2);%列数
sum=M*N;%像素总数
for k=0:255%灰度值只有0~255
o=0;%o记录每个灰度值出现的次数。比如灰度值为15的像素总共有几个
for i=1:M
for j=1:N
ifG(i,j)==k
o=o+1;
end
end
end
y(k+1)=o;
end
%y=y/sum;%百分比显示
x=0:1:255;plot(x,y,'-r');
自定义边缘提取函数
function y=selfedge(I,h,t)%I为图像,h为算子,t为自定义阈值
I=double(I);
W=conv2(I,h);%对二维图像进行卷积计算,乘以算子
M =size(W,1);%维数
N =size(W,2);for i=1:M
for j=1:N
ifW(i,j)>=t
W(i,j)=1;elseW(i,j)=0;
end
end
end
figure,imshow(W);
end