2016-07-11 9 views
1

Ich habe eine Funktion, die Flecken von 32x32 Pixel für das gegebene Bild erstellt. Es gibt eine Zelle zurück, die alle Patches enthält. Wenn das Bild im Format 350 * 350 * 3 ist, funktioniert es gut. Wenn das Bild jedoch das Format 256 * 150 hat, wird die Zelle mit leeren Bildern zurückgegeben. Lustige Sache ist, wenn ich den Code debugge, erstelle die Flecken innerhalb der Zelle fein, aber wenn er diese Flecken innerhalb der Zelle zurückbringt, wird die Zelle leer. Ich versuche, solche Bilder unter Verwendung des setimage Codes zu speichern. Könnte jemand dabei helfen?Erstellen von Patches für Deeplearning mit 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 

Antwort

1

Gerade diese Zeile hinzufügen, während für die mean2 Wertg des Blocks überprüft:

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

Danke, das nicht die iMAGESET Problem nicht erklärt, sondern macht Sinn und löst mein Problem. – Mensch

Verwandte Themen