【问题标题】:Saving images using imwrite MATLAB使用 imwrite MATLAB 保存图像
【发布时间】:2020-09-24 16:32:33
【问题描述】:

我有一个索引图像保存在轴 MATLAB GUI 中

file = 'C:\Documents and Settings\Home\Desktop\new.bmp';
        imwrite(handles.fname, file);
        imfinfo(file)

handles.fname 已索引图像。上面的代码以 24 位深度的 BMP 格式将图像保存到桌面。但我需要保存 8 位深度。我应该对代码进行哪些更改?

【问题讨论】:

  • 要实现8位图像,是降低色深还是转灰度?

标签: matlab matlab-figure


【解决方案1】:

在仔细检查imwrite 后发现 BMP 不支持写入选项“位深度”。

要转换为 8 位单色(例如参见 here),您可以尝试

imwrite(rgb2gray(im2uint8(handles.fname)), file)

除了使用亮度通道的rgb2gray 之外,还有other 转换为单色的方法。

如果你想减少颜色深度,有一个链接 here 解释了如何实现这一目标

new_4bit=uint8(16*(round((double(original)+1)/16)-1));
new_3bit=uint8(32*(round((double(original)+1)/32)-1));

这假设 original 是一个类型为 uint8 的图像。

编辑

我删除了uint8 转换语句。在执行此类操作之前,应检查图像是 double 类型还是 uint8 类型,并根据需要缩放值。我添加了im2uint8,它可以容纳不同的输入数据类型。

【讨论】:

  • @Adiel 文档指出imwrite 支持“BMP 1 位、8 位和 24 位未压缩图像”,但正确的选项不是 bitdepth
  • 是的,谢谢,imwrite(uint8(rgb2gray(handles.fname)), file) 产生了 8 位深度。但是图像很暗!
  • @Chethan 问题可能出在图像 handle.fname 的数字格式上。如果它是双倍但范围为 0-255,则需要在显示之前除以 255。如果是这样,您还需要在灰度转换之前将其除以 255。
【解决方案2】:

此代码用于将图像分成多个图像部分并保存到磁盘reference link

% clc;    % Clear the command window.
% close all;  % Close all figures (except those of imtool.)
% Read the image from disk.
rgbImage = imread('image1.jpeg');
% Test code if you want to try it with a gray scale image.
% Uncomment line below if you want to see how it works with a gray scale image.
% rgbImage = rgb2gray(rgbImage);
% Display image full screen.
imshow(rgbImage);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
drawnow;
% Get the dimensions of the image.  numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
%==========================================================================
% The first way to divide an image up into blocks is by using mat2cell().
blockSizeR = 128; % Rows in block.
blockSizeC = 128; % Columns in block.
% Figure out the size of each block in rows.
% Most will be blockSizeR but there may be a remainder amount of less than that.
wholeBlockRows = floor(rows / blockSizeR);
blockVectorR = [blockSizeR * ones(1, wholeBlockRows), rem(rows, blockSizeR)];
% Figure out the size of each block in columns.
wholeBlockCols = floor(columns / blockSizeC);
blockVectorC = [blockSizeC * ones(1, wholeBlockCols), rem(columns, blockSizeC)];
% Create the cell array, ca. 
% Each cell (except for the remainder cells at the end of the image)
% in the array contains a blockSizeR by blockSizeC by 3 color array.
% This line is where the image is actually divided up into blocks.
if numberOfColorBands > 1
    % It's a color image.
    ca = mat2cell(rgbImage, blockVectorR, blockVectorC, numberOfColorBands);
else
    ca = mat2cell(rgbImage, blockVectorR, blockVectorC);
end
% Now display all the blocks.
plotIndex = 1;
numPlotsR = size(ca, 1);
numPlotsC = size(ca, 2);
for r = 1 : numPlotsR
    for c = 1 : numPlotsC
        fprintf('plotindex = %d,   c=%d, r=%d\n', plotIndex, c, r);
        % Specify the location for display of the image.
        subplot(numPlotsR, numPlotsC, plotIndex);
          % Extract the numerical array out of the cell
          % just for tutorial purposes.
          rgbBlock = ca{r,c};
          imshow(rgbBlock);
          %write images
          imwrite(rgbBlock,plotIndex+"_image.jpeg");
          % Could call imshow(ca{r,c}) if you wanted to.
          [rowsB columnsB numberOfColorBandsB] = size(rgbBlock);
          % Make the caption the block number.
          caption = sprintf('Block #%d of %d\n%d rows by %d columns', ...
              plotIndex, numPlotsR*numPlotsC, rowsB, columnsB);
          title(caption);
          drawnow;
          % Increment the subplot to the next location.
          plotIndex = plotIndex + 1;
           %imwrite(rgbBlock,c+"_image.jpeg");
    end
end
  



% Display the original image in the upper left.
%subplot(4, 6, 1);
%imshow(rgbImage);
%title('Original Image');
% Inform user of next stage where we process a gray scale image.
promptMessage = sprintf('Now I will do the same for a gray scale image.');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'OK', 'Cancel', 'OK');
if strcmpi(button, 'Cancel')
    return;
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多