I have a problem in constructing a vector from an image. I had used a 512 x 512 colour image and separated the rgb planes. Now i want to convert these three planes into three 1D vectors which should be as given in the following example.
Consider a 4x4x3 matrix. Converting it into RGB planes is easy. Now I need to convert these three planes into 1D vectors as given below
V=[r1g1b1....r6]
W=[g6b6r7...g11]
X=[b11r12...B16]
The program ive written is as follows. I used the reshape function to convert RGB planes into 1D vectors. Now I have trouble in regrouping them into different vectors.
A=imread('C:\Users\Desktop\lena.jpg');
% Extract the individual red, green, and blue color channels.
R = A(:, :, 1);
G = A(:, :, 2);
B = A(:, :, 3);
R1 = reshape(R.',1,[]);
G1 = reshape(G.',1,[]);
B1 = reshape(B.',1,[]);
I had converted the 2D matrices R G and B into 1D vectors R1, G1 and B1. Now I just need to create new vectores with all three values.I have no idea how to proceed...Please do help...Thanks in advance.
OK, given your example, what you want to do is given a RGB image, you want to separate the image into 3 vectors such that the RGB components are interleaved. This can easily be achieved by a permutation of the dimensions first. What you can do specifically is:
What
permute
does is that it rearranges the dimensions so that it produces another matrix. Specifically, what we're going to do is we are going to take each value in the third dimension and make them appear in the first dimension. Next we will take the rows ofA
and make them unroll into the columns, and finally the columns ofA
and make them go over each plane.The result is that each column will be a unique RGB pixel that describes your image. How the unrolling will work though is that it will go in column-major order. We can then use linear indexing to split them up into arrays like so:
The job of linear indexing is that you access elements using a single index, rather than indexing each dimension separately. How linear indexing would work here is that if we had an image that was
X x Y x 3
, after permutation, the image would be reshaped such that it became a3 x X x Y
matrix.N
in our case would be the total number of elements in a single plane. Because you are trying to split up the image into 3 vectors, the above operation where it's calculatingN
should be able to evenly divide by 3 as we have three colour planes to deal with.By doing
B(1 : N)
, we would access all of the elements from the first slice, second slice, in column-major format up until we retrieveN
elements. These get placed intoV
. We then continue from this point and grabN
more elements and place them intoW
, and finally the rest go intoX
.If you want to access the pixels in row-major order, you simply need to change the way
permute
is accessing the dimensions like so:You would then just access the elements with the above code normally. If you don't want to use linear indexing, you could use
reshape
to reshape the matrix such that it becomes a three-column matrix, where each column would be the desired vector:From your 4x4x3 example it's clear that you want to index first with the color index. I assume you then want row and then column. In that case, if
A
is your image (3D array), your desired three vectors are the columns ofSo, if you need those vectors as explicit variables,
If the desired index order is color, then column, then row, use