How use the value of some pixels in an image? (with find?)

Hello!
I use actually an image with some pixels. Each pixel have a specific value, and i need to note those value for a loot.
I use the find command like this:
values= find(img);
But i get a list of value who not correspond of the value of those pixel (Normally, i get some value between 20.5 and 30.8, but here, he show values between 1000 & 100000!) .
Where is my error?
Otherwise, i try the loop like this:
[x y]=size(img);
%%Loot:
a=0;
b=0;
c=0;
d=0;
% Pieces dans l'image
for i=1:x
if (img(i)>19 && img(i)<21)
a=a+1;
end
if (img(i)>22 && img(i)<24)
b=b+1;
end
if (img(i)>24 && img(i)<25)
c=c+1;
end
if (img(i)>26 && img(i)<27)
d=d+1;
end
tot = a + b + c + d
end
But it don't work and it's easier with the "find" function to determine the end value.

 Accepted Answer

OK! I GET IT! And that work nicely.. And i need to learn english when i read the help section:
[row,col,v] = find(X, ...) returns a column or row vector v of the nonzero entries in X, as well as row and column indices. If X is a logical expression, then v is a logical array. Output v contains the non-zero elements of the logical array obtained by evaluating the expression X.
So v is the list of the values!!!!
[row col v] = find(img);
[x]=size (v);
one_cents1=0;
two_cents1=0;
five_cents1=0;
ten_cents1=0;
twinty_cents1=0;
fifty_cents1=0;
one_euro1=0;
two_euros1=0;
% Coins inside the image
for i=1:x
if (v(i)>19 && v(i)<21)
one_cent1=one_cent1+1;
end
if (v(i)>22 && v(i)<24)
two_cents1=two_cents1+1;
end
if (v(i)>24 && v(i)<25)
ten_cents1=ten_cents1+1;
end
if (v(i)>26 && v(i)<27)
five_cents1=five_cents1+1;
end
if (v(i)>27 && v(i)<28)
twenty_cents1=twenty_cents1+1;
end
if (v(i)>28 && v(i)<29)
one_euro1=one_euro1+1;
end
if (v(i)>29 && v(i)<30)
fifty_cents1=fifty_cents1+1;
end
if (v(i)>30 && v(i)<32)
two_euros1=two_euros1+1;
end
tot_obj_center=one_cents1*0.01+two_cents1*0.02+ten_cents1*0.1+five_cents1*0.05+twinty_cents1*0.2+one_euro1*1+fifty_cents1*0.5+two_euros1*2;
end

More Answers (2)

Walter Roberson
Walter Roberson on 6 Jan 2014
Edited: DGM on 14 Feb 2023
find() returns array indices, not the non-zero values.
Your code is only looping over the first column of the image. Is that what you want?
When you are counting b you count pixels with values up to but not equal to 24, and when you are counting c you count pixels that are greater than 24 but not equal to 24. Neither of them would count pixels whose values are exactly 24. Is that what you want?
Could you confirm that you do not want to count (>= 21 & 22), or (= 25 & <= 26) ?

2 Comments

Thank for the answer and attention!
So, how get the non-zero values on the image's matrice?
About the loop, I see the values of the pixel when I look my image, and it's not necessary to use >= , but yes, i can do this if necessary (By example, i've around 19.564 min and 20.20 for getting the pixel in catégorie a), i'll explain what is my goal in my next answer to "image analyst" ;) )
Oh, and yes, if i can't use "find", so i search to loop with all image value, so all column and rows .

Sign in to comment.

I don't know what loot is, except for cash or valuables stolen by robbers/burglars.
Your code doesn't make sense. First of all what you call x is the number of rows - the vertical size, not the horizontal size like you're probably thinking. Same for y - it's the columns (horizontal), not the rows (vertical dimension).
Next, your loop is not using row, column indexing, where it would have two indexes. You're using a single index which means you're using linear indexing, which goes down all the rows in a column before moving over to the next column. So you're basically finding all img between 20 and 26 inclusive in the first column. Is that what you want? If so, you can do
inRange = img > 19 & img < 27;
tot = sum(img(inRange, 1));
but I'm not sure that's what you really want or need. Please explain what a "loot" is so I can suggest the best approach to get the information you need for your loot.

