请用matlab,采用批处理技术,对下面4张杂草图像进行背景分割

图片如下所示:

代码如下所示:

% 程序运行版本为R2024b
clear all;close all;clc;
% 指定图像文件夹路径
folderPath = "C:\Users\12585\Desktop\action";
% 获取文件夹中所有图像文件的列表(假设为 png 格式,可根据需要修改)
imageFiles = dir(fullfile(folderPath, '*.png'));
% 遍历每个图像文件
for k = 1:length(imageFiles)
% 读取图像
imageName = imageFiles(k).name;
imagePath = fullfile(folderPath, imageName);
originalImage = imread(imagePath);
% imshow(originalImage);
whos;
imtool(originalImage);
[x,y]=size(originalImage);% 读取图像大小,为去噪准备
Ir=originalImage(:,:,1);% figure,imshow(Ir);
Ig=originalImage(:,:,2);% figure,imshow(Ir)
Ib=originalImage(:,:,3);% figure,imshow(Ir)
Igray=rgb2gray(originalImage);
% figure,imhist(Igray);
I2=2*Ig-Ir-Ib;%参考相关研究论文和其他文献资料提出的超绿特征 2g-r-b
I_rb=imadjust(I2);%对比度增强
% figure,imshow(I_rb);
I2=I_rb;
% figure,imhist(I2);
Obj1=imbinarize(I2,graythresh(I2));%使用OSTU方法进行阈值分割
% figure,imshow(Obj1);
Obj2=bwareaopen(Obj1,round(x*y/1600));%消除成比例小目标
% figure,imshow(Obj2);
finally_=uint8(Obj2).*originalImage;%点乘运算获取杂草彩色图像
figure,imshow(finally_);
%生成图像命名与写入当前文件夹
outputImageName = ['finally_',imageName];
outputImagePath = fullfile(folderPath, outputImageName);
imwrite(finally_, outputImagePath);
% 可选:显示保存成功的信息
disp(['Image ', imageName, ' processed and saved as ', outputImageName]);
end

效果如下所示:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值