RGB图片转换为灰度图批处理

工具性demo,从做视觉的时候可能会用到。即将一个文件夹中的rgb序列图像全部转换为灰度图保存,便于做一些其他处理。
不罗嗦,直接上源码吧:

imPath = 'img'; imExt = 'jpg'; %原图像文件夹名和图像格式
gray_imPath = 'img_gray';  %灰度图像文件夹名
% check if directory and files exist
if isdir(imPath) == 0
    error('USER ERROR : The image directory does not exist');
end

mkdir(gray_imPath); % create a folder
filearray = dir([imPath '\' '*.' imExt]);  % get all files in the directory % PC下使用'\', linux下使用'/'
NumImages = size(filearray,1); % get the number of images

if NumImages < 0
    error('No image in the directory');
end

for i=1:NumImages
    imgname = [imPath '\' filearray(i).name]; % get image name
    originalImage = imread(imgname); % load image
    grayImage = rgb2gray(originalImage);
    imwrite(grayImage, [gray_imPath '\' filearray(i).name]);
end

disp(' ... OK!');
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值