reshaping a 3D array

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

Your description is not very clear. Is it the case that Colours(:, :, 1) is currently all R values, but so is Colours(:, :, 4), Colours(:, :, 7) and Colours(:, :, 10), whereas Colours(:, :, 2), Colours(:, :, 5) and so on are G values?
If so, how are the 4 planes of R colours supposed to be merged? vertically? horizontally? interlaced somehow?
A numerical example would help, e.g given:
Colours = [100;200;300] + [10, 20] + permute(1:12, [1 3 2]) %a 3x2x12 matrix
what should the output be?
t4741
t4741 on 7 Sep 2019
Edited: t4741 on 7 Sep 2019
I have a 3D array, so a stack of images on top of each other. Colours(1,1,:) gives all the values in the stack at pixel 1,1. (Let this individual stack be called stack)This produces a 3D array of RGB values stacked on top of each other. 1 row 1 col x number of layers
The planes of R colours need to be merged in order, 1 4 7. So whatever red layer is first goes first etc.
Sorry for being unclear, I'm stuck on this one! I would appreciate any help
input:
Stack(:,:,1) = 1 RED
Stack (:,:,2) = 2 GREEN
Stack(:,:,3) = 3 BLUE
Stack(:,:,4) = 4 RED
Stack(:,:,5) = 5 GREEN
Stack(:,:,6) = 6 BLUE
Stack(:,:,1) = 1, 4 RED
Stack(:,:,3) = 2, 5 GREEN
Stack(:,:,3) = 3,6 BLUE
so overall putting all the red values into a layer of matrix, green values in another layer etc. As 1 x n x 3
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).
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

t4741
t4741 on 7 Sep 2019
Edited: t4741 on 7 Sep 2019
Thanks!! but this still sorts it into 9 layers. I think its because what I wrote is not too clear. I want to put it in three layers, where each layer is filled with a particular RGB value
i.e going from 1x1x9 to 1x3x3
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?
[m,n,p] = size(Colours);
ColoursRearrange = reshape(permute(reshape(Colours,m,n,3,[]),[1 2 4 3]),m, [], 3)

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Tags

Asked:

on 7 Sep 2019

Edited:

on 7 Sep 2019

Community Treasure Hunt

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

Start Hunting!