Filter löschen
Filter löschen

Faster Image Processing?

6 Ansichten (letzte 30 Tage)
DB am 31 Jan. 2024
Bearbeitet: Matt J am 3 Feb. 2024
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;
Thanks for your ideas!
  3 Kommentare
DB am 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 am 3 Feb. 2024
Bearbeitet: Catalytic am 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.

Melden Sie sich an, um zu kommentieren.

Antworten (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.
is efficient as opposed to
for k1=1:5
for k2=1:5
b(k1,k2) = a(k1,k2) *2;
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:

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


Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by