test_morphology.py 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371
  1. import numpy
  2. from numpy.testing import (assert_, assert_equal, assert_array_equal,
  3. assert_array_almost_equal)
  4. import pytest
  5. from pytest import raises as assert_raises
  6. from scipy import ndimage
  7. from . import types
  8. class TestNdimageMorphology:
  9. @pytest.mark.parametrize('dtype', types)
  10. def test_distance_transform_bf01(self, dtype):
  11. # brute force (bf) distance transform
  12. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  13. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  14. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  15. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  16. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  17. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  18. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  19. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  20. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  21. out, ft = ndimage.distance_transform_bf(data, 'euclidean',
  22. return_indices=True)
  23. expected = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
  24. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  25. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  26. [0, 0, 1, 2, 4, 2, 1, 0, 0],
  27. [0, 0, 1, 4, 8, 4, 1, 0, 0],
  28. [0, 0, 1, 2, 4, 2, 1, 0, 0],
  29. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  30. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  31. [0, 0, 0, 0, 0, 0, 0, 0, 0]]
  32. assert_array_almost_equal(out * out, expected)
  33. expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0],
  34. [1, 1, 1, 1, 1, 1, 1, 1, 1],
  35. [2, 2, 2, 2, 1, 2, 2, 2, 2],
  36. [3, 3, 3, 2, 1, 2, 3, 3, 3],
  37. [4, 4, 4, 4, 6, 4, 4, 4, 4],
  38. [5, 5, 6, 6, 7, 6, 6, 5, 5],
  39. [6, 6, 6, 7, 7, 7, 6, 6, 6],
  40. [7, 7, 7, 7, 7, 7, 7, 7, 7],
  41. [8, 8, 8, 8, 8, 8, 8, 8, 8]],
  42. [[0, 1, 2, 3, 4, 5, 6, 7, 8],
  43. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  44. [0, 1, 2, 2, 4, 6, 6, 7, 8],
  45. [0, 1, 1, 2, 4, 6, 7, 7, 8],
  46. [0, 1, 1, 1, 6, 7, 7, 7, 8],
  47. [0, 1, 2, 2, 4, 6, 6, 7, 8],
  48. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  49. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  50. [0, 1, 2, 3, 4, 5, 6, 7, 8]]]
  51. assert_array_almost_equal(ft, expected)
  52. @pytest.mark.parametrize('dtype', types)
  53. def test_distance_transform_bf02(self, dtype):
  54. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  55. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  56. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  57. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  58. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  59. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  60. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  61. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  62. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  63. out, ft = ndimage.distance_transform_bf(data, 'cityblock',
  64. return_indices=True)
  65. expected = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
  66. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  67. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  68. [0, 0, 1, 2, 2, 2, 1, 0, 0],
  69. [0, 0, 1, 2, 3, 2, 1, 0, 0],
  70. [0, 0, 1, 2, 2, 2, 1, 0, 0],
  71. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  72. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  73. [0, 0, 0, 0, 0, 0, 0, 0, 0]]
  74. assert_array_almost_equal(out, expected)
  75. expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0],
  76. [1, 1, 1, 1, 1, 1, 1, 1, 1],
  77. [2, 2, 2, 2, 1, 2, 2, 2, 2],
  78. [3, 3, 3, 3, 1, 3, 3, 3, 3],
  79. [4, 4, 4, 4, 7, 4, 4, 4, 4],
  80. [5, 5, 6, 7, 7, 7, 6, 5, 5],
  81. [6, 6, 6, 7, 7, 7, 6, 6, 6],
  82. [7, 7, 7, 7, 7, 7, 7, 7, 7],
  83. [8, 8, 8, 8, 8, 8, 8, 8, 8]],
  84. [[0, 1, 2, 3, 4, 5, 6, 7, 8],
  85. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  86. [0, 1, 2, 2, 4, 6, 6, 7, 8],
  87. [0, 1, 1, 1, 4, 7, 7, 7, 8],
  88. [0, 1, 1, 1, 4, 7, 7, 7, 8],
  89. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  90. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  91. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  92. [0, 1, 2, 3, 4, 5, 6, 7, 8]]]
  93. assert_array_almost_equal(expected, ft)
  94. @pytest.mark.parametrize('dtype', types)
  95. def test_distance_transform_bf03(self, dtype):
  96. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  97. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  98. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  99. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  100. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  101. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  102. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  103. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  104. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  105. out, ft = ndimage.distance_transform_bf(data, 'chessboard',
  106. return_indices=True)
  107. expected = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
  108. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  109. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  110. [0, 0, 1, 1, 2, 1, 1, 0, 0],
  111. [0, 0, 1, 2, 2, 2, 1, 0, 0],
  112. [0, 0, 1, 1, 2, 1, 1, 0, 0],
  113. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  114. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  115. [0, 0, 0, 0, 0, 0, 0, 0, 0]]
  116. assert_array_almost_equal(out, expected)
  117. expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0],
  118. [1, 1, 1, 1, 1, 1, 1, 1, 1],
  119. [2, 2, 2, 2, 1, 2, 2, 2, 2],
  120. [3, 3, 4, 2, 2, 2, 4, 3, 3],
  121. [4, 4, 5, 6, 6, 6, 5, 4, 4],
  122. [5, 5, 6, 6, 7, 6, 6, 5, 5],
  123. [6, 6, 6, 7, 7, 7, 6, 6, 6],
  124. [7, 7, 7, 7, 7, 7, 7, 7, 7],
  125. [8, 8, 8, 8, 8, 8, 8, 8, 8]],
  126. [[0, 1, 2, 3, 4, 5, 6, 7, 8],
  127. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  128. [0, 1, 2, 2, 5, 6, 6, 7, 8],
  129. [0, 1, 1, 2, 6, 6, 7, 7, 8],
  130. [0, 1, 1, 2, 6, 7, 7, 7, 8],
  131. [0, 1, 2, 2, 6, 6, 7, 7, 8],
  132. [0, 1, 2, 4, 5, 6, 6, 7, 8],
  133. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  134. [0, 1, 2, 3, 4, 5, 6, 7, 8]]]
  135. assert_array_almost_equal(ft, expected)
  136. @pytest.mark.parametrize('dtype', types)
  137. def test_distance_transform_bf04(self, dtype):
  138. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  139. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  140. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  141. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  142. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  143. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  144. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  145. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  146. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  147. tdt, tft = ndimage.distance_transform_bf(data, return_indices=1)
  148. dts = []
  149. fts = []
  150. dt = numpy.zeros(data.shape, dtype=numpy.float64)
  151. ndimage.distance_transform_bf(data, distances=dt)
  152. dts.append(dt)
  153. ft = ndimage.distance_transform_bf(
  154. data, return_distances=False, return_indices=1)
  155. fts.append(ft)
  156. ft = numpy.indices(data.shape, dtype=numpy.int32)
  157. ndimage.distance_transform_bf(
  158. data, return_distances=False, return_indices=True, indices=ft)
  159. fts.append(ft)
  160. dt, ft = ndimage.distance_transform_bf(
  161. data, return_indices=1)
  162. dts.append(dt)
  163. fts.append(ft)
  164. dt = numpy.zeros(data.shape, dtype=numpy.float64)
  165. ft = ndimage.distance_transform_bf(
  166. data, distances=dt, return_indices=True)
  167. dts.append(dt)
  168. fts.append(ft)
  169. ft = numpy.indices(data.shape, dtype=numpy.int32)
  170. dt = ndimage.distance_transform_bf(
  171. data, return_indices=True, indices=ft)
  172. dts.append(dt)
  173. fts.append(ft)
  174. dt = numpy.zeros(data.shape, dtype=numpy.float64)
  175. ft = numpy.indices(data.shape, dtype=numpy.int32)
  176. ndimage.distance_transform_bf(
  177. data, distances=dt, return_indices=True, indices=ft)
  178. dts.append(dt)
  179. fts.append(ft)
  180. for dt in dts:
  181. assert_array_almost_equal(tdt, dt)
  182. for ft in fts:
  183. assert_array_almost_equal(tft, ft)
  184. @pytest.mark.parametrize('dtype', types)
  185. def test_distance_transform_bf05(self, dtype):
  186. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  187. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  188. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  189. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  190. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  191. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  192. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  193. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  194. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  195. out, ft = ndimage.distance_transform_bf(
  196. data, 'euclidean', return_indices=True, sampling=[2, 2])
  197. expected = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
  198. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  199. [0, 0, 0, 4, 4, 4, 0, 0, 0],
  200. [0, 0, 4, 8, 16, 8, 4, 0, 0],
  201. [0, 0, 4, 16, 32, 16, 4, 0, 0],
  202. [0, 0, 4, 8, 16, 8, 4, 0, 0],
  203. [0, 0, 0, 4, 4, 4, 0, 0, 0],
  204. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  205. [0, 0, 0, 0, 0, 0, 0, 0, 0]]
  206. assert_array_almost_equal(out * out, expected)
  207. expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0],
  208. [1, 1, 1, 1, 1, 1, 1, 1, 1],
  209. [2, 2, 2, 2, 1, 2, 2, 2, 2],
  210. [3, 3, 3, 2, 1, 2, 3, 3, 3],
  211. [4, 4, 4, 4, 6, 4, 4, 4, 4],
  212. [5, 5, 6, 6, 7, 6, 6, 5, 5],
  213. [6, 6, 6, 7, 7, 7, 6, 6, 6],
  214. [7, 7, 7, 7, 7, 7, 7, 7, 7],
  215. [8, 8, 8, 8, 8, 8, 8, 8, 8]],
  216. [[0, 1, 2, 3, 4, 5, 6, 7, 8],
  217. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  218. [0, 1, 2, 2, 4, 6, 6, 7, 8],
  219. [0, 1, 1, 2, 4, 6, 7, 7, 8],
  220. [0, 1, 1, 1, 6, 7, 7, 7, 8],
  221. [0, 1, 2, 2, 4, 6, 6, 7, 8],
  222. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  223. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  224. [0, 1, 2, 3, 4, 5, 6, 7, 8]]]
  225. assert_array_almost_equal(ft, expected)
  226. @pytest.mark.parametrize('dtype', types)
  227. def test_distance_transform_bf06(self, dtype):
  228. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  229. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  230. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  231. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  232. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  233. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  234. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  235. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  236. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  237. out, ft = ndimage.distance_transform_bf(
  238. data, 'euclidean', return_indices=True, sampling=[2, 1])
  239. expected = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
  240. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  241. [0, 0, 0, 1, 4, 1, 0, 0, 0],
  242. [0, 0, 1, 4, 8, 4, 1, 0, 0],
  243. [0, 0, 1, 4, 9, 4, 1, 0, 0],
  244. [0, 0, 1, 4, 8, 4, 1, 0, 0],
  245. [0, 0, 0, 1, 4, 1, 0, 0, 0],
  246. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  247. [0, 0, 0, 0, 0, 0, 0, 0, 0]]
  248. assert_array_almost_equal(out * out, expected)
  249. expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0],
  250. [1, 1, 1, 1, 1, 1, 1, 1, 1],
  251. [2, 2, 2, 2, 2, 2, 2, 2, 2],
  252. [3, 3, 3, 3, 2, 3, 3, 3, 3],
  253. [4, 4, 4, 4, 4, 4, 4, 4, 4],
  254. [5, 5, 5, 5, 6, 5, 5, 5, 5],
  255. [6, 6, 6, 6, 7, 6, 6, 6, 6],
  256. [7, 7, 7, 7, 7, 7, 7, 7, 7],
  257. [8, 8, 8, 8, 8, 8, 8, 8, 8]],
  258. [[0, 1, 2, 3, 4, 5, 6, 7, 8],
  259. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  260. [0, 1, 2, 2, 6, 6, 6, 7, 8],
  261. [0, 1, 1, 1, 6, 7, 7, 7, 8],
  262. [0, 1, 1, 1, 7, 7, 7, 7, 8],
  263. [0, 1, 1, 1, 6, 7, 7, 7, 8],
  264. [0, 1, 2, 2, 4, 6, 6, 7, 8],
  265. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  266. [0, 1, 2, 3, 4, 5, 6, 7, 8]]]
  267. assert_array_almost_equal(ft, expected)
  268. def test_distance_transform_bf07(self):
  269. # test input validation per discussion on PR #13302
  270. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  271. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  272. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  273. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  274. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  275. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  276. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  277. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  278. [0, 0, 0, 0, 0, 0, 0, 0, 0]])
  279. with assert_raises(RuntimeError):
  280. ndimage.distance_transform_bf(
  281. data, return_distances=False, return_indices=False
  282. )
  283. @pytest.mark.parametrize('dtype', types)
  284. def test_distance_transform_cdt01(self, dtype):
  285. # chamfer type distance (cdt) transform
  286. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  287. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  288. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  289. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  290. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  291. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  292. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  293. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  294. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  295. out, ft = ndimage.distance_transform_cdt(
  296. data, 'cityblock', return_indices=True)
  297. bf = ndimage.distance_transform_bf(data, 'cityblock')
  298. assert_array_almost_equal(bf, out)
  299. expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0],
  300. [1, 1, 1, 1, 1, 1, 1, 1, 1],
  301. [2, 2, 2, 1, 1, 1, 2, 2, 2],
  302. [3, 3, 2, 1, 1, 1, 2, 3, 3],
  303. [4, 4, 4, 4, 1, 4, 4, 4, 4],
  304. [5, 5, 5, 5, 7, 7, 6, 5, 5],
  305. [6, 6, 6, 6, 7, 7, 6, 6, 6],
  306. [7, 7, 7, 7, 7, 7, 7, 7, 7],
  307. [8, 8, 8, 8, 8, 8, 8, 8, 8]],
  308. [[0, 1, 2, 3, 4, 5, 6, 7, 8],
  309. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  310. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  311. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  312. [0, 1, 1, 1, 4, 7, 7, 7, 8],
  313. [0, 1, 1, 1, 4, 5, 6, 7, 8],
  314. [0, 1, 2, 2, 4, 5, 6, 7, 8],
  315. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  316. [0, 1, 2, 3, 4, 5, 6, 7, 8]]]
  317. assert_array_almost_equal(ft, expected)
  318. @pytest.mark.parametrize('dtype', types)
  319. def test_distance_transform_cdt02(self, dtype):
  320. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  321. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  322. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  323. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  324. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  325. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  326. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  327. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  328. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  329. out, ft = ndimage.distance_transform_cdt(data, 'chessboard',
  330. return_indices=True)
  331. bf = ndimage.distance_transform_bf(data, 'chessboard')
  332. assert_array_almost_equal(bf, out)
  333. expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0],
  334. [1, 1, 1, 1, 1, 1, 1, 1, 1],
  335. [2, 2, 2, 1, 1, 1, 2, 2, 2],
  336. [3, 3, 2, 2, 1, 2, 2, 3, 3],
  337. [4, 4, 3, 2, 2, 2, 3, 4, 4],
  338. [5, 5, 4, 6, 7, 6, 4, 5, 5],
  339. [6, 6, 6, 6, 7, 7, 6, 6, 6],
  340. [7, 7, 7, 7, 7, 7, 7, 7, 7],
  341. [8, 8, 8, 8, 8, 8, 8, 8, 8]],
  342. [[0, 1, 2, 3, 4, 5, 6, 7, 8],
  343. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  344. [0, 1, 2, 2, 3, 4, 6, 7, 8],
  345. [0, 1, 1, 2, 2, 6, 6, 7, 8],
  346. [0, 1, 1, 1, 2, 6, 7, 7, 8],
  347. [0, 1, 1, 2, 6, 6, 7, 7, 8],
  348. [0, 1, 2, 2, 5, 6, 6, 7, 8],
  349. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  350. [0, 1, 2, 3, 4, 5, 6, 7, 8]]]
  351. assert_array_almost_equal(ft, expected)
  352. @pytest.mark.parametrize('dtype', types)
  353. def test_distance_transform_cdt03(self, dtype):
  354. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  355. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  356. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  357. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  358. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  359. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  360. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  361. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  362. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  363. tdt, tft = ndimage.distance_transform_cdt(data, return_indices=True)
  364. dts = []
  365. fts = []
  366. dt = numpy.zeros(data.shape, dtype=numpy.int32)
  367. ndimage.distance_transform_cdt(data, distances=dt)
  368. dts.append(dt)
  369. ft = ndimage.distance_transform_cdt(
  370. data, return_distances=False, return_indices=True)
  371. fts.append(ft)
  372. ft = numpy.indices(data.shape, dtype=numpy.int32)
  373. ndimage.distance_transform_cdt(
  374. data, return_distances=False, return_indices=True, indices=ft)
  375. fts.append(ft)
  376. dt, ft = ndimage.distance_transform_cdt(
  377. data, return_indices=True)
  378. dts.append(dt)
  379. fts.append(ft)
  380. dt = numpy.zeros(data.shape, dtype=numpy.int32)
  381. ft = ndimage.distance_transform_cdt(
  382. data, distances=dt, return_indices=True)
  383. dts.append(dt)
  384. fts.append(ft)
  385. ft = numpy.indices(data.shape, dtype=numpy.int32)
  386. dt = ndimage.distance_transform_cdt(
  387. data, return_indices=True, indices=ft)
  388. dts.append(dt)
  389. fts.append(ft)
  390. dt = numpy.zeros(data.shape, dtype=numpy.int32)
  391. ft = numpy.indices(data.shape, dtype=numpy.int32)
  392. ndimage.distance_transform_cdt(data, distances=dt,
  393. return_indices=True, indices=ft)
  394. dts.append(dt)
  395. fts.append(ft)
  396. for dt in dts:
  397. assert_array_almost_equal(tdt, dt)
  398. for ft in fts:
  399. assert_array_almost_equal(tft, ft)
  400. def test_distance_transform_cdt04(self):
  401. # test input validation per discussion on PR #13302
  402. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  403. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  404. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  405. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  406. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  407. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  408. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  409. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  410. [0, 0, 0, 0, 0, 0, 0, 0, 0]])
  411. indices_out = numpy.zeros((data.ndim,) + data.shape, dtype=numpy.int32)
  412. with assert_raises(RuntimeError):
  413. ndimage.distance_transform_bf(
  414. data,
  415. return_distances=True,
  416. return_indices=False,
  417. indices=indices_out
  418. )
  419. @pytest.mark.parametrize('dtype', types)
  420. def test_distance_transform_edt01(self, dtype):
  421. # euclidean distance transform (edt)
  422. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  423. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  424. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  425. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  426. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  427. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  428. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  429. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  430. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  431. out, ft = ndimage.distance_transform_edt(data, return_indices=True)
  432. bf = ndimage.distance_transform_bf(data, 'euclidean')
  433. assert_array_almost_equal(bf, out)
  434. dt = ft - numpy.indices(ft.shape[1:], dtype=ft.dtype)
  435. dt = dt.astype(numpy.float64)
  436. numpy.multiply(dt, dt, dt)
  437. dt = numpy.add.reduce(dt, axis=0)
  438. numpy.sqrt(dt, dt)
  439. assert_array_almost_equal(bf, dt)
  440. @pytest.mark.parametrize('dtype', types)
  441. def test_distance_transform_edt02(self, dtype):
  442. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  443. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  444. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  445. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  446. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  447. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  448. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  449. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  450. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  451. tdt, tft = ndimage.distance_transform_edt(data, return_indices=True)
  452. dts = []
  453. fts = []
  454. dt = numpy.zeros(data.shape, dtype=numpy.float64)
  455. ndimage.distance_transform_edt(data, distances=dt)
  456. dts.append(dt)
  457. ft = ndimage.distance_transform_edt(
  458. data, return_distances=0, return_indices=True)
  459. fts.append(ft)
  460. ft = numpy.indices(data.shape, dtype=numpy.int32)
  461. ndimage.distance_transform_edt(
  462. data, return_distances=False, return_indices=True, indices=ft)
  463. fts.append(ft)
  464. dt, ft = ndimage.distance_transform_edt(
  465. data, return_indices=True)
  466. dts.append(dt)
  467. fts.append(ft)
  468. dt = numpy.zeros(data.shape, dtype=numpy.float64)
  469. ft = ndimage.distance_transform_edt(
  470. data, distances=dt, return_indices=True)
  471. dts.append(dt)
  472. fts.append(ft)
  473. ft = numpy.indices(data.shape, dtype=numpy.int32)
  474. dt = ndimage.distance_transform_edt(
  475. data, return_indices=True, indices=ft)
  476. dts.append(dt)
  477. fts.append(ft)
  478. dt = numpy.zeros(data.shape, dtype=numpy.float64)
  479. ft = numpy.indices(data.shape, dtype=numpy.int32)
  480. ndimage.distance_transform_edt(
  481. data, distances=dt, return_indices=True, indices=ft)
  482. dts.append(dt)
  483. fts.append(ft)
  484. for dt in dts:
  485. assert_array_almost_equal(tdt, dt)
  486. for ft in fts:
  487. assert_array_almost_equal(tft, ft)
  488. @pytest.mark.parametrize('dtype', types)
  489. def test_distance_transform_edt03(self, dtype):
  490. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  491. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  492. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  493. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  494. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  495. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  496. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  497. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  498. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  499. ref = ndimage.distance_transform_bf(data, 'euclidean', sampling=[2, 2])
  500. out = ndimage.distance_transform_edt(data, sampling=[2, 2])
  501. assert_array_almost_equal(ref, out)
  502. @pytest.mark.parametrize('dtype', types)
  503. def test_distance_transform_edt4(self, dtype):
  504. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  505. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  506. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  507. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  508. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  509. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  510. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  511. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  512. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  513. ref = ndimage.distance_transform_bf(data, 'euclidean', sampling=[2, 1])
  514. out = ndimage.distance_transform_edt(data, sampling=[2, 1])
  515. assert_array_almost_equal(ref, out)
  516. def test_distance_transform_edt5(self):
  517. # Ticket #954 regression test
  518. out = ndimage.distance_transform_edt(False)
  519. assert_array_almost_equal(out, [0.])
  520. def test_distance_transform_edt6(self):
  521. # test input validation per discussion on PR #13302
  522. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  523. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  524. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  525. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  526. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  527. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  528. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  529. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  530. [0, 0, 0, 0, 0, 0, 0, 0, 0]])
  531. distances_out = numpy.zeros(data.shape, dtype=numpy.float64)
  532. with assert_raises(RuntimeError):
  533. ndimage.distance_transform_bf(
  534. data,
  535. return_indices=True,
  536. return_distances=False,
  537. distances=distances_out
  538. )
  539. def test_generate_structure01(self):
  540. struct = ndimage.generate_binary_structure(0, 1)
  541. assert_array_almost_equal(struct, 1)
  542. def test_generate_structure02(self):
  543. struct = ndimage.generate_binary_structure(1, 1)
  544. assert_array_almost_equal(struct, [1, 1, 1])
  545. def test_generate_structure03(self):
  546. struct = ndimage.generate_binary_structure(2, 1)
  547. assert_array_almost_equal(struct, [[0, 1, 0],
  548. [1, 1, 1],
  549. [0, 1, 0]])
  550. def test_generate_structure04(self):
  551. struct = ndimage.generate_binary_structure(2, 2)
  552. assert_array_almost_equal(struct, [[1, 1, 1],
  553. [1, 1, 1],
  554. [1, 1, 1]])
  555. def test_iterate_structure01(self):
  556. struct = [[0, 1, 0],
  557. [1, 1, 1],
  558. [0, 1, 0]]
  559. out = ndimage.iterate_structure(struct, 2)
  560. assert_array_almost_equal(out, [[0, 0, 1, 0, 0],
  561. [0, 1, 1, 1, 0],
  562. [1, 1, 1, 1, 1],
  563. [0, 1, 1, 1, 0],
  564. [0, 0, 1, 0, 0]])
  565. def test_iterate_structure02(self):
  566. struct = [[0, 1],
  567. [1, 1],
  568. [0, 1]]
  569. out = ndimage.iterate_structure(struct, 2)
  570. assert_array_almost_equal(out, [[0, 0, 1],
  571. [0, 1, 1],
  572. [1, 1, 1],
  573. [0, 1, 1],
  574. [0, 0, 1]])
  575. def test_iterate_structure03(self):
  576. struct = [[0, 1, 0],
  577. [1, 1, 1],
  578. [0, 1, 0]]
  579. out = ndimage.iterate_structure(struct, 2, 1)
  580. expected = [[0, 0, 1, 0, 0],
  581. [0, 1, 1, 1, 0],
  582. [1, 1, 1, 1, 1],
  583. [0, 1, 1, 1, 0],
  584. [0, 0, 1, 0, 0]]
  585. assert_array_almost_equal(out[0], expected)
  586. assert_equal(out[1], [2, 2])
  587. @pytest.mark.parametrize('dtype', types)
  588. def test_binary_erosion01(self, dtype):
  589. data = numpy.ones([], dtype)
  590. out = ndimage.binary_erosion(data)
  591. assert_array_almost_equal(out, 1)
  592. @pytest.mark.parametrize('dtype', types)
  593. def test_binary_erosion02(self, dtype):
  594. data = numpy.ones([], dtype)
  595. out = ndimage.binary_erosion(data, border_value=1)
  596. assert_array_almost_equal(out, 1)
  597. @pytest.mark.parametrize('dtype', types)
  598. def test_binary_erosion03(self, dtype):
  599. data = numpy.ones([1], dtype)
  600. out = ndimage.binary_erosion(data)
  601. assert_array_almost_equal(out, [0])
  602. @pytest.mark.parametrize('dtype', types)
  603. def test_binary_erosion04(self, dtype):
  604. data = numpy.ones([1], dtype)
  605. out = ndimage.binary_erosion(data, border_value=1)
  606. assert_array_almost_equal(out, [1])
  607. @pytest.mark.parametrize('dtype', types)
  608. def test_binary_erosion05(self, dtype):
  609. data = numpy.ones([3], dtype)
  610. out = ndimage.binary_erosion(data)
  611. assert_array_almost_equal(out, [0, 1, 0])
  612. @pytest.mark.parametrize('dtype', types)
  613. def test_binary_erosion06(self, dtype):
  614. data = numpy.ones([3], dtype)
  615. out = ndimage.binary_erosion(data, border_value=1)
  616. assert_array_almost_equal(out, [1, 1, 1])
  617. @pytest.mark.parametrize('dtype', types)
  618. def test_binary_erosion07(self, dtype):
  619. data = numpy.ones([5], dtype)
  620. out = ndimage.binary_erosion(data)
  621. assert_array_almost_equal(out, [0, 1, 1, 1, 0])
  622. @pytest.mark.parametrize('dtype', types)
  623. def test_binary_erosion08(self, dtype):
  624. data = numpy.ones([5], dtype)
  625. out = ndimage.binary_erosion(data, border_value=1)
  626. assert_array_almost_equal(out, [1, 1, 1, 1, 1])
  627. @pytest.mark.parametrize('dtype', types)
  628. def test_binary_erosion09(self, dtype):
  629. data = numpy.ones([5], dtype)
  630. data[2] = 0
  631. out = ndimage.binary_erosion(data)
  632. assert_array_almost_equal(out, [0, 0, 0, 0, 0])
  633. @pytest.mark.parametrize('dtype', types)
  634. def test_binary_erosion10(self, dtype):
  635. data = numpy.ones([5], dtype)
  636. data[2] = 0
  637. out = ndimage.binary_erosion(data, border_value=1)
  638. assert_array_almost_equal(out, [1, 0, 0, 0, 1])
  639. @pytest.mark.parametrize('dtype', types)
  640. def test_binary_erosion11(self, dtype):
  641. data = numpy.ones([5], dtype)
  642. data[2] = 0
  643. struct = [1, 0, 1]
  644. out = ndimage.binary_erosion(data, struct, border_value=1)
  645. assert_array_almost_equal(out, [1, 0, 1, 0, 1])
  646. @pytest.mark.parametrize('dtype', types)
  647. def test_binary_erosion12(self, dtype):
  648. data = numpy.ones([5], dtype)
  649. data[2] = 0
  650. struct = [1, 0, 1]
  651. out = ndimage.binary_erosion(data, struct, border_value=1, origin=-1)
  652. assert_array_almost_equal(out, [0, 1, 0, 1, 1])
  653. @pytest.mark.parametrize('dtype', types)
  654. def test_binary_erosion13(self, dtype):
  655. data = numpy.ones([5], dtype)
  656. data[2] = 0
  657. struct = [1, 0, 1]
  658. out = ndimage.binary_erosion(data, struct, border_value=1, origin=1)
  659. assert_array_almost_equal(out, [1, 1, 0, 1, 0])
  660. @pytest.mark.parametrize('dtype', types)
  661. def test_binary_erosion14(self, dtype):
  662. data = numpy.ones([5], dtype)
  663. data[2] = 0
  664. struct = [1, 1]
  665. out = ndimage.binary_erosion(data, struct, border_value=1)
  666. assert_array_almost_equal(out, [1, 1, 0, 0, 1])
  667. @pytest.mark.parametrize('dtype', types)
  668. def test_binary_erosion15(self, dtype):
  669. data = numpy.ones([5], dtype)
  670. data[2] = 0
  671. struct = [1, 1]
  672. out = ndimage.binary_erosion(data, struct, border_value=1, origin=-1)
  673. assert_array_almost_equal(out, [1, 0, 0, 1, 1])
  674. @pytest.mark.parametrize('dtype', types)
  675. def test_binary_erosion16(self, dtype):
  676. data = numpy.ones([1, 1], dtype)
  677. out = ndimage.binary_erosion(data, border_value=1)
  678. assert_array_almost_equal(out, [[1]])
  679. @pytest.mark.parametrize('dtype', types)
  680. def test_binary_erosion17(self, dtype):
  681. data = numpy.ones([1, 1], dtype)
  682. out = ndimage.binary_erosion(data)
  683. assert_array_almost_equal(out, [[0]])
  684. @pytest.mark.parametrize('dtype', types)
  685. def test_binary_erosion18(self, dtype):
  686. data = numpy.ones([1, 3], dtype)
  687. out = ndimage.binary_erosion(data)
  688. assert_array_almost_equal(out, [[0, 0, 0]])
  689. @pytest.mark.parametrize('dtype', types)
  690. def test_binary_erosion19(self, dtype):
  691. data = numpy.ones([1, 3], dtype)
  692. out = ndimage.binary_erosion(data, border_value=1)
  693. assert_array_almost_equal(out, [[1, 1, 1]])
  694. @pytest.mark.parametrize('dtype', types)
  695. def test_binary_erosion20(self, dtype):
  696. data = numpy.ones([3, 3], dtype)
  697. out = ndimage.binary_erosion(data)
  698. assert_array_almost_equal(out, [[0, 0, 0],
  699. [0, 1, 0],
  700. [0, 0, 0]])
  701. @pytest.mark.parametrize('dtype', types)
  702. def test_binary_erosion21(self, dtype):
  703. data = numpy.ones([3, 3], dtype)
  704. out = ndimage.binary_erosion(data, border_value=1)
  705. assert_array_almost_equal(out, [[1, 1, 1],
  706. [1, 1, 1],
  707. [1, 1, 1]])
  708. @pytest.mark.parametrize('dtype', types)
  709. def test_binary_erosion22(self, dtype):
  710. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  711. [0, 0, 0, 0, 0, 0, 0, 0],
  712. [0, 0, 0, 0, 0, 0, 0, 0],
  713. [0, 0, 0, 0, 0, 1, 0, 0],
  714. [0, 0, 0, 1, 1, 0, 0, 0],
  715. [0, 0, 1, 0, 0, 1, 0, 0],
  716. [0, 0, 0, 0, 0, 0, 0, 0],
  717. [0, 0, 0, 0, 0, 0, 0, 0]]
  718. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  719. [0, 1, 0, 0, 0, 0, 0, 0],
  720. [0, 0, 0, 0, 0, 1, 1, 1],
  721. [0, 0, 1, 1, 1, 1, 1, 1],
  722. [0, 0, 1, 1, 1, 1, 0, 0],
  723. [0, 1, 1, 1, 1, 1, 1, 0],
  724. [0, 1, 1, 0, 0, 1, 1, 0],
  725. [0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  726. out = ndimage.binary_erosion(data, border_value=1)
  727. assert_array_almost_equal(out, expected)
  728. @pytest.mark.parametrize('dtype', types)
  729. def test_binary_erosion23(self, dtype):
  730. struct = ndimage.generate_binary_structure(2, 2)
  731. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  732. [0, 0, 0, 0, 0, 0, 0, 0],
  733. [0, 0, 0, 0, 0, 0, 0, 0],
  734. [0, 0, 0, 0, 0, 0, 0, 0],
  735. [0, 0, 0, 1, 1, 0, 0, 0],
  736. [0, 0, 0, 0, 0, 0, 0, 0],
  737. [0, 0, 0, 0, 0, 0, 0, 0],
  738. [0, 0, 0, 0, 0, 0, 0, 0]]
  739. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  740. [0, 1, 0, 0, 0, 0, 0, 0],
  741. [0, 0, 0, 0, 0, 1, 1, 1],
  742. [0, 0, 1, 1, 1, 1, 1, 1],
  743. [0, 0, 1, 1, 1, 1, 0, 0],
  744. [0, 1, 1, 1, 1, 1, 1, 0],
  745. [0, 1, 1, 0, 0, 1, 1, 0],
  746. [0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  747. out = ndimage.binary_erosion(data, struct, border_value=1)
  748. assert_array_almost_equal(out, expected)
  749. @pytest.mark.parametrize('dtype', types)
  750. def test_binary_erosion24(self, dtype):
  751. struct = [[0, 1],
  752. [1, 1]]
  753. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  754. [0, 0, 0, 0, 0, 0, 0, 0],
  755. [0, 0, 0, 0, 0, 0, 0, 0],
  756. [0, 0, 0, 0, 0, 1, 1, 1],
  757. [0, 0, 0, 1, 1, 1, 0, 0],
  758. [0, 0, 1, 1, 1, 1, 0, 0],
  759. [0, 0, 1, 0, 0, 0, 1, 0],
  760. [0, 0, 0, 0, 0, 0, 0, 0]]
  761. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  762. [0, 1, 0, 0, 0, 0, 0, 0],
  763. [0, 0, 0, 0, 0, 1, 1, 1],
  764. [0, 0, 1, 1, 1, 1, 1, 1],
  765. [0, 0, 1, 1, 1, 1, 0, 0],
  766. [0, 1, 1, 1, 1, 1, 1, 0],
  767. [0, 1, 1, 0, 0, 1, 1, 0],
  768. [0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  769. out = ndimage.binary_erosion(data, struct, border_value=1)
  770. assert_array_almost_equal(out, expected)
  771. @pytest.mark.parametrize('dtype', types)
  772. def test_binary_erosion25(self, dtype):
  773. struct = [[0, 1, 0],
  774. [1, 0, 1],
  775. [0, 1, 0]]
  776. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  777. [0, 0, 0, 0, 0, 0, 0, 0],
  778. [0, 0, 0, 0, 0, 0, 0, 0],
  779. [0, 0, 0, 0, 0, 1, 0, 0],
  780. [0, 0, 0, 1, 0, 0, 0, 0],
  781. [0, 0, 1, 0, 0, 1, 0, 0],
  782. [0, 0, 0, 0, 0, 0, 0, 0],
  783. [0, 0, 0, 0, 0, 0, 0, 0]]
  784. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  785. [0, 1, 0, 0, 0, 0, 0, 0],
  786. [0, 0, 0, 0, 0, 1, 1, 1],
  787. [0, 0, 1, 1, 1, 0, 1, 1],
  788. [0, 0, 1, 0, 1, 1, 0, 0],
  789. [0, 1, 0, 1, 1, 1, 1, 0],
  790. [0, 1, 1, 0, 0, 1, 1, 0],
  791. [0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  792. out = ndimage.binary_erosion(data, struct, border_value=1)
  793. assert_array_almost_equal(out, expected)
  794. @pytest.mark.parametrize('dtype', types)
  795. def test_binary_erosion26(self, dtype):
  796. struct = [[0, 1, 0],
  797. [1, 0, 1],
  798. [0, 1, 0]]
  799. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  800. [0, 0, 0, 0, 0, 0, 0, 1],
  801. [0, 0, 0, 0, 1, 0, 0, 1],
  802. [0, 0, 1, 0, 0, 0, 0, 0],
  803. [0, 1, 0, 0, 1, 0, 0, 0],
  804. [0, 0, 0, 0, 0, 0, 0, 0],
  805. [0, 0, 0, 0, 0, 0, 0, 0],
  806. [0, 0, 0, 0, 0, 0, 0, 1]]
  807. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  808. [0, 1, 0, 0, 0, 0, 0, 0],
  809. [0, 0, 0, 0, 0, 1, 1, 1],
  810. [0, 0, 1, 1, 1, 0, 1, 1],
  811. [0, 0, 1, 0, 1, 1, 0, 0],
  812. [0, 1, 0, 1, 1, 1, 1, 0],
  813. [0, 1, 1, 0, 0, 1, 1, 0],
  814. [0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  815. out = ndimage.binary_erosion(data, struct, border_value=1,
  816. origin=(-1, -1))
  817. assert_array_almost_equal(out, expected)
  818. def test_binary_erosion27(self):
  819. struct = [[0, 1, 0],
  820. [1, 1, 1],
  821. [0, 1, 0]]
  822. expected = [[0, 0, 0, 0, 0, 0, 0],
  823. [0, 0, 0, 0, 0, 0, 0],
  824. [0, 0, 0, 0, 0, 0, 0],
  825. [0, 0, 0, 1, 0, 0, 0],
  826. [0, 0, 0, 0, 0, 0, 0],
  827. [0, 0, 0, 0, 0, 0, 0],
  828. [0, 0, 0, 0, 0, 0, 0]]
  829. data = numpy.array([[0, 0, 0, 0, 0, 0, 0],
  830. [0, 0, 0, 1, 0, 0, 0],
  831. [0, 0, 1, 1, 1, 0, 0],
  832. [0, 1, 1, 1, 1, 1, 0],
  833. [0, 0, 1, 1, 1, 0, 0],
  834. [0, 0, 0, 1, 0, 0, 0],
  835. [0, 0, 0, 0, 0, 0, 0]], bool)
  836. out = ndimage.binary_erosion(data, struct, border_value=1,
  837. iterations=2)
  838. assert_array_almost_equal(out, expected)
  839. def test_binary_erosion28(self):
  840. struct = [[0, 1, 0],
  841. [1, 1, 1],
  842. [0, 1, 0]]
  843. expected = [[0, 0, 0, 0, 0, 0, 0],
  844. [0, 0, 0, 0, 0, 0, 0],
  845. [0, 0, 0, 0, 0, 0, 0],
  846. [0, 0, 0, 1, 0, 0, 0],
  847. [0, 0, 0, 0, 0, 0, 0],
  848. [0, 0, 0, 0, 0, 0, 0],
  849. [0, 0, 0, 0, 0, 0, 0]]
  850. data = numpy.array([[0, 0, 0, 0, 0, 0, 0],
  851. [0, 0, 0, 1, 0, 0, 0],
  852. [0, 0, 1, 1, 1, 0, 0],
  853. [0, 1, 1, 1, 1, 1, 0],
  854. [0, 0, 1, 1, 1, 0, 0],
  855. [0, 0, 0, 1, 0, 0, 0],
  856. [0, 0, 0, 0, 0, 0, 0]], bool)
  857. out = numpy.zeros(data.shape, bool)
  858. ndimage.binary_erosion(data, struct, border_value=1,
  859. iterations=2, output=out)
  860. assert_array_almost_equal(out, expected)
  861. def test_binary_erosion29(self):
  862. struct = [[0, 1, 0],
  863. [1, 1, 1],
  864. [0, 1, 0]]
  865. expected = [[0, 0, 0, 0, 0, 0, 0],
  866. [0, 0, 0, 0, 0, 0, 0],
  867. [0, 0, 0, 0, 0, 0, 0],
  868. [0, 0, 0, 1, 0, 0, 0],
  869. [0, 0, 0, 0, 0, 0, 0],
  870. [0, 0, 0, 0, 0, 0, 0],
  871. [0, 0, 0, 0, 0, 0, 0]]
  872. data = numpy.array([[0, 0, 0, 1, 0, 0, 0],
  873. [0, 0, 1, 1, 1, 0, 0],
  874. [0, 1, 1, 1, 1, 1, 0],
  875. [1, 1, 1, 1, 1, 1, 1],
  876. [0, 1, 1, 1, 1, 1, 0],
  877. [0, 0, 1, 1, 1, 0, 0],
  878. [0, 0, 0, 1, 0, 0, 0]], bool)
  879. out = ndimage.binary_erosion(data, struct,
  880. border_value=1, iterations=3)
  881. assert_array_almost_equal(out, expected)
  882. def test_binary_erosion30(self):
  883. struct = [[0, 1, 0],
  884. [1, 1, 1],
  885. [0, 1, 0]]
  886. expected = [[0, 0, 0, 0, 0, 0, 0],
  887. [0, 0, 0, 0, 0, 0, 0],
  888. [0, 0, 0, 0, 0, 0, 0],
  889. [0, 0, 0, 1, 0, 0, 0],
  890. [0, 0, 0, 0, 0, 0, 0],
  891. [0, 0, 0, 0, 0, 0, 0],
  892. [0, 0, 0, 0, 0, 0, 0]]
  893. data = numpy.array([[0, 0, 0, 1, 0, 0, 0],
  894. [0, 0, 1, 1, 1, 0, 0],
  895. [0, 1, 1, 1, 1, 1, 0],
  896. [1, 1, 1, 1, 1, 1, 1],
  897. [0, 1, 1, 1, 1, 1, 0],
  898. [0, 0, 1, 1, 1, 0, 0],
  899. [0, 0, 0, 1, 0, 0, 0]], bool)
  900. out = numpy.zeros(data.shape, bool)
  901. ndimage.binary_erosion(data, struct, border_value=1,
  902. iterations=3, output=out)
  903. assert_array_almost_equal(out, expected)
  904. # test with output memory overlap
  905. ndimage.binary_erosion(data, struct, border_value=1,
  906. iterations=3, output=data)
  907. assert_array_almost_equal(data, expected)
  908. def test_binary_erosion31(self):
  909. struct = [[0, 1, 0],
  910. [1, 1, 1],
  911. [0, 1, 0]]
  912. expected = [[0, 0, 1, 0, 0, 0, 0],
  913. [0, 1, 1, 1, 0, 0, 0],
  914. [1, 1, 1, 1, 1, 0, 1],
  915. [0, 1, 1, 1, 0, 0, 0],
  916. [0, 0, 1, 0, 0, 0, 0],
  917. [0, 0, 0, 0, 0, 0, 0],
  918. [0, 0, 1, 0, 0, 0, 1]]
  919. data = numpy.array([[0, 0, 0, 1, 0, 0, 0],
  920. [0, 0, 1, 1, 1, 0, 0],
  921. [0, 1, 1, 1, 1, 1, 0],
  922. [1, 1, 1, 1, 1, 1, 1],
  923. [0, 1, 1, 1, 1, 1, 0],
  924. [0, 0, 1, 1, 1, 0, 0],
  925. [0, 0, 0, 1, 0, 0, 0]], bool)
  926. out = numpy.zeros(data.shape, bool)
  927. ndimage.binary_erosion(data, struct, border_value=1,
  928. iterations=1, output=out, origin=(-1, -1))
  929. assert_array_almost_equal(out, expected)
  930. def test_binary_erosion32(self):
  931. struct = [[0, 1, 0],
  932. [1, 1, 1],
  933. [0, 1, 0]]
  934. expected = [[0, 0, 0, 0, 0, 0, 0],
  935. [0, 0, 0, 0, 0, 0, 0],
  936. [0, 0, 0, 0, 0, 0, 0],
  937. [0, 0, 0, 1, 0, 0, 0],
  938. [0, 0, 0, 0, 0, 0, 0],
  939. [0, 0, 0, 0, 0, 0, 0],
  940. [0, 0, 0, 0, 0, 0, 0]]
  941. data = numpy.array([[0, 0, 0, 0, 0, 0, 0],
  942. [0, 0, 0, 1, 0, 0, 0],
  943. [0, 0, 1, 1, 1, 0, 0],
  944. [0, 1, 1, 1, 1, 1, 0],
  945. [0, 0, 1, 1, 1, 0, 0],
  946. [0, 0, 0, 1, 0, 0, 0],
  947. [0, 0, 0, 0, 0, 0, 0]], bool)
  948. out = ndimage.binary_erosion(data, struct,
  949. border_value=1, iterations=2)
  950. assert_array_almost_equal(out, expected)
  951. def test_binary_erosion33(self):
  952. struct = [[0, 1, 0],
  953. [1, 1, 1],
  954. [0, 1, 0]]
  955. expected = [[0, 0, 0, 0, 0, 1, 1],
  956. [0, 0, 0, 0, 0, 0, 1],
  957. [0, 0, 0, 0, 0, 0, 0],
  958. [0, 0, 0, 0, 0, 0, 0],
  959. [0, 0, 0, 0, 0, 0, 0],
  960. [0, 0, 0, 0, 0, 0, 0],
  961. [0, 0, 0, 0, 0, 0, 0]]
  962. mask = [[1, 1, 1, 1, 1, 0, 0],
  963. [1, 1, 1, 1, 1, 1, 0],
  964. [1, 1, 1, 1, 1, 1, 1],
  965. [1, 1, 1, 1, 1, 1, 1],
  966. [1, 1, 1, 1, 1, 1, 1],
  967. [1, 1, 1, 1, 1, 1, 1],
  968. [1, 1, 1, 1, 1, 1, 1]]
  969. data = numpy.array([[0, 0, 0, 0, 0, 1, 1],
  970. [0, 0, 0, 1, 0, 0, 1],
  971. [0, 0, 1, 1, 1, 0, 0],
  972. [0, 0, 1, 1, 1, 0, 0],
  973. [0, 0, 1, 1, 1, 0, 0],
  974. [0, 0, 0, 1, 0, 0, 0],
  975. [0, 0, 0, 0, 0, 0, 0]], bool)
  976. out = ndimage.binary_erosion(data, struct,
  977. border_value=1, mask=mask, iterations=-1)
  978. assert_array_almost_equal(out, expected)
  979. def test_binary_erosion34(self):
  980. struct = [[0, 1, 0],
  981. [1, 1, 1],
  982. [0, 1, 0]]
  983. expected = [[0, 0, 0, 0, 0, 0, 0],
  984. [0, 0, 0, 1, 0, 0, 0],
  985. [0, 0, 0, 1, 0, 0, 0],
  986. [0, 1, 1, 1, 1, 1, 0],
  987. [0, 0, 0, 1, 0, 0, 0],
  988. [0, 0, 0, 1, 0, 0, 0],
  989. [0, 0, 0, 0, 0, 0, 0]]
  990. mask = [[0, 0, 0, 0, 0, 0, 0],
  991. [0, 0, 0, 0, 0, 0, 0],
  992. [0, 0, 1, 1, 1, 0, 0],
  993. [0, 0, 1, 0, 1, 0, 0],
  994. [0, 0, 1, 1, 1, 0, 0],
  995. [0, 0, 0, 0, 0, 0, 0],
  996. [0, 0, 0, 0, 0, 0, 0]]
  997. data = numpy.array([[0, 0, 0, 0, 0, 0, 0],
  998. [0, 0, 0, 1, 0, 0, 0],
  999. [0, 0, 1, 1, 1, 0, 0],
  1000. [0, 1, 1, 1, 1, 1, 0],
  1001. [0, 0, 1, 1, 1, 0, 0],
  1002. [0, 0, 0, 1, 0, 0, 0],
  1003. [0, 0, 0, 0, 0, 0, 0]], bool)
  1004. out = ndimage.binary_erosion(data, struct,
  1005. border_value=1, mask=mask)
  1006. assert_array_almost_equal(out, expected)
  1007. def test_binary_erosion35(self):
  1008. struct = [[0, 1, 0],
  1009. [1, 1, 1],
  1010. [0, 1, 0]]
  1011. mask = [[0, 0, 0, 0, 0, 0, 0],
  1012. [0, 0, 0, 0, 0, 0, 0],
  1013. [0, 0, 1, 1, 1, 0, 0],
  1014. [0, 0, 1, 0, 1, 0, 0],
  1015. [0, 0, 1, 1, 1, 0, 0],
  1016. [0, 0, 0, 0, 0, 0, 0],
  1017. [0, 0, 0, 0, 0, 0, 0]]
  1018. data = numpy.array([[0, 0, 0, 1, 0, 0, 0],
  1019. [0, 0, 1, 1, 1, 0, 0],
  1020. [0, 1, 1, 1, 1, 1, 0],
  1021. [1, 1, 1, 1, 1, 1, 1],
  1022. [0, 1, 1, 1, 1, 1, 0],
  1023. [0, 0, 1, 1, 1, 0, 0],
  1024. [0, 0, 0, 1, 0, 0, 0]], bool)
  1025. tmp = [[0, 0, 1, 0, 0, 0, 0],
  1026. [0, 1, 1, 1, 0, 0, 0],
  1027. [1, 1, 1, 1, 1, 0, 1],
  1028. [0, 1, 1, 1, 0, 0, 0],
  1029. [0, 0, 1, 0, 0, 0, 0],
  1030. [0, 0, 0, 0, 0, 0, 0],
  1031. [0, 0, 1, 0, 0, 0, 1]]
  1032. expected = numpy.logical_and(tmp, mask)
  1033. tmp = numpy.logical_and(data, numpy.logical_not(mask))
  1034. expected = numpy.logical_or(expected, tmp)
  1035. out = numpy.zeros(data.shape, bool)
  1036. ndimage.binary_erosion(data, struct, border_value=1,
  1037. iterations=1, output=out,
  1038. origin=(-1, -1), mask=mask)
  1039. assert_array_almost_equal(out, expected)
  1040. def test_binary_erosion36(self):
  1041. struct = [[0, 1, 0],
  1042. [1, 0, 1],
  1043. [0, 1, 0]]
  1044. mask = [[0, 0, 0, 0, 0, 0, 0, 0],
  1045. [0, 0, 0, 0, 0, 0, 0, 0],
  1046. [0, 0, 1, 1, 1, 0, 0, 0],
  1047. [0, 0, 1, 0, 1, 0, 0, 0],
  1048. [0, 0, 1, 1, 1, 0, 0, 0],
  1049. [0, 0, 1, 1, 1, 0, 0, 0],
  1050. [0, 0, 1, 1, 1, 0, 0, 0],
  1051. [0, 0, 0, 0, 0, 0, 0, 0]]
  1052. tmp = [[0, 0, 0, 0, 0, 0, 0, 0],
  1053. [0, 0, 0, 0, 0, 0, 0, 1],
  1054. [0, 0, 0, 0, 1, 0, 0, 1],
  1055. [0, 0, 1, 0, 0, 0, 0, 0],
  1056. [0, 1, 0, 0, 1, 0, 0, 0],
  1057. [0, 0, 0, 0, 0, 0, 0, 0],
  1058. [0, 0, 0, 0, 0, 0, 0, 0],
  1059. [0, 0, 0, 0, 0, 0, 0, 1]]
  1060. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  1061. [0, 1, 0, 0, 0, 0, 0, 0],
  1062. [0, 0, 0, 0, 0, 1, 1, 1],
  1063. [0, 0, 1, 1, 1, 0, 1, 1],
  1064. [0, 0, 1, 0, 1, 1, 0, 0],
  1065. [0, 1, 0, 1, 1, 1, 1, 0],
  1066. [0, 1, 1, 0, 0, 1, 1, 0],
  1067. [0, 0, 0, 0, 0, 0, 0, 0]])
  1068. expected = numpy.logical_and(tmp, mask)
  1069. tmp = numpy.logical_and(data, numpy.logical_not(mask))
  1070. expected = numpy.logical_or(expected, tmp)
  1071. out = ndimage.binary_erosion(data, struct, mask=mask,
  1072. border_value=1, origin=(-1, -1))
  1073. assert_array_almost_equal(out, expected)
  1074. def test_binary_erosion37(self):
  1075. a = numpy.array([[1, 0, 1],
  1076. [0, 1, 0],
  1077. [1, 0, 1]], dtype=bool)
  1078. b = numpy.zeros_like(a)
  1079. out = ndimage.binary_erosion(a, structure=a, output=b, iterations=0,
  1080. border_value=True, brute_force=True)
  1081. assert_(out is b)
  1082. assert_array_equal(
  1083. ndimage.binary_erosion(a, structure=a, iterations=0,
  1084. border_value=True),
  1085. b)
  1086. def test_binary_erosion38(self):
  1087. data = numpy.array([[1, 0, 1],
  1088. [0, 1, 0],
  1089. [1, 0, 1]], dtype=bool)
  1090. iterations = 2.0
  1091. with assert_raises(TypeError):
  1092. _ = ndimage.binary_erosion(data, iterations=iterations)
  1093. def test_binary_erosion39(self):
  1094. iterations = numpy.int32(3)
  1095. struct = [[0, 1, 0],
  1096. [1, 1, 1],
  1097. [0, 1, 0]]
  1098. expected = [[0, 0, 0, 0, 0, 0, 0],
  1099. [0, 0, 0, 0, 0, 0, 0],
  1100. [0, 0, 0, 0, 0, 0, 0],
  1101. [0, 0, 0, 1, 0, 0, 0],
  1102. [0, 0, 0, 0, 0, 0, 0],
  1103. [0, 0, 0, 0, 0, 0, 0],
  1104. [0, 0, 0, 0, 0, 0, 0]]
  1105. data = numpy.array([[0, 0, 0, 1, 0, 0, 0],
  1106. [0, 0, 1, 1, 1, 0, 0],
  1107. [0, 1, 1, 1, 1, 1, 0],
  1108. [1, 1, 1, 1, 1, 1, 1],
  1109. [0, 1, 1, 1, 1, 1, 0],
  1110. [0, 0, 1, 1, 1, 0, 0],
  1111. [0, 0, 0, 1, 0, 0, 0]], bool)
  1112. out = numpy.zeros(data.shape, bool)
  1113. ndimage.binary_erosion(data, struct, border_value=1,
  1114. iterations=iterations, output=out)
  1115. assert_array_almost_equal(out, expected)
  1116. def test_binary_erosion40(self):
  1117. iterations = numpy.int64(3)
  1118. struct = [[0, 1, 0],
  1119. [1, 1, 1],
  1120. [0, 1, 0]]
  1121. expected = [[0, 0, 0, 0, 0, 0, 0],
  1122. [0, 0, 0, 0, 0, 0, 0],
  1123. [0, 0, 0, 0, 0, 0, 0],
  1124. [0, 0, 0, 1, 0, 0, 0],
  1125. [0, 0, 0, 0, 0, 0, 0],
  1126. [0, 0, 0, 0, 0, 0, 0],
  1127. [0, 0, 0, 0, 0, 0, 0]]
  1128. data = numpy.array([[0, 0, 0, 1, 0, 0, 0],
  1129. [0, 0, 1, 1, 1, 0, 0],
  1130. [0, 1, 1, 1, 1, 1, 0],
  1131. [1, 1, 1, 1, 1, 1, 1],
  1132. [0, 1, 1, 1, 1, 1, 0],
  1133. [0, 0, 1, 1, 1, 0, 0],
  1134. [0, 0, 0, 1, 0, 0, 0]], bool)
  1135. out = numpy.zeros(data.shape, bool)
  1136. ndimage.binary_erosion(data, struct, border_value=1,
  1137. iterations=iterations, output=out)
  1138. assert_array_almost_equal(out, expected)
  1139. @pytest.mark.parametrize('dtype', types)
  1140. def test_binary_dilation01(self, dtype):
  1141. data = numpy.ones([], dtype)
  1142. out = ndimage.binary_dilation(data)
  1143. assert_array_almost_equal(out, 1)
  1144. @pytest.mark.parametrize('dtype', types)
  1145. def test_binary_dilation02(self, dtype):
  1146. data = numpy.zeros([], dtype)
  1147. out = ndimage.binary_dilation(data)
  1148. assert_array_almost_equal(out, 0)
  1149. @pytest.mark.parametrize('dtype', types)
  1150. def test_binary_dilation03(self, dtype):
  1151. data = numpy.ones([1], dtype)
  1152. out = ndimage.binary_dilation(data)
  1153. assert_array_almost_equal(out, [1])
  1154. @pytest.mark.parametrize('dtype', types)
  1155. def test_binary_dilation04(self, dtype):
  1156. data = numpy.zeros([1], dtype)
  1157. out = ndimage.binary_dilation(data)
  1158. assert_array_almost_equal(out, [0])
  1159. @pytest.mark.parametrize('dtype', types)
  1160. def test_binary_dilation05(self, dtype):
  1161. data = numpy.ones([3], dtype)
  1162. out = ndimage.binary_dilation(data)
  1163. assert_array_almost_equal(out, [1, 1, 1])
  1164. @pytest.mark.parametrize('dtype', types)
  1165. def test_binary_dilation06(self, dtype):
  1166. data = numpy.zeros([3], dtype)
  1167. out = ndimage.binary_dilation(data)
  1168. assert_array_almost_equal(out, [0, 0, 0])
  1169. @pytest.mark.parametrize('dtype', types)
  1170. def test_binary_dilation07(self, dtype):
  1171. data = numpy.zeros([3], dtype)
  1172. data[1] = 1
  1173. out = ndimage.binary_dilation(data)
  1174. assert_array_almost_equal(out, [1, 1, 1])
  1175. @pytest.mark.parametrize('dtype', types)
  1176. def test_binary_dilation08(self, dtype):
  1177. data = numpy.zeros([5], dtype)
  1178. data[1] = 1
  1179. data[3] = 1
  1180. out = ndimage.binary_dilation(data)
  1181. assert_array_almost_equal(out, [1, 1, 1, 1, 1])
  1182. @pytest.mark.parametrize('dtype', types)
  1183. def test_binary_dilation09(self, dtype):
  1184. data = numpy.zeros([5], dtype)
  1185. data[1] = 1
  1186. out = ndimage.binary_dilation(data)
  1187. assert_array_almost_equal(out, [1, 1, 1, 0, 0])
  1188. @pytest.mark.parametrize('dtype', types)
  1189. def test_binary_dilation10(self, dtype):
  1190. data = numpy.zeros([5], dtype)
  1191. data[1] = 1
  1192. out = ndimage.binary_dilation(data, origin=-1)
  1193. assert_array_almost_equal(out, [0, 1, 1, 1, 0])
  1194. @pytest.mark.parametrize('dtype', types)
  1195. def test_binary_dilation11(self, dtype):
  1196. data = numpy.zeros([5], dtype)
  1197. data[1] = 1
  1198. out = ndimage.binary_dilation(data, origin=1)
  1199. assert_array_almost_equal(out, [1, 1, 0, 0, 0])
  1200. @pytest.mark.parametrize('dtype', types)
  1201. def test_binary_dilation12(self, dtype):
  1202. data = numpy.zeros([5], dtype)
  1203. data[1] = 1
  1204. struct = [1, 0, 1]
  1205. out = ndimage.binary_dilation(data, struct)
  1206. assert_array_almost_equal(out, [1, 0, 1, 0, 0])
  1207. @pytest.mark.parametrize('dtype', types)
  1208. def test_binary_dilation13(self, dtype):
  1209. data = numpy.zeros([5], dtype)
  1210. data[1] = 1
  1211. struct = [1, 0, 1]
  1212. out = ndimage.binary_dilation(data, struct, border_value=1)
  1213. assert_array_almost_equal(out, [1, 0, 1, 0, 1])
  1214. @pytest.mark.parametrize('dtype', types)
  1215. def test_binary_dilation14(self, dtype):
  1216. data = numpy.zeros([5], dtype)
  1217. data[1] = 1
  1218. struct = [1, 0, 1]
  1219. out = ndimage.binary_dilation(data, struct, origin=-1)
  1220. assert_array_almost_equal(out, [0, 1, 0, 1, 0])
  1221. @pytest.mark.parametrize('dtype', types)
  1222. def test_binary_dilation15(self, dtype):
  1223. data = numpy.zeros([5], dtype)
  1224. data[1] = 1
  1225. struct = [1, 0, 1]
  1226. out = ndimage.binary_dilation(data, struct,
  1227. origin=-1, border_value=1)
  1228. assert_array_almost_equal(out, [1, 1, 0, 1, 0])
  1229. @pytest.mark.parametrize('dtype', types)
  1230. def test_binary_dilation16(self, dtype):
  1231. data = numpy.ones([1, 1], dtype)
  1232. out = ndimage.binary_dilation(data)
  1233. assert_array_almost_equal(out, [[1]])
  1234. @pytest.mark.parametrize('dtype', types)
  1235. def test_binary_dilation17(self, dtype):
  1236. data = numpy.zeros([1, 1], dtype)
  1237. out = ndimage.binary_dilation(data)
  1238. assert_array_almost_equal(out, [[0]])
  1239. @pytest.mark.parametrize('dtype', types)
  1240. def test_binary_dilation18(self, dtype):
  1241. data = numpy.ones([1, 3], dtype)
  1242. out = ndimage.binary_dilation(data)
  1243. assert_array_almost_equal(out, [[1, 1, 1]])
  1244. @pytest.mark.parametrize('dtype', types)
  1245. def test_binary_dilation19(self, dtype):
  1246. data = numpy.ones([3, 3], dtype)
  1247. out = ndimage.binary_dilation(data)
  1248. assert_array_almost_equal(out, [[1, 1, 1],
  1249. [1, 1, 1],
  1250. [1, 1, 1]])
  1251. @pytest.mark.parametrize('dtype', types)
  1252. def test_binary_dilation20(self, dtype):
  1253. data = numpy.zeros([3, 3], dtype)
  1254. data[1, 1] = 1
  1255. out = ndimage.binary_dilation(data)
  1256. assert_array_almost_equal(out, [[0, 1, 0],
  1257. [1, 1, 1],
  1258. [0, 1, 0]])
  1259. @pytest.mark.parametrize('dtype', types)
  1260. def test_binary_dilation21(self, dtype):
  1261. struct = ndimage.generate_binary_structure(2, 2)
  1262. data = numpy.zeros([3, 3], dtype)
  1263. data[1, 1] = 1
  1264. out = ndimage.binary_dilation(data, struct)
  1265. assert_array_almost_equal(out, [[1, 1, 1],
  1266. [1, 1, 1],
  1267. [1, 1, 1]])
  1268. @pytest.mark.parametrize('dtype', types)
  1269. def test_binary_dilation22(self, dtype):
  1270. expected = [[0, 1, 0, 0, 0, 0, 0, 0],
  1271. [1, 1, 1, 0, 0, 0, 0, 0],
  1272. [0, 1, 0, 0, 0, 1, 0, 0],
  1273. [0, 0, 0, 1, 1, 1, 1, 0],
  1274. [0, 0, 1, 1, 1, 1, 0, 0],
  1275. [0, 1, 1, 1, 1, 1, 1, 0],
  1276. [0, 0, 1, 0, 0, 1, 0, 0],
  1277. [0, 0, 0, 0, 0, 0, 0, 0]]
  1278. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  1279. [0, 1, 0, 0, 0, 0, 0, 0],
  1280. [0, 0, 0, 0, 0, 0, 0, 0],
  1281. [0, 0, 0, 0, 0, 1, 0, 0],
  1282. [0, 0, 0, 1, 1, 0, 0, 0],
  1283. [0, 0, 1, 0, 0, 1, 0, 0],
  1284. [0, 0, 0, 0, 0, 0, 0, 0],
  1285. [0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  1286. out = ndimage.binary_dilation(data)
  1287. assert_array_almost_equal(out, expected)
  1288. @pytest.mark.parametrize('dtype', types)
  1289. def test_binary_dilation23(self, dtype):
  1290. expected = [[1, 1, 1, 1, 1, 1, 1, 1],
  1291. [1, 1, 1, 0, 0, 0, 0, 1],
  1292. [1, 1, 0, 0, 0, 1, 0, 1],
  1293. [1, 0, 0, 1, 1, 1, 1, 1],
  1294. [1, 0, 1, 1, 1, 1, 0, 1],
  1295. [1, 1, 1, 1, 1, 1, 1, 1],
  1296. [1, 0, 1, 0, 0, 1, 0, 1],
  1297. [1, 1, 1, 1, 1, 1, 1, 1]]
  1298. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  1299. [0, 1, 0, 0, 0, 0, 0, 0],
  1300. [0, 0, 0, 0, 0, 0, 0, 0],
  1301. [0, 0, 0, 0, 0, 1, 0, 0],
  1302. [0, 0, 0, 1, 1, 0, 0, 0],
  1303. [0, 0, 1, 0, 0, 1, 0, 0],
  1304. [0, 0, 0, 0, 0, 0, 0, 0],
  1305. [0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  1306. out = ndimage.binary_dilation(data, border_value=1)
  1307. assert_array_almost_equal(out, expected)
  1308. @pytest.mark.parametrize('dtype', types)
  1309. def test_binary_dilation24(self, dtype):
  1310. expected = [[1, 1, 0, 0, 0, 0, 0, 0],
  1311. [1, 0, 0, 0, 1, 0, 0, 0],
  1312. [0, 0, 1, 1, 1, 1, 0, 0],
  1313. [0, 1, 1, 1, 1, 0, 0, 0],
  1314. [1, 1, 1, 1, 1, 1, 0, 0],
  1315. [0, 1, 0, 0, 1, 0, 0, 0],
  1316. [0, 0, 0, 0, 0, 0, 0, 0],
  1317. [0, 0, 0, 0, 0, 0, 0, 0]]
  1318. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  1319. [0, 1, 0, 0, 0, 0, 0, 0],
  1320. [0, 0, 0, 0, 0, 0, 0, 0],
  1321. [0, 0, 0, 0, 0, 1, 0, 0],
  1322. [0, 0, 0, 1, 1, 0, 0, 0],
  1323. [0, 0, 1, 0, 0, 1, 0, 0],
  1324. [0, 0, 0, 0, 0, 0, 0, 0],
  1325. [0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  1326. out = ndimage.binary_dilation(data, origin=(1, 1))
  1327. assert_array_almost_equal(out, expected)
  1328. @pytest.mark.parametrize('dtype', types)
  1329. def test_binary_dilation25(self, dtype):
  1330. expected = [[1, 1, 0, 0, 0, 0, 1, 1],
  1331. [1, 0, 0, 0, 1, 0, 1, 1],
  1332. [0, 0, 1, 1, 1, 1, 1, 1],
  1333. [0, 1, 1, 1, 1, 0, 1, 1],
  1334. [1, 1, 1, 1, 1, 1, 1, 1],
  1335. [0, 1, 0, 0, 1, 0, 1, 1],
  1336. [1, 1, 1, 1, 1, 1, 1, 1],
  1337. [1, 1, 1, 1, 1, 1, 1, 1]]
  1338. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  1339. [0, 1, 0, 0, 0, 0, 0, 0],
  1340. [0, 0, 0, 0, 0, 0, 0, 0],
  1341. [0, 0, 0, 0, 0, 1, 0, 0],
  1342. [0, 0, 0, 1, 1, 0, 0, 0],
  1343. [0, 0, 1, 0, 0, 1, 0, 0],
  1344. [0, 0, 0, 0, 0, 0, 0, 0],
  1345. [0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  1346. out = ndimage.binary_dilation(data, origin=(1, 1), border_value=1)
  1347. assert_array_almost_equal(out, expected)
  1348. @pytest.mark.parametrize('dtype', types)
  1349. def test_binary_dilation26(self, dtype):
  1350. struct = ndimage.generate_binary_structure(2, 2)
  1351. expected = [[1, 1, 1, 0, 0, 0, 0, 0],
  1352. [1, 1, 1, 0, 0, 0, 0, 0],
  1353. [1, 1, 1, 0, 1, 1, 1, 0],
  1354. [0, 0, 1, 1, 1, 1, 1, 0],
  1355. [0, 1, 1, 1, 1, 1, 1, 0],
  1356. [0, 1, 1, 1, 1, 1, 1, 0],
  1357. [0, 1, 1, 1, 1, 1, 1, 0],
  1358. [0, 0, 0, 0, 0, 0, 0, 0]]
  1359. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  1360. [0, 1, 0, 0, 0, 0, 0, 0],
  1361. [0, 0, 0, 0, 0, 0, 0, 0],
  1362. [0, 0, 0, 0, 0, 1, 0, 0],
  1363. [0, 0, 0, 1, 1, 0, 0, 0],
  1364. [0, 0, 1, 0, 0, 1, 0, 0],
  1365. [0, 0, 0, 0, 0, 0, 0, 0],
  1366. [0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  1367. out = ndimage.binary_dilation(data, struct)
  1368. assert_array_almost_equal(out, expected)
  1369. @pytest.mark.parametrize('dtype', types)
  1370. def test_binary_dilation27(self, dtype):
  1371. struct = [[0, 1],
  1372. [1, 1]]
  1373. expected = [[0, 1, 0, 0, 0, 0, 0, 0],
  1374. [1, 1, 0, 0, 0, 0, 0, 0],
  1375. [0, 0, 0, 0, 0, 1, 0, 0],
  1376. [0, 0, 0, 1, 1, 1, 0, 0],
  1377. [0, 0, 1, 1, 1, 1, 0, 0],
  1378. [0, 1, 1, 0, 1, 1, 0, 0],
  1379. [0, 0, 0, 0, 0, 0, 0, 0],
  1380. [0, 0, 0, 0, 0, 0, 0, 0]]
  1381. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  1382. [0, 1, 0, 0, 0, 0, 0, 0],
  1383. [0, 0, 0, 0, 0, 0, 0, 0],
  1384. [0, 0, 0, 0, 0, 1, 0, 0],
  1385. [0, 0, 0, 1, 1, 0, 0, 0],
  1386. [0, 0, 1, 0, 0, 1, 0, 0],
  1387. [0, 0, 0, 0, 0, 0, 0, 0],
  1388. [0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  1389. out = ndimage.binary_dilation(data, struct)
  1390. assert_array_almost_equal(out, expected)
  1391. @pytest.mark.parametrize('dtype', types)
  1392. def test_binary_dilation28(self, dtype):
  1393. expected = [[1, 1, 1, 1],
  1394. [1, 0, 0, 1],
  1395. [1, 0, 0, 1],
  1396. [1, 1, 1, 1]]
  1397. data = numpy.array([[0, 0, 0, 0],
  1398. [0, 0, 0, 0],
  1399. [0, 0, 0, 0],
  1400. [0, 0, 0, 0]], dtype)
  1401. out = ndimage.binary_dilation(data, border_value=1)
  1402. assert_array_almost_equal(out, expected)
  1403. def test_binary_dilation29(self):
  1404. struct = [[0, 1],
  1405. [1, 1]]
  1406. expected = [[0, 0, 0, 0, 0],
  1407. [0, 0, 0, 1, 0],
  1408. [0, 0, 1, 1, 0],
  1409. [0, 1, 1, 1, 0],
  1410. [0, 0, 0, 0, 0]]
  1411. data = numpy.array([[0, 0, 0, 0, 0],
  1412. [0, 0, 0, 0, 0],
  1413. [0, 0, 0, 0, 0],
  1414. [0, 0, 0, 1, 0],
  1415. [0, 0, 0, 0, 0]], bool)
  1416. out = ndimage.binary_dilation(data, struct, iterations=2)
  1417. assert_array_almost_equal(out, expected)
  1418. def test_binary_dilation30(self):
  1419. struct = [[0, 1],
  1420. [1, 1]]
  1421. expected = [[0, 0, 0, 0, 0],
  1422. [0, 0, 0, 1, 0],
  1423. [0, 0, 1, 1, 0],
  1424. [0, 1, 1, 1, 0],
  1425. [0, 0, 0, 0, 0]]
  1426. data = numpy.array([[0, 0, 0, 0, 0],
  1427. [0, 0, 0, 0, 0],
  1428. [0, 0, 0, 0, 0],
  1429. [0, 0, 0, 1, 0],
  1430. [0, 0, 0, 0, 0]], bool)
  1431. out = numpy.zeros(data.shape, bool)
  1432. ndimage.binary_dilation(data, struct, iterations=2, output=out)
  1433. assert_array_almost_equal(out, expected)
  1434. def test_binary_dilation31(self):
  1435. struct = [[0, 1],
  1436. [1, 1]]
  1437. expected = [[0, 0, 0, 1, 0],
  1438. [0, 0, 1, 1, 0],
  1439. [0, 1, 1, 1, 0],
  1440. [1, 1, 1, 1, 0],
  1441. [0, 0, 0, 0, 0]]
  1442. data = numpy.array([[0, 0, 0, 0, 0],
  1443. [0, 0, 0, 0, 0],
  1444. [0, 0, 0, 0, 0],
  1445. [0, 0, 0, 1, 0],
  1446. [0, 0, 0, 0, 0]], bool)
  1447. out = ndimage.binary_dilation(data, struct, iterations=3)
  1448. assert_array_almost_equal(out, expected)
  1449. def test_binary_dilation32(self):
  1450. struct = [[0, 1],
  1451. [1, 1]]
  1452. expected = [[0, 0, 0, 1, 0],
  1453. [0, 0, 1, 1, 0],
  1454. [0, 1, 1, 1, 0],
  1455. [1, 1, 1, 1, 0],
  1456. [0, 0, 0, 0, 0]]
  1457. data = numpy.array([[0, 0, 0, 0, 0],
  1458. [0, 0, 0, 0, 0],
  1459. [0, 0, 0, 0, 0],
  1460. [0, 0, 0, 1, 0],
  1461. [0, 0, 0, 0, 0]], bool)
  1462. out = numpy.zeros(data.shape, bool)
  1463. ndimage.binary_dilation(data, struct, iterations=3, output=out)
  1464. assert_array_almost_equal(out, expected)
  1465. def test_binary_dilation33(self):
  1466. struct = [[0, 1, 0],
  1467. [1, 1, 1],
  1468. [0, 1, 0]]
  1469. expected = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
  1470. [0, 0, 0, 0, 0, 0, 0, 0],
  1471. [0, 0, 0, 0, 0, 0, 0, 0],
  1472. [0, 0, 0, 0, 1, 1, 0, 0],
  1473. [0, 0, 1, 1, 1, 0, 0, 0],
  1474. [0, 1, 1, 0, 1, 1, 0, 0],
  1475. [0, 0, 0, 0, 0, 0, 0, 0],
  1476. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  1477. mask = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
  1478. [0, 0, 0, 0, 0, 0, 0, 0],
  1479. [0, 0, 0, 0, 0, 0, 1, 0],
  1480. [0, 0, 0, 0, 1, 1, 0, 0],
  1481. [0, 0, 1, 1, 1, 0, 0, 0],
  1482. [0, 1, 1, 0, 1, 1, 0, 0],
  1483. [0, 0, 0, 0, 0, 0, 0, 0],
  1484. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  1485. data = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
  1486. [0, 0, 0, 0, 0, 0, 0, 0],
  1487. [0, 0, 0, 0, 0, 0, 0, 0],
  1488. [0, 0, 0, 0, 0, 0, 0, 0],
  1489. [0, 0, 0, 0, 0, 0, 0, 0],
  1490. [0, 1, 0, 0, 0, 0, 0, 0],
  1491. [0, 0, 0, 0, 0, 0, 0, 0],
  1492. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  1493. out = ndimage.binary_dilation(data, struct, iterations=-1,
  1494. mask=mask, border_value=0)
  1495. assert_array_almost_equal(out, expected)
  1496. def test_binary_dilation34(self):
  1497. struct = [[0, 1, 0],
  1498. [1, 1, 1],
  1499. [0, 1, 0]]
  1500. expected = [[0, 1, 0, 0, 0, 0, 0, 0],
  1501. [0, 1, 1, 0, 0, 0, 0, 0],
  1502. [0, 0, 1, 0, 0, 0, 0, 0],
  1503. [0, 0, 0, 0, 0, 0, 0, 0],
  1504. [0, 0, 0, 0, 0, 0, 0, 0],
  1505. [0, 0, 0, 0, 0, 0, 0, 0],
  1506. [0, 0, 0, 0, 0, 0, 0, 0],
  1507. [0, 0, 0, 0, 0, 0, 0, 0]]
  1508. mask = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
  1509. [0, 1, 1, 0, 0, 0, 0, 0],
  1510. [0, 0, 1, 0, 0, 0, 0, 0],
  1511. [0, 0, 0, 0, 0, 1, 0, 0],
  1512. [0, 0, 0, 1, 1, 0, 0, 0],
  1513. [0, 0, 1, 0, 0, 1, 0, 0],
  1514. [0, 0, 0, 0, 0, 0, 0, 0],
  1515. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  1516. data = numpy.zeros(mask.shape, bool)
  1517. out = ndimage.binary_dilation(data, struct, iterations=-1,
  1518. mask=mask, border_value=1)
  1519. assert_array_almost_equal(out, expected)
  1520. @pytest.mark.parametrize('dtype', types)
  1521. def test_binary_dilation35(self, dtype):
  1522. tmp = [[1, 1, 0, 0, 0, 0, 1, 1],
  1523. [1, 0, 0, 0, 1, 0, 1, 1],
  1524. [0, 0, 1, 1, 1, 1, 1, 1],
  1525. [0, 1, 1, 1, 1, 0, 1, 1],
  1526. [1, 1, 1, 1, 1, 1, 1, 1],
  1527. [0, 1, 0, 0, 1, 0, 1, 1],
  1528. [1, 1, 1, 1, 1, 1, 1, 1],
  1529. [1, 1, 1, 1, 1, 1, 1, 1]]
  1530. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  1531. [0, 1, 0, 0, 0, 0, 0, 0],
  1532. [0, 0, 0, 0, 0, 0, 0, 0],
  1533. [0, 0, 0, 0, 0, 1, 0, 0],
  1534. [0, 0, 0, 1, 1, 0, 0, 0],
  1535. [0, 0, 1, 0, 0, 1, 0, 0],
  1536. [0, 0, 0, 0, 0, 0, 0, 0],
  1537. [0, 0, 0, 0, 0, 0, 0, 0]])
  1538. mask = [[0, 0, 0, 0, 0, 0, 0, 0],
  1539. [0, 0, 0, 0, 0, 0, 0, 0],
  1540. [0, 0, 0, 0, 0, 0, 0, 0],
  1541. [0, 0, 1, 1, 1, 1, 0, 0],
  1542. [0, 0, 1, 1, 1, 1, 0, 0],
  1543. [0, 0, 1, 1, 1, 1, 0, 0],
  1544. [0, 0, 0, 0, 0, 0, 0, 0],
  1545. [0, 0, 0, 0, 0, 0, 0, 0]]
  1546. expected = numpy.logical_and(tmp, mask)
  1547. tmp = numpy.logical_and(data, numpy.logical_not(mask))
  1548. expected = numpy.logical_or(expected, tmp)
  1549. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  1550. [0, 1, 0, 0, 0, 0, 0, 0],
  1551. [0, 0, 0, 0, 0, 0, 0, 0],
  1552. [0, 0, 0, 0, 0, 1, 0, 0],
  1553. [0, 0, 0, 1, 1, 0, 0, 0],
  1554. [0, 0, 1, 0, 0, 1, 0, 0],
  1555. [0, 0, 0, 0, 0, 0, 0, 0],
  1556. [0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  1557. out = ndimage.binary_dilation(data, mask=mask,
  1558. origin=(1, 1), border_value=1)
  1559. assert_array_almost_equal(out, expected)
  1560. def test_binary_propagation01(self):
  1561. struct = [[0, 1, 0],
  1562. [1, 1, 1],
  1563. [0, 1, 0]]
  1564. expected = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
  1565. [0, 0, 0, 0, 0, 0, 0, 0],
  1566. [0, 0, 0, 0, 0, 0, 0, 0],
  1567. [0, 0, 0, 0, 1, 1, 0, 0],
  1568. [0, 0, 1, 1, 1, 0, 0, 0],
  1569. [0, 1, 1, 0, 1, 1, 0, 0],
  1570. [0, 0, 0, 0, 0, 0, 0, 0],
  1571. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  1572. mask = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
  1573. [0, 0, 0, 0, 0, 0, 0, 0],
  1574. [0, 0, 0, 0, 0, 0, 1, 0],
  1575. [0, 0, 0, 0, 1, 1, 0, 0],
  1576. [0, 0, 1, 1, 1, 0, 0, 0],
  1577. [0, 1, 1, 0, 1, 1, 0, 0],
  1578. [0, 0, 0, 0, 0, 0, 0, 0],
  1579. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  1580. data = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
  1581. [0, 0, 0, 0, 0, 0, 0, 0],
  1582. [0, 0, 0, 0, 0, 0, 0, 0],
  1583. [0, 0, 0, 0, 0, 0, 0, 0],
  1584. [0, 0, 0, 0, 0, 0, 0, 0],
  1585. [0, 1, 0, 0, 0, 0, 0, 0],
  1586. [0, 0, 0, 0, 0, 0, 0, 0],
  1587. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  1588. out = ndimage.binary_propagation(data, struct,
  1589. mask=mask, border_value=0)
  1590. assert_array_almost_equal(out, expected)
  1591. def test_binary_propagation02(self):
  1592. struct = [[0, 1, 0],
  1593. [1, 1, 1],
  1594. [0, 1, 0]]
  1595. expected = [[0, 1, 0, 0, 0, 0, 0, 0],
  1596. [0, 1, 1, 0, 0, 0, 0, 0],
  1597. [0, 0, 1, 0, 0, 0, 0, 0],
  1598. [0, 0, 0, 0, 0, 0, 0, 0],
  1599. [0, 0, 0, 0, 0, 0, 0, 0],
  1600. [0, 0, 0, 0, 0, 0, 0, 0],
  1601. [0, 0, 0, 0, 0, 0, 0, 0],
  1602. [0, 0, 0, 0, 0, 0, 0, 0]]
  1603. mask = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
  1604. [0, 1, 1, 0, 0, 0, 0, 0],
  1605. [0, 0, 1, 0, 0, 0, 0, 0],
  1606. [0, 0, 0, 0, 0, 1, 0, 0],
  1607. [0, 0, 0, 1, 1, 0, 0, 0],
  1608. [0, 0, 1, 0, 0, 1, 0, 0],
  1609. [0, 0, 0, 0, 0, 0, 0, 0],
  1610. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  1611. data = numpy.zeros(mask.shape, bool)
  1612. out = ndimage.binary_propagation(data, struct,
  1613. mask=mask, border_value=1)
  1614. assert_array_almost_equal(out, expected)
  1615. @pytest.mark.parametrize('dtype', types)
  1616. def test_binary_opening01(self, dtype):
  1617. expected = [[0, 1, 0, 0, 0, 0, 0, 0],
  1618. [1, 1, 1, 0, 0, 0, 0, 0],
  1619. [0, 1, 0, 0, 0, 1, 0, 0],
  1620. [0, 0, 0, 0, 1, 1, 1, 0],
  1621. [0, 0, 1, 0, 0, 1, 0, 0],
  1622. [0, 1, 1, 1, 1, 1, 1, 0],
  1623. [0, 0, 1, 0, 0, 1, 0, 0],
  1624. [0, 0, 0, 0, 0, 0, 0, 0]]
  1625. data = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
  1626. [1, 1, 1, 0, 0, 0, 0, 0],
  1627. [0, 1, 0, 0, 0, 1, 0, 0],
  1628. [0, 0, 0, 1, 1, 1, 1, 0],
  1629. [0, 0, 1, 1, 0, 1, 0, 0],
  1630. [0, 1, 1, 1, 1, 1, 1, 0],
  1631. [0, 0, 1, 0, 0, 1, 0, 0],
  1632. [0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  1633. out = ndimage.binary_opening(data)
  1634. assert_array_almost_equal(out, expected)
  1635. @pytest.mark.parametrize('dtype', types)
  1636. def test_binary_opening02(self, dtype):
  1637. struct = ndimage.generate_binary_structure(2, 2)
  1638. expected = [[1, 1, 1, 0, 0, 0, 0, 0],
  1639. [1, 1, 1, 0, 0, 0, 0, 0],
  1640. [1, 1, 1, 0, 0, 0, 0, 0],
  1641. [0, 0, 0, 0, 0, 0, 0, 0],
  1642. [0, 1, 1, 1, 0, 0, 0, 0],
  1643. [0, 1, 1, 1, 0, 0, 0, 0],
  1644. [0, 1, 1, 1, 0, 0, 0, 0],
  1645. [0, 0, 0, 0, 0, 0, 0, 0]]
  1646. data = numpy.array([[1, 1, 1, 0, 0, 0, 0, 0],
  1647. [1, 1, 1, 0, 0, 0, 0, 0],
  1648. [1, 1, 1, 1, 1, 1, 1, 0],
  1649. [0, 0, 1, 1, 1, 1, 1, 0],
  1650. [0, 1, 1, 1, 0, 1, 1, 0],
  1651. [0, 1, 1, 1, 1, 1, 1, 0],
  1652. [0, 1, 1, 1, 1, 1, 1, 0],
  1653. [0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  1654. out = ndimage.binary_opening(data, struct)
  1655. assert_array_almost_equal(out, expected)
  1656. @pytest.mark.parametrize('dtype', types)
  1657. def test_binary_closing01(self, dtype):
  1658. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  1659. [0, 1, 1, 0, 0, 0, 0, 0],
  1660. [0, 1, 1, 1, 0, 1, 0, 0],
  1661. [0, 0, 1, 1, 1, 1, 1, 0],
  1662. [0, 0, 1, 1, 1, 1, 0, 0],
  1663. [0, 1, 1, 1, 1, 1, 1, 0],
  1664. [0, 0, 1, 0, 0, 1, 0, 0],
  1665. [0, 0, 0, 0, 0, 0, 0, 0]]
  1666. data = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
  1667. [1, 1, 1, 0, 0, 0, 0, 0],
  1668. [0, 1, 0, 0, 0, 1, 0, 0],
  1669. [0, 0, 0, 1, 1, 1, 1, 0],
  1670. [0, 0, 1, 1, 0, 1, 0, 0],
  1671. [0, 1, 1, 1, 1, 1, 1, 0],
  1672. [0, 0, 1, 0, 0, 1, 0, 0],
  1673. [0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  1674. out = ndimage.binary_closing(data)
  1675. assert_array_almost_equal(out, expected)
  1676. @pytest.mark.parametrize('dtype', types)
  1677. def test_binary_closing02(self, dtype):
  1678. struct = ndimage.generate_binary_structure(2, 2)
  1679. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  1680. [0, 1, 1, 0, 0, 0, 0, 0],
  1681. [0, 1, 1, 1, 1, 1, 1, 0],
  1682. [0, 1, 1, 1, 1, 1, 1, 0],
  1683. [0, 1, 1, 1, 1, 1, 1, 0],
  1684. [0, 1, 1, 1, 1, 1, 1, 0],
  1685. [0, 1, 1, 1, 1, 1, 1, 0],
  1686. [0, 0, 0, 0, 0, 0, 0, 0]]
  1687. data = numpy.array([[1, 1, 1, 0, 0, 0, 0, 0],
  1688. [1, 1, 1, 0, 0, 0, 0, 0],
  1689. [1, 1, 1, 1, 1, 1, 1, 0],
  1690. [0, 0, 1, 1, 1, 1, 1, 0],
  1691. [0, 1, 1, 1, 0, 1, 1, 0],
  1692. [0, 1, 1, 1, 1, 1, 1, 0],
  1693. [0, 1, 1, 1, 1, 1, 1, 0],
  1694. [0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  1695. out = ndimage.binary_closing(data, struct)
  1696. assert_array_almost_equal(out, expected)
  1697. def test_binary_fill_holes01(self):
  1698. expected = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  1699. [0, 0, 1, 1, 1, 1, 0, 0],
  1700. [0, 0, 1, 1, 1, 1, 0, 0],
  1701. [0, 0, 1, 1, 1, 1, 0, 0],
  1702. [0, 0, 1, 1, 1, 1, 0, 0],
  1703. [0, 0, 1, 1, 1, 1, 0, 0],
  1704. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  1705. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  1706. [0, 0, 1, 1, 1, 1, 0, 0],
  1707. [0, 0, 1, 0, 0, 1, 0, 0],
  1708. [0, 0, 1, 0, 0, 1, 0, 0],
  1709. [0, 0, 1, 0, 0, 1, 0, 0],
  1710. [0, 0, 1, 1, 1, 1, 0, 0],
  1711. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  1712. out = ndimage.binary_fill_holes(data)
  1713. assert_array_almost_equal(out, expected)
  1714. def test_binary_fill_holes02(self):
  1715. expected = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  1716. [0, 0, 0, 1, 1, 0, 0, 0],
  1717. [0, 0, 1, 1, 1, 1, 0, 0],
  1718. [0, 0, 1, 1, 1, 1, 0, 0],
  1719. [0, 0, 1, 1, 1, 1, 0, 0],
  1720. [0, 0, 0, 1, 1, 0, 0, 0],
  1721. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  1722. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  1723. [0, 0, 0, 1, 1, 0, 0, 0],
  1724. [0, 0, 1, 0, 0, 1, 0, 0],
  1725. [0, 0, 1, 0, 0, 1, 0, 0],
  1726. [0, 0, 1, 0, 0, 1, 0, 0],
  1727. [0, 0, 0, 1, 1, 0, 0, 0],
  1728. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  1729. out = ndimage.binary_fill_holes(data)
  1730. assert_array_almost_equal(out, expected)
  1731. def test_binary_fill_holes03(self):
  1732. expected = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  1733. [0, 0, 1, 0, 0, 0, 0, 0],
  1734. [0, 1, 1, 1, 0, 1, 1, 1],
  1735. [0, 1, 1, 1, 0, 1, 1, 1],
  1736. [0, 1, 1, 1, 0, 1, 1, 1],
  1737. [0, 0, 1, 0, 0, 1, 1, 1],
  1738. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  1739. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  1740. [0, 0, 1, 0, 0, 0, 0, 0],
  1741. [0, 1, 0, 1, 0, 1, 1, 1],
  1742. [0, 1, 0, 1, 0, 1, 0, 1],
  1743. [0, 1, 0, 1, 0, 1, 0, 1],
  1744. [0, 0, 1, 0, 0, 1, 1, 1],
  1745. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  1746. out = ndimage.binary_fill_holes(data)
  1747. assert_array_almost_equal(out, expected)
  1748. def test_grey_erosion01(self):
  1749. array = numpy.array([[3, 2, 5, 1, 4],
  1750. [7, 6, 9, 3, 5],
  1751. [5, 8, 3, 7, 1]])
  1752. footprint = [[1, 0, 1], [1, 1, 0]]
  1753. output = ndimage.grey_erosion(array, footprint=footprint)
  1754. assert_array_almost_equal([[2, 2, 1, 1, 1],
  1755. [2, 3, 1, 3, 1],
  1756. [5, 5, 3, 3, 1]], output)
  1757. def test_grey_erosion01_overlap(self):
  1758. array = numpy.array([[3, 2, 5, 1, 4],
  1759. [7, 6, 9, 3, 5],
  1760. [5, 8, 3, 7, 1]])
  1761. footprint = [[1, 0, 1], [1, 1, 0]]
  1762. ndimage.grey_erosion(array, footprint=footprint, output=array)
  1763. assert_array_almost_equal([[2, 2, 1, 1, 1],
  1764. [2, 3, 1, 3, 1],
  1765. [5, 5, 3, 3, 1]], array)
  1766. def test_grey_erosion02(self):
  1767. array = numpy.array([[3, 2, 5, 1, 4],
  1768. [7, 6, 9, 3, 5],
  1769. [5, 8, 3, 7, 1]])
  1770. footprint = [[1, 0, 1], [1, 1, 0]]
  1771. structure = [[0, 0, 0], [0, 0, 0]]
  1772. output = ndimage.grey_erosion(array, footprint=footprint,
  1773. structure=structure)
  1774. assert_array_almost_equal([[2, 2, 1, 1, 1],
  1775. [2, 3, 1, 3, 1],
  1776. [5, 5, 3, 3, 1]], output)
  1777. def test_grey_erosion03(self):
  1778. array = numpy.array([[3, 2, 5, 1, 4],
  1779. [7, 6, 9, 3, 5],
  1780. [5, 8, 3, 7, 1]])
  1781. footprint = [[1, 0, 1], [1, 1, 0]]
  1782. structure = [[1, 1, 1], [1, 1, 1]]
  1783. output = ndimage.grey_erosion(array, footprint=footprint,
  1784. structure=structure)
  1785. assert_array_almost_equal([[1, 1, 0, 0, 0],
  1786. [1, 2, 0, 2, 0],
  1787. [4, 4, 2, 2, 0]], output)
  1788. def test_grey_dilation01(self):
  1789. array = numpy.array([[3, 2, 5, 1, 4],
  1790. [7, 6, 9, 3, 5],
  1791. [5, 8, 3, 7, 1]])
  1792. footprint = [[0, 1, 1], [1, 0, 1]]
  1793. output = ndimage.grey_dilation(array, footprint=footprint)
  1794. assert_array_almost_equal([[7, 7, 9, 9, 5],
  1795. [7, 9, 8, 9, 7],
  1796. [8, 8, 8, 7, 7]], output)
  1797. def test_grey_dilation02(self):
  1798. array = numpy.array([[3, 2, 5, 1, 4],
  1799. [7, 6, 9, 3, 5],
  1800. [5, 8, 3, 7, 1]])
  1801. footprint = [[0, 1, 1], [1, 0, 1]]
  1802. structure = [[0, 0, 0], [0, 0, 0]]
  1803. output = ndimage.grey_dilation(array, footprint=footprint,
  1804. structure=structure)
  1805. assert_array_almost_equal([[7, 7, 9, 9, 5],
  1806. [7, 9, 8, 9, 7],
  1807. [8, 8, 8, 7, 7]], output)
  1808. def test_grey_dilation03(self):
  1809. array = numpy.array([[3, 2, 5, 1, 4],
  1810. [7, 6, 9, 3, 5],
  1811. [5, 8, 3, 7, 1]])
  1812. footprint = [[0, 1, 1], [1, 0, 1]]
  1813. structure = [[1, 1, 1], [1, 1, 1]]
  1814. output = ndimage.grey_dilation(array, footprint=footprint,
  1815. structure=structure)
  1816. assert_array_almost_equal([[8, 8, 10, 10, 6],
  1817. [8, 10, 9, 10, 8],
  1818. [9, 9, 9, 8, 8]], output)
  1819. def test_grey_opening01(self):
  1820. array = numpy.array([[3, 2, 5, 1, 4],
  1821. [7, 6, 9, 3, 5],
  1822. [5, 8, 3, 7, 1]])
  1823. footprint = [[1, 0, 1], [1, 1, 0]]
  1824. tmp = ndimage.grey_erosion(array, footprint=footprint)
  1825. expected = ndimage.grey_dilation(tmp, footprint=footprint)
  1826. output = ndimage.grey_opening(array, footprint=footprint)
  1827. assert_array_almost_equal(expected, output)
  1828. def test_grey_opening02(self):
  1829. array = numpy.array([[3, 2, 5, 1, 4],
  1830. [7, 6, 9, 3, 5],
  1831. [5, 8, 3, 7, 1]])
  1832. footprint = [[1, 0, 1], [1, 1, 0]]
  1833. structure = [[0, 0, 0], [0, 0, 0]]
  1834. tmp = ndimage.grey_erosion(array, footprint=footprint,
  1835. structure=structure)
  1836. expected = ndimage.grey_dilation(tmp, footprint=footprint,
  1837. structure=structure)
  1838. output = ndimage.grey_opening(array, footprint=footprint,
  1839. structure=structure)
  1840. assert_array_almost_equal(expected, output)
  1841. def test_grey_closing01(self):
  1842. array = numpy.array([[3, 2, 5, 1, 4],
  1843. [7, 6, 9, 3, 5],
  1844. [5, 8, 3, 7, 1]])
  1845. footprint = [[1, 0, 1], [1, 1, 0]]
  1846. tmp = ndimage.grey_dilation(array, footprint=footprint)
  1847. expected = ndimage.grey_erosion(tmp, footprint=footprint)
  1848. output = ndimage.grey_closing(array, footprint=footprint)
  1849. assert_array_almost_equal(expected, output)
  1850. def test_grey_closing02(self):
  1851. array = numpy.array([[3, 2, 5, 1, 4],
  1852. [7, 6, 9, 3, 5],
  1853. [5, 8, 3, 7, 1]])
  1854. footprint = [[1, 0, 1], [1, 1, 0]]
  1855. structure = [[0, 0, 0], [0, 0, 0]]
  1856. tmp = ndimage.grey_dilation(array, footprint=footprint,
  1857. structure=structure)
  1858. expected = ndimage.grey_erosion(tmp, footprint=footprint,
  1859. structure=structure)
  1860. output = ndimage.grey_closing(array, footprint=footprint,
  1861. structure=structure)
  1862. assert_array_almost_equal(expected, output)
  1863. def test_morphological_gradient01(self):
  1864. array = numpy.array([[3, 2, 5, 1, 4],
  1865. [7, 6, 9, 3, 5],
  1866. [5, 8, 3, 7, 1]])
  1867. footprint = [[1, 0, 1], [1, 1, 0]]
  1868. structure = [[0, 0, 0], [0, 0, 0]]
  1869. tmp1 = ndimage.grey_dilation(array, footprint=footprint,
  1870. structure=structure)
  1871. tmp2 = ndimage.grey_erosion(array, footprint=footprint,
  1872. structure=structure)
  1873. expected = tmp1 - tmp2
  1874. output = numpy.zeros(array.shape, array.dtype)
  1875. ndimage.morphological_gradient(array, footprint=footprint,
  1876. structure=structure, output=output)
  1877. assert_array_almost_equal(expected, output)
  1878. def test_morphological_gradient02(self):
  1879. array = numpy.array([[3, 2, 5, 1, 4],
  1880. [7, 6, 9, 3, 5],
  1881. [5, 8, 3, 7, 1]])
  1882. footprint = [[1, 0, 1], [1, 1, 0]]
  1883. structure = [[0, 0, 0], [0, 0, 0]]
  1884. tmp1 = ndimage.grey_dilation(array, footprint=footprint,
  1885. structure=structure)
  1886. tmp2 = ndimage.grey_erosion(array, footprint=footprint,
  1887. structure=structure)
  1888. expected = tmp1 - tmp2
  1889. output = ndimage.morphological_gradient(array, footprint=footprint,
  1890. structure=structure)
  1891. assert_array_almost_equal(expected, output)
  1892. def test_morphological_laplace01(self):
  1893. array = numpy.array([[3, 2, 5, 1, 4],
  1894. [7, 6, 9, 3, 5],
  1895. [5, 8, 3, 7, 1]])
  1896. footprint = [[1, 0, 1], [1, 1, 0]]
  1897. structure = [[0, 0, 0], [0, 0, 0]]
  1898. tmp1 = ndimage.grey_dilation(array, footprint=footprint,
  1899. structure=structure)
  1900. tmp2 = ndimage.grey_erosion(array, footprint=footprint,
  1901. structure=structure)
  1902. expected = tmp1 + tmp2 - 2 * array
  1903. output = numpy.zeros(array.shape, array.dtype)
  1904. ndimage.morphological_laplace(array, footprint=footprint,
  1905. structure=structure, output=output)
  1906. assert_array_almost_equal(expected, output)
  1907. def test_morphological_laplace02(self):
  1908. array = numpy.array([[3, 2, 5, 1, 4],
  1909. [7, 6, 9, 3, 5],
  1910. [5, 8, 3, 7, 1]])
  1911. footprint = [[1, 0, 1], [1, 1, 0]]
  1912. structure = [[0, 0, 0], [0, 0, 0]]
  1913. tmp1 = ndimage.grey_dilation(array, footprint=footprint,
  1914. structure=structure)
  1915. tmp2 = ndimage.grey_erosion(array, footprint=footprint,
  1916. structure=structure)
  1917. expected = tmp1 + tmp2 - 2 * array
  1918. output = ndimage.morphological_laplace(array, footprint=footprint,
  1919. structure=structure)
  1920. assert_array_almost_equal(expected, output)
  1921. def test_white_tophat01(self):
  1922. array = numpy.array([[3, 2, 5, 1, 4],
  1923. [7, 6, 9, 3, 5],
  1924. [5, 8, 3, 7, 1]])
  1925. footprint = [[1, 0, 1], [1, 1, 0]]
  1926. structure = [[0, 0, 0], [0, 0, 0]]
  1927. tmp = ndimage.grey_opening(array, footprint=footprint,
  1928. structure=structure)
  1929. expected = array - tmp
  1930. output = numpy.zeros(array.shape, array.dtype)
  1931. ndimage.white_tophat(array, footprint=footprint,
  1932. structure=structure, output=output)
  1933. assert_array_almost_equal(expected, output)
  1934. def test_white_tophat02(self):
  1935. array = numpy.array([[3, 2, 5, 1, 4],
  1936. [7, 6, 9, 3, 5],
  1937. [5, 8, 3, 7, 1]])
  1938. footprint = [[1, 0, 1], [1, 1, 0]]
  1939. structure = [[0, 0, 0], [0, 0, 0]]
  1940. tmp = ndimage.grey_opening(array, footprint=footprint,
  1941. structure=structure)
  1942. expected = array - tmp
  1943. output = ndimage.white_tophat(array, footprint=footprint,
  1944. structure=structure)
  1945. assert_array_almost_equal(expected, output)
  1946. def test_white_tophat03(self):
  1947. array = numpy.array([[1, 0, 0, 0, 0, 0, 0],
  1948. [0, 1, 1, 1, 1, 1, 0],
  1949. [0, 1, 1, 1, 1, 1, 0],
  1950. [0, 1, 1, 1, 1, 1, 0],
  1951. [0, 1, 1, 1, 0, 1, 0],
  1952. [0, 1, 1, 1, 1, 1, 0],
  1953. [0, 0, 0, 0, 0, 0, 1]], dtype=numpy.bool_)
  1954. structure = numpy.ones((3, 3), dtype=numpy.bool_)
  1955. expected = numpy.array([[0, 1, 1, 0, 0, 0, 0],
  1956. [1, 0, 0, 1, 1, 1, 0],
  1957. [1, 0, 0, 1, 1, 1, 0],
  1958. [0, 1, 1, 0, 0, 0, 1],
  1959. [0, 1, 1, 0, 1, 0, 1],
  1960. [0, 1, 1, 0, 0, 0, 1],
  1961. [0, 0, 0, 1, 1, 1, 1]], dtype=numpy.bool_)
  1962. output = ndimage.white_tophat(array, structure=structure)
  1963. assert_array_equal(expected, output)
  1964. def test_white_tophat04(self):
  1965. array = numpy.eye(5, dtype=numpy.bool_)
  1966. structure = numpy.ones((3, 3), dtype=numpy.bool_)
  1967. # Check that type mismatch is properly handled
  1968. output = numpy.empty_like(array, dtype=numpy.float64)
  1969. ndimage.white_tophat(array, structure=structure, output=output)
  1970. def test_black_tophat01(self):
  1971. array = numpy.array([[3, 2, 5, 1, 4],
  1972. [7, 6, 9, 3, 5],
  1973. [5, 8, 3, 7, 1]])
  1974. footprint = [[1, 0, 1], [1, 1, 0]]
  1975. structure = [[0, 0, 0], [0, 0, 0]]
  1976. tmp = ndimage.grey_closing(array, footprint=footprint,
  1977. structure=structure)
  1978. expected = tmp - array
  1979. output = numpy.zeros(array.shape, array.dtype)
  1980. ndimage.black_tophat(array, footprint=footprint,
  1981. structure=structure, output=output)
  1982. assert_array_almost_equal(expected, output)
  1983. def test_black_tophat02(self):
  1984. array = numpy.array([[3, 2, 5, 1, 4],
  1985. [7, 6, 9, 3, 5],
  1986. [5, 8, 3, 7, 1]])
  1987. footprint = [[1, 0, 1], [1, 1, 0]]
  1988. structure = [[0, 0, 0], [0, 0, 0]]
  1989. tmp = ndimage.grey_closing(array, footprint=footprint,
  1990. structure=structure)
  1991. expected = tmp - array
  1992. output = ndimage.black_tophat(array, footprint=footprint,
  1993. structure=structure)
  1994. assert_array_almost_equal(expected, output)
  1995. def test_black_tophat03(self):
  1996. array = numpy.array([[1, 0, 0, 0, 0, 0, 0],
  1997. [0, 1, 1, 1, 1, 1, 0],
  1998. [0, 1, 1, 1, 1, 1, 0],
  1999. [0, 1, 1, 1, 1, 1, 0],
  2000. [0, 1, 1, 1, 0, 1, 0],
  2001. [0, 1, 1, 1, 1, 1, 0],
  2002. [0, 0, 0, 0, 0, 0, 1]], dtype=numpy.bool_)
  2003. structure = numpy.ones((3, 3), dtype=numpy.bool_)
  2004. expected = numpy.array([[0, 1, 1, 1, 1, 1, 1],
  2005. [1, 0, 0, 0, 0, 0, 1],
  2006. [1, 0, 0, 0, 0, 0, 1],
  2007. [1, 0, 0, 0, 0, 0, 1],
  2008. [1, 0, 0, 0, 1, 0, 1],
  2009. [1, 0, 0, 0, 0, 0, 1],
  2010. [1, 1, 1, 1, 1, 1, 0]], dtype=numpy.bool_)
  2011. output = ndimage.black_tophat(array, structure=structure)
  2012. assert_array_equal(expected, output)
  2013. def test_black_tophat04(self):
  2014. array = numpy.eye(5, dtype=numpy.bool_)
  2015. structure = numpy.ones((3, 3), dtype=numpy.bool_)
  2016. # Check that type mismatch is properly handled
  2017. output = numpy.empty_like(array, dtype=numpy.float64)
  2018. ndimage.black_tophat(array, structure=structure, output=output)
  2019. @pytest.mark.parametrize('dtype', types)
  2020. def test_hit_or_miss01(self, dtype):
  2021. struct = [[0, 1, 0],
  2022. [1, 1, 1],
  2023. [0, 1, 0]]
  2024. expected = [[0, 0, 0, 0, 0],
  2025. [0, 1, 0, 0, 0],
  2026. [0, 0, 0, 0, 0],
  2027. [0, 0, 0, 0, 0],
  2028. [0, 0, 0, 0, 0],
  2029. [0, 0, 0, 0, 0],
  2030. [0, 0, 0, 0, 0],
  2031. [0, 0, 0, 0, 0]]
  2032. data = numpy.array([[0, 1, 0, 0, 0],
  2033. [1, 1, 1, 0, 0],
  2034. [0, 1, 0, 1, 1],
  2035. [0, 0, 1, 1, 1],
  2036. [0, 1, 1, 1, 0],
  2037. [0, 1, 1, 1, 1],
  2038. [0, 1, 1, 1, 1],
  2039. [0, 0, 0, 0, 0]], dtype)
  2040. out = numpy.zeros(data.shape, bool)
  2041. ndimage.binary_hit_or_miss(data, struct, output=out)
  2042. assert_array_almost_equal(expected, out)
  2043. @pytest.mark.parametrize('dtype', types)
  2044. def test_hit_or_miss02(self, dtype):
  2045. struct = [[0, 1, 0],
  2046. [1, 1, 1],
  2047. [0, 1, 0]]
  2048. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  2049. [0, 1, 0, 0, 0, 0, 0, 0],
  2050. [0, 0, 0, 0, 0, 0, 0, 0],
  2051. [0, 0, 0, 0, 0, 0, 0, 0]]
  2052. data = numpy.array([[0, 1, 0, 0, 1, 1, 1, 0],
  2053. [1, 1, 1, 0, 0, 1, 0, 0],
  2054. [0, 1, 0, 1, 1, 1, 1, 0],
  2055. [0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  2056. out = ndimage.binary_hit_or_miss(data, struct)
  2057. assert_array_almost_equal(expected, out)
  2058. @pytest.mark.parametrize('dtype', types)
  2059. def test_hit_or_miss03(self, dtype):
  2060. struct1 = [[0, 0, 0],
  2061. [1, 1, 1],
  2062. [0, 0, 0]]
  2063. struct2 = [[1, 1, 1],
  2064. [0, 0, 0],
  2065. [1, 1, 1]]
  2066. expected = [[0, 0, 0, 0, 0, 1, 0, 0],
  2067. [0, 0, 0, 0, 0, 0, 0, 0],
  2068. [0, 0, 0, 0, 0, 0, 0, 0],
  2069. [0, 0, 0, 0, 0, 0, 0, 0],
  2070. [0, 0, 0, 0, 0, 0, 0, 0],
  2071. [0, 0, 0, 0, 0, 0, 0, 0],
  2072. [0, 0, 1, 0, 0, 0, 0, 0],
  2073. [0, 0, 0, 0, 0, 0, 0, 0]]
  2074. data = numpy.array([[0, 1, 0, 0, 1, 1, 1, 0],
  2075. [1, 1, 1, 0, 0, 0, 0, 0],
  2076. [0, 1, 0, 1, 1, 1, 1, 0],
  2077. [0, 0, 1, 1, 1, 1, 1, 0],
  2078. [0, 1, 1, 1, 0, 1, 1, 0],
  2079. [0, 0, 0, 0, 1, 1, 1, 0],
  2080. [0, 1, 1, 1, 1, 1, 1, 0],
  2081. [0, 0, 0, 0, 0, 0, 0, 0]], dtype)
  2082. out = ndimage.binary_hit_or_miss(data, struct1, struct2)
  2083. assert_array_almost_equal(expected, out)
  2084. class TestDilateFix:
  2085. def setup_method(self):
  2086. # dilation related setup
  2087. self.array = numpy.array([[0, 0, 0, 0, 0],
  2088. [0, 0, 0, 0, 0],
  2089. [0, 0, 0, 1, 0],
  2090. [0, 0, 1, 1, 0],
  2091. [0, 0, 0, 0, 0]], dtype=numpy.uint8)
  2092. self.sq3x3 = numpy.ones((3, 3))
  2093. dilated3x3 = ndimage.binary_dilation(self.array, structure=self.sq3x3)
  2094. self.dilated3x3 = dilated3x3.view(numpy.uint8)
  2095. def test_dilation_square_structure(self):
  2096. result = ndimage.grey_dilation(self.array, structure=self.sq3x3)
  2097. # +1 accounts for difference between grey and binary dilation
  2098. assert_array_almost_equal(result, self.dilated3x3 + 1)
  2099. def test_dilation_scalar_size(self):
  2100. result = ndimage.grey_dilation(self.array, size=3)
  2101. assert_array_almost_equal(result, self.dilated3x3)
  2102. class TestBinaryOpeningClosing:
  2103. def setup_method(self):
  2104. a = numpy.zeros((5, 5), dtype=bool)
  2105. a[1:4, 1:4] = True
  2106. a[4, 4] = True
  2107. self.array = a
  2108. self.sq3x3 = numpy.ones((3, 3))
  2109. self.opened_old = ndimage.binary_opening(self.array, self.sq3x3,
  2110. 1, None, 0)
  2111. self.closed_old = ndimage.binary_closing(self.array, self.sq3x3,
  2112. 1, None, 0)
  2113. def test_opening_new_arguments(self):
  2114. opened_new = ndimage.binary_opening(self.array, self.sq3x3, 1, None,
  2115. 0, None, 0, False)
  2116. assert_array_equal(opened_new, self.opened_old)
  2117. def test_closing_new_arguments(self):
  2118. closed_new = ndimage.binary_closing(self.array, self.sq3x3, 1, None,
  2119. 0, None, 0, False)
  2120. assert_array_equal(closed_new, self.closed_old)
  2121. def test_binary_erosion_noninteger_iterations():
  2122. # regression test for gh-9905, gh-9909: ValueError for
  2123. # non integer iterations
  2124. data = numpy.ones([1])
  2125. assert_raises(TypeError, ndimage.binary_erosion, data, iterations=0.5)
  2126. assert_raises(TypeError, ndimage.binary_erosion, data, iterations=1.5)
  2127. def test_binary_dilation_noninteger_iterations():
  2128. # regression test for gh-9905, gh-9909: ValueError for
  2129. # non integer iterations
  2130. data = numpy.ones([1])
  2131. assert_raises(TypeError, ndimage.binary_dilation, data, iterations=0.5)
  2132. assert_raises(TypeError, ndimage.binary_dilation, data, iterations=1.5)
  2133. def test_binary_opening_noninteger_iterations():
  2134. # regression test for gh-9905, gh-9909: ValueError for
  2135. # non integer iterations
  2136. data = numpy.ones([1])
  2137. assert_raises(TypeError, ndimage.binary_opening, data, iterations=0.5)
  2138. assert_raises(TypeError, ndimage.binary_opening, data, iterations=1.5)
  2139. def test_binary_closing_noninteger_iterations():
  2140. # regression test for gh-9905, gh-9909: ValueError for
  2141. # non integer iterations
  2142. data = numpy.ones([1])
  2143. assert_raises(TypeError, ndimage.binary_closing, data, iterations=0.5)
  2144. assert_raises(TypeError, ndimage.binary_closing, data, iterations=1.5)
  2145. def test_binary_closing_noninteger_brute_force_passes_when_true():
  2146. # regression test for gh-9905, gh-9909: ValueError for
  2147. # non integer iterations
  2148. data = numpy.ones([1])
  2149. assert ndimage.binary_erosion(
  2150. data, iterations=2, brute_force=1.5
  2151. ) == ndimage.binary_erosion(data, iterations=2, brute_force=bool(1.5))
  2152. assert ndimage.binary_erosion(
  2153. data, iterations=2, brute_force=0.0
  2154. ) == ndimage.binary_erosion(data, iterations=2, brute_force=bool(0.0))
  2155. @pytest.mark.parametrize(
  2156. 'function',
  2157. ['binary_erosion', 'binary_dilation', 'binary_opening', 'binary_closing'],
  2158. )
  2159. @pytest.mark.parametrize('iterations', [1, 5])
  2160. @pytest.mark.parametrize('brute_force', [False, True])
  2161. def test_binary_input_as_output(function, iterations, brute_force):
  2162. rstate = numpy.random.RandomState(123)
  2163. data = rstate.randint(low=0, high=2, size=100).astype(bool)
  2164. ndi_func = getattr(ndimage, function)
  2165. # input data is not modified
  2166. data_orig = data.copy()
  2167. expected = ndi_func(data, brute_force=brute_force, iterations=iterations)
  2168. assert_array_equal(data, data_orig)
  2169. # data should now contain the expected result
  2170. ndi_func(data, brute_force=brute_force, iterations=iterations, output=data)
  2171. assert_array_equal(expected, data)
  2172. def test_binary_hit_or_miss_input_as_output():
  2173. rstate = numpy.random.RandomState(123)
  2174. data = rstate.randint(low=0, high=2, size=100).astype(bool)
  2175. # input data is not modified
  2176. data_orig = data.copy()
  2177. expected = ndimage.binary_hit_or_miss(data)
  2178. assert_array_equal(data, data_orig)
  2179. # data should now contain the expected result
  2180. ndimage.binary_hit_or_miss(data, output=data)
  2181. assert_array_equal(expected, data)