how to convert a series of RGB images into grayscale and save them?
6 views (last 30 days)
Show older comments
Omar Ahmad
on 24 Nov 2014
Commented: Image Analyst
on 28 Oct 2015
I have taken snapshots using my webcam and have saved those snaps in rgb type. Now I need to convert those rgb images into gray. Please help.
I have used the following code to save: and it is better if the solution is also given using for loop.
for i=1:10
a=getsnapshot (cam);
imwrite (a,sprintf('%d.jpg',i);
end
Thank you.
2 Comments
Image Analyst
on 26 Nov 2014
Isn't this question totally contained in the question you already asked and I already answered: http://www.mathworks.com/matlabcentral/answers/163909#comment_251947? You asked the same thing plus how to subtract some image from the gray and RGB image, and I gave code for that there.
Accepted Answer
Mohammad Abouali
on 25 Nov 2014
Edited: Mohammad Abouali
on 25 Nov 2014
Actually you are only saving the last snap shot as grayscale. Change your code to this:
for i=1:10
a=getsnapshot (cam);
a=rgb2gray(a);
imwrite (a,sprintf('%d.jpg',i));
end
if you want both rgb and gray scale to be stored you can do this:
for i=1:10
a=getsnapshot (cam);
imwrite (a,sprintf('%d.jpg',i));
imwrite (rgb2gray(a),sprintf(['grayImage' filesep '%d.jpg'],i));
end
14 Comments
rohit ranjan
on 28 Oct 2015
sir my code is
clear all; close all; clc;
path='E:\OLD_MYDOCUMENT\prabhu\matcodes\'; list=dir([path, '*.bmp']); for x=1:length(list) images{x}=imread([path, list(x).name]); figure(x), imshow(images{x}); end
after this i have to convert to gray image. how to do ?
Image Analyst
on 28 Oct 2015
I'm not sure why you're storing/saving all of them in a cell array - you're likely to run out of memory. If you don't need the images after the loop, don't save them. Just do this and then use it without saving:
grayImage = rgb2gray(images{x});
More Answers (1)
Reeve
on 24 Nov 2014
Hi Omar,
You just need to add:
a = rgb2gray(a);
after your getsnapshot line.
2 Comments
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!