Forum

Author Topic: Accessing pixel coordinates of masks  (Read 3582 times)

arjunkg

  • Newbie
  • *
  • Posts: 1
    • View Profile
Accessing pixel coordinates of masks
« on: January 05, 2017, 04:46:43 PM »
After importing a mask, how do we obtain the pixel coordinates of the masked region? When I import, I can see the mask overlaid on the original image, but I'm unable to access the pixel coordinates corresponding to mask = 1.

Thanking you,

Arjun KG

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15124
    • View Profile
Re: Accessing pixel coordinates of masks
« Reply #1 on: January 18, 2017, 02:36:41 PM »
Hello Arjun,

We suggest the following solution that assumes the use of numpy module:


Using the following code you can get unmasked pixels:

Code: [Select]
    import PhotoScan, numpy

    camera = PhotoScan.app.document.chunk.cameras[0]
    mask = camera.mask.image()
    bytes = mask.tostring()
    pixels = numpy.fromstring(values, dtype=numpy.uint8)
    (unmasked,) = numpy.nonzero(a == 255)

so now in unmasked array you would have the indices of the "white" pixels, but to get the coordinates you need to perform a conversion, like:

Code: [Select]
    pix = unmasked[-1] #last white pixel
    x, y = pix % camera.sensor.width, pix // camera.sensor.width
Best regards,
Alexey Pasumansky,
Agisoft LLC