首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据时间戳值对RGB和深度图像排序代码的时间缩短

根据时间戳值对RGB和深度图像排序代码的时间缩短
EN

Code Review用户
提问于 2019-12-11 01:58:33
回答 1查看 59关注 0票数 3

我有RGB图像,深度图像和OpenPose图像(从RGB图像生成)。RGB和深度图像的数量是不同的,所以我必须找到确切的RGB图像对应于哪个深度图像。这是使用我对RGB和深度图像的时间戳值来完成的。

这里的问题是,我编写的这段代码大约需要7-10分钟才能执行,我觉得这需要花费很多时间。怎样才能缩短同样的时间?这里主要写的是花了很多时间。

代码语言:javascript
复制
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:-概要文件执行时间的屏幕截图。

EN

回答 1

Code Review用户

发布于 2020-01-06 16:51:26

按照Cris的建议,对路径进行了更改,并使用了复制文件的提示,现在大约需要21秒。这是解决办法

代码语言:javascript
复制
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
end
票数 0
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/233833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档