MATLAB中imshow()和rgb2gray()的问题

use*_*874 2 matlab image-processing

下午好,

运行以下代码时:

testImage = double(imread(testfile));
figure; imshow(testImage)
greyTestImage = rgb2gray(testImage);
figure; imshow(greyTestImage)
Run Code Online (Sandbox Code Playgroud)

我不清楚,大多是空白的图像,我无法使用色彩图来表达它.以下是原始图像和两个结果数字:

1: 原始图像

2: imshow的结果

3: rgb2gray之后的图像

CTZ*_*tef 5

您必须知道您尝试阅读的图像的格式.为了确保这一点,当我想将用户定义的图像从未知格式转换为uint8灰度时,我总是在我的程序中使用以下代码:

    % load image
    [filename, pathname] = uigetfile({'*.*'},'image file');
    fullFilename = [pathname filename];
    % Get image info, read it accordingly
    info = imfinfo(fullFilename);
    if(strcmp('truecolor',info.ColorType))
        I = imread(fullFilename);
        Igray = uint8(rgb2gray(I));
        clear I
    elseif(strcmp('grayscale',info.ColorType))
        Igray = uint8(imread(fullFilename));
    elseif(strcmp('indexed',info.ColorType))
        [I,map] = imread(fullFilename);
        Igray = uint8(ind2gray(I,map));
        clear I map
    else
        error('statPart:FormatImage','Image format error');
    end
    clear info
Run Code Online (Sandbox Code Playgroud)

此外,testImage = double(imread(testfile));如果您假设testfile是uint8并且想要将其转换为double(双倍强度范围从0到1),则无效.你必须做testImage = double(imread(testfile)) / 255;

希望这有帮助.

干杯