You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to partition an image
2 views (last 30 days)
Show older comments
Algorithms Analyst
on 29 Jan 2013
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
on 29 Jan 2013
Use blockproc() with the BorderSize option.
32 Comments
Algorithms Analyst
on 3 Feb 2013
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))')
Image Analyst
on 3 Feb 2013
I don't see 12, g, or BorderSize specified in your equation. Make sure you send those in.
Algorithms Analyst
on 4 Feb 2013
Edited: Walter Roberson
on 5 Feb 2013
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
Image Analyst
on 4 Feb 2013
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].
Algorithms Analyst
on 5 Feb 2013
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.
Walter Roberson
on 5 Feb 2013
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.
Algorithms Analyst
on 5 Feb 2013
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..
Walter Roberson
on 5 Feb 2013
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 );
Algorithms Analyst
on 5 Feb 2013
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..
Walter Roberson
on 5 Feb 2013
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)
Algorithms Analyst
on 5 Feb 2013
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
Walter Roberson
on 5 Feb 2013
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)
Algorithms Analyst
on 5 Feb 2013
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.
Walter Roberson
on 5 Feb 2013
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 ?
Walter Roberson
on 5 Feb 2013
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
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..
Walter Roberson
on 5 Feb 2013
What happens if you use TrimBorder false? What size does T come out as?
Walter Roberson
on 5 Feb 2013
Would the later results be thrown off if you used the replicate pad option?
Algorithms Analyst
on 5 Feb 2013
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
Walter Roberson
on 5 Feb 2013
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.
Algorithms Analyst
on 5 Feb 2013
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{:});
Algorithms Analyst
on 5 Feb 2013
And yes you can remove the previous results.Thanks....
Algorithms Analyst
on 5 Feb 2013
may be the author has skipped some information about image partitioning for summarizing purpose..
Walter Roberson
on 5 Feb 2013
T = blockproc(image,[6 6],fun,'BorderSize',[3 3], 'TrimBorder', false);
Algorithms Analyst
on 5 Feb 2013
Ah.The size of T is now size(T)=1028x1028x3.
Algorithms Analyst
on 5 Feb 2013
now where is overlapping region?
Image Analyst
on 5 Feb 2013
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.
Algorithms Analyst
on 5 Feb 2013
Thanks for your response.So Can I only show the overlap region?
Image Analyst
on 5 Feb 2013
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.
Algorithms Analyst
on 6 Feb 2013
thankyou very much.....just for my exploration I want it...thanks alot..
Walter Roberson
on 6 Feb 2013
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.
Algorithms Analyst
on 6 Feb 2013
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..
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)