Skip to content

Commit e2855e7

Browse files
committed
update
1 parent 4d293b7 commit e2855e7

File tree

9 files changed

+333
-146
lines changed

9 files changed

+333
-146
lines changed
114 Bytes
Binary file not shown.
15.4 KB
Binary file not shown.

docs/docs/html/genindex.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ <h2 id="H">H</h2>
250250
</li>
251251
</ul></td>
252252
<td style="width: 33%; vertical-align: top;"><ul>
253-
<li><a href="geospatial_learn.html#shape.hough2line">hough2line() (in module shape)</a>
253+
<li><a href="geospatial_learn.html#utilities.hough2line">hough2line() (in module utilities)</a>
254254
</li>
255255
</ul></td>
256256
</tr></table>
@@ -262,8 +262,6 @@ <h2 id="I">I</h2>
262262
</li>
263263
</ul></td>
264264
<td style="width: 33%; vertical-align: top;"><ul>
265-
<li><a href="geospatial_learn.html#utilities.iou_score">iou_score() (in module utilities)</a>
266-
</li>
267265
<li><a href="geospatial_learn.html#utilities.iter_ransac">iter_ransac() (in module utilities)</a>
268266
</li>
269267
</ul></td>
@@ -299,12 +297,16 @@ <h2 id="M">M</h2>
299297
<li><a href="geospatial_learn.html#shape.meshgrid">meshgrid() (in module shape)</a>
300298
</li>
301299
<li><a href="geospatial_learn.html#utilities.min_bound_rectangle">min_bound_rectangle() (in module utilities)</a>
300+
</li>
301+
<li><a href="geospatial_learn.html#shape.ms_snake">ms_snake() (in module shape)</a>
302302
</li>
303303
</ul></td>
304304
<td style="width: 33%; vertical-align: top;"><ul>
305-
<li><a href="geospatial_learn.html#shape.ms_snake">ms_snake() (in module shape)</a>
305+
<li><a href="geospatial_learn.html#utilities.ms_toposeg">ms_toposeg() (in module utilities)</a>
306306
</li>
307307
<li><a href="geospatial_learn.html#utilities.ms_toposnakes">ms_toposnakes() (in module utilities)</a>
308+
</li>
309+
<li><a href="geospatial_learn.html#utilities.ms_toposnakes2">ms_toposnakes2() (in module utilities)</a>
308310
</li>
309311
<li><a href="geospatial_learn.html#raster.multi_temp_filter">multi_temp_filter() (in module raster)</a>
310312
</li>
@@ -326,14 +328,10 @@ <h2 id="P">P</h2>
326328
<td style="width: 33%; vertical-align: top;"><ul>
327329
<li><a href="geospatial_learn.html#learning.plot_feature_importances">plot_feature_importances() (in module learning)</a>
328330
</li>
329-
<li><a href="geospatial_learn.html#raster.polygonize">polygonize() (in module raster)</a>
330-
331-
<ul>
332-
<li><a href="geospatial_learn.html#shape.polygonize">(in module shape)</a>
333-
</li>
334-
</ul></li>
335331
</ul></td>
336332
<td style="width: 33%; vertical-align: top;"><ul>
333+
<li><a href="geospatial_learn.html#raster.polygonize">polygonize() (in module raster)</a>
334+
</li>
337335
<li><a href="geospatial_learn.html#learning.prob_pixel_bloc">prob_pixel_bloc() (in module learning)</a>
338336
</li>
339337
</ul></td>
@@ -342,6 +340,8 @@ <h2 id="P">P</h2>
342340
<h2 id="R">R</h2>
343341
<table style="width: 100%" class="indextable genindextable"><tr>
344342
<td style="width: 33%; vertical-align: top;"><ul>
343+
<li><a href="geospatial_learn.html#utilities.ragmerge">ragmerge() (in module utilities)</a>
344+
</li>
345345
<li><a href="geospatial_learn.html#shape.ransac_lines">ransac_lines() (in module shape)</a>
346346
</li>
347347
<li><a href="geospatial_learn.html#module-raster">raster (module)</a>

docs/docs/html/geospatial_learn.html

Lines changed: 94 additions & 72 deletions
Large diffs are not rendered by default.

docs/docs/html/objects.inv

2 Bytes
Binary file not shown.

docs/docs/html/searchindex.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

