【问题标题】:Saving image in .tif format in MATLAB在 MATLAB 中以 .tif 格式保存图像
【发布时间】:2017-04-29 08:56:41
【问题描述】:

我无法以 .tif 格式保存多个具有不同名称的图像。 这是我当前的代码:

srcFiles = dir('path/*.tif');
for i = 1 : length(srcFiles)
  img = strcat('path/',srcFiles(i).name);
  img = imread(img);
  figure, img = imshow(img(:,:,3));
  colormap gray;
  a = strcat(srcFiles(i).name, '-run.tif');
  imwrite(img, a,'tiff');
end

我收到以下错误:

Error using imwrite (line 420)
Expected DATA to be one of these types:

numeric, logical

Instead its type was matlab.graphics.primitive.Image.

我知道我没有保存图像数组。但是,我不知道该怎么做,因为我必须打电话给colormap gray。此外,我使用imwrite 执行此操作,因为这消除了手动保存时保存的白色边框。有什么想法吗?

【问题讨论】:

    标签: matlab graphics computer-vision


    【解决方案1】:

    在随后的 3 行中,您将更改变量 img 的类型:

    这里是一个字符数组:

    img = strcat('path/',srcFiles(i).name);
    

    接下来是一个数值数组(一张图片):

    img = imread(img);
    

    最后是图形句柄:

    figure, img = imshow(img(:,:,3));
    

    为了保存此图像,您需要向imwrite 提供数字数组,最佳做法是为这些变量使用不同的名称,例如:

    imgname = strcat('path/',srcFiles(i).name);
    img = imread(imgname);
    figure, imghandle = imshow(img(:,:,3));
    

    【讨论】:

    • 是的,你是对的。糟糕的命名约定是我的错。我运行了代码,它确实修复了错误;但是,当我保存它时,它会保存彩色图像。不是灰度的。
    • 我不知道,这取决于你从磁盘读取的图像
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 2023-03-05
    相关资源
    最近更新 更多