Clear Filters
Clear Filters

How do I divide an image into possible number of blocks and apply a set of functions to each block?

1 view (last 30 days)
Here is a sample image.

Answers (2)

Walter Roberson
Walter Roberson on 7 Apr 2018
Use blockproc() .

vahid rowghanian
vahid rowghanian on 4 Apr 2021
Edited: vahid rowghanian on 10 Jun 2021
If you want to divide the image into squares of size gs ( e.g. 2,3,4,5,6,7,8,9,10,…), you can use the following code. This function extracts gs by gs patches from the input image and stores them into 'patch' variable.
function [ patchim , npatchim ] = divideimage (im , gs)
m = size(im,1);
n = size(im,2);
if mod(m,gs)>0
m = m - mod(m,gs) + gs;
end
if mod(n,gs)>0
n = n - mod(n,gs) + gs;
end
k1 = m / gs;
k2 = n / gs;
npatch = 1;
patch = zeros(gs,gs,3,k1*k2);% for 3-D images
im = imresize(im,[m,n]);
for i = 1 : k1
for j = 1 : k2
rmin = ( i - 1 ) * gs + 1;
rmax = i * gs;
cmin = ( j - 1) * gs + 1;
cmax = j * gs;
%%%%% do processes
patch ( : , : , : , npatch) = im( rmin : rmax , cmin : cmax , : );
npatch = npatch + 1;
end
end
npatchim = npatch - 1;
patchim = patch;
end

Community Treasure Hunt

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

Start Hunting!