geospatial_learn/learning.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@
88
The learning module set of functions provide a framework to optimise and classify
99
EO data for both per pixel or object properties
1010
11-
12-
Performance notes:
13-
This was tested on an i7 intel with 16gb of ram, so with large images/arrays
14-
of stats this will inevitably be slower - especially with a more standard machine.
15-
1611
"""
1712

1813
# This must go first or it causes the error:

geospatial_learn/shape.py

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,67 +1915,13 @@ def _do_phasecong(tempIm, low_t=0, hi_t=0, norient=6, nscale=6, sigma=2):#, ske
19151915
# hArray = orientIm[h]>=hT
19161916
# return vArray, hArray
19171917

1918-
def hough2line(inRas, outShp, edge='canny', sigma=2,
1918+
def _hough2line(inRas, outShp, edge='canny', sigma=2,
19191919
thresh=None, ratio=2, n_orient=6, n_scale=5, hArray=True, vArray=True,
19201920
prob=False, line_length=100,
19211921
line_gap=200, valrange=1, interval=10, band=2,
19221922
min_area=None):
19231923

1924-
"""
1925-
Detect and write Hough lines to a line shapefile
1926-
1927-
There are optionally two input arrays on the to keep line detection clean eg 2 orientations,
1928-
such as vertical and horizontal
1929-
1930-
Parameters
1931-
----------
1932-
1933-
inRaster: string
1934-
path to an input raster from which the geo-reffing is obtained
1935-
1936-
outShp: string
1937-
path to the output line shapefile a corresponding polygon will also be written to disk
1938-
1939-
edge: string
1940-
the edge detection method - either phase congruency or canny
1941-
phase is default.
1942-
1943-
sigma: int
1944-
the size of stdv defining the gaussian envelope if using canny edge or phase
1945-
a unitless value
1946-
1947-
n_orient: int
1948-
the number of orientations used if using phase congruency edge
1949-
1950-
n_scale: int
1951-
the number of scales used if using phase congruency edge
1952-
1953-
vArray: bool
1954-
whether to detect lines on approx vert axis
1955-
1956-
hArray: bool
1957-
whether to detect lines on approx horz axis
1958-
low_t:
1959-
the low hysteresis threshold
1960-
the secondary low gradient threshold permitted if connected to
1961-
a high threshold pixel
1962-
hi_t:
1963-
the high hysteresis threshold.
1964-
the principal gradient threshold from which the low values are permitted
1965-
provided they are connected to pixels of this one
1966-
1967-
prob: bool
1968-
Whether to use a probabalistic hough - default is false
1969-
1970-
line_length: int
1971-
If using prob hough the min. line length threshold
1972-
line_gap: int
1973-
If using prob hough the min. line gap threshold
1974-
val_range: int
1975-
The + - range around the orientation automatically chosen
1976-
interval: int
1977-
The no of intervals of the range of values tested if using auto
1978-
"""
1924+
19791925
#TODO this is FAR too long
19801926
inDataset = gdal.Open(inRas, gdal.GA_ReadOnly)
19811927

@@ -2135,7 +2081,7 @@ def hough2line(inRas, outShp, edge='canny', sigma=2,
21352081

21362082
polygonize(segRas, outShp[:-4]+"_poly.shp", outField=None, mask = True, band = 1)
21372083

2138-
def cv_hough2line(inRas, outShp, edge='canny', sigma=2, low_t=0,
2084+
def _cv_hough2line(inRas, outShp, edge='canny', sigma=2, low_t=0,
21392085
hi_t=0, n_orient=6, n_scale=5, increment=180, thresh=100, min_theta=None, bounds=1,
21402086
max_theta=None, min_area=None):
21412087

geospatial_learn/utilities.py

Lines changed: 225 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
from skimage.filters import sobel
5555
from skimage.future import graph
5656

57+
5758
#import multiprocessing as mp
5859
gdal.UseExceptions()
5960
ogr.UseExceptions()
@@ -1609,6 +1610,229 @@ def threshold(image, t):
16091610
name='thresholded', colormap='magenta', blending='additive'
16101611
)
16111612

1613+
def hough2line(inRas, outShp, edge='canny', sigma=2,
1614+
thresh=None, ratio=2, n_orient=6, n_scale=5, hArray=True, vArray=True,
1615+
prob=False, line_length=100,
1616+
line_gap=200, valrange=1, interval=10, band=2,
1617+
min_area=None):
1618+
1619+
# """
1620+
# Detect and write Hough lines to a line shapefile
1621+
#
1622+
# There are optionally two input arrays on the to keep line detection clean eg 2 orientations,
1623+
# such as vertical and horizontal
1624+
#
1625+
# Parameters
1626+
# ----------
1627+
#
1628+
# inRaster: string
1629+
# path to an input raster from which the geo-reffing is obtained
1630+
#
1631+
# outShp: string
1632+
# path to the output line shapefile a corresponding polygon will also be written to disk
1633+
#
1634+
# edge: string
1635+
# the edge detection method - either phase congruency or canny
1636+
# phase is default.
1637+
#
1638+
# sigma: int
1639+
# the size of stdv defining the gaussian envelope if using canny edge or phase
1640+
# a unitless value
1641+
#
1642+
# n_orient: int
1643+
# the number of orientations used if using phase congruency edge
1644+
#
1645+
# n_scale: int
1646+
# the number of scales used if using phase congruency edge
1647+
#
1648+
# vArray: bool
1649+
# whether to detect lines on approx vert axis
1650+
#
1651+
# hArray: bool
1652+
# whether to detect lines on approx horz axis
1653+
# low_t:
1654+
# the low hysteresis threshold
1655+
# the secondary low gradient threshold permitted if connected to
1656+
# a high threshold pixel
1657+
# hi_t:
1658+
# the high hysteresis threshold.
1659+
# the principal gradient threshold from which the low values are permitted
1660+
# provided they are connected to pixels of this one
1661+
#
1662+
# prob: bool
1663+
# Whether to use a probabalistic hough - default is false
1664+
#
1665+
# line_length: int
1666+
# If using prob hough the min. line length threshold
1667+
# line_gap: int
1668+
# If using prob hough the min. line gap threshold
1669+
# val_range: int
1670+
# The + - range around the orientation automatically chosen
1671+
# interval: int
1672+
# The no of intervals of the range of values tested if using auto
1673+
# """
1674+
#TODO this is FAR too long
1675+
inDataset = gdal.Open(inRas, gdal.GA_ReadOnly)
1676+
1677+
# rb = inRas.GetRasterBand(band)
1678+
rgt = inDataset.GetGeoTransform()
1679+
1680+
pixel_res = rgt[1]
1681+
1682+
ref = inDataset.GetSpatialRef()
1683+
1684+
outShapefile = outShp
1685+
outDriver = ogr.GetDriverByName("ESRI Shapefile")
1686+
1687+
# Remove output shapefile if it already exists
1688+
if os.path.exists(outShapefile):
1689+
outDriver.DeleteDataSource(outShapefile)
1690+
1691+
# get the spatial ref
1692+
# ref = vlyr.GetSpatialRef()
1693+
1694+
# Create the output shapefile
1695+
outDataSource = outDriver.CreateDataSource(outShapefile)
1696+
outLayer = outDataSource.CreateLayer("OutLyr", geom_type=ogr.wkbMultiLineString,
1697+
srs=ref)
1698+
1699+
1700+
# Add an ID field
1701+
idField = ogr.FieldDefn("id", ogr.OFTInteger)
1702+
outLayer.CreateField(idField)
1703+
1704+
empty = np.zeros((inDataset.RasterYSize, inDataset.RasterXSize), dtype=np.bool)
1705+
1706+
#because I am thick
1707+
#Degrees (°) Radians (rad) Radians (rad)
1708+
#0° 0 rad 0 rad
1709+
#30° π/6 rad 0.5235987756 rad
1710+
#45° π/4 rad 0.7853981634 rad
1711+
#60° π/3 rad 1.0471975512 rad
1712+
#90° π/2 rad 1.5707963268 rad
1713+
#120° 2π/3 rad 2.0943951024 rad
1714+
#135° 3π/4 rad 2.3561944902 rad
1715+
#150° 5π/6 rad 2.6179938780 rad
1716+
#180° π rad 3.1415926536 rad
1717+
#270° 3π/2 rad 4.7123889804 rad
1718+
#360° 2π rad 6.2831853072 rad
1719+
1720+
1721+
tempIm = inDataset.GetRasterBand(band).ReadAsArray()
1722+
bw = tempIm > 0
1723+
1724+
bwRas = inRas[:-4]+'bw.tif'
1725+
#maskShp = inRaster[:-4]+'bwmask.shp'
1726+
1727+
#polygonize(bwRas, maskShp, outField=None, mask = True, band = 1)
1728+
props = regionprops(bw*1)
1729+
orient = props[0]['Orientation']
1730+
1731+
# we will need these.....
1732+
perim = mh.bwperim(bw)
1733+
# bkgrnd = invert(bw)
1734+
1735+
1736+
bw[perim==1]=0
1737+
array2raster(bw, 1, inRas, bwRas, gdal.GDT_Byte)
1738+
1739+
1740+
# if the the binary box is pointing negatively along maj axis
1741+
1742+
if orient < 0:
1743+
orient += np.pi
1744+
1745+
if orient < np.pi:
1746+
angleD = np.pi - orient
1747+
angleV = angleD - np.deg2rad(90)
1748+
else:
1749+
# if the the binary box is pointing positively along maj axis
1750+
angleD = np.pi + orient
1751+
angleV = angleD + np.deg2rad(90)
1752+
1753+
1754+
1755+
hi_t = thresh
1756+
if hi_t >=1.0:
1757+
low_t = np.round((thresh / ratio), decimals=1)
1758+
else:
1759+
low_t = thresh / ratio
1760+
1761+
if edge == 'phase':
1762+
ph = _do_phasecong(tempIm, low_t, hi_t, norient=n_orient,
1763+
nscale=n_scale, sigma=sigma)
1764+
1765+
ph[perim==1]=0
1766+
1767+
if hArray is True:
1768+
vArray = ph
1769+
if hArray is True:
1770+
hArray = ph
1771+
del ph
1772+
1773+
else:
1774+
# else it is canny
1775+
# We must have a float to get rid of zero to nonzero image
1776+
# boundary, otherwise huff will only detect the non-zero boundary
1777+
inIm = tempIm.astype(np.float32)
1778+
inIm[inIm==0]=np.nan
1779+
1780+
if hArray is True:
1781+
hArray = canny(inIm, sigma=sigma, low_threshold=low_t,
1782+
high_threshold=hi_t)
1783+
if vArray is True:
1784+
vArray = canny(inIm, sigma=sigma, low_threshold=low_t,
1785+
high_threshold=hi_t)
1786+
del inIm
1787+
1788+
1789+
if prob is False:
1790+
"""
1791+
Standard Hough ##############################################################
1792+
"""
1793+
if hasattr(vArray, 'shape'):
1794+
1795+
empty =_std_huff(vArray, empty, outLayer, angleV, valrange, interval, rgt)#, mk=bwRas)
1796+
if hasattr(hArray, 'shape'):
1797+
empty =_std_huff(hArray, empty, outLayer, angleD, valrange, interval, rgt)#, mk=bwRas)
1798+
1799+
1800+
else:
1801+
"""
1802+
Prob Hough ##############################################################
1803+
"""
1804+
1805+
if hasattr(vArray, 'shape'):
1806+
empty =_phl_huff(vArray, empty, outLayer, angleV, valrange,
1807+
interval, rgt, line_length, line_gap)
1808+
if hasattr(hArray, 'shape'):
1809+
empty =_phl_huff(hArray, empty, outLayer, angleD, valrange,
1810+
interval, rgt, line_length, line_gap)
1811+
1812+
1813+
1814+
1815+
outDataSource.SyncToDisk()
1816+
1817+
outDataSource=None
1818+
1819+
if prob is True:
1820+
array2raster(empty, 1, inRas, outShp[:-3]+"tif", gdal.GDT_Int32)
1821+
else:
1822+
inv = np.invert(empty)
1823+
inv[tempIm==0]=0
1824+
if min_area != None:
1825+
min_final = np.round(min_area/(pixel_res*pixel_res))
1826+
if min_final <= 0:
1827+
min_final=4
1828+
remove_small_objects(inv, min_size=min_final, in_place=True)
1829+
#sg, _ = nd.label(inv)
1830+
segRas=outShp[:-3]+"seg.tif"
1831+
array2raster(inv, 1, inRas, segRas, gdal.GDT_Int32)
1832+
del tempIm, inv
1833+
1834+
polygonize(segRas, outShp[:-4]+"_poly.shp", outField=None, mask = True, band = 1)
1835+
16121836
def colorscale(seg, prop):
16131837

16141838
props = regionprops(np.int32(seg))
@@ -1896,4 +2120,4 @@ def otbMeanshift(inputImage, radius, rangeF, minSize, outShape):
18962120
# os.system(cmd4)
18972121
print('vectorisation done - process complete - phew!')
18982122
# output = subprocess.Popen([cmd], stdout=subprocess.PIPE).communicate()[0]
1899-
# print(output)
2123+
# print(output)

0 commit comments

Comments
 (0)