How to form a vector from RGB matrices

2019-09-02 22:33发布

问题:

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.

回答1:

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:

B = permute(A, [3 1 2]);

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 of A and make them unroll into the columns, and finally the columns of A 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:

N = numel(A)/3;
V = B(1 : N);
W = B(N + 1 : 2*N);
X = B(2*N + 1 : end);

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 a 3 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 calculating N 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 retrieve N elements. These get placed into V. We then continue from this point and grab N more elements and place them into W, and finally the rest go into X.

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:

B = permute(A, [3 2 1]);

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:

C = reshape(B, [], 3);
V = C(:,1);
W = C(:,2);
X = C(:,3);


回答2:

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 of

B = reshape(permute(A,[3 1 2]),[],3);

So, if you need those vectors as explicit variables,

vector1 = B(:,1);
vector2 = B(:,2);
vector3 = B(:,3);

If the desired index order is color, then column, then row, use

B = reshape(permute(A,[3 2 1]),[],3);