使用 Octave 将彩色图像转换为灰度图像
Convert Color image to grayscale image using Octave
我有一张彩色图像,正在尝试将其转换为灰度图像,但出现错误:
warning: the 'rgb2gray' function belongs to the image package from Octave Forge but has not yet been implemented
我在 Ubuntu 18.04 64 位上使用 Octave 4.2.2,目前无法将此版本升级到 Octave 5.1。
有解决办法吗?
我的目标是:
- 将彩色图像转换为灰度图像。
- 然后将每个灰度像素的intensity/brightness置于
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')
如果 RGB
是 RGB 图像(大小为 [n,m,3]
的矩阵),则转换为 gray-scale 图像 gray
([n,m]
的数组) 是通过对 3 个颜色通道进行加权平均来实现的。
根据您的应用,最好的方法可能改为只采用绿色通道(这是最敏感的通道,CCD 的绿色像素是蓝色或红色像素):
gray = rgb(:,:,2);
一个简单的 non-weighted 平均值通常就足够了:
gray = mean(rgb,3);
Adobe D65 标准 RGB 使用 0.2973769、0.6273491 和 0.0752741 的红色、绿色和蓝色权重 (source)。但是不知道rgb2gray
的MATLAB实现用的是什么权重。让我们假设它是那些重量。此代码计算加权平均值:
[n,m] = size(rgb);
gray = reshape(rgb,[],3);
gray = gray * [0.30;0.63;0.07];
gray = reshape(gray,n,m);
在 Octave 中你可以把它写成 one-liner:
gray = reshape(reshape(rgb,[],3) * [0.30;0.63;0.07], size(rgb)[1:2]);
重新安装镜像包。你不知何故安装失败。
功能rgb2gray
一直是图像包的一部分。它是自 very start.
以来一直存在的功能之一
发生的事情是,自 4.4 版以来,Octave 核心还包含 rgb2gray
的实现。为了同时支持新旧 Octave 版本,映像包会在安装期间检查 rgb2gray
是否可用。如果是这样,它会安装自己的实现。如果不是,它什么也不做,默认为 Octave 核心中的实现。如果您同时安装了映像包和 Octave 4.2,并且 rgb2gray
不可用,那么您以某种方式搞砸了映像包的安装。
您安装映像包时使用的 Octave 版本是否与您使用的版本不同运行?
此外,请考虑使用系统包管理器提供的八度包,在卸载您手动安装的包后应该不会出现此问题 (apt install octave-image
)。
根据 rgb2gray 上的 Octave 文档,转换完成如下:
I = 0.298936*R + 0.587043*G + 0.114021*B
因此可以通过以下代码将 3D RGB 图像矩阵转换为 2D gray-scale:
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
如果你想编写函数创建新函数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)
然后在命令 window 中输入
>> image = imread('yourimage.jpg');
>> convgray(image)
它将正确显示您的灰度图像
here's my output
我有一张彩色图像,正在尝试将其转换为灰度图像,但出现错误:
warning: the 'rgb2gray' function belongs to the image package from Octave Forge but has not yet been implemented
我在 Ubuntu 18.04 64 位上使用 Octave 4.2.2,目前无法将此版本升级到 Octave 5.1。
有解决办法吗?
我的目标是:
- 将彩色图像转换为灰度图像。
- 然后将每个灰度像素的intensity/brightness置于
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')
如果 RGB
是 RGB 图像(大小为 [n,m,3]
的矩阵),则转换为 gray-scale 图像 gray
([n,m]
的数组) 是通过对 3 个颜色通道进行加权平均来实现的。
根据您的应用,最好的方法可能改为只采用绿色通道(这是最敏感的通道,CCD 的绿色像素是蓝色或红色像素):
gray = rgb(:,:,2);
一个简单的 non-weighted 平均值通常就足够了:
gray = mean(rgb,3);
Adobe D65 标准 RGB 使用 0.2973769、0.6273491 和 0.0752741 的红色、绿色和蓝色权重 (source)。但是不知道rgb2gray
的MATLAB实现用的是什么权重。让我们假设它是那些重量。此代码计算加权平均值:
[n,m] = size(rgb);
gray = reshape(rgb,[],3);
gray = gray * [0.30;0.63;0.07];
gray = reshape(gray,n,m);
在 Octave 中你可以把它写成 one-liner:
gray = reshape(reshape(rgb,[],3) * [0.30;0.63;0.07], size(rgb)[1:2]);
重新安装镜像包。你不知何故安装失败。
功能rgb2gray
一直是图像包的一部分。它是自 very start.
发生的事情是,自 4.4 版以来,Octave 核心还包含 rgb2gray
的实现。为了同时支持新旧 Octave 版本,映像包会在安装期间检查 rgb2gray
是否可用。如果是这样,它会安装自己的实现。如果不是,它什么也不做,默认为 Octave 核心中的实现。如果您同时安装了映像包和 Octave 4.2,并且 rgb2gray
不可用,那么您以某种方式搞砸了映像包的安装。
您安装映像包时使用的 Octave 版本是否与您使用的版本不同运行?
此外,请考虑使用系统包管理器提供的八度包,在卸载您手动安装的包后应该不会出现此问题 (apt install octave-image
)。
根据 rgb2gray 上的 Octave 文档,转换完成如下:
I = 0.298936*R + 0.587043*G + 0.114021*B
因此可以通过以下代码将 3D RGB 图像矩阵转换为 2D gray-scale:
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
如果你想编写函数创建新函数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)
然后在命令 window 中输入
>> image = imread('yourimage.jpg');
>> convgray(image)
它将正确显示您的灰度图像 here's my output