Image processing code Help

3 Ansichten (letzte 30 Tage)
Anas Bilal
Anas Bilal am 16 Apr. 2020
Kommentiert: Anas Bilal am 17 Apr. 2020
NEED help to use imread for 20 images together. This code can work on 1 image at a time i want to use this code for all 20 images at once and want to save all images in one new folder after processing .Please can anyone help me to modify this code.
RGB=imread('1.jpg');
% input 768*576 pixel uint8 type true color image.
R=RGB(:,:,1);
% red surface image extraction
G=RGB(:,:,2);
B=RGB(:,:,3);
figure;
imshow(RGB)
title('original image');
% create a new empty graphics window
% view images
figure,
imshow(R)
title('red face version');
figure,
imshow(G)
title('green face version');
figure,
imshow(B)
  3 Kommentare
Anas Bilal
Anas Bilal am 16 Apr. 2020
all are images are in same golder. green red an dblue att want to save. names can img1 red, img2 green, img3 blue,....till 20. Images can save in "D:\First"
Anas Bilal
Anas Bilal am 16 Apr. 2020
Image type is .jpg

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 16 Apr. 2020
Bearbeitet: Ameer Hamza am 16 Apr. 2020
Here is a general structure of the code
files = dir('*.jpg'); % get name of all files with jpg extension
for i=1:numel(files) % run the for loop over all images
name = files(i).name; % get name of i-th image file
img = imread(name); % load the image
R=RGB(:,:,1);
% red surface image extraction
G=RGB(:,:,2);
B=RGB(:,:,3);
imwrite(R, ['img' num2str(i) ' red.jpg']);
imwrite(G, ['img' num2str(i) ' green.jpg']);
imwrite(B, ['img' num2str(i) ' blue.jpg']);
end
  20 Kommentare
Image Analyst
Image Analyst am 17 Apr. 2020
Anas: I've corrected your code. This works. Just uncomment the imwrite() lines:
folder = pwd; % Whever you want...
filePattern = fullfile(folder, '*.ppm');
files = dir(filePattern); % get name of all files with jpg extension
if numel(files) == 0
warningMessage = sprintf('No files found in folder\n%s.\n', folder);
fprintf('%s\n', warningMessage);
beep;
uiwait(errordlg(warningMessage));
return;
end
numberOfImageFiles = length(files)
for k = 1 : numberOfImageFiles % run the for loop over all images
baseFileName = files(k).name; % get name of i-th image file
fullFileName = fullfile(folder, baseFileName);
fprintf('Reading in file %d of %d : %s\n', k, numberOfImageFiles, fullFileName);
img = imread(fullFileName); % load the image
subplot(1, 2, 1);
imshow(img);
caption = sprintf('Original Image :\n%s', baseFileName);
title(caption, 'FontSize', 15, 'Interpreter', 'none');
[rows, columns, numberOfColorChannels] = size(img);
if numberOfColorChannels == 3
R = img(:, :, 1); % Red channel image extraction
G = img(:, :, 2);
B = img(:, :, 3);
% Write out RGB image as TIFF format.
outputBaseFileName = sprintf('img %d RGB.tif', k);
outputFullFileName = fullfile(folder, outputBaseFileName);
fprintf(' Writing file %s\n', outputFullFileName);
% imwrite(R, outputFullFileName);
% Write out red image as TIFF format.
outputBaseFileName = sprintf('img %d red.tif', k);
outputFullFileName = fullfile(folder, outputBaseFileName);
fprintf(' Writing file %s\n', outputFullFileName);
% imwrite(R, outputFullFileName);
% Write out green image as TIFF format.
outputBaseFileName = sprintf('img %d green.tif', k);
outputFullFileName = fullfile(folder, outputBaseFileName);
fprintf(' Writing file %s\n', outputFullFileName);
% imwrite(R, outputFullFileName);
% Write out blue image as TIFF format.
outputBaseFileName = sprintf('img %d blue.tif', k);
outputFullFileName = fullfile(folder, outputBaseFileName);
fprintf(' Writing file %s\n', outputFullFileName);
% imwrite(R, outputFullFileName);
else
% img is already a gray scale image.
G = img;
end
% Histogram equivilzation of Green Channel
b=adapthisteq(G);
% Two times limited contrast adaptive direct equalization
ba=adapthisteq(b);
% adjusts contrast in grayscale images.
subplot(1, 2, 2);
imshow(ba)
title('Enhanced contrast', 'FontSize', 15, 'Interpreter', 'none');
outputBaseFileName = sprintf('img %d enhanced.tif', k);
outputFullFileName = fullfile(folder, outputBaseFileName);
fprintf(' Writing file %s\n', outputFullFileName);
% imwrite(ba, outputFullFileName);
drawnow;
pause(0.5);
end
Anas Bilal
Anas Bilal am 17 Apr. 2020
Great work thank you very much .

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by