How dispaly .mat as an image, then save it, then crop the saved image from the center ?

2 次查看(过去 30 天)
-looking to dispaly multi (.mat) files as images
-save or crop the center then save the cropped images it
  7 个评论
Image Analyst
Image Analyst 2020-11-24
If the help in my Answer below did not work for you, then attach one of the mat files with the paperclip icon and tell us what part of the center you want to save and what you'd like the output filename to be.

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2020-11-24
Use the FAQ to read in a sequence of lots of .mat files.
In the loop, get your image from the mat file then crop it. Here's a start.
% Specify the folder where the files live.
myFolder = pwd; % or wherever, like 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.mat'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
s = load(fullFileName);
% See if this structure has a field called myImage or whatever it's called in your programs.
if isfield(s, 'myImage')
imageArray = imread(fullFileName);
imageArray = imageArray(row1:row2, col1:col2, :);
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
fprintf('Displaying myImage from %s\n', baseFileName);
else
fprintf(' myImage not found in %s\n', baseFileName);
end
end
Of course you need to assign row1, row2, col1, and col2 according to how you'd like to do the cropping.
  5 个评论
Image Analyst
Image Analyst 2020-11-25
My images did not have a white frame. Are you sure you used imwrite() and not saveas(), print(), or exportgraphics()?

请先登录,再进行评论。

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by