数字图像处理实验边缘检测、图像分割、图像合成等
内容要求:
采用图1,Matlab编程实现如下处理结果:
1、转换为灰度图像;
2、将夜空中的星星当作噪声滤除;.
3、将月亮、山脉和湖泊进行图像分割,并填充成
不同的灰度级做标记;
4、计算月亮的面积;
5、将校徽logo叠加在月球中心,大小一致;
先上效果图:
输入图像、图像灰度化、滤除星星
clc
clear
close all
%----------------------------灰度化和中值滤波-------------------------------
Image = imread('1.png'); % 读取图片
Gray = rgb2gray(Image); % 图像灰度化
% 滤除星星
Med = medfilt2(Gray,[6,6]); % 5*5中值滤波
figure;
subplot(1,3,1);imshow(Image);title('原图像');
subplot(1,3,2);imshow(Gray);title('灰度图像');
subplot(1,3,3);imshow(Med);title('中值滤波');
用Sobel算子进行边缘检测
%--------------------------分割月亮、山脉和湖泊-----------------------------
% Sobel算子分割
Med = im2double(Med);
mBlock = fspecial('sobel');
mImgDy = imfilter(Med, mBlock, 'replicate');
mImgDx = imfilter(Med, mBlock','replicate');
mImgDxy = sqrt(mImgDy.*mImgDy+mImgDx.*mImgDx);
figure;
subplot(1,2,1),imshow(Med),title('灰度图');
subplot(1,2,2),imshow(mat2gray(mImgDxy)),title('sobel梯度图');
标记边缘和优化边缘痕迹
% 标记湖泊边缘
Fenge = mImgDxy;
[n,m] = size(Med);
for j = 1:m
Fenge(360,j) = 1;
end
% 突出月亮
Fenge = imfill(Fenge,'holes'); % 填充黑色背景内的白色部分
% 优化山脉边缘
for i = 1:n
for j = 1:m
if Fenge(i,j) >= 0.33
Fenge(i,j) = 1;
end
end
end
% 转为二值图像
for i = 1:n
for j = 1:m
if Fenge(i,j) ~= 1
Fenge(i,j) = 0;
end
end
end
月亮、山脉和湖泊分为不同灰度级
% 不同灰度分别标记
% 湖泊
for j = 1:m
for i = n:-1:360
if Fenge(i,j) == 0
Fenge(i,j) = 0.3;
end
end
end
% 山脉
for j = 1:m
for i = 359:-1:1
if Fenge(i,j) == 0
Fenge(i,j) = 0.6;
end
if Fenge(i,j) == 1
break;
end
end
end
% 月亮
for i = 1:n
for j = 1:m
if Fenge(i,j) == 1
Fenge(i,j) = 0.1;
end
end
end
Fenge = Fenge + Med;
figure;
imshow(Fenge);title('分割图像');
计算月亮面积并记录在TXT内
%------------------------------计算月亮面积---------------------------------
mImgDxy = imfill(mImgDxy,'holes'); % 填充黑色背景内的白色部分
for i = 1:n
for j = 1:m
if mImgDxy(i,j) > 1
mImgDxy(i,j) = 1;
end
end
end
for i = 1:n
for j = 1:m
if mImgDxy(i,j) < 1
mImgDxy(i,j) = 0;
end
end
end
round_area = regionprops(mImgDxy,'Area');
fprintf('月亮面积是%f Area\n',round_area(1,1).Area);
% 记录在TXT文件内
fid=fopen('S.txt','a+');
fprintf(fid,'%s\r\n',sprintf('月亮面积是%f Area\n',round_area(1,1).Area));
月亮和二工大logo合成
%--------------------------月亮和二工大logo合成-----------------------------
logo = imread('logo.png'); % 读取logo图片
% 滤除背景
[n,m] = size(logo);
for i = 1:n
for j = 1:m
if logo(i,j) >= 235
logo(i,j) = 0;
end
end
end
% 对logo三个通道中值滤波
R = logo(:,:,1);
G = logo(:,:,2);
B = logo(:,:,3);
R = medfilt2(R,[4,4]);
G = medfilt2(G,[4,4]);
B = medfilt2(B,[4,4]);
logo = cat(3,R,G,B);
% figure;
% imshow(logo);title('logo图像');
% 取两张图片的三个通道
logo = imresize(logo,0.4); % logo图像缩小以匹配月亮大小
R_logo=logo(:,:,1);
G_logo=logo(:,:,2);
B_logo=logo(:,:,3);
HeCheng = Image;
R_HeCheng = HeCheng(:,:,1);
G_HeCheng = HeCheng(:,:,2);
B_HeCheng = HeCheng(:,:,3);
% 计算月亮所处坐标
[n,m] = size(mImgDxy);
for i = 1:n
for j = 1:m
if mImgDxy(i,j) == 1
hang = i;
break
end
end
if mImgDxy(i,j) == 1
break
end
end
for j = 1:m
for i = 1:n
if mImgDxy(i,j) == 1
lie = j;
break
end
end
if mImgDxy(i,j) == 1
break
end
end
% 通过三个通道合成
[n,m] = size(logo);
for i = 1:n
for j = 1:162
R_HeCheng(i+hang-1,j+lie-1) = R_HeCheng(i+hang-1,j+lie-1) + R_logo(i,j);
G_HeCheng(i+hang-1,j+lie-1) = G_HeCheng(i+hang-1,j+lie-1) + G_logo(i,j);
B_HeCheng(i+hang-1,j+lie-1) = B_HeCheng(i+hang-1,j+lie-1) + B_logo(i,j);
end
end
HeCheng = cat(3,R_HeCheng,G_HeCheng,B_HeCheng);
figure;
imshow(HeCheng);title('合成图像');
二工大logo覆盖月亮
% 通过三个通道覆盖
for i = 1:n
for j = 1:162
if logo(i,j) == 0
R_HeCheng(i+hang-1,j+lie-1) = R_HeCheng(i+hang-1,j+lie-1) + R_logo(i,j);
G_HeCheng(i+hang-1,j+lie-1) = G_HeCheng(i+hang-1,j+lie-1) + G_logo(i,j);
B_HeCheng(i+hang-1,j+lie-1) = B_HeCheng(i+hang-1,j+lie-1) + B_logo(i,j);
else
R_HeCheng(i+hang-1,j+lie-1) = R_logo(i,j);
G_HeCheng(i+hang-1,j+lie-1) = G_logo(i,j);
B_HeCheng(i+hang-1,j+lie-1) = B_logo(i,j);
end
end
end
FuGai = cat(3,R_HeCheng,G_HeCheng,B_HeCheng);
figure;
imshow(FuGai);title('覆盖图像');
完整代码
clc
clear
close all
%----------------------------灰度化和中值滤波-------------------------------
Image = imread('1.png'); % 读取图片
Gray = rgb2gray(Image); % 图像灰度化
% 滤除星星
Med = medfilt2(Gray,[6,6]); % 5*5中值滤波
figure;
subplot(1,3,1);imshow(Image);title('原图像');
subplot(1,3,2);imshow(Gray);title('灰度图像');
subplot(1,3,3);imshow(Med);title('中值滤波');
%--------------------------分割月亮、山脉和湖泊-----------------------------
% Sobel算子分割
Med = im2double(Med);
mBlock = fspecial('sobel');
mImgDy = imfilter(Med, mBlock, 'replicate');
mImgDx = imfilter(Med, mBlock','replicate');
mImgDxy = sqrt(mImgDy.*mImgDy+mImgDx.*mImgDx);
% figure;
% subplot(1,2,1),imshow(Med),title('灰度图');
% subplot(1,2,2),imshow(mat2gray(mImgDxy)),title('sobel梯度图');
% 标记湖泊边缘
Fenge = mImgDxy;
[n,m] = size(Med);
for j = 1:m
Fenge(360,j) = 1;
end
% 突出月亮
Fenge = imfill(Fenge,'holes'); % 填充黑色背景内的白色部分
% 优化山脉边缘
for i = 1:n
for j = 1:m
if Fenge(i,j) >= 0.33
Fenge(i,j) = 1;
end
end
end
% 转为二值图像
for i = 1:n
for j = 1:m
if Fenge(i,j) ~= 1
Fenge(i,j) = 0;
end
end
end
% 不同灰度分别标记
% 湖泊
for j = 1:m
for i = n:-1:360
if Fenge(i,j) == 0
Fenge(i,j) = 0.3;
end
end
end
% 山脉
for j = 1:m
for i = 359:-1:1
if Fenge(i,j) == 0
Fenge(i,j) = 0.6;
end
if Fenge(i,j) == 1
break;
end
end
end
% 月亮
for i = 1:n
for j = 1:m
if Fenge(i,j) == 1
Fenge(i,j) = 0.1;
end
end
end
Fenge = Fenge + Med;
figure;
imshow(Fenge);title('分割图像');
%------------------------------计算月亮面积---------------------------------
mImgDxy = imfill(mImgDxy,'holes'); % 填充黑色背景内的白色部分
for i = 1:n
for j = 1:m
if mImgDxy(i,j) > 1
mImgDxy(i,j) = 1;
end
end
end
for i = 1:n
for j = 1:m
if mImgDxy(i,j) < 1
mImgDxy(i,j) = 0;
end
end
end
round_area = regionprops(mImgDxy,'Area');
fprintf('月亮面积是%f Area\n',round_area(1,1).Area);
% 记录在TXT文件内
fid=fopen('S.txt','a+');
fprintf(fid,'%s\r\n',sprintf('月亮面积是%f Area\n',round_area(1,1).Area));
%--------------------------月亮和二工大logo合成-----------------------------
logo = imread('logo.png'); % 读取logo图片
% 滤除背景
[n,m] = size(logo);
for i = 1:n
for j = 1:m
if logo(i,j) >= 235
logo(i,j) = 0;
end
end
end
% 对logo三个通道中值滤波
R = logo(:,:,1);
G = logo(:,:,2);
B = logo(:,:,3);
R = medfilt2(R,[4,4]);
G = medfilt2(G,[4,4]);
B = medfilt2(B,[4,4]);
logo = cat(3,R,G,B);
% figure;
% imshow(logo);title('logo图像');
% 取两张图片的三个通道
logo = imresize(logo,0.4); % logo图像缩小以匹配月亮大小
R_logo=logo(:,:,1);
G_logo=logo(:,:,2);
B_logo=logo(:,:,3);
HeCheng = Image;
R_HeCheng = HeCheng(:,:,1);
G_HeCheng = HeCheng(:,:,2);
B_HeCheng = HeCheng(:,:,3);
% 计算月亮所处坐标
[n,m] = size(mImgDxy);
for i = 1:n
for j = 1:m
if mImgDxy(i,j) == 1
hang = i;
break
end
end
if mImgDxy(i,j) == 1
break
end
end
for j = 1:m
for i = 1:n
if mImgDxy(i,j) == 1
lie = j;
break
end
end
if mImgDxy(i,j) == 1
break
end
end
% 通过三个通道合成
[n,m] = size(logo);
for i = 1:n
for j = 1:162
R_HeCheng(i+hang-1,j+lie-1) = R_HeCheng(i+hang-1,j+lie-1) + R_logo(i,j);
G_HeCheng(i+hang-1,j+lie-1) = G_HeCheng(i+hang-1,j+lie-1) + G_logo(i,j);
B_HeCheng(i+hang-1,j+lie-1) = B_HeCheng(i+hang-1,j+lie-1) + B_logo(i,j);
end
end
HeCheng = cat(3,R_HeCheng,G_HeCheng,B_HeCheng);
figure;
imshow(HeCheng);title('合成图像');
% 通过三个通道覆盖
for i = 1:n
for j = 1:162
if logo(i,j) == 0
R_HeCheng(i+hang-1,j+lie-1) = R_HeCheng(i+hang-1,j+lie-1) + R_logo(i,j);
G_HeCheng(i+hang-1,j+lie-1) = G_HeCheng(i+hang-1,j+lie-1) + G_logo(i,j);
B_HeCheng(i+hang-1,j+lie-1) = B_HeCheng(i+hang-1,j+lie-1) + B_logo(i,j);
else
R_HeCheng(i+hang-1,j+lie-1) = R_logo(i,j);
G_HeCheng(i+hang-1,j+lie-1) = G_logo(i,j);
B_HeCheng(i+hang-1,j+lie-1) = B_logo(i,j);
end
end
end
FuGai = cat(3,R_HeCheng,G_HeCheng,B_HeCheng);
figure;
imshow(FuGai);title('覆盖图像');
分类:
Matlab数字图像处理实验
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~