我有RGB图像,深度图像和OpenPose图像(从RGB图像生成)。RGB和深度图像的数量是不同的,所以我必须找到确切的RGB图像对应于哪个深度图像。这是使用我对RGB和深度图像的时间戳值来完成的。
这里的问题是,我编写的这段代码大约需要7-10分钟才能执行,我觉得这需要花费很多时间。怎样才能缩短同样的时间?这里主要写的是花了很多时间。
function image_sorting(color_img, depth_img, Openpose, selected_color, selected_depth)
filePattern = fullfile(depth_img, '*.timestamp');
file = dir(filePattern);
filePattern2 = fullfile(color_img, '*.timestamp');
file2 = dir(filePattern2);
filePattern3 = fullfile(depth_img, '*.bmp');
file3 = dir(filePattern3);
filePattern4 = fullfile(Openpose, '*.png');
file4 = dir(filePattern4);
for k = 1:length(file)
depthTimestampBase = file(k).name;
depthTimestamp = fullfile(depth_img, depthTimestampBase);
fileID = fopen(depthTimestamp,'r');
% format longG
A(k,:) = textscan(fileID,'%d64') ;
fclose(fileID);
end
for m = 1:length(file2)
rgbTimestampBase = file2(m).name;
rgbTimestamp = fullfile(color_img, rgbTimestampBase);
fileID2 = fopen(rgbTimestamp,'r');
%format longG
B(m,:) = textscan(fileID2,'%d64') ;
fclose(fileID2);
end
%%%%%% Here there are two parts. Use any of them according to conditions
if length(file2) <= length(file)
%PART 1:- If RGB images are less than depth then use the following code
for m = 1:length(file2)
for k = 1:length(file)
C{k,m} = A{k,1} - B{m,1};
if C{k,m}<0
C{k,m} = -C{k,m};
end
end
end
[V,X] = min(cell2mat(C),[],1); % Is a row vector containing the minimum value of columns
% V gives minimum value and X gives Index
m = 1;
for w= X
depthDataBase = file3(w).name;
rgbDataBase = file4(m).name;
depthData = fullfile(depth_img, depthDataBase);
rgbData = fullfile(Openpose, rgbDataBase);
imageArrayy = imread(depthData);
imageArrayy2 = imread(rgbData);
depthData2 = fullfile(selected_depth, depthDataBase);
imwrite(imageArrayy, depthData2);
rgbData2 = fullfile(selected_color, rgbDataBase);
imwrite(imageArrayy2, rgbData2);
m = m+1; % for part 2 m = m+1
end
%
% PART 2:- If RGB images are more than depth then use the following code
else
for m = 1:length(file2)
for k = 1:length(file)
C{m,k} = A{k,1} - B{m,1};
if C{m,k}<0
C{m,k} = -C{m,k};
end
end
end
[V,X] = min(cell2mat(C),[],1); % Is a row vector containing the minimum value of columns
% V gives minimum value and X gives Index
w = 1;
for m= X
depthDataBase = file3(w).name;
rgbDataBase = file4(m).name;
depthData = fullfile(depth_img, depthDataBase);
rgbData = fullfile(Openpose, rgbDataBase);
imageArrayy = imread(depthData);
imageArrayy2 = imread(rgbData);
depthData2 = fullfile(selected_depth, depthDataBase);
imwrite(imageArrayy, depthData2);
rgbData2 = fullfile(selected_color, rgbDataBase);
imwrite(imageArrayy2, rgbData2);
w = w+1;
end
end更新1:-概要文件执行时间的屏幕截图。

发布于 2020-01-06 16:51:26
按照Cris的建议,对路径进行了更改,并使用了复制文件的提示,现在大约需要21秒。这是解决办法
function image_sorting(color_img, depth_img, Openpose, selected_color, selected_depth)
file = dir(fullfile(depth_img, '*.timestamp'));
file2 = dir(fullfile(color_img, '*.timestamp'));
file3 = dir(fullfile(depth_img, '*.bmp'));
file4 = dir(fullfile(Openpose, '*.png'));
for k = 1:length(file)
fileID = fopen(fullfile(depth_img, file(k).name),'r');
% format longG
A(k,:) = textscan(fileID,'%d64') ;
fclose(fileID);
end
for m = 1:length(file2)
fileID2 = fopen(fullfile(color_img, file2(m).name),'r');
%format longG
B(m,:) = textscan(fileID2,'%d64') ;
fclose(fileID2);
end
%%%%%% Here there are two parts. Use any of them according to conditions
if length(file2) <= length(file)
%PART 1:- If RGB images are less than depth then use the following code
for m = 1:length(file2)
for k = 1:length(file)
C{k,m} = A{k,1} - B{m,1};
if C{k,m}<0
C{k,m} = -C{k,m};
end
end
end
[V,X] = min(cell2mat(C),[],1); % Is a row vector containing the minimum value of columns
% V gives minimum value and X gives Index
m = 1;
copyfile(Openpose, selected_color)
for w= X
depthDataBase = file3(w).name;
%rgbDataBase = file4(m).name;
depthData = fullfile(depth_img, depthDataBase);
%rgbData = fullfile(Openpose, rgbDataBase);
imageArrayy = imread(depthData);
%imageArrayy2 = imread(rgbData);
depthData2 = fullfile(selected_depth, depthDataBase);
imwrite(imageArrayy, depthData2);
%rgbData2 = fullfile(selected_color, rgbDataBase);
%imwrite(imageArrayy2, rgbData2);
%movefile(file4, file5)
m = m+1; % for part 2 m = m+1
end
%
% PART 2:- If RGB images are more than depth then use the following code
else
for m = 1:length(file2)
for k = 1:length(file)
C{m,k} = A{k,1} - B{m,1};
if C{m,k}<0
C{m,k} = -C{m,k};
end
end
end
[V,X] = min(cell2mat(C),[],1); % Is a row vector containing the minimum value of columns
% V gives minimum value and X gives Index
copyfile(depth_img, selected_depth)
for m= X
%depthDataBase = file3(w).name;
rgbDataBase = file4(m).name;
%depthData = fullfile(depth_img, depthDataBase);
rgbData = fullfile(Openpose, rgbDataBase);
%imageArrayy = imread(depthData);
imageArrayy2 = imread(rgbData);
%depthData2 = fullfile(selected_depth, depthDataBase);
%imwrite(imageArrayy, depthData2);
rgbData2 = fullfile(selected_color, rgbDataBase);
imwrite(imageArrayy2, rgbData2);
w = w+1;
end
endhttps://codereview.stackexchange.com/questions/233833
复制相似问题