MATLAB Answers

Dividing image into grid - imwrite breaks when processing an empty image

5 ビュー (過去 30 日間)
David Bukowski
David Bukowski 2021 年 6 月 15 日
コメント済み: David Bukowski 2021 年 6 月 15 日
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

採用された回答

KSSV
KSSV 2021 年 6 月 15 日
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
  1 件のコメント
David Bukowski
David Bukowski 2021 年 6 月 15 日
You are an absolute saint, thank you so much! It worked when I tested it on my small program. I'm going run my large program overnight. If that doesn't work, I'll let you know.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by