Split image into blocks and rejoin....
4 views (last 30 days)
Show older comments
Saud Alfalasi
on 16 Dec 2020
Commented: Saud Alfalasi
on 19 Dec 2020
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
0 Comments
Accepted Answer
Image Analyst
on 16 Dec 2020
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
More Answers (0)
See Also
Categories
Find more on Red 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!