My function uses multiple RGB images as a 1xn 1D cell array and the output creates an action shot.
For example, this will consist of 6 different images containing one moon, but when combined it creates one image (an action shot).
So far I'm using a nested for loop for the rows, columns and number of images but is there any other way to approach this?

1 Kommentar

Sara Foster
Sara Foster am 10 Sep. 2019
I asked what the process is for the code, I’m writing my own code but I was unsure how to approach it. (:

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Matt J
Matt J am 9 Sep. 2019
Bearbeitet: Matt J am 9 Sep. 2019

3 Stimmen

Yes, here is an approach that uses no loops,
Image4D=double(cat(4,ImageCell{:}));
MedianImage = median(Image4D,4);
[~,idx]= max(vecnorm(Image4D-MedianImage,2,3),[],4);
mask=idx==reshape(1:numel(ImageCell),1,1,1,[]);
result=uint8( sum(Image4D.*mask,4) );

4 Kommentare

Sara Foster
Sara Foster am 9 Sep. 2019
Bearbeitet: Sara Foster am 9 Sep. 2019
Thanks I’ll give it a try now! If there is a possible way to make loops, even though it’s less efficient, how could we do that for this function?
Here's one way you could incorporate a loop:
Image4D=double(cat(4,ImageCell{:}));
MedianImage = median(Image4D,4);
[~,idx]= max(vecnorm(Image4D-MedianImage,2,3),[],4);
result=0;
for i=1:numel(ImageCell)
result=result+Image4D(:,:,:,i).*(idx==i);
end
result=uint8( result );
Matt J
Matt J am 9 Sep. 2019
Bearbeitet: Matt J am 9 Sep. 2019
Here's another way with a double for-loop. This one might even be more efficient than the original because it allocates no memory for a mask.
Image4D=double(cat(4,ImageCell{:}));
MedianImage = median(Image4D,4);
[~,idx]= max(vecnorm(Image4D-MedianImage,2,3),[],4);
result=num2cell(zeros(size(MedianImage),'uint8'),[1,2]);
for i=1:numel(ImageCell)%images
loc=(idx==i);
for j=1:3 %color channels
tmp=Image4D(:,:,j,i);
result{j}(loc)=tmp(loc);
end
end
result=cell2mat(result);
Peter Bier
Peter Bier am 11 Sep. 2019
Interesting approach, although well beyond the scope of what we've taught our students.
This is a reminder to all 131 students that as mentioned in class you need to write your own code for the project that is due shortly (rather than getting code written by one of the kind folks who answer questions on the mathworks forum). Copying any of the supplied code from above and submitting it as your own will be detected by our plagiarism detection software when we come to mark the project.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by