2016-07-11 9 views
1

私は与えられた画像に対して32x32ピクセルのパッチを作成する関数を持っています。すべてのパッチを含むセルを返します。イメージのフォーマットが350 * 350 * 3の場合は、イメージのフォーマットが256 * 150の場合は空のイメージのセルが返されますが、イメージは正常に動作します。面白いのは、セル内のパッチを作成するコードをデバッグする場合ですが、そのセル内のパッチを返すと、セルは空になります。setimageコードを使用してそのようなイメージを保存しようとしています。誰もがこれを助けることができますか?matlabを使用してディーププレイングのパッチを作成する

% 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 

答えて

1

ブロックのmean2のvalueeをチェックしながら、ただ、これらの行を追加します。imageSetの問題を説明するが、理にかなっていると私の問題を解決していません

if mean2(rgbBlock) < 15|isempty(rgbBlock) == 1 
+0

おかげで、。 – Mensch

関連する問題