Split image into blocks and rejoin....
Show older comments
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
More Answers (0)
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
