how to save the image as a RGB image???

22 views (last 30 days)
This is the code for median filtering on RGB images. I wanted to save only the resorted image into a new folder. how do i do that?? can anyone help me out to save that image..
myFolder='E:\MRP\accuracy\class1';
m=input('Type the Number of Images to Process:');
for k = 1:m
jpgFilename = sprintf('%d.jpg', k);
fullFileName = fullfile(myFolder, jpgFilename);
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
figure,imshow(rgbImage);
title('Original color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Display the individual red, green, and blue color channels.
figure,imshow(redChannel);
title('Red Channel', 'FontSize', fontSize);
figure,imshow(greenChannel);
title('Green Channel', 'FontSize', fontSize);
figure,imshow(blueChannel);
title('Blue Channel', 'FontSize', fontSize);
% Generate a noisy image. This has salt and pepper noise independently on
% each color channel so the noise may be colored.
noisyRGB = imnoise(rgbImage,'salt & pepper', 0.05);
figure,imshow(noisyRGB);
title('Image with Salt and Pepper Noise', 'FontSize', fontSize);
% Extract the individual red, green, and blue color channels.
redChannel = noisyRGB(:, :, 1);
greenChannel = noisyRGB(:, :, 2);
blueChannel = noisyRGB(:, :, 3);
% Display the noisy channel images.
figure,imshow(redChannel);
title('Noisy Red Channel', 'FontSize', fontSize);
figure,imshow(greenChannel);
title('Noisy Green Channel', 'FontSize', fontSize);
figure,imshow(blueChannel);
title('Noisy Blue Channel', 'FontSize', fontSize);
% Median Filter the channels:
redMF = medfilt2(redChannel, [3 3]);
greenMF = medfilt2(greenChannel, [3 3]);
blueMF = medfilt2(blueChannel, [3 3]);
% Find the noise in the red.
noiseImage = (redChannel == 0 | redChannel == 255);
% Get rid of the noise in the red by replacing with median.
noiseFreeRed = redChannel;
noiseFreeRed(noiseImage) = redMF(noiseImage);
% Find the noise in the green.
noiseImage = (greenChannel == 0 | greenChannel == 255);
% Get rid of the noise in the green by replacing with median.
noiseFreeGreen = greenChannel;
noiseFreeGreen(noiseImage) = greenMF(noiseImage);
% Find the noise in the blue.
noiseImage = (blueChannel == 0 | blueChannel == 255);
% Get rid of the noise in the blue by replacing with median.
noiseFreeBlue = blueChannel;
noiseFreeBlue(noiseImage) = blueMF(noiseImage);
% Reconstruct the noise free RGB image
rgbFixed = cat(3, noiseFreeRed, noiseFreeGreen, noiseFreeBlue);
figure,imshow(rgbFixed);
title('Restored Image', 'FontSize', fontSize);
end

Accepted Answer

Image Analyst
Image Analyst on 2 Mar 2013
Edited: Image Analyst on 2 Mar 2013
fullFileName = fullfile(yourFolder, 'fixed image.PNG');
imwrite(rgbFixed, fullFileName);
yourFolder is whatever folder you want to store your images in. It is a character string. For example
yourFolder = 'E:\MRP\accuracy\class2';
or
yourFolder = 'E:\MRP\accuracy\class1\output';
if ~exist(yourFolder, 'dir');
mkdir(yourFolder);
end
  8 Comments
sudha
sudha on 4 Mar 2013
This is the code for saving..i wanted to save in "output" folder like 1,2..etc. ya, i hve put tis code inside the "for k" loop.
yourFolder = 'E:\MRP\accuracy\class1\output';
if ~exist(yourFolder, 'dir');
mkdir(yourFolder);
end
fullFileName = fullfile(yourFolder, '1.JPG');
imwrite(rgbFixed, fullFileName);
Image Analyst
Image Analyst on 4 Mar 2013
You haven't looked at the FAQ, http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F, yet, have you? It shows you how you can use sprintf() to create the base filename.

Sign in to comment.

More Answers (2)

Youssef  Khmou
Youssef Khmou on 2 Mar 2013
hi sudha,
finish your code by :
filename='E:\MRP\accuracy\class1\rgbFixed.jpg' % as jpg file
% or specifiy the directory you want 3
imwrite(rgbFixed,filename);
  4 Comments
sudha
sudha on 2 Mar 2013
this is the error m getting
??? Undefined function or variable 'rgbFixed'.
Error in ==> Untitled4 at 14 imwrite(rgbFixed,filename);
Walter Roberson
Walter Roberson on 2 Mar 2013
Your posted code has
% Reconstruct the noise free RGB image
rgbFixed = cat(3, noiseFreeRed, noiseFreeGreen, noiseFreeBlue);
That is the code that defines the rgbFixed variable, which is the variable that holds the restored image.
That code is, though, far past line 14, which suggests you are now trying to write out something other than the restored image that you asked for assistance with. If you are going to try to do something other than what you asked for instructions on, you are going to need to learn how to adapt code examples instead of expecting that they will work exactly as given by the volunteers.

Sign in to comment.


nkumar
nkumar on 2 Mar 2013
Edited: nkumar on 2 Mar 2013
paste the following lines at starting of your code
pickind='jpg';
f1=fullfile('C:','framesnew');
if (exist(f1) == 0)
mkdir (f1);
end
paste the following line at last line before end statement
strtemp=strcat('C:\framesnew\',int2str(k),'.',pickind);
imwrite(rgbFixed,strtemp);

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!