8 Comments

Hi and thank for the attention!
It's not "loot", but "loop", my bad.
In fact, i'm student and beginner in matlab and you are close to the true, (not about robbery but about cash): the goal is to recognise some coins on a image with different method. I get one solution with the "Boundingbox+erosion method", but it's not enough precise for some particular case.
So I used the "Ultimate erosion + bwdist" method: I take the image of the coins, and, I doing the maximum erosion (until there are a pixel), and the Distance transform of binary image.
The resulting image is 16 pixels (1 pixel per coin) with different value in function of the value of the coin.
The problem now is to find all of those values, and use this list for distinguish all coins for every images.
With "values= find(img)" , I expected get the non-zero value, but apparently, it's not the case.
Otherwise, i wanted to make a loop where i take directly the values in the image's matrix, I showed here an "exemple", but yes, i want to take a method in a [x y] value and recognise all coins in the image.
That's almost what my Image Segmentation Tutorial does. Go here: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862 It identifies 5 and 10 cent coins, though in a more efficient approach than you. So basically, just throw out your approach (it's no good anyway) and look what I did. No need to call bwdist() and bwulterode() like you're doing. If your image is more complicated, then post it. But if it's just a big pile of random and overlapping coins then it will be hard to identify coins that are just partially visible.
Oh, that seem good, i'll take a look ^^ .
Otherwise, it's about an image where coins can be in border (right of left), and can be in contact, like this one: http://img15.hostingpics.net/pics/337676euros.png
that why erosion is an important function, and ultimate erosion can seem cool for more precision.
It's a problem with my loop i think, but i don't know what.
No, ultimate erosion (erosion down to a single point) is not what you want. Why do that when all you need to do is to erode 1 or 2 layers and they'll be separated?
Okey! I want to try to finish this one is despite of image analyst method.
in fact, i get the method to get the coordinates of each value:
[row col] = find(img)
After this, I take a look in help about the linear indexing:
The element at row 3, column 2 of matrix A (value = 5) can also be identified as element 6 in the actual storage sequence. To access this element, you have a choice of using the standard A(3,2) syntax, or you can use A(6), which is referred to as linear indexing.
So 2 possibility:
  • i get the value if i type a think like this:
values = img(row,col);
This one don't work, i get another matrix.
  • I visualise the coordinate with a first board like this:
A = [row col];
And i make a loop like this:
for i = 1
line=row[i,0]; colomn = row[i,1]; val = img[row,col];
With this loop, i get the second board of value and a can go for the next loop we was seen precedently.
What's the purpose of the "line" variable? Also no need to have a for loop if "i" only takes on the single value of 1. And row and col just have one index, not two. And you should not use i (the imaginary constant) as a loop variable.
What you can do to address each pixel (in this inefficient method) is to do this:
[rows, columns] = find(img);
for k = 1 : length(rows)
thisRow = rows(k);
thisColumn = columns(k);
thisGrayLevel = img(thisRow, thisColumn);
% Any other stuff you want to do....
end
Thank, i think i'm close to the good way now to verify if this idea is really useless:
[row col] = find(img);
A = [row col];
[x y]=size (A);
for i=1:x
Graylevel=img(A(i,1),A(i,2));
%all_grays_lvl=????
end
I think i'm close to get all graylevel value, actually, i get only the last one, i need to repertory all graylevel of each pixel, and that will be ok!
Be aware that size() returns [rows, columns], which is [y, x], NOT [x, y] as you have it! And I have no idea why you introduced the A variable when you already had rows and columns. Introducing A just complicated the code for no reason.

Sign in to comment.

Asked:

on 6 Jan 2014

Edited:

DGM
on 14 Feb 2023

Community Treasure Hunt

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

Start Hunting!