使用imwrite MATLAB保存图像

时间:2013-08-29 08:37:44

标签: matlab matlab-figure

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

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

handles.fname已编入索引图像。上面的代码以24位深度的BMP格式将图像保存到桌面。但我需要保存在8位深度。我应该在代码中做些什么改变?

2 个答案:

答案 0 :(得分:1)

更仔细地检查imwrite后发现BMP不支持写选项'bitdepth'。

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

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

rgb2gray转换为单色而不是new_4bit=uint8(16*(round((double(original)+1)/16)-1)); new_3bit=uint8(32*(round((double(original)+1)/32)-1)); ,使用亮度通道。

如果您想减少颜色深度,可以使用链接other来解释如何通过

来实现这一目标。
uint8

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

修改

我删除了im2uint8转化语句。在执行此类操作之前,应检查图像是double类型还是uint8,并根据需要缩放值。我添加了{{1}},它适用于不同的输入数据类型。

答案 1 :(得分:0)

此代码用于将图像分为多个图像部分并保存到磁盘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