1

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

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

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

4

2 回答 2

1

After checking imwrite more closely found that the write option 'bitdepth' is not supported for BMP.

For conversion to 8-bit monochrome (see for instance here) you can try

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

There are other ways of converting to monochrome other than rgb2gray, which uses the luminance channel.

If you want to reduce the color depth there is a link here that explains how you can achieve that with

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

This assumes original is a type uint8 image.

edit

I removed uint8 conversion statements. Before performing such operations one should check whether an image is type double or uint8, and scale values as necessary. I added im2uint8 which accomomdates different input data types.

于 2013-08-29T08:43:58.523 回答
0

此代码用于将图像划分为多个图像部分并保存到磁盘参考链接

% 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
于 2020-09-24T08:32:33.717 回答