【发布时间】:2016-11-13 10:24:48
【问题描述】:
我有一个函数可以为给定的图像创建 32x32 像素的补丁。它返回一个包含所有补丁的单元格。如果图像的格式为 350*350*3,则效果很好,但如果图像的格式为 256*150,则返回带有空图像的单元格。有趣的是,如果我调试它在单元格内创建补丁的代码很好,但是当它在单元格内返回这些补丁时,单元格变为空。我正在尝试使用 setimage 代码保存这些图像。有人可以帮忙吗?
% Demo to divide a color image up into blocks.
function [imageSet] = CreatePatches(imag)
fontSize = 20;
%rgbImage = imread(imag);
rgbImage =imag;
% 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)
%==========================================================================
% divide an image up into blocks is by using mat2cell().
blockSizeR = 32; % Rows in block.
blockSizeC = 32; % 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};
if mean2(rgbBlock) < 15 % Or whatever value you want
continue;
end
% imshow(rgbBlock); % Could call imshow(ca{r,c}) if you wanted to.
[rowsB columnsB numberOfColorBandsB] = size(rgbBlock);
%imwrite(ca{r,c},['image',num2str(plotIndex),'.jpeg']);
% 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;
imageSet ={};
for x =1: plotIndex
%imshow(rgbBlock);
imageSet{end+1} = rgbBlock;
end
end
end
【问题讨论】:
标签: matlab image-processing deep-learning