how to iterate through all the pixels within a bbox returned by vision.BlobAnalysis
2 次查看(过去 30 天)
显示 更早的评论
Hi I want to iterate through all the pixels within a bbox returned by vision.BlobAnalysis to sum their values, tried following code assuming (x,y) are the coordinates for first pixel, width is number of columns moving right and height is number of rows moving down (seems like I was wrong get an error indicating negative values as k,j coordinates). Can someone kindly explain a bit.
sum=0;
for k=track.bbox(1,1):track.bbox(1,1)+tracks.bbox(1,3)
for j=tracks.bbox(1,2):tracks.bbox(1,2)+tracks.bbox(1,4)
sum=sum+img(k,j);
end
end
0 个评论
回答(1 个)
Walter Roberson
2015-10-14
%remember, bounding box is _x_ first and _x_ is column not row!
fr = track.bbox(1,2);
fc = track.bbox(1,1);
lr = fr + track.bbox(1,4) - 1; %height includes first pixel
lc = fc + track.bbox(1,3) - 1; %width includes first pixel
pixtotal = sum( sum( img(fr:lr, fc:lc) ) );
and never use "sum" as the name of a variable: it interferes with using sum() as the important MATLAB routine
Note: if you are adding the pixel values only in order to find the mean pixel value, then do not bother adding them:
pixmean = mean2( img(fr:lr, fc:lc) );
3 个评论
Walter Roberson
2015-10-14
What does track.bbox show up as?
Please show your code for creating track.bbox as there are a number of different bounding box detectors.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!