【问题标题】:Convert Color image to grayscale image using Octave使用 Octave 将彩色图像转换为灰度图像
【发布时间】:2019-10-24 20:05:18
【问题描述】:

我正在尝试将彩色图像转换为灰度图像,但出现错误:

警告:'rgb2gray' 功能属于 Octave Forge 的图像包,但尚未实现

我在 Ubuntu 18.04 64 位上使用 Octave 4.2.2,目前无法将此版本升级到 Octave 5.1。

有解决办法吗?

我的目标是:

  1. 将彩色图像转换为灰度图像。
  2. 然后将每个灰度像素的强度/亮度置于0-1之间的范围内。

我的代码:

pkg load image
% read image from url (I took  a random  image on internet)..
[url_img, map] = imread('http://i.imgur.com/9PDZb7i.png');
figure, imshow(url_img), title('Image from url')

% resize it..
resized_img1 = imresize(url_img, 0.2); % resize by a factor here 0.2
resized_img2 = imresize(url_img, [600 500]); % resize to a specific dimensions

% there are many ways of interpolation to perform resizing 
%resized_img3 = imresize(url_img, 0.2,'method','nearest'); % rsize by a specific interpolation method

figure, imshow(resized_img1), title('Resized image')

% change color did you mean from RGB to grayscale 
gray_img = rgb2gray(resized_img1);
figure, imshow(gray_img), title ('Grayscale image')

【问题讨论】:

  • 我想你没有安装镜像包吧?

标签: image octave


【解决方案1】:

如果你想写函数 create new function convgray.m 然后粘贴

function[G] = convgray(F)
rgb=double(F);
[height,width,c] = size(rgb);
gray = reshape(rgb,[],3);
gray = gray * [0.30;0.63;0.07];
gray = reshape(gray,height,width);
gray=uint8(gray);
imshow(gray)

然后在命令窗口输入

>> image = imread('yourimage.jpg');
>> convgray(image)

它将显示您的灰度图像而没有错误 here's my output

【讨论】:

    【解决方案2】:

    重新安装镜像包。不知何故,您的安装很糟糕。

    函数rgb2gray 一直是镜像包的一部分。这是自very start 以来一直存在的功能之一。

    发生的情况是,从 4.4 版开始,Octave 核心还包括rgb2gray 的实现。为了同时支持新旧 Octave 版本,镜像包会在安装过程中检查 rgb2gray 是否可用。如果是这样,它会安装自己的实现。如果没有,它什么也不做,默认使用 Octave 核心中的实现。如果您同时安装了镜像包和 Octave 4.2,而 rgb2gray 不可用,那么您不知何故弄乱了镜像包的安装。

    您是否使用与您正在运行的版本不同的 Octave 版本来安装映像包?

    另外,请考虑使用系统包管理器提供的 octave 包,在卸载您手动安装的包后应该不会出现此问题 (apt install octave-image)。

    【讨论】:

      【解决方案3】:

      如果 RGB 是 RGB 图像(大小为 [n,m,3] 的矩阵),则转换为灰度图像 gray[n,m] 的数组)是通过 3 个颜色通道的加权平均完成的.

      根据您的应用,最好的方法可能是只采用绿色通道(这是最敏感的一种,CCD 的绿色像素是蓝色或红色像素的两倍):

      gray = rgb(:,:,2);
      

      简单的非加权平均值通常就足够了:

      gray = mean(rgb,3);
      

      Adobe D65 标准 RGB 对红色、绿色和蓝色 (source) 使用 0.2973769、0.6273491 和 0.0752741 的权重。但我不知道rgb2gray 的 MATLAB 实现使用了哪些权重。让我们假设是那些权重。此代码计算加权平均值:

      [n,m] = size(rgb);
      gray = reshape(rgb,[],3);
      gray = gray * [0.30;0.63;0.07];
      gray = reshape(gray,n,m);
      

      在 Octave 中,您可以将其编写为单行:

      gray = reshape(reshape(rgb,[],3) * [0.30;0.63;0.07], size(rgb)[1:2]);
      

      【讨论】:

        【解决方案4】:

        根据rgb2gray 上的 Octave 文档,转换如下:

        I = 0.298936*R + 0.587043*G + 0.114021*B
        

        因此,可以通过以下代码将 3D RGB 图像矩阵转换为 2D 灰度:

        gray_img = (...
         0.298936 * resized_img1(:,:,1) +...
         0.587043 * resized_img1(:,:,2) +...
         0.114021 * resized_img1(:,:,3));
        

        当您调用imread 时,像素是uint8 类型的整数,在这种情况下,您可以通过添加0.5 来对结果进行四舍五入以获得更高的准确性:

        gray_img = (...
         0.298936 * resized_img1(:,:,1) +...
         0.587043 * resized_img1(:,:,2) +...
         0.114021 * resized_img1(:,:,3) + 0.5);
        

        要将像素置于0-1 之间的范围内,请使用im2double

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-12-28
          • 1970-01-01
          • 1970-01-01
          • 2016-01-11
          • 2015-10-07
          • 1970-01-01
          相关资源
          最近更新 更多