Problem in imwrite command
2 次查看(过去 30 天)
显示 更早的评论
I am stuck in a piece of code. Sometimes it works absolutely fine and sometimes the same code shows error. Let me explain the entire work I am doing. I have stored the LAP ISOGD dataset in a folder. In this dataset there is a folder named 'Train' which contains 180 folders. Each of these 180 folders contain depth and RGB videos. First half is the depth videos and second half contains RGB videos. I have to access these RGB videos one by one, then do some processing on each frame of all the RGB videos contained in all these 180 folders. While writing the code, I am able to access them all, and after accessing and extracting the frames I am trying to save all those frames in the similar way as it had been saved in the dataset.
eg: Suppose I accessed video 'train/001/M_00001.avi'. then after extracting the frames I want to write and save the frames in a folder named 'Navneet1/001/M_00001/...'.
I am attaching here the piece of code I am working on. And also the screenshot of error I am getting.
clc;
clear all;
close all;
Folder='E:\PhD ppts and papers\datasets\ISOGD\train';
%accessing the basic folder
s=dir(Folder);
% making a new parent folder in which the extracted frames will be saved.
mkdir Navneet1
% Using the for loop to access the 180 folders conatined in folder 'train'.
% first 2 elemnts of s are '.' and '..' so m starts from 3, as the third
% element contains the first folder 001 and 182nd element contains 180th
% folder.
for m=3:length(s)
% extracting the entity 'name' from the structure 's' formed in line no. 6
T=s(m).name;
% giving path to all 180 folders one-by-one
Folder1=fullfile(Folder, T);
%accessing each of these 180 folders one-by-one
s1=dir(Folder1);
% first 2 elemnts of s1 are '.' and '..' and 1st half of videos are depth videos,
% so n starts from (length(s1)/2)+2) and ends at length(s1)
for n=((length(s1)/2)+2):length(s1)
% extracting the entity 'name' from the structure 's1' formed in
% line no. 19
t1=s1(n).name;
%giving path to read videos one-by-one i.e. all RGB videos from
%folder 001 and then all RGB videos from folder 002 and so on till
%last RGB video of 180th folder
vid=VideoReader(fullfile(Folder1, t1));
%extracting number of frames.
numframe=vid.NumberOfFrames;
% giving path of subfolders under the parent folder 'Navneet1' to save
%extracted frames
a=fullfile(T);
b=fullfile(t1);
%this for loop is to write the frames in the specified folder
for iFrame = 1:1:numframe
frames = read(vid, iFrame);
imwrite(frames, fullfile('Navneet1',a,b,sprintf('%06d.jpg', iFrame)));
end
end
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/365374/image.png)
In case of any query regarding the clarification of problem, feel free to comment. Any suggestion regarding my task and problem is wholeheartedly welcome.
3 个评论
VBBV
2020-9-24
编辑:VBBV
2020-9-24
Use imwrite as
% if true
% code
% end
imwrite(frames,a,b,sprintf('%06d.jpg',iFrame),'jpg')% You don't need to use fullfile again inside imwrite and specify the format type as 'jpg' inside imwrite
if exist('folder\filename.jpg', 'file') delete('folder\filename.jpg')
end
% use the above to check if the folder already has the filenames it is trying to write
采纳的回答
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!