how to calculat the sum of pixel vaules inside a particular region

1 次查看(过去 30 天)
hai
i am studing about localization of the optic disc
here i have to define two feature maps namely
1.vertical map
2. horizontal map
i have completed that
my second step is to define a rectangular window of width=30 and height=image height in vertical map
third step is two move the window from left to right and calculate the sum of pixel values inside the rectangular region in the vertical map
as i can't find a function to move the rectangle of specified height and width and then calculating the sum i have used the following code for calculating the sum
but i am getting the error as index exceeds matrix dimension and this is my error please help me
[width, height] = size(final2);
count=20;
inc1=1;
k=0;
for k=1:count
sum(k)=0;
end
a=1;
b=30;
while(inc1 < count-1)
for i=1:700
for j=a:b
sum(inc1)=sum(inc1)+final2(j,i);
end
a=a+30;
b=b+30;
end
i =inc1+1;
end
thanks

回答(1 个)

Andrei Bobrov
Andrei Bobrov 2011-8-29
variant 1
final2 = randi([2 25],5,25);
b = 5;
[width, height] = size(final2);
sum1 = squeeze(sum(reshape(final2,width,b,[]),2))
variant 2
idl = cumsum([1:b; b*ones(fix(height/b)-1,b)]);
sum1 = cell2mat(arrayfun(@(i1)sum(final2(:,idl(i1,:)),2),1:size(idl,1),'un',0));
more variant with use function from Image Processing Toolbox - blockproc
sum2 = blockproc(final2,[width b],@(block_struct) sum(block_struct.data,2))

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by