MATLAB中imshow()和rgb2gray()的问题 [英] problems with imshow() and rgb2gray() in MATLAB

查看:1077
本文介绍了MATLAB中imshow()和rgb2gray()的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好,

运行以下代码时:

testImage = double(imread(testfile));
figure; imshow(testImage)
greyTestImage = rgb2gray(testImage);
figure; imshow(greyTestImage)

我不清楚并且大部分是空白图片,我无法使用色彩图进行fx 。以下是原始图像和两个结果数字:

I get unclear and mostly blank Images, I was unable to fx it using the colormap. The following is teh original image and the two resulting figures:

1:

2:

3:

推荐答案

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

You have to know the format of the image you are trying to read. To make this sure, I always use the following piece of code in my programs when I want to convert user defined image from unknown format to uint8 grayscale :

    % 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

此外, testImage = double(imread(testfile)); 如果你假设testfile是uint8并且想要将它转换为double(双倍强度范围从0到1)将不起作用。你必须做 testImage = double(imread(testfile))/ 255;

Also, this : testImage = double(imread(testfile)); won't work if you assume testfile is uint8 and want to convert it to double (in double intensity shall range from 0 to 1). You have to do testImage = double(imread(testfile)) / 255;

希望这个帮助。

干杯

这篇关于MATLAB中imshow()和rgb2gray()的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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