- imread: https://www.mathworks.com/help/releases/R2024b/matlab/ref/imread.html
- rgb2gray: https://www.mathworks.com/help/releases/R2024b/matlab/ref/rgb2gray.html
how can i divide image (grayscale) into overlapping blocks 8*8 ??
4 次查看(过去 30 天)
显示 更早的评论
how can i divide image (grayscale) into overlapping blocks 8*8 ??
0 个评论
回答(1 个)
Abhipsa
2025-4-1
To divide a grayscale image into overlapping 8x8 blocks in MATLAB R2024b, nested loops can be used to iterate over the image and extract the blocks.
The following code snippet demonstrates this:
% Read the image
img = imread('sample.jpg');
if size(img, 3) == 3
img = rgb2gray(img); % convert the image to grayscale if it's RGB
end
% Define block size and overlap
blockSize = 8;
overlap = 4; % it can be calculated based on the percentage of overlapping needed
%in the above case, 50% overlapping is considered
% get image dimensions
[img_height, img_width] = size(img);
% Calculate the step size which will determine how much to move the window each time
stepSize = blockSize - overlap;
% initialize a cell array to store the blocks
blocks = {};
blkIdx = 1; %index of the current extracted block
% loop over the image to extract blocks
for row = 1:stepSize:img_height-blockSize+1
for col = 1:stepSize:img_width-blockSize+1
% extract the current block(even partial blocks at the edges have been added)
block = img(row:min(row+blockSize-1, img_height), ...
col:min(col+blockSize-1, img_width));
% store the extracted block in the cell array at blkIdx
blocks{blkIdx} = block;
blkIdx = blkIdx + 1;
end
end
% display the number of blocks extracted
fprintf('Extracted %d blocks.\n', blkIdx-1);
% Display some of the extracted blocks
figure;
for k = 1:min(9, blkIdx-1) % Display the first 9 blocks or fewer
subplot(3, 3, k);
imshow(blocks{k});
title(['Block ' num2str(k)]);
end
You can refer to the following MATLAB documentations for more details:
I hope this helps.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!