[gdal-dev] Automatically creating an Alpha mask for a RGB raster to extract the backgound? (magic wand style)

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[gdal-dev] Automatically creating an Alpha mask for a RGB raster to extract the backgound? (magic wand style)

Idan Miara
Hi,

I'm looking for an automated/semi-automated way to remove the plain color background of some (jpeg compressed) Orthophoto RGB rasters without NoDataValue or an Alpha band.

Usually I get an image of some specific region with the background as a plain color (usually it's plain white or plain black but it could be also almost white or almost black or some other color, or a composition of some patches that each one is a plain color).
 
The problem begins when when I want to make a mosaic. It could be hard to remove those plain regions which should be transparent - setting a single NoDataValue won't do the trick for most cases and it will also remove some data pixels.
It gets even worse if the outside was not black and then I use gdal-warp to transform the CRS and I get extra black filling on the outside.

A simple rule to classify the background pixels - a large cluster of pixels with exactly (or almost exactly) the same value - a human can tell the background instantly.

Is there an easy way to create an alpha binary mask band to classify the background?
Afterwards, is there a tool to crop the image so I'll get a minimal frame of background?

Kind regards,
Idan

_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: Automatically creating an Alpha mask for a RGB raster to extract the backgound? (magic wand style)

Even Rouault-2
> I'm looking for an automated/semi-automated way to remove the plain color
> background of some (jpeg compressed) Orthophoto RGB rasters without
> NoDataValue or an Alpha band.

https://www.gdal.org/nearblack.html ?

--
Spatialys - Geospatial professional services
http://www.spatialys.com
_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: Automatically creating an Alpha mask for a RGB raster to extract the backgound? (magic wand style)

Idan Miara
Hi Even,

Thanks! it does solve most of the problem!

The only issue that I think is left is that nearblack scans from the borders till it reaches a valid data point and stops there.
So in case of diagonal zigzag cutting (or background in holes) there will be still background left inside the zigzags or holes.

A solution could be to add a parameter for a different scanning mode, instead of 4 scans from the borders till the first data point, do one or two scans (horizontal/vertical) and in each scan cover the whole scanline/row so we won't miss background trapped inside holes.

What do you think?

Kind regards,
Idan

On Tue, 29 Jan 2019 at 14:48, Even Rouault <[hidden email]> wrote:
> I'm looking for an automated/semi-automated way to remove the plain color
> background of some (jpeg compressed) Orthophoto RGB rasters without
> NoDataValue or an Alpha band.

https://www.gdal.org/nearblack.html ?

--
Spatialys - Geospatial professional services
http://www.spatialys.com

_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: Automatically creating an Alpha mask for a RGB raster to extract the backgound? (magic wand style)

Even Rouault-2
On mardi 29 janvier 2019 15:49:40 CET Idan Miara wrote:

> Hi Even,
>
> Thanks! it does solve most of the problem!
>
> The only issue that I think is left is that nearblack scans from the
> borders till it reaches a valid data point and stops there.
> So in case of diagonal zigzag cutting (or background in holes) there will
> be still background left inside the zigzags or holes.
>
> A solution could be to add a parameter for a different scanning mode,
> instead of 4 scans from the borders till the first data point, do one or
> two scans (horizontal/vertical) and in each scan cover the whole
> scanline/row so we won't miss background trapped inside holes.

Yes an additional flag would be needed ("-interior" ?) since there are
situations where you could have zero values inside the raster (lakes, etc)
that you don't want to treat as nodata.

--
Spatialys - Geospatial professional services
http://www.spatialys.com
_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev