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

6 ビュー (過去 30 日間)
Omar Ahmad
Omar Ahmad 2014 年 11 月 24 日
コメント済み: Image Analyst 2015 年 10 月 28 日
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 件のコメント
Image Analyst
Image Analyst 2014 年 11 月 26 日
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.
Omar Ahmad
Omar Ahmad 2014 年 11 月 26 日
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.

サインインしてコメントする。

採用された回答

Mohammad Abouali
Mohammad Abouali 2014 年 11 月 25 日
編集済み: Mohammad Abouali 2014 年 11 月 25 日
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 件のコメント
rohit ranjan
rohit ranjan 2015 年 10 月 28 日
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
Image Analyst 2015 年 10 月 28 日
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});

サインインしてコメントする。

その他の回答 (1 件)

Reeve
Reeve 2014 年 11 月 24 日
Hi Omar,
You just need to add:
a = rgb2gray(a);
after your getsnapshot line.
  2 件のコメント
Omar Ahmad
Omar Ahmad 2014 年 11 月 25 日
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.
Omar Ahmad
Omar Ahmad 2014 年 11 月 25 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by