第四周作业:利用matlab制作图像的二值模板并分别利用模板进行“与模板相与”、“与模板相或”、“与模板异或”操作

第四周作业

第一题

题目:
利用 MATLAB 编程,打开自己的一张照片,依次完成下列要求:
1) 以照片的自己作为目标,制作二值模板
2) 分别利用模板进行“与模板相与”、“与模板相或”、“与模板异或”操作,并把结果分别显示 出来。

clc;clear;
image = imread('D:/1.jpg'); %读取原图像bw=im2bw(f);
bw = im2bw(image);
level=graythresh(image);%自动确定二值化阈值
bw2=im2bw(image,level);%对图像二值化
imwrite(bw2,'D:/2.bmp','bmp');   %写入二值化图像
templet = imread('D:/2.bmp');  %读取二值化图像
% class(image)
% class(templet)  %查看数据类型
subplot(2,3,1),imshow(image),title('原图像');
subplot(2,3,2),imshow(templet),title('二值化模板')
a = uint8(templet);  % 将逻辑矩阵转换成uint8类型
a(a==1)=255;  %1变为255
result1 = bitand(a,image );   %相与
result2 = bitor(a,image);  %相或
result3 = bitxor(a,image); %异或
subplot(2,3,3),imshow(result1),title('与原图相与');
subplot(2,3,4),imshow(result2),title('与原图像相或');
subplot(2,3,5),imshow(result3),title('与原图像异或');  %展示结果

结果:
在这里插入图片描述

第二题

题目:
合成图像
1)在网络下载大海图片,并将其分辨率调整与将自己的照片相同
2)利用元素加法进行照片与大海背景的合成
3)显示合成结果

background = imread('D:/大海.jpg');  
image = imread('D:/路飞.jpeg');  %读取图像
result = imadd(background,image);  %图像合成
subplot(1,3,1),imshow(background),title('背景图--大海');
subplot(1,3,2),imshow(image),title('原图--路飞')
subplot(1,3,3),imshow(result),title('合成后的图像');  %展示图像

结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44921056/article/details/115256534