图片如下所示:
代码如下所示:
% 程序运行版本为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
效果如下所示: