How to partition an image
4 次查看(过去 30 天)
显示 更早的评论
Algorithms Analyst
2013-1-29
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
采纳的回答
Image Analyst
2013-1-29
Use blockproc() with the BorderSize option.
32 个评论
Algorithms Analyst
2013-2-3
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
2013-2-3
I don't see 12, g, or BorderSize specified in your equation. Make sure you send those in.
Algorithms Analyst
2013-2-4
编辑:Walter Roberson
2013-2-5
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
2013-2-4
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
2013-2-5
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
2013-2-5
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
2013-2-5
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
2013-2-5
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
2013-2-5
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
2013-2-5
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
2013-2-5
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
2013-2-5
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
2013-2-5
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
2013-2-5
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
2013-2-5
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
2013-2-5
编辑:Walter Roberson
2013-2-5
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
2013-2-5
Would the later results be thrown off if you used the replicate pad option?
Algorithms Analyst
2013-2-5
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
2013-2-5
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
2013-2-5
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
2013-2-5
may be the author has skipped some information about image partitioning for summarizing purpose..
Image Analyst
2013-2-5
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.
Image Analyst
2013-2-5
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
2013-2-6
thankyou very much.....just for my exploration I want it...thanks alot..
Walter Roberson
2013-2-6
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
2013-2-6
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..
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)