0
$\begingroup$

I detect a single bright object in an image with the following code;

enter image description here

binImage = Binarize[image, 35/255];
newImage = ImageMultiply[image, binImage];
result = ComponentMeasurements[
 DeleteSmallComponents[newImage, 2], "IntensityCentroid"]

{1 -> {71.0501, 56.9741}}

To get the coordinate I have to write:

result[[1]][[2]]

{71.0501, 56.9741}

Can the conversion to the coordinate be obtained different?

$\endgroup$
  • 3
    $\begingroup$ 1 /. result will give the same answer $\endgroup$ – bill s Oct 10 '16 at 16:36
5
$\begingroup$

There are several ways to get individual measurements, apart from the obvious:

result[[1,2]]

As @bills commented:

1 /. result

You probably want to do something with the centroid, e.g. call some function - you can make that function accept data in ComponentMeasurements format:

foo[index_ -> {x_, y_}] := (* do something useful with x/y *)

and then apply that function to each element in result:

Map[foo, result]

Or, you can use replacement rules to turn result to any format you want, e.g.:

circles = result /. (index_ -> centroid_) :> Circle[centroid, 5];
HighlightImage[image, circles]

In recent versions of Mathematica, ComponentMeasurements can also return different output formats, e.g. "PropertyAssociation" returns an Association of property names to value lists:

resultAssociation = 
 ComponentMeasurements[
  newImage, {"Area", "IntensityCentroid"}, #1 >= 2 &, 
  "PropertyAssociation"]

<|"Area" -> {45., 2.}, "IntensityCentroid" -> {{71.0501, 56.9741}, {67.0714, 54.5}}|>

(Note that I've replaced DeleteSmallComponents with a criteria function as 3rd parameter.)

resultAssociation["IntensityCentroid"][[1]]

{71.0501, 56.9741}

$\endgroup$
  • $\begingroup$ Thank you very much for your explanation. $\endgroup$ – mrz Oct 10 '16 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.