summation of image exceeds matrix dimention
Afficher commentaires plus anciens
Using Matlab 2012, I want to count the number of pixels in certain portions of image so i try the following code which give error at sum(sum ) function . could any one help me ? or is there another solution to get the number of pixels within certain coordinates rather than this code:
clear sum
clear all
close all
image=imread('Headers-29.jpg');
image=im2bw(image,graythresh(image)); %binarization
image=imcomplement(image);
[m n]=size(image);
X_segmented=[];
Y_segmented=[];
Width_segmented=[];
Hight_segmented=[];
cropped_segmented={};
Bounding=regionprops( image, 'BoundingBox'); % Minimum x-y coordinates of a block and its x, y lengths
for g=1:length(Bounding)
X_segmented(g,1)=Bounding(g).BoundingBox(1);
Y_segmented(g,1)=Bounding(g).BoundingBox(2);
Width_segmented(g,1)=Bounding(g).BoundingBox(3);
Hight_segmented(g,1)=Bounding(g).BoundingBox(4);
x_Plus(g,1)=X_segmented(g)+Width_segmented(g);
y_plus(g,1)=Y_segmented(g)+Hight_segmented(g);
bvv(g,:)=[X_segmented(g,1) x_Plus(g,1) Y_segmented(g,1) y_plus(g,1)];
end
for g=1:size(bvv,1)
sumnn(g,:)=sum(sum( image(bvv(g,1): bvv(g,2) , bvv(g,3):bvv(g,4))));
end
end
2 commentaires
Stephen23
le 1 Nov 2017
clear sum
is not a good sign: have you defined a variable named sum ?
Rana Sobhy
le 1 Nov 2017
Réponse acceptée
Plus de réponses (1)
Henrik Jacobsen
le 1 Nov 2017
I tried your code with a random image, of size [309 367]. In my image, g runs from 1 to 15. For g=1, I have
bvv = [6.5000 359.5000 31.5000 290.5000].
So your code is trying to do
sum(sum( image(6.5000:359.5000 , 31.5000:290.5000)));
The x indices exceed the size of the image, which only goes up to 309. In addition, all the indices are half integers, which can't really be used for indexing. So your code clearly does not do what you intend it to do.
1 commentaire
Rana Sobhy
le 1 Nov 2017
Catégories
En savoir plus sur Image Arithmetic dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!