Forum

Author Topic: Create custom mask  (Read 2935 times)

SensorFusion

  • Newbie
  • *
  • Posts: 4
    • View Profile
Create custom mask
« on: March 12, 2020, 12:47:35 PM »
I am just starting to use the python API and have some problems.

I wanted to test if i can add a custom masks to an image.
Code: [Select]
    doc = Metashape.Document()
    doc.open(pathToProject)

    for camera in doc.chunks[0].cameras:
        # create mask out of rectangle       
        newMask = Metashape.Mask()
        camera.mask = newMask

How can i create a custom mask? For example a rectangle. How would i do it?

If i want to see the signature of Mask() the following appears in my IDE:
Code: [Select]
def __init__(self, *args, **kwargs): # real signature unknown
« Last Edit: March 17, 2020, 11:09:31 AM by SensorFusion »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14847
    • View Profile
Re: Get Started with Python API
« Reply #1 on: March 12, 2020, 01:41:14 PM »
Hello SensorFusion,

Do you need to import the mask from the file or to "draw" it?
Best regards,
Alexey Pasumansky,
Agisoft LLC

SensorFusion

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Get Started with Python API
« Reply #2 on: March 12, 2020, 03:03:59 PM »
I need to "draw" it. I want to specify a rectangle on pixel base. Like this
Code: [Select]
Rect(left = 0, top = 0, right = 200, bottom = 200).and then add it to an image as mask.

SensorFusion

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Create custom mask
« Reply #3 on: March 17, 2020, 11:10:30 AM »
Does anyone have an idea? Or do I need to create my masks as PNG-files and then load it? Is this the only way to create a mask?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14847
    • View Profile
Re: Create custom mask
« Reply #4 on: March 23, 2020, 05:05:35 PM »
Hello SensorFusion,

I'll post an example soon.
Best regards,
Alexey Pasumansky,
Agisoft LLC

SensorFusion

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Create custom mask
« Reply #5 on: March 24, 2020, 11:49:08 AM »
I found a workaround: Creating an image file as mask and set it to the camera. But perhaps it is possible to set the rectangle directly?
Code: [Select]
from PIL import Image, ImageDraw

def createFullMaskFor(camera):
    width = int(camera.photo.meta["File/ImageWidth"])
    height = int(camera.photo.meta["File/ImageHeight"])
    img = Image.new("RGB", (width, height))
    draw = ImageDraw.Draw(img)
    draw.rectangle(((0, 0), (width, height)), fill="black")
    name = "MASK.JPEG"
    img.save(name, "JPEG")
    newMask = Metashape.Mask()
    newMask.load(name)
    camera.mask = newMask

smiller

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Create custom mask
« Reply #6 on: January 21, 2022, 11:27:31 AM »
Is there a way to draw the rectangle directly, like in the GUI? Then apply that selection as a mask?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14847
    • View Profile
Re: Create custom mask
« Reply #7 on: January 21, 2022, 01:28:18 PM »
Hello smiller,

You can create an empty single-band image (Metashape.Image) of the required size and fill it with black and white values pixel by pixel, then assign this image to a Mask class variable, and assign that mask to the camera.

To speed up the process, external modules, like numpy, can be used.
Best regards,
Alexey Pasumansky,
Agisoft LLC