Divide an image into 16x128x128 tiles each
3 views (last 30 days)
I have an image which I want to divide into several blocks as defined in sample.png. This image file is just a demo image like whom I want to divide my original image CCffdmdatabase1.png. I want to divide my oroginal image file into the same way as shown in sample.png. I am planning to analyze each block separately later. I tried the following line of code. My final image is shown in result.png. This file is different from the sample.png file. Any suggestions would be appreciated.
rgbImage = imread('CCffdmdatabase1.png');
% For demo purposes, let's resize it to be 64 by 64;
rgbImage = imresize(rgbImage, [64 64]);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage)
ca = mat2cell(rgbImage,8*ones(1,size(rgbImage,1)/8),8*ones(1,size(rgbImage,2)/8),3);
plotIndex = 1;
for c = 1 : size(ca, 2)
for r = 1 : size(ca, 1)
fprintf('c=%d, r=%d\n', c, r);
plotIndex = plotIndex + 1
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
Animesh Gupta on 30 Aug 2022
It is my understanding that you want to divide the image into several blocks.
The idea is to extract the interested rows and columns for that part of the image, for which the block represents.
That is, for the 1st block, it should extract rows of index [1,8], columns of index [1,8] and for 2nd block it should extract rows of index [1,8], columns of the index [2,9], and so on.
You may refer the following script to undestand this procedure.
M1 = imread("CCffdmdatabase1.png");
J = imresize(M1, [64 64]);
[numRows, numCols, numChannels] = size(J);
blockSize = 8; % Block Size (32x32)
index = 1;
for i = 1:(numRows/blockSize)
for j = 1:(numCols/blockSize)
imageTile = J((blockSize * (i-1) + 1 : blockSize * (i-1) + blockSize), ... % number of rows
(blockSize * (j-1) + 1 : blockSize * (j-1) + blockSize), ... % number of columns
: ... % to include all the channels
); % interested image for the block at index position
subplot(numRows/blockSize, numCols/blockSize, index); % for creating a subplot as a tile in 8X8 plot
index = index + 1;
I hope it helps.