Search code examples
matlabimage-processingparallel-processingimread

How to reduce processing time for imread and imwrite


I am working on a 3d reconstruction using parallel computing in Matlab. I profiled my code and found that imread and imwrite took most of the processing time. My goal is to significantly reduce the processing time as my simulations involve large data sets and iterations. My code:

projection_length = 4100;
parfor q = 1:projection_length
    tmpData = zeros(1600, 500);
    for i = 1:500
        fname= sprintf('pre%03d.tif', i);
        tmpData(:, i) = imread(fname, 'PixelRegion', {[1 1600], [q q]});

        disp(['Analyzing projection ' num2str(q)  ' of ' num2str(projection_length) ', Angle ' num2str(i) '...']);
    end
    idata=255-tmpData;
    H = iradon(idata, 0.72, 'Hann', 0.8, 1600 );
    postfname= sprintf('post%06d.tif', q);
    imwrite(H, postfname);
end

Solution

  • It looks like you read the same 500 images over and over again, only to access different pixel regions. Better to read the images once, and then access the pixel regions using standard array slicing.

    I can't really test the code because I don't have your image files and functions, but it should look something like this:

    % preload images
    images = {};
    for i = 1:500
        fname= sprintf('pre%03d.tif', i);
        images{i} = imread(fname);
    end
    % analyze images
    projection_length = 4100;
    parfor q = 1:projection_length
        tmpData = zeros(1600, 500);
        for i = 1:500
            tmpData(:, i) = images{i}(1 : 1600, q, :);
            disp(['Analyzing projection ' num2str(q)  ' of ' num2str(projection_length) ', Angle ' num2str(i) '...']);
        end
        idata=255-tmpData;
        H = iradon(idata, 0.72, 'Hann', 0.8, 1600 );
        postfname= sprintf('post%06d.tif', q);
        imwrite(H, postfname);
    end