How to convert multiple rgb snapshots into grayscale and save them into a folder?

I've taken multiple rgb snapshots and saved them into the folder using for-loop. But now I need to know how those snapshots can be converted into grayscale and save them into a folder.
After the above has been done, I need to know how both of these (rgb and gray) can be subtracted so that the difference is spotted.
Please help. thank you very much in advance.

 Accepted Answer

Try this:
grayImage = rgb2gray(rgbImage);
diffGray = double(grayImage) - double(grayReferenceImage);
diffRGB = double(rgbImage) - double(rgbReferenceImage);
if max(abs(diffGray(:))) > 0
% There is at least one different pixel.
end

13 Comments

Hi sir,
Could you explain the above answer please.
% Turn color RGB image into a grayscale image.
grayImage = rgb2gray(rgbImage);
% Subtract this gray scale image from some reference image.
% You have to define some reference image in advance.
% Only you know what you want to subtract, not me.
diffGray = double(grayImage) - double(grayReferenceImage);
% Subtract this color image from some color reference image.
% Same as before but just using the color image instead of the gray scale one.
diffRGB = double(rgbImage) - double(rgbReferenceImage);
% If there is a difference, there will be a non-zero value.
% Check for that.
if max(abs(diffGray(:))) > 0
% There is at least one different pixel.
% Now do whatever you want to or need to do knowing this fact.
% I have no idea what you want to do if there is a difference.
% You can do the same thing for diffRGB.
end
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.
thank you very much in advance.
That's because the cd comes after. A much much better way is to use fullfile:
colorFolder = pwd;
grayscaleFolder = fullfile(pwd, 'grayImage');
if exist(grayscaleFolder, 'dir')
mkdir(grayscaleFolder);
end
for frameNumber = 1 : 5
rgbImage = getsnapshot(cam);
% Save color image in its folder.
baseFileName = sprintf('%d.jpg', frameNumber);
fullFileName = fullfile(colorFolder, baseFileName);
imwrite(a, fullFileName);
grayImage = rgb2gray(rgbImage);
% Save gray scale image in its folder.
fullFileName = fullfile(grayscaleFolder, baseFileName);
imwrite(grayImage, fullFileName);
end
In the above coding, may I know what does variable a has to do with. I replaced it with rgbImage and I got it running smoothly. Please advice me If I have replaced with a correct one. And also the above coding perfectly writes the gray images into grayImage folder but fails to write the rgb images into its corresponding folder named rgbImage but another instead. (Matlab's default folder under Documents).
Somehow it writes both the image types onto disk.
thank you very much sir and May God Bless you for your in-depth knowledge and sharing personality. Factually, your way of answering is a bit too detailed and takes time for me to understand... but once understood it gives me a broad idea as to how things work.
I' am a newbie here for this Matlab answers tool and hence I happened to generate two similar questions. I shouldn't have done that. What I discovered was the answers provided to me by both questions were of a different style and I equally liked both. Because the one answered in the other question was short and your one was long and interesting as well. Therefore it took sometime to learn and implement your coding. And that's why it took little time to accept your answer. Thank you so much.
"a" is a color image from your camera. It was such a bad name that I renamed it rgbImage. Your original code was saving the color image into the current folder, so that's what I left it doing. If you want, you can create a "color images" folder off of the current folder, or at any other specific location that is allowed. For example, you could do
colorFolder = 'c:/users/omar/documents/color images';
if exist(colorFolder, 'dir')
mkdir(colorFolder);
end
It writes both images to disk because I call imwrite() twice , with a different folder and basefilename specified each time.
I'm glad you find my descriptions helpful, even though sometime, to describe things thoroughly, it can take a lot of words. Thanks for accepting the answer (it gives us answerers "reputation points").
Sir, I attempted to view grayscale image differences using your code.
%the difference and saving them
diffGray = double ('2.jpg') - double ('1.jpg');
Could you help me show the difference. I tried to use imshow ('diffGray'); but it doesn't show what was expected, instead a thick line like. I' am afraid I' am using the wrong function or could you help me identify how I see the actual difference. And how can I save those differences into a folder. plz help
That's not my code. My code passed in variables that were numerical arrays, not strings. You need to pass the filename strings into imread() to get the arrays of pixel values
grayImage1 = imread('1.jpg');
grayReferenceImage = imread('2.jpg');
diffGray = double(grayImage) - double(grayReferenceImage);
thank you. How can I save the difference by converting them to binary?
What do you mean by binary? Logical, like true/false? Or binary like everything in a computer, which of course is all binary? Or do you mean a string like if you converted an integer to a binary representation of the decimal number using dec2bin()? Or like I said in your other question, use save().
My supervisor said, converting them to Binary would transform the difference image into Black and white. And We can see the difference in black and white rather than grayscale. May I also know in which format does it view at present. When I input imshow (diffGray); it shows the difference but in which format do we see it. (I' am referring to the color format.
So to make the question short and simple. I want the result of diffGray converted into Binary and save it.By this I must be able to view in Black and White. Please help. This could be my last question. You (image analyst and Mohammed Abouali) helped me alot.
OK, you want to binarize the difference signal by thresholding it and turning it into a logical array. So you can do something like
binaryImage = diffRGB > someValue; % Like 2 or 5 or whatever
Pick a value that you want to see if the difference is greater than. Like if the difference between the images is more than 3 gray levels, show it, otherwise consider them the same with essentially no difference at all.

Sign in to comment.

More Answers (0)

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!