读取图像image = imreadCUserszhangbodaDesktopkiwi 1jpg; 将图像转换为灰度图像grayImage = rgb2grayimage; 使用自适应阈值方法进行图像二值化binaryImage = imbinarizegrayImage adaptive;filteredImage = imgaussfiltgrayImage 1; 调整标准差sigma以适
你可以使用循环来读取文件夹中的所有图片,并将处理后的图像保存为新的文件。以下是一个示例代码:
folder = 'C:\Users\zhangboda\Desktop\images'; % 文件夹路径
outputFolder = 'C:\Users\zhangboda\Desktop\processed_images'; % 输出文件夹路径
% 获取文件夹中的所有图片文件
fileList = dir(fullfile(folder, '*.jpg')); % 假设图片格式为.jpg
for i = 1:length(fileList)
% 读取图像
image = imread(fullfile(folder, fileList(i).name));
% 将图像转换为灰度图像
grayImage = rgb2gray(image);
% 使用自适应阈值方法进行图像二值化
binaryImage = imbinarize(grayImage, 'adaptive');
filteredImage = imgaussfilt(grayImage, 1); % 调整标准差sigma以适应图像
% 拉普拉斯掩模
laplacianMask = [0, -1, 0; -1, 4, -1; 0, -1, 0];
filteredImage = imfilter(filteredImage, laplacianMask, 'replicate');
% 零交叉点检测
zeroCrossings = edge(filteredImage, 'zerocross');
binaryImageRGB = uint8(cat(3, binaryImage, binaryImage, binaryImage));
zeroCrossings = uint8(zeroCrossings);
% 将zeroCrossings中的白色像素位置记录下,并将其用红色添加到binaryImage中
[row, col] = find(zeroCrossings == 1); % 找到白色像素位置
for j = 1:length(row)
binaryImageRGB(row(j), col(j), 1) = 255; % 将红色通道置为255,添加红色像素
binaryImageRGB(row(j), col(j), 2) = 0; % 将绿色通道置为0
binaryImageRGB(row(j), col(j), 3) = 0; % 将蓝色通道置为0
end
% 保存处理后的图像
[~, name, ~] = fileparts(fileList(i).name); % 获取文件名(不包括后缀)
outputName = fullfile(outputFolder, strcat(name, '_processed.jpg')); % 输出文件路径
imwrite(binaryImageRGB, outputName);
end
请确保将folder和outputFolder变量修改为你实际的文件夹路径。处理后的图像将保存在outputFolder文件夹中,并以原始文件名_processed.jpg的格式命名
原文地址: https://www.cveoy.top/t/topic/hSvt 著作权归作者所有。请勿转载和采集!