如何从RGB矩阵形成向量 [英] How to form a vector from RGB matrices

查看:107
本文介绍了如何从RGB矩阵形成向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从图像构造矢量时遇到问题.我使用了512 x 512彩色图像并将rgb平面分开.现在,我想将这三个平面转换为三个一维矢量,如下面的示例所示.

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.

考虑4x4x3矩阵.将其转换为RGB平面很容易.现在,我需要将这三个平面转换为如下所示的一维矢量

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]

我编写的程序如下.我使用了重塑功能将RGB平面转换为1D向量.现在,我很难将它们重新组合为不同的向量.

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,[]);

我已经将2D矩阵R G和B转换为1D向量R1,G1和B1.现在我只需要创建具有所有三个值的新向量.我不知道如何进行...请提供帮助...谢谢.

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.

推荐答案

好的,在您的示例中,您要执行的操作是给定RGB图像,您希望将图像分成3个向量,以便RGB分量交错.这可以通过首先对尺寸进行排列来轻松实现.您可以具体做的是:

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]);

permute 的作用是重新排列尺寸,因此它产生另一个矩阵.具体来说,我们要做的是将每个值都放在第三个维度中,并使它们出现在第一个维度中.接下来,我们将获取A的行,并将其展开到各列中,最后显示A的列,并使它们遍历每个平面.

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.

结果是,每个将是描述您图像的唯一RGB像素.展开方式的工作方式是按主要列进行.然后,我们可以使用线性索引将它们分成数组,如下所示:

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);

线性索引的工作是您使用单个索引访问元素,而不是分别为每个维度建立索引.线性索引在这里的工作方式是,如果我们有一个X x Y x 3的图像,则在置换后,该图像将被重塑,使其成为一个3 x X x Y矩阵.在我们的案例中,N单个平面中的元素总数.由于您正在尝试将图像拆分为3个向量,因此上述计算N的操作应该能够将3均分,因为我们要处理三个颜色平面.

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.

通过执行B(1 : N),我们将以 column-major 的格式访问第一个切片(第二个切片)中的所有元素,直到检索到N个元素.这些放入V中.然后,我们从这一点继续,并获取更多的N元素并将其放入W,最后将其余的元素放入X.

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.

如果要按行主要顺序访问像素,只需更改permute访问尺寸的方式,如下所示:

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]);

然后,您只需按上述代码即可正常访问元素.如果您不想使用线性索引,则可以使用 reshape 重塑矩阵,使其变为三列矩阵,其中每一列都是所需的向量:

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);

这篇关于如何从RGB矩阵形成向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