Clear Filters
Clear Filters

reshaping a 3D array

1 view (last 30 days)
t4741
t4741 on 7 Sep 2019
Edited: Bruno Luong on 7 Sep 2019
I have a 3D stack of RGB values.
Colours( 1, 1, :) = a stack of values eg , 1 through 12 in the form R G B R G B R G B R G B.
I need to format it into three layers instead of 12, where each layer is the individual colour etc.
so it would be in the form
R R R (layer 1)
G G G (layer 2)
B B B (layer 3)
any help for this please?
  4 Comments
Guillaume
Guillaume on 7 Sep 2019
It's still no clearer I'm afraid. Typically, a stack of colour image would be stored as a 4D array, where stack(r, c, 1, i) is the Red value of pixel (r, c) of image i, stack(r, c, 2, i) is the Green value of the same pixel of the same image, and stack(r, c, 3, i) the Blue value.
"This produces a 3D array"
If this refers to Colours(1, 1, :), then no, it produces a 1x1x12 vector.
Perhaps you have a stack of 4 images, with Colour(r, c, 1) being the R value of the 1st image at pixel (r, c) and Colour(r, c, 12) being the B value of the 4th image at pixel (r, c).
I'm not sure. Best is to tell us what the result should be for the example matrix I've given you. This would remove any ambiguity.
For that matter, it's not clear how you want to go from multiple images to a 3D array (which would contain just one image).
Guillaume
Guillaume on 7 Sep 2019
I didn't see your edit to your earlier comment.
The planes of R colours need to be merged
What does merge mean?
Stack(:,:,1) = 1, 4 RED
You can only put one value in a matrix element, so not both 1 RED and 4 RED at the same time, so how should 1, 4 (and 7 and 10) be merged?

Sign in to comment.

Answers (1)

Bruno Luong
Bruno Luong on 7 Sep 2019
Edited: Bruno Luong on 7 Sep 2019
[m,n,p] = size(Colours);
ColoursRearrange = reshape(permute(reshape(Colours,m,n,3,[]),[1 2 4 3]),[m n p]);
  3 Comments
Bruno Luong
Bruno Luong on 7 Sep 2019
Edited: Bruno Luong on 7 Sep 2019
OK take an example of Colors is (2 x 2 x 6) (2 "stacks" [sic] of RGB1 = [1 2 3] and RGB2 = [11 12 13]):
Colours=repelem(reshape((1:3)'+[0 10],1,1,6),2,2,1)
That is
Colours(:,:,1) =
1 1
1 1
Colours(:,:,2) =
2 2
2 2
Colours(:,:,3) =
3 3
3 3
Colours(:,:,4) =
11 11
11 11
Colours(:,:,5) =
12 12
12 12
Colours(:,:,6) =
13 13
13 13
What do you want the reshape outcome be?
Bruno Luong
Bruno Luong on 7 Sep 2019
Edited: Bruno Luong on 7 Sep 2019
[m,n,p] = size(Colours);
ColoursRearrange = reshape(permute(reshape(Colours,m,n,3,[]),[1 2 4 3]),m, [], 3)

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!