GeoEco.Datasets.Virtual.CannyEdgeGrid
- class GeoEco.Datasets.Virtual.CannyEdgeGrid(grid, highThreshold=None, lowThreshold=None, sigma=1.4142, minSize=None)
Bases:
GridA
Gridthat represents the edges in anotherGridusing the Canny edge detection algorithm..To run this tool, you either must have MATLAB R2026a or MATLAB Runtime R2026a installed. The MATLAB Runtime is free and may be downloaded from https://www.mathworks.com/help/compiler/install-the-matlab-runtime.html. Please follow the installation instructions carefully. Version R2026a must be used; other versions will not work. MATLAB Runtime allows multiple versions can be installed at the same time.
The Canny edge detection algorithm is a generic, widely-used algorithm for delineating edges between objects in digital images. The algorithm may be successfully applied to a wide variety of problems, including the problem of detecting fronts between water masses. Canny (1986) describes the algorithm in full detail, including its mathematical derivation. Some readers may find the paper difficult due to its length and technical detail. Shorter, more approachable descriptions may be found by searching the Internet for “Canny algorithm”.
Example usage:
# Load a sea surface temperature raster. from GeoEco.Datasets.GDAL import GDALDataset grid = GDALDataset.GetRasterBand(r'/home/jason/test/thetao_0000.5_20210101.img') # Instantiate a CannyEdgeGrid using the default parameters. The edges # won't be detected until data are requested from the CannyEdgeGrid # instance, which will happen when we write the output raster below. from GeoEco.Datasets.Virtual import CannyEdgeGrid edgeGrid = CannyEdgeGrid(grid) # Write the output raster. GDALDataset.CreateRaster('/home/jason/test/thetao_0000.5_20210101_fronts.img', edgeGrid)
Requires: MATLAB Runtime R2026a (which can be freely downloaded from https://www.mathworks.com/products/compiler/matlab-runtime.html) or the full version of MATLAB R2026a.
- Parameters:
grid (
Grid) –Gridin which to detect edges. If it is 3D or 4D, edges will be detected in each 2D time and/or depth slice.highThreshold (
float, optional) –High threshold for the Canny edge detection algorithm. The Canny algorithm uses the high threshold to find “strong edges”, which are pixels that have such a high gradient magnitude that they may confidently be classified as edges. These are pixels where those that surround them show a strong increase or decrease in value as you move across the image in some direction. The units of the Canny thresholds are the change in the units of the image per pixel traveled. For example, if the image represents sea surface temperature in degrees C, the units are change in degrees C per pixel.
If you do not provide a value, one will be chosen automatically based on the data within the image. If you are detecting edges in multiple images, a value will be selected for each image separately.
If you do not know what value to use, try the automatic selection and check the results carefully to see if they are acceptable for your problem. If not, you may select a value by trial and error. To see the values selected automatically, you enable verbose logging. (Please consult the MGET team for help with this, if necessary.)
Increasing the high threshold will reduce the number of edges detected; reducing it will increase the number of edges detected.
Must be greater than 0.0. Must be less than 1.0.
lowThreshold (
float, optional) –Low threshold for the Canny edge detection algorithm. If you supply a low threshold, you must also supply a high threshold. The low threshold must be less than the high threshold.
After using the high threshold to find “strong edges”, the Canny algorithm uses the low threshold to find “weak edges”, which are those that have a gradient magnitude that is high enough that they might be edges, but not so high that they may be confidently classified as edges on the basis of their gradient magnitude alone. After identifying the weak edge pixels, the algorithm checks whether 8-connected groups of weak pixels are connected to strong ones (i.e. whether they touch them horizontally, vertically, or diagonally). Groups of weak pixels that do not touch a strong pixel are discarded. This approach essentially allows the strong edges to be “extended” by the weak ones.
The units of the Canny thresholds are the change in the units of the image per pixel traveled. For example, if the image represents sea surface temperature in degrees C, the units are change in degrees C per pixel.
If you do not provide a value, one will be chosen automatically based on the data within the image. If you are detecting edges in multiple images, a value will be selected for each image separately. The value selected will be the high threshold multiplied by 0.4.
If you do not know what value to use, try the automatic selection and check the results carefully to see if they are acceptable for your problem. If not, you may select a value by trial and error. To see the values selected automatically, you enable verbose logging. (Please consult the MGET team for help with this, if necessary.)
Increasing the low threshold will reduce the number of edges detected; reducing it will increase the number of edges detected.
Must be greater than 0.0. Must be less than 1.0.
sigma (
float, optional) –Sigma parameter for the Gaussian filter applied by the Canny edge detection algorithm. As its first step, before performing edge detection, the Canny algorithm applies a Gaussian filter to the image to smooth out noise. The sigma parameter controls the degree of smoothing. Higher values producing more smoothing, resulting in fewer detected edges. Lower values yield less smoothing and more detected edges.
If you do not provide a value, the square root of 2 will be used by default. This is a good value for many problems. If your image is very noisy, you might need a higher value. Try increasing it a little bit at a time (e.g. by 0.1 or 0.2). Be careful not to increase it too much; otherwise it may smooth out the edges that you are trying to detect. If your image is not very noisy–e.g. it is from an ocean model that produces theoretical results that do not contain noise–consider reducing the value to 1 or less (it must be greater than zero, however).
It is not necessary to specify the window size of the Gaussian filter; the algorithm automatically selects the optimal window size based on the value of the sigma parameter.
Must be greater than 0.0.
minSize (
int, optional) –Minimum number of pixels an individual edge must occupy for it to be retained. Edges with fewer number of pixels will be discarded. For this option, an “edge” is defined as the chain of pixels that are 8-connected (i.e. they are connected horizontally, vertically, or diagonally). The edge may be any shape, such as long and thin and branching or short and blob-like.
This option is applied after the Canny algorithm is complete. If a value is not provided, all edges will be retained.
Must be greater than 0.
- Returns:
CannyEdgeGridinstance.- Return type:
Properties
- property CenterCoords
(
object) Coordinates of the grid cell centers, indexed using the 1-character dimension of interest and optionally arangeto retrieve anumpy.ndarrayof coordinates (e.g.CenterCoords['x', 0:4]) or an integer to retrieve afloatfor a single coordinate (e.g.CenterCoords['x', 10]). Coordinates for thetdimension are returned asdatetimeinstances. Read only.
- property CoordDependencies
(
tupleofstr) Same length asDimensions. Dimensions that each dimension depends on for determining its coordinates.Nonefor dimensions that have a constant coordinate increment. Read only.
- property CoordIncrements
(
tupleoffloat) Same length asDimensions. Coordinate increment for each dimension.Nonefor dimensions that do not have a constant coordinate increment. Read only.
- property Data
(
object) This grid’s data, indexable using slices (e.g.grid.Data[:, 5:10, -10:]) or integers (e.g.grid.Data[0,1,-2]) or both in combination. Strides and negative indexes are supported in the traditional manner. If the grid is writable,Datacan be assigned to write values to the grid, e.g.grid.Data[0,1] = 5orgrid.Data[:,:] = numpy.zeros(grid.Shape). Returns and acceptsnumpy.ndarray,float, andint. Read only.
- property DataIsScaled
(
bool) If True, the underlying raw data are stored as theUnscaledDataTypeto save storage space and then transformed by a scaling equation on the fly when they are returned byData. The raw data can be accessed withUnscaledData. If False, the raw data are returned as is, with no transformation needed, andUnscaledDataTypeandDataTypeare the same, andUnscaledDatareturns the same values asData. Read only.
- property DataType
(
str) Numeric data type of the grid, after the scaling function (if any) has been applied to the raw data.numpy.ndarrays returned byDatahave this type. Read only. Allowed values꞉'int8','uint8','int16','uint16','int32','uint32','float32','float64'. Case sensitive.
- property Dimensions
(
str) Dimensions of this grid. Read only. Allowed values꞉'yx','zyx','tyx','tzyx'. Case sensitive.
- property DisplayName
(
str) Informal name of this object, suitable to be displayed to the user. Read only. Minimum length꞉ 1.
- property MaxCoords
(
object) Maximum coordinate value for each cell (i.e., the coordinates of the cells’ right edges), indexed using the 1-character dimension of interest and optionally arangeto retrieve anumpy.ndarrayof coordinates (e.g.MaxCoords['x', 0:4]) or an integer to retrieve afloatfor a single coordinate (e.g.MaxCoords['x', 10]). Coordinates for thetdimension are returned asdatetimeinstances. Read only.
- property MinCoords
(
object) Minimum coordinate value for each cell (i.e., the coordinates of the cells’ left edges), indexed using the 1-character dimension of interest and optionally arangeto retrieve anumpy.ndarrayof coordinates (e.g.MinCoords['x', 0:4]) or an integer to retrieve afloatfor a single coordinate (e.g.MinCoords['x', 10]). Coordinates for thetdimension are returned asdatetimeinstances. Read only.
- property NoDataValue
(
objectorNone)int,float, or single-element numpy array giving the value that indicates that cells ofDatashould be interpreted as having no data (these are also known as missing, NA, or NULL cells), orNoneif all cells must have data. Read only.
- property ParentCollection
(
DatasetCollectionorNone) ParentDatasetCollectionthat this object is part of (if any). Read only.
- property Shape
(
tupleofint) Same length asDimensions. Length (number of grid cells) of each dimension. Read only.
- property TCountPerSemiRegularPeriod
(
intorNone) Number of time slices per semi-regular period (i.e. per year).Noneif the grid’s dimensions do not contain atcoordinate or thetcoordinate is not semi-regular. Read only.
- property TIncrementUnit
(
strorNone) Unit of thetcoordinate.Noneif the grid’s dimensions do not contain atcoordinate. Read only. Allowed values꞉'year','month','day','hour','minute','second'. Case sensitive.
- property TSemiRegularity
(
strorNone) Type of semi-regularity used for thetcoordinate.Noneif the grid’s dimensions do not contain atcoordinate or thetcoordinate is not semi-regular. Read only. Allowed values꞉'annual'. Case sensitive.
- property UnscaledData
(
object) This grid’s data underlying raw data, before it has been transformed by a scaling equation.UnscaledDatais indexable using slices (e.g.grid.UnscaledData[:, 5:10, -10:]) or integers (e.g.grid.UnscaledData[0,1,-2]) or both in combination. Strides and negative indexes are supported in the traditional manner. If the grid is writable,UnscaledDatacan be assigned to write values to the grid, e.g.grid.UnscaledData[0,1] = 5orgrid.UnscaledData[:,:] = numpy.zeros(grid.Shape). Returns and acceptsnumpy.ndarray,float, andint. Read only.
- property UnscaledDataType
(
str) Numeric data type of the grid’s raw data, before it has been transformed by a scaling equation.numpy.ndarrays returned byUnscaledDatahave this type. If no transformation is needed (DataIsScaledis False), thenUnscaledDataTypeandScaledDataTypeare the same, andUnscaledDatareturns the same values asData. Read only. Allowed values꞉'int8','uint8','int16','uint16','int32','uint32','float32','float64'. Case sensitive.
- property UnscaledNoDataValue
(
objectorNone)intorfloatvalue that indicates that cells ofUnscaledDatashould be interpreted as having no data (these are also known as missing, NA, or NULL cells), orNoneif all cells must have data. Read only.
Methods
Closes any open files or connections associated with this object and releases any other resources allocated to access it.
Converts a spatial reference from one format to another, such as an OGC WKT string to a Proj4 string.
Deletes the lazy property with the specified name.
Returns a list of all queryable attributes.
Given a
tupleorlistof coordinates, returns alistofintindices intoDatafor the cell that contains the coordinates.Returns the value of the lazy property with the specified name.
Returns the queryable attribute with the specified name.
Returns the value of the queryable attribute with the specified name.
Returns a list queryable attributes having the specified data type.
Returns the spatial reference of this dataset.
Returns True if the specified lazy property has a value.
Sets the lazy property with the specified name to the specified value.
Sets the spatial reference of this dataset.
Tests whether a capability is supported by this class or an instance of it.