Dividing image into grid - imwrite breaks when processing an empty image
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
David Bukowski
el 15 de Jun. de 2021
Comentada: David Bukowski
el 15 de Jun. de 2021
Hello All,
Basically, I have a set of code that will split images into equal sized squares. At the end, the code names each square according to a grid pattern. (Letters on x-axis, numbers on y.) However, the imwrite function at the very end gives me the following error:
Error using imwrite (line 442)
Expected DATA to be nonempty.
Error in simultaneous (line 92)
imwrite(ca{r,c}, filename);
I know this is because it tried to create a column where none of the picture exists. In my most recent attempt, I set the numbers so that the original picture would split into a 2x2 grid. However, the program tried to create a 3rd column, column AC, and subsequently tried to generate image AC_001. Since AC_001 was empty, the program broke.
Does anyone have a solution to stop my code from breaking?
Full code is attached below, the problem area is at the very bottom.
Thank you!
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\imdata');
baseFileName = 'cameraman.tif';
% 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);
% 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); % Could call imshow(ca{r,c}) if you wanted to.
[rowsB columnsB numberOfColorBandsB] = size(rgbBlock);
% Make the caption the block number.
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');
for r = 1 : numPlotsR
for c = 1 : numPlotsC
filename = sprintf('%s_%03d.png',append(char(idivide(c,int32(26),"floor")+65),char(mod(c,26)+64)), r)
imwrite(ca{r,c}, filename);
end
end
0 comentarios
Respuesta aceptada
KSSV
el 15 de Jun. de 2021
Error is clear right..it seems there is no data in ca{r,c} in the case when error pops. Keep a check whether data is empty or not and proceed.
if ~isempty(ca{r,c})
imwrite(ca{r,c}, filename);
end
Más respuestas (0)
Ver también
Categorías
Más información sobre Red en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!