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

6 visualizaciones (últimos 30 días)
Baptiste
Baptiste el 6 de En. de 2014
Editada: DGM el 14 de Feb. de 2023
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.

Respuesta aceptada

Baptiste
Baptiste el 7 de En. de 2014
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

Más respuestas (2)

Walter Roberson
Walter Roberson el 6 de En. de 2014
Editada: DGM el 14 de Feb. de 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 comentarios
Baptiste
Baptiste el 6 de En. de 2014
Editada: Baptiste el 6 de En. de 2014
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" ;) )
Baptiste
Baptiste el 6 de En. de 2014
Oh, and yes, if i can't use "find", so i search to loop with all image value, so all column and rows .

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 6 de En. de 2014
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 comentarios
Baptiste
Baptiste el 7 de En. de 2014
Movida: DGM el 14 de Feb. de 2023
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!
Image Analyst
Image Analyst el 7 de En. de 2014
Movida: DGM el 14 de Feb. de 2023
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by