Octave:'rgb2gray'未定义错误

时间:2014-01-05 21:05:26

标签: octave

请告知如何使rgb2gray在Octave中工作。 我找到了 this thread ,但我仍然希望使用rgb2gray功能,而不是手动计算每个颜色通道。

我运行的代码:

I = imread('smile.jpg');
G = rgb2gray(I);


我得到的错误:

error: 'rgb2gray' is undefined near line 10 column6


附加:
八度版:3.6.4
图像包已安装。版本:2.0.0。
'rgb2gray.m'安装在C:\ Octave \ share \ octave \ packages \ image-2.0.0

谢谢,

2 个答案:

答案 0 :(得分:10)

问题是你没有加载你的包。键入pkg list时,您可以在名称前面找到由星号加载的文件。使用pkg load image加载包。

必须加载包是大多数用户觉得奇怪的东西但是如果你与其他语言比较,比如Python,Perl或C ++,你会期望它们importuse,或#include默认情况下系统中可用的每个库?有关详细信息,请参阅Octave's FAQ

答案 1 :(得分:0)

函数 rgb2ntsc 在历史上一直是 Octave 的一部分(我的意思是历史上,自 1994 年以来)。但是,自 Octave 4.4 版(2018 年发布)以来,该功能已从 Octave 移至 Octave Forge 映像包。自 2.8.0 版本(2018 年发布)以来,它是 Octave Forge 映像包的一部分。

基本上,如何使用 rgb2ntsc 取决于您拥有的版本:

八度 >= 4.4.0 您需要安装并加载2.8.0或更高版本的镜像包(最新的是2.12.0)。

octave> pkg install -forge image
octave> pkg load image

不幸的是,它对我不起作用。所以我使用代码,它有效。来自Octave rgb2ntsc missing in latest version

    function yiq_img = rgb2ntsc(rgb_img)
%RGB2NTSC Transform a colormap or image from red-green-blue (RGB) 
%   color space to luminance-chrominance (NTSC) space. 
%   The input may be of class uint8, uint16, single, or double. 
%   The output is of class double.

% https://octave.sourceforge.io/octave/function/rgb2ntsc.html

if isa(rgb_img, 'uint8') || isa(rgb_img, 'uint16') || ...
        isa(rgb_img, 'double')
    
    red = rgb_img(:, :, 1);
    green = rgb_img(:, :, 2);
    blue = rgb_img(:, :, 3);
    
    y = 0.299 * red + 0.587 * green + 0.114 * blue;
    i = 0.596 * red - 0.274 * green - 0.322 * blue;
    q = 0.211 * red - 0.523 * green + 0.312 * blue;
    
    yiq(:, :, 1) = y;
    yiq(:, :, 2) = i;
    yiq(:, :, 3) = q;
    
    yiq_img = double(yiq);
else
    error('Input image datatype is not supported')
end

end

我正在查看 Andrew Ng 的第 5 周讲义。 AI机器学习课程。 https://www.coursera.org/learn/machine-learning/resources/EcbzQ

参考:Octave rgb2ntsc missing in latest version