4
$\begingroup$

Here is the code to find image edge and record the morphological connected pixels positions. How to translate the row/col positions to x/y coordinates?

i = ExampleData[{"TestImage", "Lena"}];
im = EdgeDetect[i, 5];
imm = MorphologicalComponents[im];
a = Position[imm,200];

"a" gives a list of positions, how to translate them to x/y coordinates?

$\endgroup$
8
$\begingroup$

The image coordinate x is the second element of the position; the image coordinate y is the first position subtracted from the number of rows plus 1.

rows = First@ImageDimensions[i];
xy1 = AffineTransform[{{{0, 1}, {-1, 0}}, {0, rows + 1}}][a]

Another way is to use PixelValuePositions on the morphological components, converted to an Image:

xy2 = PixelValuePositions[Image[imm], 200]

Check:

xy1 == xy2
(* True *)

(They happen to be in the same order.)

| improve this answer | |
$\endgroup$

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.