如何在 MATLAB 中将彩色图像转换为灰度图像?

发布于 2024-08-11 16:03:50 字数 259 浏览 2 评论 0原文

我正在尝试在计算机视觉中实现一种算法,并且想在一组图片上进行尝试。图片都是彩色的,但我不想处理这个。我想将它们转换为灰度,这足以测试算法。

如何将彩色图像转换为灰度图像?

我正在阅读它:

x = imread('bla.jpg');

是否有任何参数可以添加到 imread 中以将其读取为灰度?有什么方法可以在读取后将x更改为灰度吗?

I am trying to implement an algorithm in computer vision and I want to try it on a set of pictures. The pictures are all in color, but I don't want to deal with that. I want to convert them to grayscale which is enough for testing the algorithm.

How can I convert a color image to grayscale?

I'm reading it with:

x = imread('bla.jpg');

Is there any argument I can add to imread to read it as grayscale? Is there any way I change x to grayscale after reading it?

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

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

发布评论

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

评论(7

心头的小情儿 2024-08-18 16:03:50

使用 rgb2gray 去除色调和饱和度(即转换为灰度)。 文档

Use rgb2gray to strip hue and saturation (ie, convert to grayscale). Documentation

不可一世的女人 2024-08-18 16:03:50
x = imread('bla.jpg');
k = rgb2gray(x);
figure(1),imshow(k);
x = imread('bla.jpg');
k = rgb2gray(x);
figure(1),imshow(k);
怀念你的温柔 2024-08-18 16:03:50

我找到了这个链接:
http://blogs.mathworks.com/steve/2007/ 07/20/imoverlay-and-imagesc/
有用。

它说:

im=imread('your image');
m=mat2gray(im);
in=gray2ind(m,256);
rgb=ind2rgb(in,hot(256));
imshow(rgb);

I found this link:
http://blogs.mathworks.com/steve/2007/07/20/imoverlay-and-imagesc/
it works.

it says:

im=imread('your image');
m=mat2gray(im);
in=gray2ind(m,256);
rgb=ind2rgb(in,hot(256));
imshow(rgb);
快乐很简单 2024-08-18 16:03:50

你可以使用这段代码:

im=imread('your image');
k=rgb2gray(im);
imshow(k);

using to matlab

you can using this code:

im=imread('your image');
k=rgb2gray(im);
imshow(k);

using to matlab

随心而道 2024-08-18 16:03:50
I=imread('yourimage.jpg');
p=rgb2gray(I)
I=imread('yourimage.jpg');
p=rgb2gray(I)
熟人话多 2024-08-18 16:03:50

使用 imread()rgb2gray() 函数获取灰度图像。

示例:

I = imread('input.jpg');
J = rgb2gray(I);
figure, imshow(I), figure, imshow(J); 

如果您有颜色映射图像,则必须执行如下操作:

[X,map] = imread('input.tif');
gm = rgb2gray(map);
imshow(X,gm);

您自己实现的 rgb2gray 算法是:

f(R,G,B) = (0.2989 * R) + (0.5870 * G) + (0.1140 * B)

Use the imread() and rgb2gray() functions to get a gray scale image.

Example:

I = imread('input.jpg');
J = rgb2gray(I);
figure, imshow(I), figure, imshow(J); 

If you have a color-map image, you must do like below:

[X,map] = imread('input.tif');
gm = rgb2gray(map);
imshow(X,gm);

The rgb2gray algorithm for your own implementation is :

f(R,G,B) = (0.2989 * R) + (0.5870 * G) + (0.1140 * B)
明天过后 2024-08-18 16:03:50

彩色图像

彩色图像

灰度图像

灰度图像

  bg = imread('C:\Users\Ali Sahzil\Desktop\Media.png');  // Add your image 
  redChannel = bg(:, :, 1);
  greenChannel = bg(:, :, 2);
  blueChannel = bg(:, :, 3);
  grayImage = .299*double(redChannel) + .587*double(greenChannel) 
  +.114*double(blueChannel);
  imshow(grayImage);

Color Image

Color Image

Gray Scale image

Gray Scale image

  bg = imread('C:\Users\Ali Sahzil\Desktop\Media.png');  // Add your image 
  redChannel = bg(:, :, 1);
  greenChannel = bg(:, :, 2);
  blueChannel = bg(:, :, 3);
  grayImage = .299*double(redChannel) + .587*double(greenChannel) 
  +.114*double(blueChannel);
  imshow(grayImage);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文