在MATLAB中读取灰度图像 [英] Reading grayscale image in matlab

查看:1974
本文介绍了在MATLAB中读取灰度图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有灰度图像"lena.bmp".我想使用imread()函数在matlab中读取此图像. 当我使用下面的代码读取和显示图像时,我的图像很暗(黑色).

I have gray scale image "lena.bmp". I want read this image in matlab using imread() function. When i use code below to read and show image my image is dark (black).

    img = imread('lena.bmp');
    imshow(img);

但是当我使用下面的代码时,我没问题查看.

But when i use code below, I have no problem to view.

    [img map]= imread('lena.bmp');
    imshow(img,map);

似乎我的第一个代码不以灰度模式读取图像(如rgb2gray函数生成的图像)​​.

It seems that my first code doses not reading image in grayscale mode (like what rgb2gray function generate).

我的图片如下:

我该怎么做才能解决这个问题?

What can i do to solve this problem?

推荐答案

您的图片是索引"图片.这意味着它包含的整数值比任何东西都更充当标签",并且这些标签中的每一个都映射到一种颜色(即rgb三元组).您的map变量表示该映射;例如,在第5行的rgb三元组对应于"label""5".

Your image is an "indexed" image. That means it contains integer values which act as "labels" more than anything, and each of those labels is mapped to a colour (i.e. an rgb triplet). Your map variable represents that mapping; at row 5 you have the rgb triplet that corresponds to 'label' "5", for instance.

要了解我的意思,请执行unique(img),您会发现img数组的值实际上是很规则的.命令rgbplot可以图形方式显示实际的颜色图.在地图变量上运行rgbplot(map),以查看每种红色,绿色和蓝色的映射.

To see what I mean, do unique(img) and you'll see that the values of your img array are in fact quite regular. The command rgbplot can demonstrate the actual colourmap graphically. Run rgbplot(map) on your map variable to see the mapping for each of the red green and blue colours.

现在,以img2的格式保存并读取下面的图像,然后比较数组值.

Now, save and read the image below on your computer as img2 and compare the array values.

此图像是通过使用照片编辑软件(GIMP)将链接到的索引"图像转换为灰度"图像而生成的.区别在于 在灰度图像中,像素值代表实际强度,而不是整数标签". Imread默认将灰度图像读取为uint8图像,这意味着它将亮度值分配给介于0(黑色)到255(白色)之间的像素.由于这些值恰好是整数,因此您仍然可以作弊并将其视为标签",并对其进行颜色映射.但是,如果您分配了线性图"(即值1 =强度1,值2 =强度2,依此类推),则您的图像将看起来像您期望的那样.

This image was generated by converting from the "indexed" image you linked to, to a "grayscale" one using photoediting software (the GIMP). The difference is that in a grayscale image, the pixel values represent actual intensities, rather than integer 'labels'. Imread reads grayscale images as uint8 images by default, meaning it assigns intensity values to pixels ranging from 0 (black) to 255 (white). Since these values happen to be integers you could still cheat and treat them as 'labels' and force a colour-mapping on them. But if you assign a 'linear map' (i.e. value 1 = intensity 1, value 2 = intensity 2, etc) then your image will look as you would expect.

您将看到unique(img2)中的值完全不同.如果您imshow(img2),您会看到如您所愿的显示.如果您未为imshow指定色彩映射表,则将假定该映射表是图像数组中从最低值到最高值的线性映射,这解释了为什么索引图像看上去很奇怪,因为它的值永远不会对应强度.

You'll see that the values from unique(img2) are quite different. If you imshow(img2) you'll see this displays as you'd expect. If you don't specify a colormap for imshow, it will assume that the map is a linear mapping from the lowest to the highest value in the image array, which explains why your indexed image looked weird, since its values were never supposed to correspond to intensities.

也可以尝试imagesc(img2),它将使用当前"颜色图显示此内容. imagesc使颜色图缩放",以便最低的颜色变为图像中的最低值,并且相似地变为最高的颜色. 默认的色图是jet,因此您应该看到看起来很迷幻的图像,但是应该能够清楚地辨认出lena.如果尝试colormap gray,应该会再次看到灰色版本.也尝试colormap hot.现在要弄清楚颜色图,请尝试在它们上使用rgbplot命令(例如rgbplot(gray)rgbplot(hot)等).

Also try imagesc(img2) which will show this but using the "current" colormap. imagesc causes the colormap to be "scaled", so that the lowest colour goes to the lowest value in the image, and similarly for the highest. The default colormap is jet so you should see a psychedelic looking image but you should be able to make out lena clearly. If you try colormap gray you should see the gray version again. Also try colormap hot. Now to make sense of the colormaps, try the rgbplot command on them (e.g. rgbplot(gray), rgbplot(hot) etc).

因此,回到imshow,imshow基本上允许您显示索引图像,并指定要用于显示它的颜色图.如果未指定颜色图,它将仅使用从最小值到最大值的线性插值作为地图.因此,imshow(img)将以与带有灰色色图的imagesc(img)几乎相同的方式显示图像.而且由于第一个img中的值代表均匀分布的标签"而不是实际强度,因此您会得到一张垃圾图片.

So, going back to imshow, imshow basically allows you to display an indexed image, and specify what colormap you want to use to display it. If you don't specify the colormap, it will just use a linear interpolation from the lowest value to the highest as your map. Therefore imshow(img) will show the image pretty much in the same way as imagesc(img) with a gray colormap. And since the values in your first img represent evenly spaced 'labels' rather than actual intensities, you'll get a rubbish picture out.

编辑:如果要将索引图像转换为灰度图像,matlab提供了ind2gray函数,例如:

If you want to convert your indexed image to a grayscale image, matlab provides the ind2gray function, e.g.:

[img, map] = imread('lena.bmp');
img_gray = ind2gray(img, map);

如果您打算将像素值作为强度进行处理,这可能就是您需要的.

This is probably what you need if you mean to process pixel values as intensities.

这篇关于在MATLAB中读取灰度图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