How use the value of some pixels in an image? (with find?)
Show older comments
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
More Answers (2)
Walter Roberson
on 6 Jan 2014
Edited: DGM
on 14 Feb 2023
0 votes
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
Baptiste
on 6 Jan 2014
Image Analyst
on 6 Jan 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 Comments
Baptiste
on 6 Jan 2014
Image Analyst
on 6 Jan 2014
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.
Image Analyst
on 6 Jan 2014
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?
Image Analyst
on 7 Jan 2014
Moved: DGM
on 14 Feb 2023
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
Image Analyst
on 7 Jan 2014
Moved: DGM
on 14 Feb 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.
Categories
Find more on Image Segmentation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!