Faster Image Processing?
조회 수: 6 (최근 30일)
이전 댓글 표시
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
Catalytic
2024년 2월 3일
편집: Catalytic
2024년 2월 3일
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.
답변 (2개)
Constantino Carlos Reyes-Aldasoro
2024년 2월 2일
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(2:4,2:4) =2;
b= 2*a;
b(a==2) = 5;
disp (b)
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
댓글 수: 0
Matt J
2024년 2월 3일
편집: Matt J
2024년 2월 3일
[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;
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!