Split image into blocks and rejoin....

Im trying to split an image into block
I will eventually do something to each block
I then want to rejoin blocks to make a new image.
When trying to rejoin my blocks I'm getting the below error:
Unable to perform assignment because the size of the left side is 50-by-1-by-50 and the size of the right side is 50-by-50-by-3.
Error in blocktest (line 57)
imMatr((r-1)*blockRows+1:r*blockRows,rem(rows, blockSizeR),...
Can someone please help
I = imread('hide.jpg');
[rows, columns, planes] = size(I);
blockSizeR = 50; % Rows in block.
blockSizeC = 50; % Columns in block.
FullBlockRows = floor(rows / blockSizeR);
blockVectorR = [blockSizeR * ones(1, FullBlockRows), rem(rows, blockSizeR)];
% Figure out the size of each block in columns.
FullBlockColums = floor(columns / blockSizeC);
blockVectorC = [blockSizeC * ones(1, FullBlockColums), rem(columns, blockSizeC)];
% Create the cell array
Cellarray = mat2cell(I, blockVectorR, blockVectorC, planes);
% Display all the blocks.
count =1;
plotIndex = 1;
numPlotsR = size(Cellarray, 1);
numPlotsC = size(Cellarray, 2);
for r = 1 : numPlotsR
for c = 1 : numPlotsC
fprintf('plotindex = %d, c=%d, r=%d\n', plotIndex, c, r);
subplot(numPlotsR, numPlotsC, plotIndex);
rgbBlock = Cellarray{r,c};
imshow(rgbBlock);% Could call imshow(ca{r,c})
[rowsB, columnsB, numberOfColorBandsB] = size(rgbBlock);
caption = sprintf('B#%d/%d', ...
plotIndex, numPlotsR*numPlotsC);
title(caption);
drawnow;
% set dimensions for cell holding RGB Blocks
num_cols = FullBlockColums;
num_rows = FullBlockRows;
cellBlocks = cell(num_rows, num_cols);
% input block dimensions
blockRows = blockSizeR;
blockCols = blockSizeC;
% calculate dimensions of image
numPixWidth = rows;
numPixHeight = columns;
numChannels = 3;
% allocate memory with zeros matrix
imMatr = zeros(numPixWidth, numPixHeight, numChannels);
block = Cellarray{r,c}
fprintf('plotindex = %d, col=%d, row=%d\n', plotIndex, c, r);
figure
imshow(block)
imMatr((r-1)*blockRows+1:r*blockRows,rem(rows, blockSizeR),...
(c-1)*blockCols +1:c*blockCols,rem(columns, blockSizeC), :) = block;
figure
imshow(imMatr)
% Increment the subplot to the next location.
plotIndex = plotIndex + 1;
end
% show matrix
figure;
imshow(imMatr(:,:,1)); %showing only red channel
end

 Accepted Answer

Count the commas in
imMatr((r-1)*blockRows+1:r*blockRows,rem(rows, blockSizeR),...
(c-1)*blockCols +1:c*blockCols,rem(columns, blockSizeC), :) = block;
You have 4 of them, meaning 5 dimensions. You have
row1 = (r-1)*blockRows+1
row2 = r*blockRows,
columnRange = rem(rows, blockSizeR),...
index3RangeStart = (c-1)*blockCols +1
index3RangeEnd = c*blockCols
index4 Range = rem(columns, blockSizeC)
index5 = :; % (everything);
Why do yo uhave 5 dimensions? What's with those rem() functions in there???
I suggest you simplify it by making 4 variables:
row1 = whatever
row2 = whatever
col1 = whatever
col2 = whatever
% Now use them
imMatr(row1:row1, col1:col2, :) = block;

6 Comments

doesn't work.
still get the error:
Unable to perform assignment because the size of the left side is 50-by-1-by-50 and the size of the right side is 50-by-50-by-3.
and imMar does not represent the collage of blocks when run
You forgot to upload your improved code. How did you fix the 5-dimensions problem? Make it easy for me to help you not hard. Upload your latest code each time you comment.
And do I need 'hide.jpg'? Or can we use any color image?
Hey man, thanks for the swift reply,
You can use any colour image.
Come on buddy make my christmas wish come true: all i want is to have a new image of the combined blocks. In this case because I haven't actually done anything to any of the blocks it should just look exactly the same as the orignal image.
Saud
I = imread('hide.jpg');
[rows, columns, planes] = size(I);
blockSizeR = 50; % Rows in block.
blockSizeC = 50; % Columns in block.
FullBlockRows = floor(rows / blockSizeR);
blockVectorR = [blockSizeR * ones(1, FullBlockRows), rem(rows, blockSizeR)];
% Figure out the size of each block in columns.
FullBlockColums = floor(columns / blockSizeC);
blockVectorC = [blockSizeC * ones(1, FullBlockColums), rem(columns, blockSizeC)];
% Create the cell array
Cellarray = mat2cell(I, blockVectorR, blockVectorC, planes);
% Display all the blocks.
count =1;
plotIndex = 1;
numPlotsR = size(Cellarray, 1);
numPlotsC = size(Cellarray, 2);
for r = 1 : numPlotsR
for c = 1 : numPlotsC
fprintf('plotindex = %d, c=%d, r=%d\n', plotIndex, c, r);
subplot(numPlotsR, numPlotsC, plotIndex);
rgbBlock = Cellarray{r,c};
imshow(rgbBlock);% Could call imshow(ca{r,c})
[rowsB, columnsB, numberOfColorBandsB] = size(rgbBlock);
caption = sprintf('B#%d/%d', ...
plotIndex, numPlotsR*numPlotsC);
title(caption);
drawnow;
% set dimensions for cell holding RGB Blocks
num_cols = FullBlockColums;
num_rows = FullBlockRows;
cellBlocks = cell(num_rows, num_cols);
% input block dimensions
blockRows = blockSizeR;
blockCols = blockSizeC;
% calculate dimensions of image
numPixWidth = rows;
numPixHeight = columns;
numChannels = 3;
% allocate memory with zeros matrix
imMatr = zeros(numPixWidth, numPixHeight, numChannels);
block = Cellarray{r,c}
fprintf('plotindex = %d, col=%d, row=%d\n', plotIndex, c, r);
row1 = (r-1)*blockRows+1
row2 = r*blockRows
col1 = (c-1)*blockCols +1
col2 = c*blockCols
imMatr(row1:row2, col1:col2, :) = block;
% Increment the subplot to the next location.
plotIndex = plotIndex + 1;
end
% show matrix
figure;
imshow(imMatr(:,:,1)); %showing only red channel
end
You forgot to check to see if any of the blocks would be past the edge of the image when you pasted them on to the output image. Corrected code is below:
I = imread('peppers.png');
[rows, columns, planes] = size(I);
blockSizeR = 50; % Rows in block.
blockSizeC = 50; % Columns in block.
FullBlockRows = floor(rows / blockSizeR);
blockVectorR = [blockSizeR * ones(1, FullBlockRows), rem(rows, blockSizeR)];
% Figure out the size of each block in columns.
FullBlockColums = floor(columns / blockSizeC);
blockVectorC = [blockSizeC * ones(1, FullBlockColums), rem(columns, blockSizeC)];
% Create the cell array
Cellarray = mat2cell(I, blockVectorR, blockVectorC, planes);
% Display all the blocks.
count = 1;
plotIndex = 1;
numPlotsR = size(Cellarray, 1);
numPlotsC = size(Cellarray, 2);
% Create an output image
outputImageRows = sum(blockVectorR)
outputImageColumns = sum(blockVectorC)
reconstructedRgbImage = zeros(outputImageRows, outputImageColumns, planes, 'uint8');
for r = 1 : numPlotsR
for c = 1 : numPlotsC
fprintf('Plot index = %d, c=%d, r=%d\n', plotIndex, c, r);
subplot(numPlotsR, numPlotsC, plotIndex);
% Get this block.
rgbBlock = Cellarray{r, c};
% Display this block.
imshow(rgbBlock);% Could call imshow(ca{r,c})
[rowsB, columnsB, numberOfColorBandsB] = size(rgbBlock);
caption = sprintf('B#%d/%d', ...
plotIndex, numPlotsR*numPlotsC);
title(caption);
drawnow;
% Load this block into the output matrix.
fprintf('plotindex = %d, col=%d, row=%d\n', plotIndex, c, r);
row1 = (r-1)*blockRows+1;
row2 = r*blockRows;
if row2 > outputImageRows
row2 = outputImageRows;
end
col1 = (c-1)*blockCols +1;
col2 = c*blockCols;
if col2 > outputImageColumns
col2 = outputImageColumns;
end
fprintf('Pasting block #%d into rows %d-%d, columns %d-%d\n', ...
plotIndex, row1, row2, col1, col2);
reconstructedRgbImage(row1:row2, col1:col2, :) = rgbBlock;
% Increment the subplot to the next location.
plotIndex = plotIndex + 1;
end
end
% Show images
figure;
subplot(2, 2, 1);
imshow(reconstructedRgbImage); % Showing full RGB iamge.
title('Reconstructed RBG Image', 'FontSize', 20);
subplot(2, 2, 2);
imshow(reconstructedRgbImage(:,:,1)); %showing only red channel
title('Red Channel Image', 'FontSize', 20);
subplot(2, 2, 3);
imshow(reconstructedRgbImage(:,:,2)); %showing only green channel
title('Green Channel Image', 'FontSize', 20);
subplot(2, 2, 4);
imshow(reconstructedRgbImage(:,:,3)); %showing only blue channel
title('Blue Channel Image', 'FontSize', 20);
Change your name to Image Angel. Wow oh wow.
Thank you so so much, I hope you're able to get this message. I wish to find a way to express my gratitude.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!