Faster Image Processing?

8 views (last 30 days)
DB
DB on 31 Jan 2024
Edited: Matt J on 3 Feb 2024
Hello,
I'm trying to make my code faster and I haven't been successful and I'm hoping that someone has great ideas.
I have 3D arrays: Image(512,512,60) and RefImage(1000,288,16).
I'm trying to update the 3D Image array based on the calculations using the values in RefImage pages(16 of them).
Here's the simplified version of what I'm trying to make it faster:
for i = 1:1000
for j = 1:288
RCPointer(varies depends on how many row and col are occupied) = pointer{j,i};
ImagePage(16 of them) = page{j,i} * ConstantMultiplier;
for k = 1:16
ImagePointer = (ImagePage(k)-1) * 262144 + RCPointer;
Pixel = Image(ImagePointer);
NewPixel = Pixel + RefImage(i,j,k);
Image(ImagePointer) = NewPixel;
end
end
end
Thanks for your ideas!
  3 Comments
DB
DB on 1 Feb 2024
Sorry, I can't provide the full code. However, the main purpose should be be pretty clear from the simplified code above. I need to update the Image array(512x512x60) with the RefImage(1000x288x16) array values. Where to update is based on the cell array pointer and page values.
Catalytic
Catalytic on 3 Feb 2024
Edited: Catalytic on 3 Feb 2024
However, the main purpose should be be pretty clear from the simplified code above.
No, because code isn't enough to tell us what's going on. We need to see input data as well, so we can understand the size and shape of the matrix variables it contains. If you're going to provide simplified code, you need to provide simplified input data to go with it - in this case pointer, page, and ConstantMultiplier - and we should be able to run the code with the input you provide.

Sign in to comment.

Answers (2)

Constantino Carlos Reyes-Aldasoro
Two nested for loops with images is not an efficient way to deal with matrices or images. The point of Matlab is to handle everything as a matrix. I.e.
a=ones(5,5);
b=2*a;
is efficient as opposed to
for k1=1:5
for k2=1:5
b(k1,k2) = a(k1,k2) *2;
end
end
Now, how do you handle the to and from matrices? You can use indices, i.e., addressing the matrix. For example
a= ones(5,5);
a = 5×5
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
a(2:4,2:4) =2;
b= 2*a;
b(a==2) = 5;
disp (b)
2 2 2 2 2 2 5 5 5 2 2 5 5 5 2 2 5 5 5 2 2 2 2 2 2
In that way, you do not need to use for loops, and can directly operate in certain regions of the images/matrices (i.e. the address that you pass, in this case a==2)
I wrote a book on image analysis, addressing a matrix is part of chapter 1:
https://onlinelibrary.wiley.com/doi/epdf/10.1002/9781118657546.ch1

Matt J
Matt J on 3 Feb 2024
Edited: Matt J on 3 Feb 2024
[m,n,p]=size(Image);
N=cellfun(@numel, pointer(:));
K=cell2mat( cellfun(@(q)q(:)', page(:),'uni',0)); %*ConstantMultiplier? No idea what that was doing
K=repelem(pointer,N,1);
[I,J]=ind2sub([m,n], vertcat(pointer{:}));
I=repmat(I,1,width(K));
J=repmat(J,1,width(K));
Update=accumarray([I(:),J(:),K(:)], RefImage(:), [m,n,p]);
Image = Image + Update;

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!