How to speed up/ Reduced Time the following code Using Parallel Processing/GPU

1 view (last 30 days)
I have a code that processes input signals and generates images of size 10,000 x 19,000. The image's width remains constant at 10,000 pixels, while the height depends on the signal length. Unfortunately, the current implementation takes approximately 1.842 seconds to complete. I'm seeking ways to optimize this code and reduce the execution time to milliseconds. Any assistance in modifying the code for this purpose would be greatly appreciated.
tic
[numImages, lenImage] = size( signal);
imbg = false(10000,lenImage); % background "color"
imfg = ~imbg(1,1); % forground "color"
imSizeOut=[10000 lenImage];
% ImageSize
for k= 1:numImages
imData = round( signal(k,:)); % get pattern
[~,Y] = meshgrid(1:lenImage,1:10000); % make a grid
% black and white image
BW = imbg;
BW(Y==imData)=imfg;
valueestimation=imbinarize(imresize(uint8(BW),imSizeOut));
% convert to uint8 (0 255)
valueestimationimage = im2uint8(valueestimation);
% resize (from 1000x1000)
SE=strel('disk',2);
BW=imdilate(BW,SE);
BW=imbinarize(imresize(uint8(BW),imSizeOut));
% convert to uint8 (0 255)
imoriginalestimate = im2uint8(BW);
end
toc
  11 Comments

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 31 Oct 2023
Why is this inside the loop:
[~,Y] = meshgrid(1:lenImage,1:10000); % make a grid
lenImage doesn't change so that should be outside the loop. Same thing for the strel() line. No need to calculate it on every iteration. And what about imbinarize() to compute an image-specific threshold. Could you use a constant/fixed threshold instead?
Are you resizing an integer factor? If so you could probably speed it up by just using indexing to downsample the images.
Also, you tagged it with parallel processing but you don't seem to be using it. Try parfor and other stuff to turn it into a parallel process.
Plus, you compute all kinds of things in the loop but you do not save anything. Each iteration just overwrites a variable but once the loop exits they just have the values of the final iteration. You don't display anything, save it to an array that survives the loop, write it to disk, or anything. The loop just does stuff then ends.
Also if BW is a binary image, then there is no point in calling imbinarize on it. If the resize makes it have non-binary values, then you can just use the 'nearest' option in imresize. If it's not of logical class, then you can call logical instead of imbinarize which would be faster since no determination of a threshold would be required.
  2 Comments
Hammad Younas
Hammad Younas on 1 Nov 2023
@Image Analyst Basically this example is basically on one signal which have the length of 1x19000. But we have different signal which have different length. thats why we used meshgrid(1:lenImage,1:10000); inside the loop. when the length is change the size of image is also changed.
2. Currently i am not using any parallel processing/GPU But i need to modified the code to run it on Parallel Processing or GPU so my time should be in miliseconds.

Sign in to comment.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!