Convert Color image to grayscale image using Octave

梦想与她 提交于 2019-12-12 22:22:57

问题


I have a color image that I'm trying to convert to grayscale, but I get an error:

warning: the 'rgb2gray' function belongs to the image package from Octave Forge but has not yet been implemented

I'm using Octave 4.2.2 on Ubuntu 18.04 64-bit and can't upgrade this version to Octave 5.1 yet.

Is there a workaround?

My goal is:

  1. To convert a color image into grayscale.
  2. Then place the intensity/brightness of each grayscale pixel into a range between 0-1.

My code:

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

回答1:


According to Octave documentation on rgb2gray, the conversion is done as follows:

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

So converting a 3D RGB image matrix to a 2D gray-scale can be done by this code:

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

When you call imread than the pixels are integers of type uint8, in this case you can round the result for better accuracy by adding 0.5:

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

To get the pixels into a range between 0-1 use im2double




回答2:


Reinstall the image package. You somehow have a botched installation.

The function rgb2gray has always been part of the image package. It is one of the functions that has been there since the very start.

What has happened is that since version 4.4, Octave core also includes an implementation of rgb2gray. To support both old and new Octave versions, the image package checks if rgb2gray is available during installation. If so, it installs its own implementation. If not, it does nothing and defaults to the implementation in Octave core. If you have both image package and Octave 4.2 installed, and rgb2gray is not available, then you somehow messed up your installation of the image package.

Is your installation of the image package maybe done with a version of Octave different from the one you are running?

Also, consider using the octave packages that are provided by your system package manager which should not have this problem (apt install octave-image) after uninstalling the ones you installed manually.




回答3:


If RGB is an RGB image (a matrix of size [n,m,3]) then converting to a gray-scale image gray (an array of [n,m]) is accomplished by a weighted averaging of the 3 color channels.

Depending on your application, the best approach could be instead to take only the green channel (this is the most sensitive one, CCDs have twice as many green pixels than blue or red pixels):

gray = rgb(:,:,2);

A simple non-weighted average is often good enough:

gray = mean(rgb,3);

The Adobe D65 standard RGB uses weights of 0.2973769, 0.6273491 and 0.0752741 for red, green and blue (source). But I don’t know what weights are used by the MATLAB implementation of rgb2gray. Let’s assume it’s those weights. This code computes the weighted average:

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

In Octave you can write it as a one-liner:

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


来源:https://stackoverflow.com/questions/56526100/convert-color-image-to-grayscale-image-using-octave

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!