0% found this document useful (0 votes)
3 views2 pages

problem-2

The document outlines a MATLAB script for processing images in a specified folder, extracting properties such as dimensions and channels, and saving both grayscale and individual RGB channel images to an output folder. It also generates a text file with image details, saves the properties in a MAT file, and converts the data to a CSV format. The script includes error handling for file saving and displays progress during processing.

Uploaded by

Tasneem Nadira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

problem-2

The document outlines a MATLAB script for processing images in a specified folder, extracting properties such as dimensions and channels, and saving both grayscale and individual RGB channel images to an output folder. It also generates a text file with image details, saves the properties in a MAT file, and converts the data to a CSV format. The script includes error handling for file saving and displays progress during processing.

Uploaded by

Tasneem Nadira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

% Specify folder containing images

imageFolder = '/MATLAB Drive/images'; % Folder containing the input images


outputFolder = '/MATLAB Drive/Output'; % Folder to save processed images

% Create output folder if it doesn't exist


if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end

% Get list of all .jpg files in the folder


imageFiles = dir(fullfile(imageFolder, '*.jpg')); % Change to '*.png', '*.bmp',
etc., if needed
numImages = length(imageFiles);

% Initialize structure array to store results


imageProperties = struct('Name', {}, 'Height', {}, 'Width', {}, 'Channels', {},
'FileInfo', {});

% Open a text file to write image information


txtFilePath = fullfile(outputFolder, 'ImageProperties.txt');
fileID = fopen(txtFilePath, 'w');

% Process each image


for i = 1:numImages
% Get the full path of the image
imagePath = fullfile(imageFolder, imageFiles(i).name);

% Read the image


img = imread(imagePath);

% Get image information


imageInfo = imfinfo(imagePath);

% Extract image dimensions


[height, width, numChannels] = size(img);

% Extract RGB channels (if applicable)


if numChannels == 3
redChannel = img(:,:,1);
greenChannel = img(:,:,2);
blueChannel = img(:,:,3);
else
redChannel = img; % For grayscale images
greenChannel = [];
blueChannel = [];
end

% Convert image to grayscale and save


grayImage = rgb2gray(img);
grayImagePath = fullfile(outputFolder, ['Gray_' imageFiles(i).name]);
imwrite(grayImage, grayImagePath);

% Save blue channel image (if applicable)


if ~isempty(blueChannel)
blueChannelImagePath = fullfile(outputFolder, ['BlueChannel_'
imageFiles(i).name]);
imwrite(blueChannel, blueChannelImagePath);
end
% Save red and green channel images
if ~isempty(redChannel)
redChannelImagePath = fullfile(outputFolder, ['RedChannel_'
imageFiles(i).name]);
imwrite(redChannel, redChannelImagePath);
end

if ~isempty(greenChannel)
greenChannelImagePath = fullfile(outputFolder, ['GreenChannel_'
imageFiles(i).name]);
imwrite(greenChannel, greenChannelImagePath);
end

% Display progress (optional)


fprintf('Processing image %d/%d: %s\n', i, numImages, imageFiles(i).name);

% Write image information to text file


fprintf(fileID, 'Image %d: %s\n', i, imageFiles(i).name);
fprintf(fileID, 'Dimensions: %d x %d\n', height, width);
fprintf(fileID, 'Channels: %d\n', numChannels);
fprintf(fileID, 'File Info:\n');
fprintf(fileID, '%s\n', evalc('disp(imageInfo)'));
fprintf(fileID, '-----------------------\n');

% Store properties into the structure array


imageProperties(i).Name = imageFiles(i).name;
imageProperties(i).Height = height;
imageProperties(i).Width = width;
imageProperties(i).Channels = numChannels;
imageProperties(i).FileInfo = imageInfo;
end

% Close the text file


fclose(fileID);

% Save results to a MAT file


matFilePath = fullfile(outputFolder, 'ImageProperties.mat');
save(matFilePath, 'imageProperties');
if exist(matFilePath, 'file')
fprintf('MAT file saved successfully: %s\n', matFilePath);
else
warning('Failed to save MAT file: %s\n', matFilePath);
end

% Convert structure array to table and write to CSV file


csvFilePath = fullfile(outputFolder, 'ImageProperties.csv');
propertiesTable = struct2table(imageProperties);
writetable(propertiesTable, csvFilePath);
if exist(csvFilePath, 'file')
fprintf('CSV file saved successfully: %s\n', csvFilePath);
else
warning('Failed to save CSV file: %s\n', csvFilePath);
end

disp('Processing complete!');

You might also like