繁体   English   中英

在数字图像处理中是否有任何标准算法将输入RGB图像分成所需尺寸的子图像(例8x8)

[英]Is There Any Standard Algorithm In Digital Image Processing that Divides Input RGB image into Sub-Images of Desired Size (Example 8x8)

您好,我是数字图像处理的初学者,我只想知道有没有标准算法? 将输入RGB图像分成所需大小的子图像,然后组合这些子图像以形成原始图像。

% Demo to divide a color image up into blocks.
clc;    % Clear the command window.
close all;  % Close all figures (except those of imtool.)
workspace;  % Make sure the workspace panel is showing.
fontSize = 20;

% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
    % Didn't find it there.  Check the search path for it.
    fullFileName = baseFileName; % No path this time.
    if ~exist(fullFileName, 'file')
        % Still didn't find it.  Alert user.
        errorMessage = sprintf('Error: %s does not exist.', fullFileName);
        uiwait(warndlg(errorMessage));
        return;
    end
end
% Read the image from disk.
rgbImage = imread(fullFileName);

% 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 = 150; % Rows in block.
blockSizeC = 100; % 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); % 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;
    end
end

% Display the original image in the upper left.
subplot(4, 6, 1);
imshow(rgbImage);
title('Original Image');

%==============================================================================
% Another way to split the image up into blocks is to use indexing.
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
    % Didn't find it there.  Check the search path for it.
    fullFileName = baseFileName; % No path this time.
    if ~exist(fullFileName, 'file')
        % Still didn't find it.  Alert user.
        errorMessage = sprintf('Error: %s does not exist.', fullFileName);
        uiwait(warndlg(errorMessage));
        return;
    end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.  numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
figure;
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);

% Divide the image up into 4 blocks.
% Let's assume we know the block size and that all blocks will be the same size.
blockSizeR = 128; % Rows in block.
blockSizeC = 128; % Columns in block.
% Figure out the size of each block. 
wholeBlockRows = floor(rows / blockSizeR);
wholeBlockCols = floor(columns / blockSizeC);
% Preallocate a 3D image
image3d = zeros(wholeBlockRows, wholeBlockCols, 3);
% Now scan though, getting each block and putting it as a slice of a 3D array.
sliceNumber = 1;
for row = 1 : blockSizeR : rows
    for col = 1 : blockSizeR : columns
        % Let's be a little explicit here in our variables
        % to make it easier to see what's going on.
        row1 = row;
        row2 = row1 + blockSizeR - 1;
        col1 = col;
        col2 = col1 + blockSizeC - 1;
        % Extract out the block into a single subimage.
        oneBlock = grayImage(row1:row2, col1:col2);
        % Specify the location for display of the image.
        subplot(2, 2, sliceNumber);
        imshow(oneBlock);
        % Make the caption the block number.
        caption = sprintf('Block #%d of 4', sliceNumber);
        title(caption);
        drawnow;
        % Assign this slice to the image we just extracted.
        image3D(:, :, sliceNumber) = oneBlock;
        sliceNumber = sliceNumber + 1;
    end
end
% Now image3D is a 3D image where each slice, 
% or plane, is one quadrant of the original 2D image.

msgbox('Done with demo!  Check out the two figures.');

您还可以查看此帖子算法,将图像分割成较小的图像,减少空白量并指定最大矩形量

MATLAB为您提供了一个名为blockproc的函数。 在你的情况下,对于输入图像I ,你会说I2 = blockproc(I, [8, 8], @fun); fun是您定义的功能。 http://www.mathworks.com/help/images/ref/blockproc.html显示了简单的示例。

请注意,这实际上仅适用于您想要将图像I拆分为给定大小的图块(例如8 x 8),以fun某种特定方式处理每个图块,然后从结果中重新组合图像I2的情况。每个瓷砖。

结果可以是单个数字或矩阵,也可以使用,但是blockproc的使用实际上仅限于分割为切片,处理每个切片,然后将结果重新组合成输出图像的情况。 然而,在那,它是非常方便的。

你可以使用reshape来实现这一目标。 例如,假设我们有一个64x64像素的RGB图像

im=rand(64,64,3);

我们把它分成8x8矩阵

ims=reshape(im,8,8,3,[]);
size(ims)
ans =
 8     8     3    64

im=reshape(ims,64,64,[]); 将它恢复到与它相同的大小。

所以这是将图像划分为64个子矩阵的粗略示例,每个子矩阵都保留其RGB分量。 很可能你需要的东西,不会使用这种通用的重塑,而是需要在本地邻域上应用一些功能。 例如,使用medfilt2(im,[8 8])或图像stdfilt(im, [8 8])或已经提到的blockproc等的局部标准偏差的2-d中值文件...所以当你得到至于你会知道你想要什么,请问一个更具体的问题......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM