RGB 到标准 RGB 转换。矢量化

发布于 2024-12-11 13:59:20 字数 1292 浏览 0 评论 0原文

我正在编写一段代码,必须将 RGB 图像转换为 RGB 标准化空间。我已经让它可以使用 for 格式,但运行速度太慢,而且我需要评估大量图像。我正在尝试对整个函数进行矢量化以加快速度。我目前所拥有的内容如下:

     R = im(:,:,1);
     G = im(:,:,2);
     B = im(:,:,3);

     r=reshape(R,[],1);
     g=reshape(G,[],1);
     b=reshape(B,[],1);

     clear R G B;

     VNormalizedRed = r(:)/(r(:)+g(:)+b(:));
     VNormalizedGreen = g(:)/(r(:)+g(:)+b(:));
     VNormalizedBlue = b(:)/(r(:)+g(:)+b(:));

     NormalizedRed = reshape(VNormalizedRed,height,width);
     NormalizedGreen = reshape(VNormalizedGreen,height,width);
     NormalizedBlue = reshape(VNormalizedBlue,height,width);

主要问题是当它到达 VNormalizedRed = r(:)/(r(:)+g(:)+b(:));它显示内存不足错误(这真的很奇怪,因为我刚刚释放了三个相同大小的向量)。是错误吗? (已解决

是否可以以更有效的方式完成相同的过程?

编辑:

使用 Martin Sugestions 后,我发现 reshape 函数不是必需的,可以使用简单的代码执行相同的操作:

     R = im(:,:,1);
     G = im(:,:,2);
     B = im(:,:,3);

     NormalizedRed = R(:,:)./sqrt(R(:,:).^2+G(:,:).^2+B(:,:).^2);
     NormalizedGreen = G(:,:)./sqrt(R(:,:).^2+G(:,:).^2+B(:,:).^2);
     NormalizedBlue = B(:,:)./sqrt(R(:,:).^2+G(:,:).^2+B(:,:).^2);

     norm(:,:,1) = NormalizedRed(:,:);
     norm(:,:,2) = NormalizedGreen(:,:);
     norm(:,:,3) = NormalizedBlue(:,:);

I'm writing a piece of code that has to transform from an RGB image to an rgb normalized space. I've got it working with a for format but it runs too slow and I need to evaluate lots of images. I'm trying to vectorize the full function in order to faster it. What I have for the moment is the following:

     R = im(:,:,1);
     G = im(:,:,2);
     B = im(:,:,3);

     r=reshape(R,[],1);
     g=reshape(G,[],1);
     b=reshape(B,[],1);

     clear R G B;

     VNormalizedRed = r(:)/(r(:)+g(:)+b(:));
     VNormalizedGreen = g(:)/(r(:)+g(:)+b(:));
     VNormalizedBlue = b(:)/(r(:)+g(:)+b(:));

     NormalizedRed = reshape(VNormalizedRed,height,width);
     NormalizedGreen = reshape(VNormalizedGreen,height,width);
     NormalizedBlue = reshape(VNormalizedBlue,height,width);

The main problem is that when it arrives at VNormalizedRed = r(:)/(r(:)+g(:)+b(:)); it displays an out of memory error (wich is really strange because i just have freed three vectors of the same size). Were is the error? (solved)

Its possible to do the same process in a more efficiently way?

Edit:

After using Martin sugestions I found the reshape function was not necessary, being able to do the same with a simple code:

     R = im(:,:,1);
     G = im(:,:,2);
     B = im(:,:,3);

     NormalizedRed = R(:,:)./sqrt(R(:,:).^2+G(:,:).^2+B(:,:).^2);
     NormalizedGreen = G(:,:)./sqrt(R(:,:).^2+G(:,:).^2+B(:,:).^2);
     NormalizedBlue = B(:,:)./sqrt(R(:,:).^2+G(:,:).^2+B(:,:).^2);

     norm(:,:,1) = NormalizedRed(:,:);
     norm(:,:,2) = NormalizedGreen(:,:);
     norm(:,:,3) = NormalizedBlue(:,:);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

命硬 2024-12-18 13:59:20

我相信您需要

VNormalizedRed = r(:)./(r(:)+g(:)+b(:));

注意 / 前面的点,它指定逐个元素的划分。如果没有点,您将求解一个方程组 - 这可能不是您想要做的。这可能也解释了为什么您会看到高内存消耗。

I believe you want

VNormalizedRed = r(:)./(r(:)+g(:)+b(:));

Note the dot in front of the /, which specifies an element-by-element divide. Without the dot, you're solving a system of equations -- which is likely not what you want to do. This probably also explains why you're seeing the high memory consumption.

ㄖ落Θ余辉 2024-12-18 13:59:20

您的整个第一个代码可以用一个矢量化行重写:

im_normalized = bsxfun(@rdivide, im, sum(im,3,'native'));

您的第二个稍微修改的版本为:

im_normalized = bsxfun(@rdivide, im, sqrt(sum(im.^2,3,'native')));

顺便说一句,您应该知道用于图像的数据类型,否则可能会得到意外的结果(例如由于整数除法)。因此,在执行标准化计算之前,我会将图像转换为 double :

im = im2double(im);

Your entire first code can be rewritten in one vectorized line:

im_normalized = bsxfun(@rdivide, im, sum(im,3,'native'));

Your second slightly modified version as:

im_normalized = bsxfun(@rdivide, im, sqrt(sum(im.^2,3,'native')));

BTW, you should be aware of the data type used for the image, otherwise one can get unexpected results (due to integer division for example). Therefore I would convert the image to double before performing the normalization calculations:

im = im2double(im);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文