数字图像识别这门课结束,留存下最终作业的代码。感觉matlab的语法确实不是很友好,其实有些在python里面写也是可以的呀~
逻辑运算:
题目:用程序设计语言编程画出下图中的S、T。图像R、Q是由图像S和T的逻辑运算得到,请编码实现之。
%S:
x1(65:192,65:192) = uint8(0);
x1(1:64,1:256) = uint8(255);
x1(193:256,1:256) = uint8(255);
x1(65:192,1:64) = uint8(255);
x1(65:192,193:256) = uint8(255);
figure,imshow(x1);
%T:
x8(1:128,1:128) = uint8(0);
x8(129:256,1:256) = uint8(255);
x8(1:128,129:256) = uint8(255);
figure,imshow(x8);
%R:
imshow((x1)|(x8));
%Q:
imshow((x1)&(x8));
分段线性变换:
题目:编程实现把图像f(x,y)经分段线性变换修正为图像g(x,y)的灰度分段变换方程。
X1=imread('C:\Users\ASUS\Pictures\Saved Pictures\lena256.jpg');
subplot(1,2,1),imshow(X1);
f0=0;g0=0;
f1=10;g1=50;
f2=245;g2=200;
f3=255;g3=255;
r1=(g1-g0)/(f1-f0);
b1=g0;
r2=(g2-g1)/(f2-f1);
b2=g1;
r3=(g3-g2)/(f3-f2);
b3=g2;
[m,n]=size(X1);
X2=double(X1);
for i=1:m
for j=1:n
f=X2(i,j);
g(i,j)=0;
if(f>=f0)&&(f<=f1)
g(i,j)=abs(r1*f+b1);
elseif(f>f1)&&(f<=f2)
g(i,j)=r2*(f-f1)+b2;
else
g(i,j)=r3*(f-f2)+b3;
end
end
end
subplot(1,2,2),imshow(mat2gray(g));
H算子锐化滤波:
题目:2. 课堂提供的lena256.bmp图像文件,请对其进行如下H算子锐化滤波,滤波后的结果图像数据存放在image_c.bmp中,并显示原图像及锐化滤波后结果图像。试编码实现之。
提示:需通过编程获取相关图像信息如:图像尺寸、灰度级等。
要求:1)提交原始代码;2)提交2幅图像显示的截图;
I0=imread('C:\Users\ASUS\Pictures\Saved Pictures\lena256.jpg'); %读取图像
I1=double(I0);
subplot(1,2,1),imshow(I0),title('原图');
model1=[1,-2,1;
-2,4,-2;
1,-2,1];
[m,n]=size(I1);
I2=I1;
for i=2:m-1
for j=2:n-1
I2(i,j)=I1(i+1,j+1)-2*I1(i+1,j)+I1(i+1,j-1)+I1(i-1,j+1)-2*I1(i-1,j)+I1(i-1,j-1)-2*I1(i,j+1)+4*I1(i,j)-2*I1(i,j-1);
%这里需要注意取值的问题,用abs
I2(i,j) = abs(I2(i,j));
end
end
subplot(1,2,2),imshow(uint8(I2)),title('锐化滤波后');
imwrite(uint8(I2), 'C:\Users\ASUS\Pictures\Saved Pictures\image_c.bmp');
运行结果:
边缘检测:
题目:下图是一幅256 * 256的灰度图像,左侧区域占128列,右侧区域中间的高亮度区(白色区域)是128*128。试通过matlab编程实现下列:
- 绘制出该图像;
- 分别用边缘检测算子 [-1, 0, 1] 和 [-1; 0; 1]对该图像进行边缘检测;
- 将上述三幅图像显示出来;
- 将此三幅图像分别保存到image1.bmp、image1.bmp和image3.bmp中。
x9(1:256,1:128) = uint8(0);
x9(1:64,128:256) = uint8(128);
x9(194:256,128:256) = uint8(128);
x9(65:193,128:256) = uint8(255);
figure,imshow(x9);
I0=x9;
I1=double(I0);
subplot(1,3,1),imshow(I0),title('原图');
imwrite(I1, 'C:\Users\ASUS\Pictures\Saved Pictures\image1.bmp');
I2=I1;
model1=[-1,0,1];
[m,n]=size(I1);
for i=2:m-1
for j=2:n-1
I2(i,j)= I1(i,j-1)*model1(1)+I1(i,j)*model1(2)+I1(i,j+1)*model1(3);
end
end
subplot(1,3,2),imshow(I2),title('边缘检测算子1');
imwrite(I2, 'C:\Users\ASUS\Pictures\Saved Pictures\image2.bmp');
model2=[-1; 0; 1];
I3 = I1;
for i=2:m-1
for j=2:n-1
I3(i,j)=I1(i-1,j)*model2(1,1)+I1(i,j)*model2(2,1)+I1(i+1,j)*model2(3,1);
I3(i,j) = abs(I3(i,j));
end
end
imwrite(I3, 'C:\Users\ASUS\Pictures\Saved Pictures\image3.bmp');
subplot(1,3,3),imshow(I3),title('边缘检测算子2');