Saving .png images in directory

조회 수: 8 (최근 30일)
Hassan Ashraf
Hassan Ashraf 2019년 4월 28일
댓글: Hassan Ashraf 2019년 4월 29일
Greetings Everyone!
I am encountering a problem while saving .png files in a loop. I have multiple .png images with 256x256 unit8 dimesnions. I am combining those images to form a single image by using below code. But after saving the file the file has ne information of its height and width in its properties. Due to which I am unable to process those images further. Can anybody help me out?
%Specify training and test folders
train_fold = ['..',filesep,'Train_Set',filesep];
test_fold = ['..',filesep,'Test_Set',filesep];
%Read train and test set images folder information
train_ims = dir(train_fold);
test_ims = dir(test_fold);
for y=1:1:3
%read 2nd training image's all masks
im_selected = 1;
train_ims_masks = dir([train_fold,train_ims(y).name,filesep,'masks',filesep]);
%read 2nd training image's all masks in sequence
for i = 1:length(train_ims_masks)
im{i} = imread([train_fold,train_ims(y).name,filesep,'masks',filesep,train_ims_masks(i).name]);
end
% Making new image by combining multiple images
compositeImage = im{1};
for k=2:length(train_ims_masks)
compositeImage = compositeImage + im{k};
end
% saving new image into directory
save (['compositeImage' num2str(y), '.png']);
dir(train_fold);
end
figure
image(compositeImage)
colormap(gray)
111.png

채택된 답변

Guillaume
Guillaume 2019년 4월 28일
The counterpart to imread is imwrite, not save. save as you've used it saves all the workspace variable in a mat file (irrespective of the extension you use).
Replace your save by:
imwrite(compositeImage, sprintf('CompsiteImage%d.png', y));
Note that I'm using sprintf to build the filename instead of string concatenation as you'd done. In my opinion, it's more readable. If you're going to use string concatenation, at the very least be consistent in your use of separator. You used a space to separate 'compositeImage' and num2str(y) and a comma to separate num2str(y) and '.png'.
  댓글 수: 1
Hassan Ashraf
Hassan Ashraf 2019년 4월 29일
Thank you Guillaume :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by