How to partition an image

Hi all
I am working on some algorithm to implement it and in one stage I have been confused.Lets say that I have an image 512x512.
I want to make image partitioned in nxn squarre like regions that are overlapped with the same gap g for both image.So n=12 and g=3.
Thanks in Advance

 Accepted Answer

Image Analyst
Image Analyst on 29 Jan 2013

0 votes

Use blockproc() with the BorderSize option.

32 Comments

Thanks for your response but how can I overlapped them with gap g as i used
I=blockproc(image,[8 8],'std2(image)*ones(size(image))')
I don't see 12, g, or BorderSize specified in your equation. Make sure you send those in.
According to paper on local kernel color histogram by ( http://philippe.noriega.free.fr/fichiers/visapp06.pdf)
The first step is to partition an image like nxn square regions with same gap g where n=12 and g=3 so I did it like that but i am not sure is this correct
clc
close all
clear all
source='lab.AVI';
vidinput=videoreader(source);
frames=vidinput.NumberofFrames;
for f=1:frames
thisframe=read(vidinput,f);
figure(1);imshow(thisframe);
% Local kernel Color Histograms Implementation
% First step is to partitioned image in to nxn square like regions which is
% overlapped with same gap g =3 and n=12
thisframe=rgb2gray(thisframe);
thisframe=double(thisframe);
[M N Colors]=size(thisframe);
fun=mean2(thisframe)*ones(size(thisframe));
thisframe=blkproc(thisframe,[12 12],[3 3],'std2(thisframe)*ones(size(thisframe))');
figure(2);imshow(uint8(thisframe));
end
I don't know if I still have the obsolete function blkproc. I do have blockproc, and in that I believe you have to pass in the bordersize as a name,value pair, like 'BorderSize', [3 3].
yes as you told I have passed these values like that
image=imread('lena.bmp');
fun=@(block_struct) std2(block_struct.data)*ones(size(block_struct.data))
B=blockproc(image,[12 12],fun,'BorderSize',[3 3])
Now where is overlapping region?As I intended to segment image into to overlapped local squares of size nxn where n =12 and gap g=3.
fun = @(block_struct) {block_struct.data};
B = blockproc(image,[6 6],fun,'BorderSize',[3 3]);
Now B should be a cell array, with each cell being an array 12 x 12 of pixels including the overlap.
Good response but getting another crucial error when I compile it
image=imread('lena.bmp');
fun=@(block_struct) {block_struct.data};
B=blockproc(image,[6 6],fun,'BordeerSize',[3 3]);
>> fun=@(block_struct) {block_struct.data}; >> B=blockproc(image,[6 6],fun,'BorderSize',[3 3]) ??? Error using ==> zeros Trailing string input must be a valid numeric class name.
Error in ==> blockprocInMemory at 122 b = zeros(final_size,class(ul_output));
Error in ==> blockproc at 248 result_image = blockprocInMemory(a,block_size,fun,border_size,...
Trailing input strings must be a valid class name?where is problem image class is double.I changes it logical(image) but still the same..
fun = @(block_struct) block_struct.data;
T = blockproc(image,[6 6],fun,'BorderSize',[3 3]);
B = mat2cell( T, 12 * ones(1, size(T,1)/12), 12 * ones(1, size(T,2)/12 );
Good but another error arrised
fun = @(block_struct) block_struct.data;
T = blockproc(image,[6 6],fun,'BorderSize',[3 3]);
B = mat2cell( T, 12 * ones(1, size(T,1)/12), 12 * ones(1, size(T,2)/12 );
Number of input vector arguments, 2, does not match the input matrix's number of dimensions, 3.
May be this is because of the sizes because size(T) is 512x512x3 but size(12*ones(1,size(T,1)/12)) is 1x42 I replicate it but not get solution..
Ummm right.
B = mat2cell( T, 12 * ones(1, size(T,1)/12), 12 * ones(1, size(T,2)/12), 3 );
(I had also missed a ')' earlier)
Yes another error arises in their sizes as I told earlier
size(T) is 512x512x3
and
size(12*ones(1,size(T,1))/12) is 1x42
therefore I am getting another error
Input arguments, D1 through D3, must sum to each dimension of the input matrix size, [512 512 3]. How solve this issue?Thank u very much
Please re-check size(T) against size(image). They should not be the same.
You put a ')' after ones(1,size(T,1) and before '/12' but it should not be there. Like I corrected above,
B = mat2cell( T, 12 * ones(1, size(T,1)/12), 12 * ones(1, size(T,2)/12), 3 );
Notice the divisions are inside the ones() calls.
12 * ones(1, size(T,1)/12) is another way of expressing repmat(12, 1, size(T,1)/12)
Yes size(T) and size(image) both are same as 512x512x3 in color case and 512x512 in grayscale.As my code below is
image=imread('lena.bmp');|%size(image) 512x512x3
fun=@(block_struct) block_struct.data
T=blockproc(image,[6 6],fun,'BorderSize',[3 3]);%size(T) 512x512x3
B = mat2cell( T, 12 * ones(1, size(T,1)/12), 12 * ones(1, size(T,2)/12), 3);
The error is the same.
You did not mention the warning,
Warning: Size vector should be a row vector with integer elements.
Unfortunately I do not have Image Processing Toolbox so I cannot experiment with blockproc().
Out of curiosity, what do you get if you use
blockproc(image, [6 6], @(block_struct) length(block_struct.data), 'BorderSize', [3 3])
It should be an array of constant values, but is it 6's or 12's ?
Ah...
T = blockproc(image,[6 6],fun,'BorderSize',[3 3], 'TrimBorder', 'false');
Okay, now we still have a problem: your 512 x 512 x 3 image is not evenly divisible into 12 x 12 with overlap of 3. 12 is divisible by 3, the overlap is divisible by 3, so (as a first-pass calculation) if the image size is not divisible by 3 then you are not going to be able to divide it exactly. What do you want to have happen with the bit that is necessarily left over? See the 'PadPartialBlocks' blockproc() option.
Algorithms Analyst
Algorithms Analyst on 5 Feb 2013
Edited: Walter Roberson on 5 Feb 2013
Yes exactly this is what I was thinking about it.Actually I am implementing the algorithm stated in the link
I am on Image partitioning part which says about it. Please read it for any confusion only image partitioning part on page no 2.
The only solution i am thinking it is to divide it by 8x8 as 512/8 gives even number.But i need to work on overalpping region cannot find overlapping block in cell array as you stated.
Thanks you for concern..
What happens if you use TrimBorder false? What size does T come out as?
Would the later results be thrown off if you used the replicate pad option?
As stated in paper that
'The image is partitioned in nxn square like regions that are overlapped with the same gap g for both the image axis coordinates.So excluding the image edges,a pixel belongs to N=(n/g).^2 regions' where n=12 and g=3
Right, I read that much earlier. But mechanically, what happens when you use TrimBorder false ? Without being concerned about the partial blocks right at the moment.
Yes I used it but getting error as below.
??? Error using ==> blockproc>checkTrimBorder at 999 Invalid "TrimBorder" parameter. BLOCKPROC expects the TrimBorder parameter to a logical scalar.
Error in ==> blockproc>parse_inputs at 916 validateFcn(param_value);
Error in ==> blockproc at 215 input_args = parse_inputs(input_args,Input,block_size,fun,varargin{:});
And yes you can remove the previous results.Thanks....
may be the author has skipped some information about image partitioning for summarizing purpose..
T = blockproc(image,[6 6],fun,'BorderSize',[3 3], 'TrimBorder', false);
Ah.The size of T is now size(T)=1028x1028x3.
now where is overlapping region?
It moves over by 6 pixels, but it takes an extra 3 pixels on each side when it gets the block of pixels, so it takes a width of 3+6+3 = 12. The window width is thus 12 but it only moves over by 6 each time, so there is an overlap of 6 (=12-6) pixels each time.
Thanks for your response.So Can I only show the overlap region?
Sure, if you want. Have blockproc() call a custom routine that you wrote. In there you have the whole 12x12 block. If you want, you could blacken the central 6x6 chunk of the block so that it is a square annulus. Then use imshow() to display that annulus. I have no idea why you'd want to do this though.
thankyou very much.....just for my exploration I want it...thanks alot..
The 1028 is due to there being partial block 2 pixels wide. mod(512,6) = 2. That block gets extended by the overlap, 3 pixels before and 3 after, to become a total of 8 pixel. 85 full blocks of 12, plus the partial 8, gives 1028. The 85 is floor(512/6). You need to take this partial block into account unless you request padding of the blocks out to full width.
I am not clear in that point kindly explain it plz with some piece of code.Thanks.I further quantize the colors by using the K-means algorithm with K=16 as described in paper..

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!