how to convert a series of RGB images into grayscale and save them?

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

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.
yes, I did because I quite wasn't familiar with this website. I' am sorry for that. I described more about the situation in the link that you've posted above. I didn't overlook your advice instead it was good and detailed therefore it took a while for me to learn and adopt. Please accept my limited knowledge. thank you both of you.
I would like to make a special request: Please do not delete this or the other thread claiming both are of the same type. Even though the question is the same the answers are interestingly awesome because they are in two different techniques.

Sign in to comment.

 Accepted Answer

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

In the second imwrite function you used above, I quite didn't get what it meant. I' am referring to the word 'grayImage'. Neither did Matlab recognize it and returned an error saying "Unable to determine the file format from the filename". Then I replaced it as 'gray_%d.jpg' and it worked but still with some warnings.
Could you elaborate on that please.
However you almost brought me to a solution and Thank you very much for that sir. May Allah bless you.
I noticed from your code that you are storing the RGB images in the current directory but it appeared that you are storing the gray scale images in a subfolder called "grayImage", (you had "cd grayImage" somewhere in one of your posts).
sprintf(['grayImage' filesep '%d.jpg'],i)
The above code pretty much generates filename in that folder. if i=1 we get "grayImage/1.jpg" on linux/Mac systems, and "grayImage\1.jpg" on windows systems.
Of course if grayImage folder does not exists that would generate an error. So make that folder first. You can get around the error that you currently get by changing the imwrite code to:
imwrite (rgb2gray(a),sprintf(['grayImage' filesep '%d.jpg'],i),'jpg');
In this method, you are explicitly defining the format of the image, i.e. jpg.
changing the sprintf part of the code to
sprintf('gray_%d.jpg',i)
is perfectly ok, as you have already found out. This all depends where and how you want to store your gray images. So, that depends on your choices.
I agree, and I gave him more robust code for that here in his duplicate question so not sure why he didn't take the advice.
I see. Now he has two answers. Yours is indeed with much more detail.
Sir Mohammed Abouali, the 'grayImage' wasn't a subfolder but another folder outside 'rgbImage' folder. How do I get back there and save them accordingly. thank you sir.
let me recall my code:
for i=1:5
a=getsnapshot (cam);
imwrite (a,sprintf('%d.jpg',i));
cd ../ %In here I' am going out of rgbImage folder
cd grayImage %And here I'am trying to write the gray level into this directory
a=rgb2gray(a);
imwrite (a,sprintf('%d.jpg',i));
end
The easiest seems to be
filename=fullfile('../grayImage',sprintf('%d.jpg',i));
and then
imwrite(rgb2gray(a),filename,'jpg');
also have a look at imageAnalyst post. He has answered you in more detail regarding this file path issue.
Hi Sir, Have you got an idea as to how gray images can be subtracted.
With the help of Image Analyst I was able to find one technique. And that was similar to the below code:
diffGray= double ('2.jpg')-('1.jpg'); %%Sorry, I may have misunderstood Image Analyst.. he says this isn't his code.
Have you got other techniques plus how do I view the difference after the above code has been input. I used imshow (diffGray); but didn't work. Please do also help me to save the image with difference, into a folder. thank you very much.
I think you need to first load them:
so
g1=imread('1.jpg');
g2=imread('2.jpg');
diffGray=double(g2)-double(g1);
You can't directly store this as image. You need to reseale it to get rid off negative numbers, or you can store it as Tiff image with floating point so that it supports negative numbers.
imsubtract may be also become useful.
I do not want to save neither in Jpg nor Tiff. I want the difference converted into Binary format and then save it. Please help. thanks
Use save() to save it in a proprietary binary Mathworks format that can handle negative numbers. Most other programs can't understand that format though. But if you're just using MATLAB, this will be fine.
My last requirement is this: I want to save the result of diffGray into Binary? How can I? please help me at this. thank you very much.
From your other question where we're discussing this (why are we discussing this in two places?????) just threshold
binaryImage = diffImage > someValue;
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 ?
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});

Sign in to comment.

More Answers (1)

Hi Omar,
You just need to add:
a = rgb2gray(a);
after your getsnapshot line.

2 Comments

thank you Reeve for this one. But my question was as to how can the already captured snapshots using the above for loop code can be converted into grayscale images so that I can calculate the difference between the similar.
I somehow managed to write a code which takes multiple snapshots and saves them into a file. It also converts into gray at the same time. the code is as follows:
//The rgb images are saved into a folder called rgbImages.
for i=1:5
a=getsnapshot (cam);
imwrite (a,sprintf('%d.jpg',i));
cd ../
cd grayImage
a=rgb2gray(a);
imwrite (a,sprintf('%d.jpg',i));
end
The problem here is that the rgb images are correctly captured and are saved into the directory but only the 1st rgb image gets converted into grayscale and is saved into grayImages folder. Could you please help me to get all of those rgb images get converted and saved.
In the other hand, when I put your answer (Reeve) I' am only getting the grayscale images into the correct folder but when I look inside rgb file only the 1st rgb is saved which is the opposite to my first case.
Please give me an answer (code) which will capture and save the same images correspondingly as rgb and grayscale format.
thank you very much in advance.

Sign in to comment.

Categories

Find more on Convert Image Type 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!