简体   繁体   English

MATLAB imread bmp图像不正确

[英]MATLAB imread bmp image is not correct

I have a grayscale image. 我有一个灰度图像。

When I load it in MATLAB, I found that the gray levels don't match the original image. 当我在MATLAB中加载它时,我发现灰度级与原始图像不匹配。 The read in image with MATLAB is more brighter than the original image. 使用MATLAB读入的图像比原始图像更亮。 What am I doing wrong? 我究竟做错了什么? How can I solve it? 我该如何解决?

Left one is read matlab, right one is original 左边一个是matlab,右边一个是原始的

在此输入图像描述

The original bmp file can be downloaded here. 原始的bmp文件可以在这里下载。

It turns out your image has an associated colour map with it. 事实证明你的图像有一个相关的颜色图。 When you do X = imread('Lena.bmp'); 当你做X = imread('Lena.bmp'); , you are reading in an indexed image. ,您正在阅读索引图像。 This means that each value is an index into a colour map - this isn't the same the actual intensities themselves. 这意味着每个值都是颜色图的索引 - 这与实际强度本身不同。

Therefore, read in the image with the colour map, then convert the indexed image with the colour map to an actual image. 因此,使用颜色映射读入图像,然后将带有颜色映射的索引图像转换为实际图像。 You'd have to call the two output variant of imread , then convert the indexed image accordingly with ind2rgb : 您必须调用imread的两个输出变体,然后使用ind2rgb相应地转换索引图像:

[X,map] = imread('Lena.bmp');
im = ind2rgb(X,map);
imshow(im);

I get this image, which matches with your right image: 我得到的这张图片与你的右图相符:

在此输入图像描述


In the future, if you're not sure whether your image has a colour map with it or not, call the two-output variant, then check to see if the second output, which contains the colour map, is non-empty. 将来,如果您不确定图像是否带有颜色贴图,请调用双输出变量,然后检查包含颜色贴图的第二个输出是否为非空。 If it is, then call ind2rgb accordingly: 如果是,则相应地调用ind2rgb

[im, map] = imread('...'); %// Place your input image location here
if ~isempty(map)
    im = ind2rgb(im,map);
end

Because your image is grayscale, if you want to convert this to single channel, either use rgb2gray , or extract any channel from the image. 由于您的图像是灰度图像,如果要将其转换为单个通道,请使用rgb2gray ,或从图像中提取任何通道。 Grayscale works such that each channel in the RGB image is exactly the same. 灰度工作使得RGB图像中的每个通道完全相同。

Therefore: 因此:

im = rgb2gray(im); 
%// Or
%im = im(:,:,1);

The image will also be of type double , so to convert to uint8 (the most common type), simply do: 图像也将是double类型,因此要转换为uint8 (最常见的类型),只需执行以下操作:

im = im2uint8(im);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM