Agisoft Metashape
Agisoft Metashape => Python and Java API => Topic started by: 7eicher on September 09, 2013, 11:39:21 AM
-
Dear folks,
i wonder if anyone tried yet to autogenerating masks. In my case I treat lots of high reso images (> 500) by hand to mask the sky out. Taking lots of time, little bit annyoing :)
So does anyone or does you guys from Agisoft has a semi-automated solution for that?
When I think of that task as a developer I would let the user (me) pick a color and tolerance, than search in the image for lets say larger areas of this color and similar color (tolerance), like the magic picker function does. Than append that area to the mask. Hence save. Afterwards the user needs to check each image, which should be easy, by looking at the mask-preview in the photo area.
I would prefer a python API solution, to have controll over the process.
Any ideas? Maybe an external tool can help?
Take care,
Markus
-
Hello Markus,
You can try the following function - it works both on background image (like Import from Background feature in GUI, but also you can input color as background argument):
function:: createDifferenceMask(image, background, tolerance=10, fit_colors=False)
Creates mask from a pair of images or an image and specified color.
:arg image: Image to be masked.
:type image: :class:`Image`
:arg background: Background image or color value.
:type background: :class:`Image` or color tuple
:arg tolerance: Tolerance value.
:type tolerance: int
:arg fit_colors: Enables white balance correction.
:type fit_colors: boolean
:return: Resulting mask.
:rtype: :class:`Image`
-
Dear Alexey,
thanks for your valuable feedback. I tried the following line:
mask = PhotoScan.Utils.createDifferenceMask(cam.image(), (108,145,195), 10, False)
and got a strange exception:
TypeError: descriptor 'createDifferenceMask' requires a 'PhotoScan.Utils' object but received a 'PhotoScan.Image'
What I am missing?
-
Hello 7eicher,
I think you should use PhotoScan.utils.createDifferenceMask().
-
Dear Alexey,
thanks for your reply. That worked so far. So I made some tests and got a small success detecting a simple black/white colored image using this command:
img = PhotoScan.app.document.activeChunk.cameras[0].image()
PhotoScan.utils.createDifferenceMask(img, (0,0,0), 50, True).save(r"d:/tmp/test_mask.png")
The RGB tuple represents black color (0,0,0). So far so good.
Then i tried a real image (see attachment). I picked up different colors (e.g. paint) from the sky in the image and tried the same command as above but without success. I also tried to use 100 tolerance, but no success. Here are colors I tried out:
137,230,216
185,214,205
231,247,255
The result was always an empty (white) mask image.
What did I do wrong? Do I need to use another color picker, other color format?
-
Hello 7eicher,
If you are using color and not background image for difference masking you should turn off fit_colors (use False as the parameter). I think that's the problem in this case.
-
Great Alexey, it works.
Thanks! So now I can continue my script... :)
-
Have posted a sample script for mask generation from user selected color with tolerance:
http://wiki.agisoft.com/wiki/Masks_From_Color.py