Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +5 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/data/README.txt +9 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/data/_registry.py +184 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/data/multipage.tif +0 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/draw/__init__.py +5 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/draw/__pycache__/__init__.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/draw/__pycache__/draw.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/draw/__pycache__/draw_nd.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/draw/_polygon2mask.py +74 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/draw/_random_shapes.py +459 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/draw/draw.py +973 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/draw/draw3d.py +107 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/draw/draw_nd.py +108 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/draw/tests/__init__.py +0 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/draw/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/draw/tests/__pycache__/test_draw_nd.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/draw/tests/__pycache__/test_random_shapes.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/draw/tests/test_draw.py +1261 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/draw/tests/test_draw3d.py +224 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/draw/tests/test_draw_nd.py +18 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/draw/tests/test_polygon2mask.py +15 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/draw/tests/test_random_shapes.py +178 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/feature/_texture.cpython-310-x86_64-linux-gnu.so +3 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/morphology/__pycache__/_util.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/morphology/__pycache__/gray.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/morphology/_convex_hull.cpython-310-x86_64-linux-gnu.so +3 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/morphology/_grayreconstruct.cpython-310-x86_64-linux-gnu.so +3 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/morphology/ball_decompositions.npy +3 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/morphology/tests/__pycache__/test_skeletonize.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/registration/__init__.py +5 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/registration/__init__.pyi +8 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/registration/__pycache__/__init__.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/registration/__pycache__/_masked_phase_cross_correlation.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/registration/__pycache__/_optical_flow.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/registration/__pycache__/_phase_cross_correlation.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/registration/_masked_phase_cross_correlation.py +306 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/registration/_optical_flow.py +429 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/registration/_optical_flow_utils.py +150 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/registration/_phase_cross_correlation.py +420 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/registration/tests/__init__.py +0 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/registration/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/registration/tests/__pycache__/test_ilk.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/registration/tests/__pycache__/test_masked_phase_cross_correlation.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/registration/tests/__pycache__/test_phase_cross_correlation.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/registration/tests/__pycache__/test_tvl1.cpython-310.pyc +0 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/registration/tests/test_ilk.py +101 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/registration/tests/test_masked_phase_cross_correlation.py +278 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/registration/tests/test_phase_cross_correlation.py +251 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/registration/tests/test_tvl1.py +118 -0
- infer_4_47_1/lib/python3.10/site-packages/skimage/transform/__init__.pyi +86 -0
.gitattributes
CHANGED
|
@@ -884,3 +884,8 @@ infer_4_47_1/lib/python3.10/site-packages/skimage/morphology/_skeletonize_variou
|
|
| 884 |
infer_4_47_1/lib/python3.10/site-packages/skimage/filters/rank/core_cy_3d.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 885 |
infer_4_47_1/lib/python3.10/site-packages/skimage/morphology/_extrema_cy.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 886 |
infer_4_47_1/lib/python3.10/site-packages/skimage/morphology/_skeletonize_lee_cy.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 884 |
infer_4_47_1/lib/python3.10/site-packages/skimage/filters/rank/core_cy_3d.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 885 |
infer_4_47_1/lib/python3.10/site-packages/skimage/morphology/_extrema_cy.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 886 |
infer_4_47_1/lib/python3.10/site-packages/skimage/morphology/_skeletonize_lee_cy.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 887 |
+
infer_4_47_1/lib/python3.10/site-packages/skimage/morphology/_grayreconstruct.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 888 |
+
infer_4_47_1/lib/python3.10/site-packages/skimage/morphology/_convex_hull.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 889 |
+
infer_4_47_1/lib/python3.10/site-packages/skimage/transform/_radon_transform.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 890 |
+
infer_4_47_1/lib/python3.10/site-packages/skimage/feature/_texture.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 891 |
+
infer_4_47_1/lib/python3.10/site-packages/skimage/transform/_hough_transform.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
infer_4_47_1/lib/python3.10/site-packages/skimage/data/README.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
This directory contains sample data from scikit-image.
|
| 2 |
+
|
| 3 |
+
By default, it only contains a small subset of the entire dataset.
|
| 4 |
+
|
| 5 |
+
The full detaset can be downloaded by using the following commands from
|
| 6 |
+
a python console.
|
| 7 |
+
|
| 8 |
+
>>> from skimage.data import download_all
|
| 9 |
+
>>> download_all()
|
infer_4_47_1/lib/python3.10/site-packages/skimage/data/_registry.py
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
+
# This minimal dataset was available as part of
|
| 2 |
+
# scikit-image 0.15 and will be retained until
|
| 3 |
+
# further notice.
|
| 4 |
+
# Testing data and additional datasets should only
|
| 5 |
+
# be made available by pooch
|
| 6 |
+
legacy_datasets = [
|
| 7 |
+
'astronaut.png',
|
| 8 |
+
'brick.png',
|
| 9 |
+
'camera.png',
|
| 10 |
+
'chessboard_GRAY.png',
|
| 11 |
+
'chessboard_RGB.png',
|
| 12 |
+
'chelsea.png',
|
| 13 |
+
'clock_motion.png',
|
| 14 |
+
'coffee.png',
|
| 15 |
+
'coins.png',
|
| 16 |
+
'color.png',
|
| 17 |
+
'cell.png',
|
| 18 |
+
'grass.png',
|
| 19 |
+
'gravel.png',
|
| 20 |
+
'horse.png',
|
| 21 |
+
'hubble_deep_field.jpg',
|
| 22 |
+
'ihc.png',
|
| 23 |
+
'lbpcascade_frontalface_opencv.xml',
|
| 24 |
+
'lfw_subset.npy',
|
| 25 |
+
'logo.png',
|
| 26 |
+
'microaneurysms.png',
|
| 27 |
+
'moon.png',
|
| 28 |
+
'page.png',
|
| 29 |
+
'text.png',
|
| 30 |
+
'retina.jpg',
|
| 31 |
+
'rocket.jpg',
|
| 32 |
+
'phantom.png',
|
| 33 |
+
'motorcycle_disp.npz',
|
| 34 |
+
'motorcycle_left.png',
|
| 35 |
+
'motorcycle_right.png',
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
# Registry of datafiles that can be downloaded along with their SHA256 hashes
|
| 39 |
+
# To generate the SHA256 hash, use the command
|
| 40 |
+
# openssl sha256 filename
|
| 41 |
+
registry = {
|
| 42 |
+
"color/tests/data/lab_array_a_10.npy": "a3ef76f1530e374f9121020f1f220bc89767dc866f4bbd1b1f47e5b84891a38c",
|
| 43 |
+
"color/tests/data/lab_array_a_2.npy": "793d5981cbffceb14b5fb589f998a2b1acdb5ff9c14d364c8e9e8bd45a80b275",
|
| 44 |
+
"color/tests/data/lab_array_a_r.npy": "3d3613da109d0c87827525fc49b58111aefc12438fa6426654979f66807b9227",
|
| 45 |
+
"color/tests/data/lab_array_b_10.npy": "e8d648b28077c1bfcef55ec6dc8679819612b56a01647f8c0a78625bb06f99b6",
|
| 46 |
+
"color/tests/data/lab_array_b_2.npy": "da9c6aa99e4ab3af8ec3107bbf11647cc483a0760285dd5c9fb66988be393ca1",
|
| 47 |
+
"color/tests/data/lab_array_b_r.npy": "d9eee96f4d65a2fbba82039508aac8c18304752ee8e33233e2a013e65bb91464",
|
| 48 |
+
"color/tests/data/lab_array_c_10.npy": "88b4ff2a2d2c4f48e7bb265609221d4b9ef439a4e2d8a86989696bfdb47790e6",
|
| 49 |
+
"color/tests/data/lab_array_c_2.npy": "e1b8acfdc7284ab9cd339de66948134304073b6f734ecf9ad42f8297b83d3405",
|
| 50 |
+
"color/tests/data/lab_array_c_r.npy": "09ffba2ed69e467864fea883493cd2d2706da028433464e3e858a8086842867e",
|
| 51 |
+
"color/tests/data/lab_array_d50_10.npy": "42e2ff26cb10e2a98fcf1bc06c2483302ff4fabf971fe8d49b530f490b5d24c7",
|
| 52 |
+
"color/tests/data/lab_array_d50_2.npy": "4aa03b7018ff7276643d3c082123cf07304f9d8d898ae92a5756a86955de4faf",
|
| 53 |
+
"color/tests/data/lab_array_d50_r.npy": "57db02009f9a68dade33ce1ecffead0418d8ac8113b2a589fc02a20e6bf7e799",
|
| 54 |
+
"color/tests/data/lab_array_d55_10.npy": "ab4f21368b6d8351578ab093381c44b49ae87a6b7f25c11aa094b07f215eed7d",
|
| 55 |
+
"color/tests/data/lab_array_d55_2.npy": "0319723de4632a252bae828b7c96d038fb075a7df05beadfbad653da05efe372",
|
| 56 |
+
"color/tests/data/lab_array_d55_r.npy": "060ebc446f7b4da4df58a60f0006133dbca735da87ba61854f4a75d28db67a3a",
|
| 57 |
+
"color/tests/data/lab_array_d65_10.npy": "5cb9e9c384d2577aaf8b7d2d21ff5b505708b80605a2f59d10e89d22c3d308d2",
|
| 58 |
+
"color/tests/data/lab_array_d65_2.npy": "16e847160f7ba4f19806d8194ed44a6654c9367e5a2cb240aa6e7eece44a6649",
|
| 59 |
+
"color/tests/data/lab_array_d65_r.npy": "82d0dd7a46741f627b8868793e64cdc2f9944fe1e049b573f752a93760a1577c",
|
| 60 |
+
"color/tests/data/lab_array_d75_10.npy": "c2d3de5422c785c925926b0c6223aeaf50b9393619d1c30830190d433606cbe1",
|
| 61 |
+
"color/tests/data/lab_array_d75_2.npy": "c94d53da398d36e076471ff7e0dafcaffc64ce4ba33b4d04849c32d19c87494a",
|
| 62 |
+
"color/tests/data/lab_array_e_2.npy": "ac05f17a83961b020ceccbdd46bddc86943d43e678dabcc898caf4a1e4be6165",
|
| 63 |
+
"color/tests/data/luv_array_a_10.npy": "c8af67f9fd64a6e9c610ac0c12c5315a49ca229363f048e5d851409d4a3ae5b6",
|
| 64 |
+
"color/tests/data/luv_array_a_2.npy": "eaf05dc61f4a70ece367d5e751a14d42b7c397c7b1c2df4cfecec9ddf26e1c1a",
|
| 65 |
+
"color/tests/data/luv_array_a_r.npy": "2c0891add787ec757601f9c61ad14dd9621dd969af4e32753f2e64df437081b7",
|
| 66 |
+
"color/tests/data/luv_array_b_10.npy": "a5407736b8a43071139ca178d12cdf930f32f52a0644f0b13f89d8895c8b43db",
|
| 67 |
+
"color/tests/data/luv_array_b_2.npy": "8e74173d54dc549b6c0ebd1f1d70489d2905cad87744e41ed74384f21f22986d",
|
| 68 |
+
"color/tests/data/luv_array_b_r.npy": "0a74c41df369cbb5fc0a00c16d60dc6f946ebf144bc5e506545b0d160fa53dfa",
|
| 69 |
+
"color/tests/data/luv_array_c_10.npy": "3a5f975ffa57f69a1be9e02b153e8161f83040ce3002ea1b0a05b9fbdd0d8ec4",
|
| 70 |
+
"color/tests/data/luv_array_c_2.npy": "32506cd50ea2181997cb88d3511e275740e8151d6c693cd178f5eafd8b0c6e47",
|
| 71 |
+
"color/tests/data/luv_array_c_r.npy": "c0fbf98cc0e62ed426ab4d228986d6660089444a7bbfcc64cbb1c632644067bb",
|
| 72 |
+
"color/tests/data/luv_array_d50_10.npy": "fe223db556222ce3a59198bed3a3324c2c719b8083fb84dc5b00f214b4773b16",
|
| 73 |
+
"color/tests/data/luv_array_d50_2.npy": "48e8989048904bdf2c3c1ada265c1c29c5eff60f02f848a25cde622982c84901",
|
| 74 |
+
"color/tests/data/luv_array_d50_r.npy": "f93f0def9c93f872dd10ce4a91fdb3f06eea61ddb6e72387b7669909827d4f9c",
|
| 75 |
+
"color/tests/data/luv_array_d55_10.npy": "d88d53d2bad230c2331442187712ec52ffdee62bf0f60b200c33411bfed76c60",
|
| 76 |
+
"color/tests/data/luv_array_d55_2.npy": "c761b40475df591ae9c0475d54ef712d067190ca4652efc6308b69080a652061",
|
| 77 |
+
"color/tests/data/luv_array_d55_r.npy": "05fbd57e3602ee4d5202b9f18f9b5fc05b545891a9b4456d2a88aa798a5a774a",
|
| 78 |
+
"color/tests/data/luv_array_d65_10.npy": "41a5452ffac4d31dd579d9528e725432c60d77b5f505d801898d9401429c89bf",
|
| 79 |
+
"color/tests/data/luv_array_d65_2.npy": "962ce180132c6c11798cbc423b2b204d1d10187670f6eb5dec1058eaad301e0e",
|
| 80 |
+
"color/tests/data/luv_array_d65_r.npy": "78db8c19af26dd802ce98b039a33855f7c8d6a103a2721d094b1d9c619717449",
|
| 81 |
+
"color/tests/data/luv_array_d75_10.npy": "e1cc70d56eb6789633d4c2a4059b9533f616a7c8592c9bd342403e41d72f45e4",
|
| 82 |
+
"color/tests/data/luv_array_d75_2.npy": "07db3bd59bd89de8e5ff62dad786fe5f4b299133495ba9bea30495b375133a98",
|
| 83 |
+
"color/tests/data/luv_array_e_2.npy": "41b1037d81b267305ffe9e8e97e0affa9fa54b18e60413b01b8f11861cb32213",
|
| 84 |
+
"color/tests/ciede2000_test_data.txt": "2e005c6f76ddfb7bbcc8f68490f1f7b4b4a2a4b06b36a80c985677a2799c0e40",
|
| 85 |
+
"data/astronaut.png": "88431cd9653ccd539741b555fb0a46b61558b301d4110412b5bc28b5e3ea6cb5",
|
| 86 |
+
"data/brick.png": "7966caf324f6ba843118d98f7a07746d22f6a343430add0233eca5f6eaaa8fcf",
|
| 87 |
+
"data/cell.png": "8d23a7fb81f7cc877cd09f330357fc7f595651306e84e17252f6e0a1b3f61515",
|
| 88 |
+
"data/camera.png": "b0793d2adda0fa6ae899c03989482bff9a42d3d5690fc7e3648f2795d730c23a",
|
| 89 |
+
"data/chessboard_GRAY.png": "3e51870774515af4d07d820bd8827364c70839bf9b573c746e485095e893df90",
|
| 90 |
+
"data/chessboard_RGB.png": "1ac01eff2d4e50f4eda55a2ddecdc28a6576623a58d7a7ef84513c5cc19a0331",
|
| 91 |
+
"data/chelsea.png": "596aa1e7cb875eb79f437e310381d26b338a81c2da23439704a73c4651e8c4bb",
|
| 92 |
+
"data/clock_motion.png": "f029226b28b642e80113d86622e9b215ee067a0966feaf5e60604a1e05733955",
|
| 93 |
+
"data/coffee.png": "cc02f8ca188b167c775a7101b5d767d1e71792cf762c33d6fa15a4599b5a8de7",
|
| 94 |
+
"data/coins.png": "f8d773fc9cfa6f4d8e5942dc34d0a0788fcaed2a4fefbbed0aef5398d7ef4cba",
|
| 95 |
+
"data/color.png": "7d2df993de2b4fa2a78e04e5df8050f49a9c511aa75e59ab3bd56ac9c98aef7e",
|
| 96 |
+
"data/eagle.png": "928f1bbe7403b533265f56db3a6b07c835dfa8e2513f5c5075ca2f1960f6179e",
|
| 97 |
+
"data/horse.png": "c7fb60789fe394c485f842291ea3b21e50d140f39d6dcb5fb9917cc178225455",
|
| 98 |
+
"data/grass.png": "b6b6022426b38936c43a4ac09635cd78af074e90f42ffa8227ac8b7452d39f89",
|
| 99 |
+
"data/hubble_deep_field.jpg": "3a19c5dd8a927a9334bb1229a6d63711b1c0c767fb27e2286e7c84a3e2c2f5f4",
|
| 100 |
+
"data/ihc.png": "f8dd1aa387ddd1f49d8ad13b50921b237df8e9b262606d258770687b0ef93cef",
|
| 101 |
+
"data/logo.png": "f2c57fe8af089f08b5ba523d95573c26e62904ac5967f4c8851b27d033690168",
|
| 102 |
+
"data/lfw_subset.npy": "9560ec2f5edfac01973f63a8a99d00053fecd11e21877e18038fbe500f8e872c",
|
| 103 |
+
"data/microaneurysms.png": "a1e1be59aa447f8ce082f7fa809997ab369a2b137cb6c4202abc647c7ccf6456",
|
| 104 |
+
"data/moon.png": "78739619d11f7eb9c165bb5d2efd4772cee557812ec847532dbb1d92ef71f577",
|
| 105 |
+
"data/motorcycle_left.png": "db18e9c4157617403c3537a6ba355dfeafe9a7eabb6b9b94cb33f6525dd49179",
|
| 106 |
+
"data/motorcycle_right.png": "5fc913ae870e42a4b662314bc904d1786bcad8e2f0b9b67dba5a229406357797",
|
| 107 |
+
"data/motorcycle_disp.npz": "2e49c8cebff3fa20359a0cc6880c82e1c03bbb106da81a177218281bc2f113d7",
|
| 108 |
+
"data/mssim_matlab_output.npz": "cc11a14bfa040c75b02db32282439f2e2e3e96779196c171498afaa70528ed7a",
|
| 109 |
+
"data/page.png": "341a6f0a61557662b02734a9b6e56ec33a915b2c41886b97509dedf2a43b47a3",
|
| 110 |
+
"data/phantom.png": "552ff698167aa402cceb17981130607a228a0a0aa7c519299eaa4d5f301ba36c",
|
| 111 |
+
"data/retina.jpg": "38a07f36f27f095e818aea7b96d34202c05176d30253c66733f2e00379e9e0e6",
|
| 112 |
+
"data/rocket.jpg": "c2dd0de7c538df8d111e479619b129464d0269d0ae5fd18ca91d33a7fdfea95c",
|
| 113 |
+
"data/gravel.png": "c48615b451bf1e606fbd72c0aa9f8cc0f068ab7111ef7d93bb9b0f2586440c12",
|
| 114 |
+
"data/text.png": "bd84aa3a6e3c9887850d45d606c96b2e59433fbef50338570b63c319e668e6d1",
|
| 115 |
+
"data/chessboard_GRAY_U16.tif": "9fd3392c5b6cbc5f686d8ff83eb57ef91d038ee0852ac26817e5ac99df4c7f45",
|
| 116 |
+
"data/chessboard_GRAY_U16B.tif": "b0a9270751f0fc340c90b8b615b62b88187b9ab5995942717566735d523cddb2",
|
| 117 |
+
"data/chessboard_GRAY_U8.npy": "71f394694b721e8a33760a355b3666c9b7d7fc1188ff96b3cd23c2a1d73a38d8",
|
| 118 |
+
"data/lbpcascade_frontalface_opencv.xml": "03097789a3dcbb0e40d20b9ef82537dbc3b670b6a7f2268d735470f22e003a91",
|
| 119 |
+
"data/astronaut_GRAY_hog_L1.npy": "5d8ab22b166d1dd49c12caeff9d178ed28132efea3852b952e9d75f7f7f94954",
|
| 120 |
+
"data/astronaut_GRAY_hog_L2-Hys.npy": "c4dd6e50d1129aada358311cf8880ce8c775f31e0e550fc322c16e43a96d56fe",
|
| 121 |
+
"data/rank_filter_tests.npz": "efaf5699630f4a53255e91681dc72a965acd4a8aa1f84671c686fb93e7df046d",
|
| 122 |
+
"data/rank_filters_tests_3d.npz": "1741c2b978424e93558a07d345b2a0d9bfbb33c095c123da147fca066714ab16",
|
| 123 |
+
"data/multi.fits": "5c71a83436762a52b1925f2f0d83881af7765ed50aede155af2800e54bbd5040",
|
| 124 |
+
"data/simple.fits": "cd36087fdbb909b6ba506bbff6bcd4c5f4da3a41862608fbac5e8555ef53d40f",
|
| 125 |
+
"data/palette_color.png": "c4e817035fb9f7730fe95cff1da3866dea01728efc72b6e703d78f7ab9717bdd",
|
| 126 |
+
"data/palette_gray.png": "bace7f73783bf3ab3b7fdaf701707e4fa09f0dbd0ea72cf5b12ddc73d50b02a9",
|
| 127 |
+
"data/green_palette.png": "42d49d94be8f9bc76e50639d3701ed0484258721f6b0bd7f50bb1b9274a010f0",
|
| 128 |
+
"data/truncated.jpg": "4c226038acc78012d335efba29c6119a24444a886842182b7e18db378f4a557d",
|
| 129 |
+
"data/multipage.tif": "4da0ad0d3df4807a9847247d1b5e565b50d46481f643afb5c37c14802c78130f",
|
| 130 |
+
"data/multipage_rgb.tif": "1d23b844fd38dce0e2d06f30432817cdb85e52070d8f5460a2ba58aebf34a0de",
|
| 131 |
+
"data/no_time_for_that_tiny.gif": "20abe94ba9e45f18de416c5fbef8d1f57a499600be40f9a200fae246010eefce",
|
| 132 |
+
"data/foo3x5x4indexed.png": "48a64c25c6da000ffdb5fcc34ebafe9ba3b1c9b61d7984ea7ca6dc54f9312dfa",
|
| 133 |
+
"data/gray_morph_output.npz": "49a0dae607cd8d31e134b4bfcbf0d86b13751fdce9667d8bf1ade93d435191b1",
|
| 134 |
+
"data/disk-matlab-output.npz": "8a39d5c866f6216d6a9c9166312aa4bbf4d18fab3d0dcd963c024985bde5856b",
|
| 135 |
+
"data/diamond-matlab-output.npz": "02fca68907e2b252b501dfe977eef71ae39fadaaa3702ebdc855195422ae1cc2",
|
| 136 |
+
"data/bw_text.png": "308c2b09f8975a69b212e103b18520e8cbb7a4eccfce0f757836cd371f1b9094",
|
| 137 |
+
"data/bw_text_skeleton.npy": "9ff4fc23c6a01497d7987f14e3a97cbcc39cce54b2b3b7ee33b84c1b661d0ae1",
|
| 138 |
+
"data/_blobs_3d_fiji_skeleton.tif": "e3449ad9819425959952050c147278555e5ffe1c2c4a30df29f6a1f9023e10c3",
|
| 139 |
+
"data/checker_bilevel.png": "2e207e486545874a2a3e69ba653b28fdef923157be9017559540e65d1bcb8e28",
|
| 140 |
+
"restoration/tests/astronaut_rl.npy": "3f8373e2c6182a89366e51cef6624e3625deac75fdda1079cbdad2a33322152c",
|
| 141 |
+
"restoration/tests/camera_rl.npy": "fd4f59af84dd471fbbe79ee70c1b7e68a69864c461f0db5ac587e7975363f78f",
|
| 142 |
+
"restoration/tests/camera_unsup.npy": "3de10a0b97267352b18886b25d66a967f9e1d78ada61050577d78586cab82baa",
|
| 143 |
+
"restoration/tests/camera_unsup2.npy": "29cdc60605eb528c5f014baa8564d7d1ba0bd4b3170a66522058cbe5aed0960b",
|
| 144 |
+
"restoration/tests/camera_wiener.npy": "4505ea8b0d63d03250c6d756560d615751b76dd6ffc4a95972fa260c0c84633e",
|
| 145 |
+
"registration/tests/data/OriginalX-130Y130.png": "bf24a06d99ae131c97e582ef5e1cd0c648a8dad0caab31281f3564045492811f",
|
| 146 |
+
"registration/tests/data/OriginalX130Y130.png": "7fdd4c06d504fec35ee0703bd7ed2c08830b075a74c8506bae4a70d682f5a2db",
|
| 147 |
+
"registration/tests/data/OriginalX75Y75.png": "c5cd58893c93140df02896df80b13ecf432f5c86eeaaf8fb311aec52a65c7016",
|
| 148 |
+
"registration/tests/data/TransformedX-130Y130.png": "1cda90ed69c921eb7605b73b76d141cf4ea03fb8ce3336445ca08080e40d7375",
|
| 149 |
+
"registration/tests/data/TransformedX130Y130.png": "bb10c6ae3f91a313b0ac543efdb7ca69c4b95e55674c65a88472a6c4f4692a25",
|
| 150 |
+
"registration/tests/data/TransformedX75Y75.png": "a1e9ead5f8e4a0f604271e1f9c50e89baf53f068f1d19fab2876af4938e695ea",
|
| 151 |
+
"data/brain.tiff": "bcdbaf424fbad7b1fb0f855f608c68e5a838f35affc323ff04ea17f678eef5c6",
|
| 152 |
+
"data/cells3d.tif": "afc7c7d80d38bfde09788b4064ac1e64ec14e88454ab785ebdc8dbba5ca3b222",
|
| 153 |
+
"data/palisades_of_vogt.tif": "7f205b626407e194974cead67c4d3909344cf59c42d229a349da0198183e5bd0",
|
| 154 |
+
"data/kidney.tif": "80c0799bc58b08cf6eaa53ecd202305eb42fd7bc73746cb6c5064dbeae7e8476",
|
| 155 |
+
"data/lily.tif": "395c2f0194c25b9824a8cd79266920362a0816bc9e906dd392adce2d8309af03",
|
| 156 |
+
"data/mitosis.tif": "2751ba667c4067c5d30817cff004aa06f6f6287f1cdbb5b8c9c6a500308cb456",
|
| 157 |
+
"data/skin.jpg": "8759fe080509712163453f4b17106582b8513e73b0788d80160abf840e272075",
|
| 158 |
+
"data/pivchallenge-B-B001_1.tif": "e95e09abbcecba723df283ac7d361766328abd943701a2ec2f345d4a2014da2a",
|
| 159 |
+
"data/pivchallenge-B-B001_2.tif": "4ceb5407e4e333476a0f264c14b7a3f6c0e753fcdc99ee1c4b8196e5f823805e",
|
| 160 |
+
"data/protein_transport.tif": "a8e24e8d187f33e92ee28508d5615286c850ca75374af7e74e527d290e8b06ea",
|
| 161 |
+
"data/solidification.tif": "50ef9a52c621b7c0c506ad1fe1b8ee8a158a4d7c8e50ddfce1e273a422dca3f9",
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
registry_urls = {
|
| 165 |
+
"data/brain.tiff": "https://gitlab.com/scikit-image/data/-/raw/2cdc5ce89b334d28f06a58c9f0ca21aa6992a5ba/brain.tiff",
|
| 166 |
+
"data/cells3d.tif": "https://gitlab.com/scikit-image/data/-/raw/2cdc5ce89b334d28f06a58c9f0ca21aa6992a5ba/cells3d.tif",
|
| 167 |
+
"data/palisades_of_vogt.tif": "https://gitlab.com/scikit-image/data/-/raw/b2bc880f3bac23a583724befe8388dae368c52fe/in-vivo-cornea-spots.tif",
|
| 168 |
+
"data/eagle.png": "https://gitlab.com/scikit-image/data/-/raw/1e4f62ac31ba4553d176d4473a5967ad1b076d62/eagle.png",
|
| 169 |
+
"data/kidney.tif": "https://gitlab.com/scikit-image/data/-/raw/2cdc5ce89b334d28f06a58c9f0ca21aa6992a5ba/kidney-tissue-fluorescence.tif",
|
| 170 |
+
"data/lily.tif": "https://gitlab.com/scikit-image/data/-/raw/2cdc5ce89b334d28f06a58c9f0ca21aa6992a5ba/lily-of-the-valley-fluorescence.tif",
|
| 171 |
+
"data/mitosis.tif": "https://gitlab.com/scikit-image/data/-/raw/2cdc5ce89b334d28f06a58c9f0ca21aa6992a5ba/AS_09125_050116030001_D03f00d0.tif",
|
| 172 |
+
"data/rank_filters_tests_3d.npz": "https://gitlab.com/scikit-image/data/-/raw/2cdc5ce89b334d28f06a58c9f0ca21aa6992a5ba/Tests_besides_Equalize_Otsu/add18_entropy/rank_filters_tests_3d.npz",
|
| 173 |
+
"data/skin.jpg": "https://gitlab.com/scikit-image/data/-/raw/2cdc5ce89b334d28f06a58c9f0ca21aa6992a5ba/Normal_Epidermis_and_Dermis_with_Intradermal_Nevus_10x.JPG",
|
| 174 |
+
"data/pivchallenge-B-B001_1.tif": "https://gitlab.com/scikit-image/data/-/raw/2cdc5ce89b334d28f06a58c9f0ca21aa6992a5ba/pivchallenge/B/B001_1.tif",
|
| 175 |
+
"data/pivchallenge-B-B001_2.tif": "https://gitlab.com/scikit-image/data/-/raw/2cdc5ce89b334d28f06a58c9f0ca21aa6992a5ba/pivchallenge/B/B001_2.tif",
|
| 176 |
+
"data/protein_transport.tif": "https://gitlab.com/scikit-image/data/-/raw/2cdc5ce89b334d28f06a58c9f0ca21aa6992a5ba/NPCsingleNucleus.tif",
|
| 177 |
+
"data/solidification.tif": "https://gitlab.com/scikit-image/data/-/raw/2cdc5ce89b334d28f06a58c9f0ca21aa6992a5ba/nickel_solidification.tif",
|
| 178 |
+
"restoration/tests/astronaut_rl.npy": "https://gitlab.com/scikit-image/data/-/raw/2cdc5ce89b334d28f06a58c9f0ca21aa6992a5ba/astronaut_rl.npy",
|
| 179 |
+
"data/gray_morph_output.npz": "https://gitlab.com/scikit-image/data/-/raw/806548e112bcf2b708a9a32275d335cb592480fd/Tests_besides_Equalize_Otsu/gray_morph_output.npz",
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
legacy_registry = {
|
| 183 |
+
('data/' + filename): registry['data/' + filename] for filename in legacy_datasets
|
| 184 |
+
}
|
infer_4_47_1/lib/python3.10/site-packages/skimage/data/multipage.tif
ADDED
|
|
infer_4_47_1/lib/python3.10/site-packages/skimage/draw/__init__.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Drawing primitives, such as lines, circles, text, etc."""
|
| 2 |
+
|
| 3 |
+
import lazy_loader as _lazy
|
| 4 |
+
|
| 5 |
+
__getattr__, __dir__, __all__ = _lazy.attach_stub(__name__, __file__)
|
infer_4_47_1/lib/python3.10/site-packages/skimage/draw/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (355 Bytes). View file
|
|
|
infer_4_47_1/lib/python3.10/site-packages/skimage/draw/__pycache__/draw.cpython-310.pyc
ADDED
|
Binary file (33 kB). View file
|
|
|
infer_4_47_1/lib/python3.10/site-packages/skimage/draw/__pycache__/draw_nd.cpython-310.pyc
ADDED
|
Binary file (3.85 kB). View file
|
|
|
infer_4_47_1/lib/python3.10/site-packages/skimage/draw/_polygon2mask.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
from . import draw
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def polygon2mask(image_shape, polygon):
|
| 7 |
+
"""Create a binary mask from a polygon.
|
| 8 |
+
|
| 9 |
+
Parameters
|
| 10 |
+
----------
|
| 11 |
+
image_shape : tuple of size 2
|
| 12 |
+
The shape of the mask.
|
| 13 |
+
polygon : (N, 2) array_like
|
| 14 |
+
The polygon coordinates of shape (N, 2) where N is
|
| 15 |
+
the number of points. The coordinates are (row, column).
|
| 16 |
+
|
| 17 |
+
Returns
|
| 18 |
+
-------
|
| 19 |
+
mask : 2-D ndarray of type 'bool'
|
| 20 |
+
The binary mask that corresponds to the input polygon.
|
| 21 |
+
|
| 22 |
+
See Also
|
| 23 |
+
--------
|
| 24 |
+
polygon:
|
| 25 |
+
Generate coordinates of pixels inside a polygon.
|
| 26 |
+
|
| 27 |
+
Notes
|
| 28 |
+
-----
|
| 29 |
+
This function does not do any border checking. Parts of the polygon that
|
| 30 |
+
are outside the coordinate space defined by `image_shape` are not drawn.
|
| 31 |
+
|
| 32 |
+
Examples
|
| 33 |
+
--------
|
| 34 |
+
>>> import skimage as ski
|
| 35 |
+
>>> image_shape = (10, 10)
|
| 36 |
+
>>> polygon = np.array([[1, 1], [2, 7], [8, 4]])
|
| 37 |
+
>>> mask = ski.draw.polygon2mask(image_shape, polygon)
|
| 38 |
+
>>> mask.astype(int)
|
| 39 |
+
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 40 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 41 |
+
[0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
|
| 42 |
+
[0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
|
| 43 |
+
[0, 0, 0, 1, 1, 1, 1, 0, 0, 0],
|
| 44 |
+
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
|
| 45 |
+
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
|
| 46 |
+
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
|
| 47 |
+
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
|
| 48 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
|
| 49 |
+
|
| 50 |
+
If vertices / points of the `polygon` are outside the coordinate space
|
| 51 |
+
defined by `image_shape`, only a part (or none at all) of the polygon is
|
| 52 |
+
drawn in the mask.
|
| 53 |
+
|
| 54 |
+
>>> offset = np.array([[2, -4]])
|
| 55 |
+
>>> ski.draw.polygon2mask(image_shape, polygon - offset).astype(int)
|
| 56 |
+
array([[0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
|
| 57 |
+
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
|
| 58 |
+
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
|
| 59 |
+
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
|
| 60 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
|
| 61 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
| 62 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
| 63 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 64 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 65 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
|
| 66 |
+
"""
|
| 67 |
+
polygon = np.asarray(polygon)
|
| 68 |
+
vertex_row_coords, vertex_col_coords = polygon.T
|
| 69 |
+
fill_row_coords, fill_col_coords = draw.polygon(
|
| 70 |
+
vertex_row_coords, vertex_col_coords, image_shape
|
| 71 |
+
)
|
| 72 |
+
mask = np.zeros(image_shape, dtype=bool)
|
| 73 |
+
mask[fill_row_coords, fill_col_coords] = True
|
| 74 |
+
return mask
|
infer_4_47_1/lib/python3.10/site-packages/skimage/draw/_random_shapes.py
ADDED
|
@@ -0,0 +1,459 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
from .draw import polygon as draw_polygon, disk as draw_disk, ellipse as draw_ellipse
|
| 6 |
+
from .._shared.utils import warn
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def _generate_rectangle_mask(point, image, shape, random):
|
| 10 |
+
"""Generate a mask for a filled rectangle shape.
|
| 11 |
+
|
| 12 |
+
The height and width of the rectangle are generated randomly.
|
| 13 |
+
|
| 14 |
+
Parameters
|
| 15 |
+
----------
|
| 16 |
+
point : tuple
|
| 17 |
+
The row and column of the top left corner of the rectangle.
|
| 18 |
+
image : tuple
|
| 19 |
+
The height, width and depth of the image into which the shape
|
| 20 |
+
is placed.
|
| 21 |
+
shape : tuple
|
| 22 |
+
The minimum and maximum size of the shape to fit.
|
| 23 |
+
random : `numpy.random.Generator`
|
| 24 |
+
|
| 25 |
+
The random state to use for random sampling.
|
| 26 |
+
|
| 27 |
+
Raises
|
| 28 |
+
------
|
| 29 |
+
ArithmeticError
|
| 30 |
+
When a shape cannot be fit into the image with the given starting
|
| 31 |
+
coordinates. This usually means the image dimensions are too small or
|
| 32 |
+
shape dimensions too large.
|
| 33 |
+
|
| 34 |
+
Returns
|
| 35 |
+
-------
|
| 36 |
+
label : tuple
|
| 37 |
+
A (category, ((r0, r1), (c0, c1))) tuple specifying the category and
|
| 38 |
+
bounding box coordinates of the shape.
|
| 39 |
+
indices : 2-D array
|
| 40 |
+
A mask of indices that the shape fills.
|
| 41 |
+
|
| 42 |
+
"""
|
| 43 |
+
available_width = min(image[1] - point[1], shape[1]) - shape[0]
|
| 44 |
+
available_height = min(image[0] - point[0], shape[1]) - shape[0]
|
| 45 |
+
|
| 46 |
+
# Pick random widths and heights.
|
| 47 |
+
r = shape[0] + random.integers(max(1, available_height)) - 1
|
| 48 |
+
c = shape[0] + random.integers(max(1, available_width)) - 1
|
| 49 |
+
rectangle = draw_polygon(
|
| 50 |
+
[
|
| 51 |
+
point[0],
|
| 52 |
+
point[0] + r,
|
| 53 |
+
point[0] + r,
|
| 54 |
+
point[0],
|
| 55 |
+
],
|
| 56 |
+
[
|
| 57 |
+
point[1],
|
| 58 |
+
point[1],
|
| 59 |
+
point[1] + c,
|
| 60 |
+
point[1] + c,
|
| 61 |
+
],
|
| 62 |
+
)
|
| 63 |
+
label = ('rectangle', ((point[0], point[0] + r + 1), (point[1], point[1] + c + 1)))
|
| 64 |
+
|
| 65 |
+
return rectangle, label
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def _generate_circle_mask(point, image, shape, random):
|
| 69 |
+
"""Generate a mask for a filled circle shape.
|
| 70 |
+
|
| 71 |
+
The radius of the circle is generated randomly.
|
| 72 |
+
|
| 73 |
+
Parameters
|
| 74 |
+
----------
|
| 75 |
+
point : tuple
|
| 76 |
+
The row and column of the top left corner of the rectangle.
|
| 77 |
+
image : tuple
|
| 78 |
+
The height, width and depth of the image into which the shape is placed.
|
| 79 |
+
shape : tuple
|
| 80 |
+
The minimum and maximum size and color of the shape to fit.
|
| 81 |
+
random : `numpy.random.Generator`
|
| 82 |
+
The random state to use for random sampling.
|
| 83 |
+
|
| 84 |
+
Raises
|
| 85 |
+
------
|
| 86 |
+
ArithmeticError
|
| 87 |
+
When a shape cannot be fit into the image with the given starting
|
| 88 |
+
coordinates. This usually means the image dimensions are too small or
|
| 89 |
+
shape dimensions too large.
|
| 90 |
+
|
| 91 |
+
Returns
|
| 92 |
+
-------
|
| 93 |
+
label : tuple
|
| 94 |
+
A (category, ((r0, r1), (c0, c1))) tuple specifying the category and
|
| 95 |
+
bounding box coordinates of the shape.
|
| 96 |
+
indices : 2-D array
|
| 97 |
+
A mask of indices that the shape fills.
|
| 98 |
+
"""
|
| 99 |
+
if shape[0] == 1 or shape[1] == 1:
|
| 100 |
+
raise ValueError('size must be > 1 for circles')
|
| 101 |
+
min_radius = shape[0] // 2.0
|
| 102 |
+
max_radius = shape[1] // 2.0
|
| 103 |
+
left = point[1]
|
| 104 |
+
right = image[1] - point[1]
|
| 105 |
+
top = point[0]
|
| 106 |
+
bottom = image[0] - point[0]
|
| 107 |
+
available_radius = min(left, right, top, bottom, max_radius) - min_radius
|
| 108 |
+
if available_radius < 0:
|
| 109 |
+
raise ArithmeticError('cannot fit shape to image')
|
| 110 |
+
radius = int(min_radius + random.integers(max(1, available_radius)))
|
| 111 |
+
# TODO: think about how to deprecate this
|
| 112 |
+
# while draw_circle was deprecated in favor of draw_disk
|
| 113 |
+
# switching to a label of 'disk' here
|
| 114 |
+
# would be a breaking change for downstream libraries
|
| 115 |
+
# See discussion on naming convention here
|
| 116 |
+
# https://github.com/scikit-image/scikit-image/pull/4428
|
| 117 |
+
disk = draw_disk((point[0], point[1]), radius)
|
| 118 |
+
# Until a deprecation path is decided, always return `'circle'`
|
| 119 |
+
label = (
|
| 120 |
+
'circle',
|
| 121 |
+
(
|
| 122 |
+
(point[0] - radius + 1, point[0] + radius),
|
| 123 |
+
(point[1] - radius + 1, point[1] + radius),
|
| 124 |
+
),
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
return disk, label
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def _generate_triangle_mask(point, image, shape, random):
|
| 131 |
+
"""Generate a mask for a filled equilateral triangle shape.
|
| 132 |
+
|
| 133 |
+
The length of the sides of the triangle is generated randomly.
|
| 134 |
+
|
| 135 |
+
Parameters
|
| 136 |
+
----------
|
| 137 |
+
point : tuple
|
| 138 |
+
The row and column of the top left corner of a up-pointing triangle.
|
| 139 |
+
image : tuple
|
| 140 |
+
The height, width and depth of the image into which the shape
|
| 141 |
+
is placed.
|
| 142 |
+
shape : tuple
|
| 143 |
+
The minimum and maximum size and color of the shape to fit.
|
| 144 |
+
random : `numpy.random.Generator`
|
| 145 |
+
The random state to use for random sampling.
|
| 146 |
+
|
| 147 |
+
Raises
|
| 148 |
+
------
|
| 149 |
+
ArithmeticError
|
| 150 |
+
When a shape cannot be fit into the image with the given starting
|
| 151 |
+
coordinates. This usually means the image dimensions are too small or
|
| 152 |
+
shape dimensions too large.
|
| 153 |
+
|
| 154 |
+
Returns
|
| 155 |
+
-------
|
| 156 |
+
label : tuple
|
| 157 |
+
A (category, ((r0, r1), (c0, c1))) tuple specifying the category and
|
| 158 |
+
bounding box coordinates of the shape.
|
| 159 |
+
indices : 2-D array
|
| 160 |
+
A mask of indices that the shape fills.
|
| 161 |
+
|
| 162 |
+
"""
|
| 163 |
+
if shape[0] == 1 or shape[1] == 1:
|
| 164 |
+
raise ValueError('dimension must be > 1 for triangles')
|
| 165 |
+
available_side = min(image[1] - point[1], point[0], shape[1]) - shape[0]
|
| 166 |
+
side = shape[0] + random.integers(max(1, available_side)) - 1
|
| 167 |
+
triangle_height = int(np.ceil(np.sqrt(3 / 4.0) * side))
|
| 168 |
+
triangle = draw_polygon(
|
| 169 |
+
[
|
| 170 |
+
point[0],
|
| 171 |
+
point[0] - triangle_height,
|
| 172 |
+
point[0],
|
| 173 |
+
],
|
| 174 |
+
[
|
| 175 |
+
point[1],
|
| 176 |
+
point[1] + side // 2,
|
| 177 |
+
point[1] + side,
|
| 178 |
+
],
|
| 179 |
+
)
|
| 180 |
+
label = (
|
| 181 |
+
'triangle',
|
| 182 |
+
((point[0] - triangle_height, point[0] + 1), (point[1], point[1] + side + 1)),
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
return triangle, label
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
def _generate_ellipse_mask(point, image, shape, random):
|
| 189 |
+
"""Generate a mask for a filled ellipse shape.
|
| 190 |
+
|
| 191 |
+
The rotation, major and minor semi-axes of the ellipse are generated
|
| 192 |
+
randomly.
|
| 193 |
+
|
| 194 |
+
Parameters
|
| 195 |
+
----------
|
| 196 |
+
point : tuple
|
| 197 |
+
The row and column of the top left corner of the rectangle.
|
| 198 |
+
image : tuple
|
| 199 |
+
The height, width and depth of the image into which the shape is
|
| 200 |
+
placed.
|
| 201 |
+
shape : tuple
|
| 202 |
+
The minimum and maximum size and color of the shape to fit.
|
| 203 |
+
random : `numpy.random.Generator`
|
| 204 |
+
The random state to use for random sampling.
|
| 205 |
+
|
| 206 |
+
Raises
|
| 207 |
+
------
|
| 208 |
+
ArithmeticError
|
| 209 |
+
When a shape cannot be fit into the image with the given starting
|
| 210 |
+
coordinates. This usually means the image dimensions are too small or
|
| 211 |
+
shape dimensions too large.
|
| 212 |
+
|
| 213 |
+
Returns
|
| 214 |
+
-------
|
| 215 |
+
label : tuple
|
| 216 |
+
A (category, ((r0, r1), (c0, c1))) tuple specifying the category and
|
| 217 |
+
bounding box coordinates of the shape.
|
| 218 |
+
indices : 2-D array
|
| 219 |
+
A mask of indices that the shape fills.
|
| 220 |
+
"""
|
| 221 |
+
if shape[0] == 1 or shape[1] == 1:
|
| 222 |
+
raise ValueError('size must be > 1 for ellipses')
|
| 223 |
+
min_radius = shape[0] / 2.0
|
| 224 |
+
max_radius = shape[1] / 2.0
|
| 225 |
+
left = point[1]
|
| 226 |
+
right = image[1] - point[1]
|
| 227 |
+
top = point[0]
|
| 228 |
+
bottom = image[0] - point[0]
|
| 229 |
+
available_radius = min(left, right, top, bottom, max_radius)
|
| 230 |
+
if available_radius < min_radius:
|
| 231 |
+
raise ArithmeticError('cannot fit shape to image')
|
| 232 |
+
# NOTE: very conservative because we could take into account the fact that
|
| 233 |
+
# we have 2 different radii, but this is a good first approximation.
|
| 234 |
+
# Also, we can afford to have a uniform sampling because the ellipse will
|
| 235 |
+
# be rotated.
|
| 236 |
+
r_radius = random.uniform(min_radius, available_radius + 1)
|
| 237 |
+
c_radius = random.uniform(min_radius, available_radius + 1)
|
| 238 |
+
rotation = random.uniform(-np.pi, np.pi)
|
| 239 |
+
ellipse = draw_ellipse(
|
| 240 |
+
point[0],
|
| 241 |
+
point[1],
|
| 242 |
+
r_radius,
|
| 243 |
+
c_radius,
|
| 244 |
+
shape=image[:2],
|
| 245 |
+
rotation=rotation,
|
| 246 |
+
)
|
| 247 |
+
max_radius = math.ceil(max(r_radius, c_radius))
|
| 248 |
+
min_x = np.min(ellipse[0])
|
| 249 |
+
max_x = np.max(ellipse[0]) + 1
|
| 250 |
+
min_y = np.min(ellipse[1])
|
| 251 |
+
max_y = np.max(ellipse[1]) + 1
|
| 252 |
+
label = ('ellipse', ((min_x, max_x), (min_y, max_y)))
|
| 253 |
+
|
| 254 |
+
return ellipse, label
|
| 255 |
+
|
| 256 |
+
|
| 257 |
+
# Allows lookup by key as well as random selection.
|
| 258 |
+
SHAPE_GENERATORS = dict(
|
| 259 |
+
rectangle=_generate_rectangle_mask,
|
| 260 |
+
circle=_generate_circle_mask,
|
| 261 |
+
triangle=_generate_triangle_mask,
|
| 262 |
+
ellipse=_generate_ellipse_mask,
|
| 263 |
+
)
|
| 264 |
+
SHAPE_CHOICES = list(SHAPE_GENERATORS.values())
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
def _generate_random_colors(num_colors, num_channels, intensity_range, random):
|
| 268 |
+
"""Generate an array of random colors.
|
| 269 |
+
|
| 270 |
+
Parameters
|
| 271 |
+
----------
|
| 272 |
+
num_colors : int
|
| 273 |
+
Number of colors to generate.
|
| 274 |
+
num_channels : int
|
| 275 |
+
Number of elements representing color.
|
| 276 |
+
intensity_range : {tuple of tuples of ints, tuple of ints}, optional
|
| 277 |
+
The range of values to sample pixel values from. For grayscale images
|
| 278 |
+
the format is (min, max). For multichannel - ((min, max),) if the
|
| 279 |
+
ranges are equal across the channels, and
|
| 280 |
+
((min_0, max_0), ... (min_N, max_N)) if they differ.
|
| 281 |
+
random : `numpy.random.Generator`
|
| 282 |
+
The random state to use for random sampling.
|
| 283 |
+
|
| 284 |
+
Raises
|
| 285 |
+
------
|
| 286 |
+
ValueError
|
| 287 |
+
When the `intensity_range` is not in the interval (0, 255).
|
| 288 |
+
|
| 289 |
+
Returns
|
| 290 |
+
-------
|
| 291 |
+
colors : array
|
| 292 |
+
An array of shape (num_colors, num_channels), where the values for
|
| 293 |
+
each channel are drawn from the corresponding `intensity_range`.
|
| 294 |
+
|
| 295 |
+
"""
|
| 296 |
+
if num_channels == 1:
|
| 297 |
+
intensity_range = (intensity_range,)
|
| 298 |
+
elif len(intensity_range) == 1:
|
| 299 |
+
intensity_range = intensity_range * num_channels
|
| 300 |
+
colors = [random.integers(r[0], r[1] + 1, size=num_colors) for r in intensity_range]
|
| 301 |
+
return np.transpose(colors)
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
def random_shapes(
|
| 305 |
+
image_shape,
|
| 306 |
+
max_shapes,
|
| 307 |
+
min_shapes=1,
|
| 308 |
+
min_size=2,
|
| 309 |
+
max_size=None,
|
| 310 |
+
num_channels=3,
|
| 311 |
+
shape=None,
|
| 312 |
+
intensity_range=None,
|
| 313 |
+
allow_overlap=False,
|
| 314 |
+
num_trials=100,
|
| 315 |
+
rng=None,
|
| 316 |
+
*,
|
| 317 |
+
channel_axis=-1,
|
| 318 |
+
):
|
| 319 |
+
"""Generate an image with random shapes, labeled with bounding boxes.
|
| 320 |
+
|
| 321 |
+
The image is populated with random shapes with random sizes, random
|
| 322 |
+
locations, and random colors, with or without overlap.
|
| 323 |
+
|
| 324 |
+
Shapes have random (row, col) starting coordinates and random sizes bounded
|
| 325 |
+
by `min_size` and `max_size`. It can occur that a randomly generated shape
|
| 326 |
+
will not fit the image at all. In that case, the algorithm will try again
|
| 327 |
+
with new starting coordinates a certain number of times. However, it also
|
| 328 |
+
means that some shapes may be skipped altogether. In that case, this
|
| 329 |
+
function will generate fewer shapes than requested.
|
| 330 |
+
|
| 331 |
+
Parameters
|
| 332 |
+
----------
|
| 333 |
+
image_shape : tuple
|
| 334 |
+
The number of rows and columns of the image to generate.
|
| 335 |
+
max_shapes : int
|
| 336 |
+
The maximum number of shapes to (attempt to) fit into the shape.
|
| 337 |
+
min_shapes : int, optional
|
| 338 |
+
The minimum number of shapes to (attempt to) fit into the shape.
|
| 339 |
+
min_size : int, optional
|
| 340 |
+
The minimum dimension of each shape to fit into the image.
|
| 341 |
+
max_size : int, optional
|
| 342 |
+
The maximum dimension of each shape to fit into the image.
|
| 343 |
+
num_channels : int, optional
|
| 344 |
+
Number of channels in the generated image. If 1, generate monochrome
|
| 345 |
+
images, else color images with multiple channels. Ignored if
|
| 346 |
+
``multichannel`` is set to False.
|
| 347 |
+
shape : {rectangle, circle, triangle, ellipse, None} str, optional
|
| 348 |
+
The name of the shape to generate or `None` to pick random ones.
|
| 349 |
+
intensity_range : {tuple of tuples of uint8, tuple of uint8}, optional
|
| 350 |
+
The range of values to sample pixel values from. For grayscale
|
| 351 |
+
images the format is (min, max). For multichannel - ((min, max),)
|
| 352 |
+
if the ranges are equal across the channels, and
|
| 353 |
+
((min_0, max_0), ... (min_N, max_N)) if they differ. As the
|
| 354 |
+
function supports generation of uint8 arrays only, the maximum
|
| 355 |
+
range is (0, 255). If None, set to (0, 254) for each channel
|
| 356 |
+
reserving color of intensity = 255 for background.
|
| 357 |
+
allow_overlap : bool, optional
|
| 358 |
+
If `True`, allow shapes to overlap.
|
| 359 |
+
num_trials : int, optional
|
| 360 |
+
How often to attempt to fit a shape into the image before skipping it.
|
| 361 |
+
rng : {`numpy.random.Generator`, int}, optional
|
| 362 |
+
Pseudo-random number generator.
|
| 363 |
+
By default, a PCG64 generator is used (see :func:`numpy.random.default_rng`).
|
| 364 |
+
If `rng` is an int, it is used to seed the generator.
|
| 365 |
+
channel_axis : int or None, optional
|
| 366 |
+
If None, the image is assumed to be a grayscale (single channel) image.
|
| 367 |
+
Otherwise, this parameter indicates which axis of the array corresponds
|
| 368 |
+
to channels.
|
| 369 |
+
|
| 370 |
+
.. versionadded:: 0.19
|
| 371 |
+
``channel_axis`` was added in 0.19.
|
| 372 |
+
|
| 373 |
+
Returns
|
| 374 |
+
-------
|
| 375 |
+
image : uint8 array
|
| 376 |
+
An image with the fitted shapes.
|
| 377 |
+
labels : list
|
| 378 |
+
A list of labels, one per shape in the image. Each label is a
|
| 379 |
+
(category, ((r0, r1), (c0, c1))) tuple specifying the category and
|
| 380 |
+
bounding box coordinates of the shape.
|
| 381 |
+
|
| 382 |
+
Examples
|
| 383 |
+
--------
|
| 384 |
+
>>> import skimage.draw
|
| 385 |
+
>>> image, labels = skimage.draw.random_shapes((32, 32), max_shapes=3)
|
| 386 |
+
>>> image # doctest: +SKIP
|
| 387 |
+
array([
|
| 388 |
+
[[255, 255, 255],
|
| 389 |
+
[255, 255, 255],
|
| 390 |
+
[255, 255, 255],
|
| 391 |
+
...,
|
| 392 |
+
[255, 255, 255],
|
| 393 |
+
[255, 255, 255],
|
| 394 |
+
[255, 255, 255]]], dtype=uint8)
|
| 395 |
+
>>> labels # doctest: +SKIP
|
| 396 |
+
[('circle', ((22, 18), (25, 21))),
|
| 397 |
+
('triangle', ((5, 6), (13, 13)))]
|
| 398 |
+
"""
|
| 399 |
+
if min_size > image_shape[0] or min_size > image_shape[1]:
|
| 400 |
+
raise ValueError('Minimum dimension must be less than ncols and nrows')
|
| 401 |
+
max_size = max_size or max(image_shape[0], image_shape[1])
|
| 402 |
+
|
| 403 |
+
if channel_axis is None:
|
| 404 |
+
num_channels = 1
|
| 405 |
+
|
| 406 |
+
if intensity_range is None:
|
| 407 |
+
intensity_range = (0, 254) if num_channels == 1 else ((0, 254),)
|
| 408 |
+
else:
|
| 409 |
+
tmp = (intensity_range,) if num_channels == 1 else intensity_range
|
| 410 |
+
for intensity_pair in tmp:
|
| 411 |
+
for intensity in intensity_pair:
|
| 412 |
+
if not (0 <= intensity <= 255):
|
| 413 |
+
msg = 'Intensity range must lie within (0, 255) interval'
|
| 414 |
+
raise ValueError(msg)
|
| 415 |
+
|
| 416 |
+
rng = np.random.default_rng(rng)
|
| 417 |
+
user_shape = shape
|
| 418 |
+
image_shape = (image_shape[0], image_shape[1], num_channels)
|
| 419 |
+
image = np.full(image_shape, 255, dtype=np.uint8)
|
| 420 |
+
filled = np.zeros(image_shape, dtype=bool)
|
| 421 |
+
labels = []
|
| 422 |
+
|
| 423 |
+
num_shapes = rng.integers(min_shapes, max_shapes + 1)
|
| 424 |
+
colors = _generate_random_colors(num_shapes, num_channels, intensity_range, rng)
|
| 425 |
+
shape = (min_size, max_size)
|
| 426 |
+
for shape_idx in range(num_shapes):
|
| 427 |
+
if user_shape is None:
|
| 428 |
+
shape_generator = rng.choice(SHAPE_CHOICES)
|
| 429 |
+
else:
|
| 430 |
+
shape_generator = SHAPE_GENERATORS[user_shape]
|
| 431 |
+
for _ in range(num_trials):
|
| 432 |
+
# Pick start coordinates.
|
| 433 |
+
column = rng.integers(max(1, image_shape[1] - min_size))
|
| 434 |
+
row = rng.integers(max(1, image_shape[0] - min_size))
|
| 435 |
+
point = (row, column)
|
| 436 |
+
try:
|
| 437 |
+
indices, label = shape_generator(point, image_shape, shape, rng)
|
| 438 |
+
except ArithmeticError:
|
| 439 |
+
# Couldn't fit the shape, skip it.
|
| 440 |
+
indices = []
|
| 441 |
+
continue
|
| 442 |
+
# Check if there is an overlap where the mask is nonzero.
|
| 443 |
+
if allow_overlap or not filled[indices].any():
|
| 444 |
+
image[indices] = colors[shape_idx]
|
| 445 |
+
filled[indices] = True
|
| 446 |
+
labels.append(label)
|
| 447 |
+
break
|
| 448 |
+
else:
|
| 449 |
+
warn(
|
| 450 |
+
'Could not fit any shapes to image, '
|
| 451 |
+
'consider reducing the minimum dimension'
|
| 452 |
+
)
|
| 453 |
+
|
| 454 |
+
if channel_axis is None:
|
| 455 |
+
image = np.squeeze(image, axis=2)
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
from .._shared._geometry import polygon_clip
|
| 4 |
+
from .._shared.version_requirements import require
|
| 5 |
+
from .._shared.compat import NP_COPY_IF_NEEDED
|
| 6 |
+
from ._draw import (
|
| 7 |
+
_coords_inside_image,
|
| 8 |
+
_line,
|
| 9 |
+
_line_aa,
|
| 10 |
+
_polygon,
|
| 11 |
+
_ellipse_perimeter,
|
| 12 |
+
_circle_perimeter,
|
| 13 |
+
_circle_perimeter_aa,
|
| 14 |
+
_bezier_curve,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def _ellipse_in_shape(shape, center, radii, rotation=0.0):
|
| 19 |
+
"""Generate coordinates of points within ellipse bounded by shape.
|
| 20 |
+
|
| 21 |
+
Parameters
|
| 22 |
+
----------
|
| 23 |
+
shape : iterable of ints
|
| 24 |
+
Shape of the input image. Must be at least length 2. Only the first
|
| 25 |
+
two values are used to determine the extent of the input image.
|
| 26 |
+
center : iterable of floats
|
| 27 |
+
(row, column) position of center inside the given shape.
|
| 28 |
+
radii : iterable of floats
|
| 29 |
+
Size of two half axes (for row and column)
|
| 30 |
+
rotation : float, optional
|
| 31 |
+
Rotation of the ellipse defined by the above, in radians
|
| 32 |
+
in range (-PI, PI), in contra clockwise direction,
|
| 33 |
+
with respect to the column-axis.
|
| 34 |
+
|
| 35 |
+
Returns
|
| 36 |
+
-------
|
| 37 |
+
rows : iterable of ints
|
| 38 |
+
Row coordinates representing values within the ellipse.
|
| 39 |
+
cols : iterable of ints
|
| 40 |
+
Corresponding column coordinates representing values within the ellipse.
|
| 41 |
+
"""
|
| 42 |
+
r_lim, c_lim = np.ogrid[0 : float(shape[0]), 0 : float(shape[1])]
|
| 43 |
+
r_org, c_org = center
|
| 44 |
+
r_rad, c_rad = radii
|
| 45 |
+
rotation %= np.pi
|
| 46 |
+
sin_alpha, cos_alpha = np.sin(rotation), np.cos(rotation)
|
| 47 |
+
r, c = (r_lim - r_org), (c_lim - c_org)
|
| 48 |
+
distances = ((r * cos_alpha + c * sin_alpha) / r_rad) ** 2 + (
|
| 49 |
+
(r * sin_alpha - c * cos_alpha) / c_rad
|
| 50 |
+
) ** 2
|
| 51 |
+
return np.nonzero(distances < 1)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def ellipse(r, c, r_radius, c_radius, shape=None, rotation=0.0):
|
| 55 |
+
"""Generate coordinates of pixels within ellipse.
|
| 56 |
+
|
| 57 |
+
Parameters
|
| 58 |
+
----------
|
| 59 |
+
r, c : double
|
| 60 |
+
Centre coordinate of ellipse.
|
| 61 |
+
r_radius, c_radius : double
|
| 62 |
+
Minor and major semi-axes. ``(r/r_radius)**2 + (c/c_radius)**2 = 1``.
|
| 63 |
+
shape : tuple, optional
|
| 64 |
+
Image shape which is used to determine the maximum extent of output pixel
|
| 65 |
+
coordinates. This is useful for ellipses which exceed the image size.
|
| 66 |
+
By default the full extent of the ellipse are used. Must be at least
|
| 67 |
+
length 2. Only the first two values are used to determine the extent.
|
| 68 |
+
rotation : float, optional (default 0.)
|
| 69 |
+
Set the ellipse rotation (rotation) in range (-PI, PI)
|
| 70 |
+
in contra clock wise direction, so PI/2 degree means swap ellipse axis
|
| 71 |
+
|
| 72 |
+
Returns
|
| 73 |
+
-------
|
| 74 |
+
rr, cc : ndarray of int
|
| 75 |
+
Pixel coordinates of ellipse.
|
| 76 |
+
May be used to directly index into an array, e.g.
|
| 77 |
+
``img[rr, cc] = 1``.
|
| 78 |
+
|
| 79 |
+
Examples
|
| 80 |
+
--------
|
| 81 |
+
>>> from skimage.draw import ellipse
|
| 82 |
+
>>> img = np.zeros((10, 12), dtype=np.uint8)
|
| 83 |
+
>>> rr, cc = ellipse(5, 6, 3, 5, rotation=np.deg2rad(30))
|
| 84 |
+
>>> img[rr, cc] = 1
|
| 85 |
+
>>> img
|
| 86 |
+
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 87 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 88 |
+
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0],
|
| 89 |
+
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
|
| 90 |
+
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
|
| 91 |
+
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
|
| 92 |
+
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
| 93 |
+
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
|
| 94 |
+
[0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0],
|
| 95 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
|
| 96 |
+
|
| 97 |
+
Notes
|
| 98 |
+
-----
|
| 99 |
+
The ellipse equation::
|
| 100 |
+
|
| 101 |
+
((x * cos(alpha) + y * sin(alpha)) / x_radius) ** 2 +
|
| 102 |
+
((x * sin(alpha) - y * cos(alpha)) / y_radius) ** 2 = 1
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
Note that the positions of `ellipse` without specified `shape` can have
|
| 106 |
+
also, negative values, as this is correct on the plane. On the other hand
|
| 107 |
+
using these ellipse positions for an image afterwards may lead to appearing
|
| 108 |
+
on the other side of image, because ``image[-1, -1] = image[end-1, end-1]``
|
| 109 |
+
|
| 110 |
+
>>> rr, cc = ellipse(1, 2, 3, 6)
|
| 111 |
+
>>> img = np.zeros((6, 12), dtype=np.uint8)
|
| 112 |
+
>>> img[rr, cc] = 1
|
| 113 |
+
>>> img
|
| 114 |
+
array([[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1],
|
| 115 |
+
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1],
|
| 116 |
+
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1],
|
| 117 |
+
[1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1],
|
| 118 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 119 |
+
[1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1]], dtype=uint8)
|
| 120 |
+
"""
|
| 121 |
+
|
| 122 |
+
center = np.array([r, c])
|
| 123 |
+
radii = np.array([r_radius, c_radius])
|
| 124 |
+
# allow just rotation with in range +/- 180 degree
|
| 125 |
+
rotation %= np.pi
|
| 126 |
+
|
| 127 |
+
# compute rotated radii by given rotation
|
| 128 |
+
r_radius_rot = abs(r_radius * np.cos(rotation)) + c_radius * np.sin(rotation)
|
| 129 |
+
c_radius_rot = r_radius * np.sin(rotation) + abs(c_radius * np.cos(rotation))
|
| 130 |
+
# The upper_left and lower_right corners of the smallest rectangle
|
| 131 |
+
# containing the ellipse.
|
| 132 |
+
radii_rot = np.array([r_radius_rot, c_radius_rot])
|
| 133 |
+
upper_left = np.ceil(center - radii_rot).astype(int)
|
| 134 |
+
lower_right = np.floor(center + radii_rot).astype(int)
|
| 135 |
+
|
| 136 |
+
if shape is not None:
|
| 137 |
+
# Constrain upper_left and lower_right by shape boundary.
|
| 138 |
+
upper_left = np.maximum(upper_left, np.array([0, 0]))
|
| 139 |
+
lower_right = np.minimum(lower_right, np.array(shape[:2]) - 1)
|
| 140 |
+
|
| 141 |
+
shifted_center = center - upper_left
|
| 142 |
+
bounding_shape = lower_right - upper_left + 1
|
| 143 |
+
|
| 144 |
+
rr, cc = _ellipse_in_shape(bounding_shape, shifted_center, radii, rotation)
|
| 145 |
+
rr.flags.writeable = True
|
| 146 |
+
cc.flags.writeable = True
|
| 147 |
+
rr += upper_left[0]
|
| 148 |
+
cc += upper_left[1]
|
| 149 |
+
return rr, cc
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
def disk(center, radius, *, shape=None):
|
| 153 |
+
"""Generate coordinates of pixels within circle.
|
| 154 |
+
|
| 155 |
+
Parameters
|
| 156 |
+
----------
|
| 157 |
+
center : tuple
|
| 158 |
+
Center coordinate of disk.
|
| 159 |
+
radius : double
|
| 160 |
+
Radius of disk.
|
| 161 |
+
shape : tuple, optional
|
| 162 |
+
Image shape as a tuple of size 2. Determines the maximum
|
| 163 |
+
extent of output pixel coordinates. This is useful for disks that
|
| 164 |
+
exceed the image size. If None, the full extent of the disk is used.
|
| 165 |
+
The shape might result in negative coordinates and wraparound
|
| 166 |
+
behaviour.
|
| 167 |
+
|
| 168 |
+
Returns
|
| 169 |
+
-------
|
| 170 |
+
rr, cc : ndarray of int
|
| 171 |
+
Pixel coordinates of disk.
|
| 172 |
+
May be used to directly index into an array, e.g.
|
| 173 |
+
``img[rr, cc] = 1``.
|
| 174 |
+
|
| 175 |
+
Examples
|
| 176 |
+
--------
|
| 177 |
+
>>> import numpy as np
|
| 178 |
+
>>> from skimage.draw import disk
|
| 179 |
+
>>> shape = (4, 4)
|
| 180 |
+
>>> img = np.zeros(shape, dtype=np.uint8)
|
| 181 |
+
>>> rr, cc = disk((0, 0), 2, shape=shape)
|
| 182 |
+
>>> img[rr, cc] = 1
|
| 183 |
+
>>> img
|
| 184 |
+
array([[1, 1, 0, 0],
|
| 185 |
+
[1, 1, 0, 0],
|
| 186 |
+
[0, 0, 0, 0],
|
| 187 |
+
[0, 0, 0, 0]], dtype=uint8)
|
| 188 |
+
>>> img = np.zeros(shape, dtype=np.uint8)
|
| 189 |
+
>>> # Negative coordinates in rr and cc perform a wraparound
|
| 190 |
+
>>> rr, cc = disk((0, 0), 2, shape=None)
|
| 191 |
+
>>> img[rr, cc] = 1
|
| 192 |
+
>>> img
|
| 193 |
+
array([[1, 1, 0, 1],
|
| 194 |
+
[1, 1, 0, 1],
|
| 195 |
+
[0, 0, 0, 0],
|
| 196 |
+
[1, 1, 0, 1]], dtype=uint8)
|
| 197 |
+
>>> img = np.zeros((10, 10), dtype=np.uint8)
|
| 198 |
+
>>> rr, cc = disk((4, 4), 5)
|
| 199 |
+
>>> img[rr, cc] = 1
|
| 200 |
+
>>> img
|
| 201 |
+
array([[0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
|
| 202 |
+
[0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
| 203 |
+
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
|
| 204 |
+
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
|
| 205 |
+
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
|
| 206 |
+
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
|
| 207 |
+
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
|
| 208 |
+
[0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
| 209 |
+
[0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
|
| 210 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
|
| 211 |
+
"""
|
| 212 |
+
r, c = center
|
| 213 |
+
return ellipse(r, c, radius, radius, shape)
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
@require("matplotlib", ">=3.3")
|
| 217 |
+
def polygon_perimeter(r, c, shape=None, clip=False):
|
| 218 |
+
"""Generate polygon perimeter coordinates.
|
| 219 |
+
|
| 220 |
+
Parameters
|
| 221 |
+
----------
|
| 222 |
+
r : (N,) ndarray
|
| 223 |
+
Row coordinates of vertices of polygon.
|
| 224 |
+
c : (N,) ndarray
|
| 225 |
+
Column coordinates of vertices of polygon.
|
| 226 |
+
shape : tuple, optional
|
| 227 |
+
Image shape which is used to determine maximum extents of output pixel
|
| 228 |
+
coordinates. This is useful for polygons that exceed the image size.
|
| 229 |
+
If None, the full extents of the polygon is used. Must be at least
|
| 230 |
+
length 2. Only the first two values are used to determine the extent of
|
| 231 |
+
the input image.
|
| 232 |
+
clip : bool, optional
|
| 233 |
+
Whether to clip the polygon to the provided shape. If this is set
|
| 234 |
+
to True, the drawn figure will always be a closed polygon with all
|
| 235 |
+
edges visible.
|
| 236 |
+
|
| 237 |
+
Returns
|
| 238 |
+
-------
|
| 239 |
+
rr, cc : ndarray of int
|
| 240 |
+
Pixel coordinates of polygon.
|
| 241 |
+
May be used to directly index into an array, e.g.
|
| 242 |
+
``img[rr, cc] = 1``.
|
| 243 |
+
|
| 244 |
+
Examples
|
| 245 |
+
--------
|
| 246 |
+
.. testsetup::
|
| 247 |
+
>>> import pytest; _ = pytest.importorskip('matplotlib')
|
| 248 |
+
|
| 249 |
+
>>> from skimage.draw import polygon_perimeter
|
| 250 |
+
>>> img = np.zeros((10, 10), dtype=np.uint8)
|
| 251 |
+
>>> rr, cc = polygon_perimeter([5, -1, 5, 10],
|
| 252 |
+
... [-1, 5, 11, 5],
|
| 253 |
+
... shape=img.shape, clip=True)
|
| 254 |
+
>>> img[rr, cc] = 1
|
| 255 |
+
>>> img
|
| 256 |
+
array([[0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
|
| 257 |
+
[0, 0, 0, 1, 0, 0, 0, 1, 0, 0],
|
| 258 |
+
[0, 0, 1, 0, 0, 0, 0, 0, 1, 0],
|
| 259 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
|
| 260 |
+
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
| 261 |
+
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
| 262 |
+
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
| 263 |
+
[0, 1, 1, 0, 0, 0, 0, 0, 0, 1],
|
| 264 |
+
[0, 0, 0, 1, 0, 0, 0, 1, 1, 0],
|
| 265 |
+
[0, 0, 0, 0, 1, 1, 1, 0, 0, 0]], dtype=uint8)
|
| 266 |
+
|
| 267 |
+
"""
|
| 268 |
+
if clip:
|
| 269 |
+
if shape is None:
|
| 270 |
+
raise ValueError("Must specify clipping shape")
|
| 271 |
+
clip_box = np.array([0, 0, shape[0] - 1, shape[1] - 1])
|
| 272 |
+
else:
|
| 273 |
+
clip_box = np.array([np.min(r), np.min(c), np.max(r), np.max(c)])
|
| 274 |
+
|
| 275 |
+
# Do the clipping irrespective of whether clip is set. This
|
| 276 |
+
# ensures that the returned polygon is closed and is an array.
|
| 277 |
+
r, c = polygon_clip(r, c, *clip_box)
|
| 278 |
+
|
| 279 |
+
r = np.round(r).astype(int)
|
| 280 |
+
c = np.round(c).astype(int)
|
| 281 |
+
|
| 282 |
+
# Construct line segments
|
| 283 |
+
rr, cc = [], []
|
| 284 |
+
for i in range(len(r) - 1):
|
| 285 |
+
line_r, line_c = line(r[i], c[i], r[i + 1], c[i + 1])
|
| 286 |
+
rr.extend(line_r)
|
| 287 |
+
cc.extend(line_c)
|
| 288 |
+
|
| 289 |
+
rr = np.asarray(rr)
|
| 290 |
+
cc = np.asarray(cc)
|
| 291 |
+
|
| 292 |
+
if shape is None:
|
| 293 |
+
return rr, cc
|
| 294 |
+
else:
|
| 295 |
+
return _coords_inside_image(rr, cc, shape)
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
def set_color(image, coords, color, alpha=1):
|
| 299 |
+
"""Set pixel color in the image at the given coordinates.
|
| 300 |
+
|
| 301 |
+
Note that this function modifies the color of the image in-place.
|
| 302 |
+
Coordinates that exceed the shape of the image will be ignored.
|
| 303 |
+
|
| 304 |
+
Parameters
|
| 305 |
+
----------
|
| 306 |
+
image : (M, N, C) ndarray
|
| 307 |
+
Image
|
| 308 |
+
coords : tuple of ((K,) ndarray, (K,) ndarray)
|
| 309 |
+
Row and column coordinates of pixels to be colored.
|
| 310 |
+
color : (C,) ndarray
|
| 311 |
+
Color to be assigned to coordinates in the image.
|
| 312 |
+
alpha : scalar or (K,) ndarray
|
| 313 |
+
Alpha values used to blend color with image. 0 is transparent,
|
| 314 |
+
1 is opaque.
|
| 315 |
+
|
| 316 |
+
Examples
|
| 317 |
+
--------
|
| 318 |
+
>>> from skimage.draw import line, set_color
|
| 319 |
+
>>> img = np.zeros((10, 10), dtype=np.uint8)
|
| 320 |
+
>>> rr, cc = line(1, 1, 20, 20)
|
| 321 |
+
>>> set_color(img, (rr, cc), 1)
|
| 322 |
+
>>> img
|
| 323 |
+
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 324 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 325 |
+
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
|
| 326 |
+
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
|
| 327 |
+
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
|
| 328 |
+
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
|
| 329 |
+
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
|
| 330 |
+
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
|
| 331 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
| 332 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1]], dtype=uint8)
|
| 333 |
+
|
| 334 |
+
"""
|
| 335 |
+
rr, cc = coords
|
| 336 |
+
|
| 337 |
+
if image.ndim == 2:
|
| 338 |
+
image = image[..., np.newaxis]
|
| 339 |
+
|
| 340 |
+
color = np.array(color, ndmin=1, copy=NP_COPY_IF_NEEDED)
|
| 341 |
+
|
| 342 |
+
if image.shape[-1] != color.shape[-1]:
|
| 343 |
+
raise ValueError(
|
| 344 |
+
f'Color shape ({color.shape[0]}) must match last '
|
| 345 |
+
'image dimension ({image.shape[-1]}).'
|
| 346 |
+
)
|
| 347 |
+
|
| 348 |
+
if np.isscalar(alpha):
|
| 349 |
+
# Can be replaced by ``full_like`` when numpy 1.8 becomes
|
| 350 |
+
# minimum dependency
|
| 351 |
+
alpha = np.ones_like(rr) * alpha
|
| 352 |
+
|
| 353 |
+
rr, cc, alpha = _coords_inside_image(rr, cc, image.shape, val=alpha)
|
| 354 |
+
|
| 355 |
+
alpha = alpha[..., np.newaxis]
|
| 356 |
+
|
| 357 |
+
color = color * alpha
|
| 358 |
+
vals = image[rr, cc] * (1 - alpha)
|
| 359 |
+
|
| 360 |
+
image[rr, cc] = vals + color
|
| 361 |
+
|
| 362 |
+
|
| 363 |
+
def line(r0, c0, r1, c1):
|
| 364 |
+
"""Generate line pixel coordinates.
|
| 365 |
+
|
| 366 |
+
Parameters
|
| 367 |
+
----------
|
| 368 |
+
r0, c0 : int
|
| 369 |
+
Starting position (row, column).
|
| 370 |
+
r1, c1 : int
|
| 371 |
+
End position (row, column).
|
| 372 |
+
|
| 373 |
+
Returns
|
| 374 |
+
-------
|
| 375 |
+
rr, cc : (N,) ndarray of int
|
| 376 |
+
Indices of pixels that belong to the line.
|
| 377 |
+
May be used to directly index into an array, e.g.
|
| 378 |
+
``img[rr, cc] = 1``.
|
| 379 |
+
|
| 380 |
+
Notes
|
| 381 |
+
-----
|
| 382 |
+
Anti-aliased line generator is available with `line_aa`.
|
| 383 |
+
|
| 384 |
+
Examples
|
| 385 |
+
--------
|
| 386 |
+
>>> from skimage.draw import line
|
| 387 |
+
>>> img = np.zeros((10, 10), dtype=np.uint8)
|
| 388 |
+
>>> rr, cc = line(1, 1, 8, 8)
|
| 389 |
+
>>> img[rr, cc] = 1
|
| 390 |
+
>>> img
|
| 391 |
+
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 392 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 393 |
+
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
|
| 394 |
+
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
|
| 395 |
+
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
|
| 396 |
+
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
|
| 397 |
+
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
|
| 398 |
+
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
|
| 399 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
| 400 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
|
| 401 |
+
"""
|
| 402 |
+
return _line(r0, c0, r1, c1)
|
| 403 |
+
|
| 404 |
+
|
| 405 |
+
def line_aa(r0, c0, r1, c1):
|
| 406 |
+
"""Generate anti-aliased line pixel coordinates.
|
| 407 |
+
|
| 408 |
+
Parameters
|
| 409 |
+
----------
|
| 410 |
+
r0, c0 : int
|
| 411 |
+
Starting position (row, column).
|
| 412 |
+
r1, c1 : int
|
| 413 |
+
End position (row, column).
|
| 414 |
+
|
| 415 |
+
Returns
|
| 416 |
+
-------
|
| 417 |
+
rr, cc, val : (N,) ndarray (int, int, float)
|
| 418 |
+
Indices of pixels (`rr`, `cc`) and intensity values (`val`).
|
| 419 |
+
``img[rr, cc] = val``.
|
| 420 |
+
|
| 421 |
+
References
|
| 422 |
+
----------
|
| 423 |
+
.. [1] A Rasterizing Algorithm for Drawing Curves, A. Zingl, 2012
|
| 424 |
+
http://members.chello.at/easyfilter/Bresenham.pdf
|
| 425 |
+
|
| 426 |
+
Examples
|
| 427 |
+
--------
|
| 428 |
+
>>> from skimage.draw import line_aa
|
| 429 |
+
>>> img = np.zeros((10, 10), dtype=np.uint8)
|
| 430 |
+
>>> rr, cc, val = line_aa(1, 1, 8, 8)
|
| 431 |
+
>>> img[rr, cc] = val * 255
|
| 432 |
+
>>> img
|
| 433 |
+
array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 434 |
+
[ 0, 255, 74, 0, 0, 0, 0, 0, 0, 0],
|
| 435 |
+
[ 0, 74, 255, 74, 0, 0, 0, 0, 0, 0],
|
| 436 |
+
[ 0, 0, 74, 255, 74, 0, 0, 0, 0, 0],
|
| 437 |
+
[ 0, 0, 0, 74, 255, 74, 0, 0, 0, 0],
|
| 438 |
+
[ 0, 0, 0, 0, 74, 255, 74, 0, 0, 0],
|
| 439 |
+
[ 0, 0, 0, 0, 0, 74, 255, 74, 0, 0],
|
| 440 |
+
[ 0, 0, 0, 0, 0, 0, 74, 255, 74, 0],
|
| 441 |
+
[ 0, 0, 0, 0, 0, 0, 0, 74, 255, 0],
|
| 442 |
+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
|
| 443 |
+
"""
|
| 444 |
+
return _line_aa(r0, c0, r1, c1)
|
| 445 |
+
|
| 446 |
+
|
| 447 |
+
def polygon(r, c, shape=None):
|
| 448 |
+
"""Generate coordinates of pixels inside a polygon.
|
| 449 |
+
|
| 450 |
+
Parameters
|
| 451 |
+
----------
|
| 452 |
+
r : (N,) array_like
|
| 453 |
+
Row coordinates of the polygon's vertices.
|
| 454 |
+
c : (N,) array_like
|
| 455 |
+
Column coordinates of the polygon's vertices.
|
| 456 |
+
shape : tuple, optional
|
| 457 |
+
Image shape which is used to determine the maximum extent of output
|
| 458 |
+
pixel coordinates. This is useful for polygons that exceed the image
|
| 459 |
+
size. If None, the full extent of the polygon is used. Must be at
|
| 460 |
+
least length 2. Only the first two values are used to determine the
|
| 461 |
+
extent of the input image.
|
| 462 |
+
|
| 463 |
+
Returns
|
| 464 |
+
-------
|
| 465 |
+
rr, cc : ndarray of int
|
| 466 |
+
Pixel coordinates of polygon.
|
| 467 |
+
May be used to directly index into an array, e.g.
|
| 468 |
+
``img[rr, cc] = 1``.
|
| 469 |
+
|
| 470 |
+
See Also
|
| 471 |
+
--------
|
| 472 |
+
polygon2mask:
|
| 473 |
+
Create a binary mask from a polygon.
|
| 474 |
+
|
| 475 |
+
Notes
|
| 476 |
+
-----
|
| 477 |
+
This function ensures that `rr` and `cc` don't contain negative values.
|
| 478 |
+
Pixels of the polygon that whose coordinates are smaller 0, are not drawn.
|
| 479 |
+
|
| 480 |
+
Examples
|
| 481 |
+
--------
|
| 482 |
+
>>> import skimage as ski
|
| 483 |
+
>>> r = np.array([1, 2, 8])
|
| 484 |
+
>>> c = np.array([1, 7, 4])
|
| 485 |
+
>>> rr, cc = ski.draw.polygon(r, c)
|
| 486 |
+
>>> img = np.zeros((10, 10), dtype=int)
|
| 487 |
+
>>> img[rr, cc] = 1
|
| 488 |
+
>>> img
|
| 489 |
+
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 490 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 491 |
+
[0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
|
| 492 |
+
[0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
|
| 493 |
+
[0, 0, 0, 1, 1, 1, 1, 0, 0, 0],
|
| 494 |
+
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
|
| 495 |
+
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
|
| 496 |
+
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
|
| 497 |
+
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
|
| 498 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
|
| 499 |
+
|
| 500 |
+
If the image `shape` is defined and vertices / points of the `polygon` are
|
| 501 |
+
outside this coordinate space, only a part (or none at all) of the polygon's
|
| 502 |
+
pixels is returned. Shifting the polygon's vertices by an offset can be used
|
| 503 |
+
to move the polygon around and potentially draw an arbitrary sub-region of
|
| 504 |
+
the polygon.
|
| 505 |
+
|
| 506 |
+
>>> offset = (2, -4)
|
| 507 |
+
>>> rr, cc = ski.draw.polygon(r - offset[0], c - offset[1], shape=img.shape)
|
| 508 |
+
>>> img = np.zeros((10, 10), dtype=int)
|
| 509 |
+
>>> img[rr, cc] = 1
|
| 510 |
+
>>> img
|
| 511 |
+
array([[0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
|
| 512 |
+
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
|
| 513 |
+
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
|
| 514 |
+
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
|
| 515 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
|
| 516 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
| 517 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
| 518 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 519 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 520 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
|
| 521 |
+
"""
|
| 522 |
+
return _polygon(r, c, shape)
|
| 523 |
+
|
| 524 |
+
|
| 525 |
+
def circle_perimeter(r, c, radius, method='bresenham', shape=None):
|
| 526 |
+
"""Generate circle perimeter coordinates.
|
| 527 |
+
|
| 528 |
+
Parameters
|
| 529 |
+
----------
|
| 530 |
+
r, c : int
|
| 531 |
+
Centre coordinate of circle.
|
| 532 |
+
radius : int
|
| 533 |
+
Radius of circle.
|
| 534 |
+
method : {'bresenham', 'andres'}, optional
|
| 535 |
+
bresenham : Bresenham method (default)
|
| 536 |
+
andres : Andres method
|
| 537 |
+
shape : tuple, optional
|
| 538 |
+
Image shape which is used to determine the maximum extent of output
|
| 539 |
+
pixel coordinates. This is useful for circles that exceed the image
|
| 540 |
+
size. If None, the full extent of the circle is used. Must be at least
|
| 541 |
+
length 2. Only the first two values are used to determine the extent of
|
| 542 |
+
the input image.
|
| 543 |
+
|
| 544 |
+
Returns
|
| 545 |
+
-------
|
| 546 |
+
rr, cc : (N,) ndarray of int
|
| 547 |
+
Bresenham and Andres' method:
|
| 548 |
+
Indices of pixels that belong to the circle perimeter.
|
| 549 |
+
May be used to directly index into an array, e.g.
|
| 550 |
+
``img[rr, cc] = 1``.
|
| 551 |
+
|
| 552 |
+
Notes
|
| 553 |
+
-----
|
| 554 |
+
Andres method presents the advantage that concentric
|
| 555 |
+
circles create a disc whereas Bresenham can make holes. There
|
| 556 |
+
is also less distortions when Andres circles are rotated.
|
| 557 |
+
Bresenham method is also known as midpoint circle algorithm.
|
| 558 |
+
Anti-aliased circle generator is available with `circle_perimeter_aa`.
|
| 559 |
+
|
| 560 |
+
References
|
| 561 |
+
----------
|
| 562 |
+
.. [1] J.E. Bresenham, "Algorithm for computer control of a digital
|
| 563 |
+
plotter", IBM Systems journal, 4 (1965) 25-30.
|
| 564 |
+
.. [2] E. Andres, "Discrete circles, rings and spheres", Computers &
|
| 565 |
+
Graphics, 18 (1994) 695-706.
|
| 566 |
+
|
| 567 |
+
Examples
|
| 568 |
+
--------
|
| 569 |
+
>>> from skimage.draw import circle_perimeter
|
| 570 |
+
>>> img = np.zeros((10, 10), dtype=np.uint8)
|
| 571 |
+
>>> rr, cc = circle_perimeter(4, 4, 3)
|
| 572 |
+
>>> img[rr, cc] = 1
|
| 573 |
+
>>> img
|
| 574 |
+
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 575 |
+
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
|
| 576 |
+
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
|
| 577 |
+
[0, 1, 0, 0, 0, 0, 0, 1, 0, 0],
|
| 578 |
+
[0, 1, 0, 0, 0, 0, 0, 1, 0, 0],
|
| 579 |
+
[0, 1, 0, 0, 0, 0, 0, 1, 0, 0],
|
| 580 |
+
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
|
| 581 |
+
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
|
| 582 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 583 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
|
| 584 |
+
"""
|
| 585 |
+
return _circle_perimeter(r, c, radius, method, shape)
|
| 586 |
+
|
| 587 |
+
|
| 588 |
+
def circle_perimeter_aa(r, c, radius, shape=None):
|
| 589 |
+
"""Generate anti-aliased circle perimeter coordinates.
|
| 590 |
+
|
| 591 |
+
Parameters
|
| 592 |
+
----------
|
| 593 |
+
r, c : int
|
| 594 |
+
Centre coordinate of circle.
|
| 595 |
+
radius : int
|
| 596 |
+
Radius of circle.
|
| 597 |
+
shape : tuple, optional
|
| 598 |
+
Image shape which is used to determine the maximum extent of output
|
| 599 |
+
pixel coordinates. This is useful for circles that exceed the image
|
| 600 |
+
size. If None, the full extent of the circle is used. Must be at least
|
| 601 |
+
length 2. Only the first two values are used to determine the extent of
|
| 602 |
+
the input image.
|
| 603 |
+
|
| 604 |
+
Returns
|
| 605 |
+
-------
|
| 606 |
+
rr, cc, val : (N,) ndarray (int, int, float)
|
| 607 |
+
Indices of pixels (`rr`, `cc`) and intensity values (`val`).
|
| 608 |
+
``img[rr, cc] = val``.
|
| 609 |
+
|
| 610 |
+
Notes
|
| 611 |
+
-----
|
| 612 |
+
Wu's method draws anti-aliased circle. This implementation doesn't use
|
| 613 |
+
lookup table optimization.
|
| 614 |
+
|
| 615 |
+
Use the function ``draw.set_color`` to apply ``circle_perimeter_aa``
|
| 616 |
+
results to color images.
|
| 617 |
+
|
| 618 |
+
References
|
| 619 |
+
----------
|
| 620 |
+
.. [1] X. Wu, "An efficient antialiasing technique", In ACM SIGGRAPH
|
| 621 |
+
Computer Graphics, 25 (1991) 143-152.
|
| 622 |
+
|
| 623 |
+
Examples
|
| 624 |
+
--------
|
| 625 |
+
>>> from skimage.draw import circle_perimeter_aa
|
| 626 |
+
>>> img = np.zeros((10, 10), dtype=np.uint8)
|
| 627 |
+
>>> rr, cc, val = circle_perimeter_aa(4, 4, 3)
|
| 628 |
+
>>> img[rr, cc] = val * 255
|
| 629 |
+
>>> img
|
| 630 |
+
array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 631 |
+
[ 0, 0, 60, 211, 255, 211, 60, 0, 0, 0],
|
| 632 |
+
[ 0, 60, 194, 43, 0, 43, 194, 60, 0, 0],
|
| 633 |
+
[ 0, 211, 43, 0, 0, 0, 43, 211, 0, 0],
|
| 634 |
+
[ 0, 255, 0, 0, 0, 0, 0, 255, 0, 0],
|
| 635 |
+
[ 0, 211, 43, 0, 0, 0, 43, 211, 0, 0],
|
| 636 |
+
[ 0, 60, 194, 43, 0, 43, 194, 60, 0, 0],
|
| 637 |
+
[ 0, 0, 60, 211, 255, 211, 60, 0, 0, 0],
|
| 638 |
+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 639 |
+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
|
| 640 |
+
|
| 641 |
+
>>> from skimage import data, draw
|
| 642 |
+
>>> image = data.chelsea()
|
| 643 |
+
>>> rr, cc, val = draw.circle_perimeter_aa(r=100, c=100, radius=75)
|
| 644 |
+
>>> draw.set_color(image, (rr, cc), [1, 0, 0], alpha=val)
|
| 645 |
+
"""
|
| 646 |
+
return _circle_perimeter_aa(r, c, radius, shape)
|
| 647 |
+
|
| 648 |
+
|
| 649 |
+
def ellipse_perimeter(r, c, r_radius, c_radius, orientation=0, shape=None):
|
| 650 |
+
"""Generate ellipse perimeter coordinates.
|
| 651 |
+
|
| 652 |
+
Parameters
|
| 653 |
+
----------
|
| 654 |
+
r, c : int
|
| 655 |
+
Centre coordinate of ellipse.
|
| 656 |
+
r_radius, c_radius : int
|
| 657 |
+
Minor and major semi-axes. ``(r/r_radius)**2 + (c/c_radius)**2 = 1``.
|
| 658 |
+
orientation : double, optional
|
| 659 |
+
Major axis orientation in clockwise direction as radians.
|
| 660 |
+
shape : tuple, optional
|
| 661 |
+
Image shape which is used to determine the maximum extent of output
|
| 662 |
+
pixel coordinates. This is useful for ellipses that exceed the image
|
| 663 |
+
size. If None, the full extent of the ellipse is used. Must be at
|
| 664 |
+
least length 2. Only the first two values are used to determine the
|
| 665 |
+
extent of the input image.
|
| 666 |
+
|
| 667 |
+
Returns
|
| 668 |
+
-------
|
| 669 |
+
rr, cc : (N,) ndarray of int
|
| 670 |
+
Indices of pixels that belong to the ellipse perimeter.
|
| 671 |
+
May be used to directly index into an array, e.g.
|
| 672 |
+
``img[rr, cc] = 1``.
|
| 673 |
+
|
| 674 |
+
References
|
| 675 |
+
----------
|
| 676 |
+
.. [1] A Rasterizing Algorithm for Drawing Curves, A. Zingl, 2012
|
| 677 |
+
http://members.chello.at/easyfilter/Bresenham.pdf
|
| 678 |
+
|
| 679 |
+
Examples
|
| 680 |
+
--------
|
| 681 |
+
>>> from skimage.draw import ellipse_perimeter
|
| 682 |
+
>>> img = np.zeros((10, 10), dtype=np.uint8)
|
| 683 |
+
>>> rr, cc = ellipse_perimeter(5, 5, 3, 4)
|
| 684 |
+
>>> img[rr, cc] = 1
|
| 685 |
+
>>> img
|
| 686 |
+
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 687 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 688 |
+
[0, 0, 0, 1, 1, 1, 1, 1, 0, 0],
|
| 689 |
+
[0, 0, 1, 0, 0, 0, 0, 0, 1, 0],
|
| 690 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
|
| 691 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
|
| 692 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
|
| 693 |
+
[0, 0, 1, 0, 0, 0, 0, 0, 1, 0],
|
| 694 |
+
[0, 0, 0, 1, 1, 1, 1, 1, 0, 0],
|
| 695 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
|
| 696 |
+
|
| 697 |
+
|
| 698 |
+
Note that the positions of `ellipse` without specified `shape` can have
|
| 699 |
+
also, negative values, as this is correct on the plane. On the other hand
|
| 700 |
+
using these ellipse positions for an image afterwards may lead to appearing
|
| 701 |
+
on the other side of image, because ``image[-1, -1] = image[end-1, end-1]``
|
| 702 |
+
|
| 703 |
+
>>> rr, cc = ellipse_perimeter(2, 3, 4, 5)
|
| 704 |
+
>>> img = np.zeros((9, 12), dtype=np.uint8)
|
| 705 |
+
>>> img[rr, cc] = 1
|
| 706 |
+
>>> img
|
| 707 |
+
array([[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1],
|
| 708 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
|
| 709 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
|
| 710 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
|
| 711 |
+
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1],
|
| 712 |
+
[1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
|
| 713 |
+
[0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
|
| 714 |
+
[0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
|
| 715 |
+
[1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]], dtype=uint8)
|
| 716 |
+
"""
|
| 717 |
+
return _ellipse_perimeter(r, c, r_radius, c_radius, orientation, shape)
|
| 718 |
+
|
| 719 |
+
|
| 720 |
+
def bezier_curve(r0, c0, r1, c1, r2, c2, weight, shape=None):
|
| 721 |
+
"""Generate Bezier curve coordinates.
|
| 722 |
+
|
| 723 |
+
Parameters
|
| 724 |
+
----------
|
| 725 |
+
r0, c0 : int
|
| 726 |
+
Coordinates of the first control point.
|
| 727 |
+
r1, c1 : int
|
| 728 |
+
Coordinates of the middle control point.
|
| 729 |
+
r2, c2 : int
|
| 730 |
+
Coordinates of the last control point.
|
| 731 |
+
weight : double
|
| 732 |
+
Middle control point weight, it describes the line tension.
|
| 733 |
+
shape : tuple, optional
|
| 734 |
+
Image shape which is used to determine the maximum extent of output
|
| 735 |
+
pixel coordinates. This is useful for curves that exceed the image
|
| 736 |
+
size. If None, the full extent of the curve is used.
|
| 737 |
+
|
| 738 |
+
Returns
|
| 739 |
+
-------
|
| 740 |
+
rr, cc : (N,) ndarray of int
|
| 741 |
+
Indices of pixels that belong to the Bezier curve.
|
| 742 |
+
May be used to directly index into an array, e.g.
|
| 743 |
+
``img[rr, cc] = 1``.
|
| 744 |
+
|
| 745 |
+
Notes
|
| 746 |
+
-----
|
| 747 |
+
The algorithm is the rational quadratic algorithm presented in
|
| 748 |
+
reference [1]_.
|
| 749 |
+
|
| 750 |
+
References
|
| 751 |
+
----------
|
| 752 |
+
.. [1] A Rasterizing Algorithm for Drawing Curves, A. Zingl, 2012
|
| 753 |
+
http://members.chello.at/easyfilter/Bresenham.pdf
|
| 754 |
+
|
| 755 |
+
Examples
|
| 756 |
+
--------
|
| 757 |
+
>>> import numpy as np
|
| 758 |
+
>>> from skimage.draw import bezier_curve
|
| 759 |
+
>>> img = np.zeros((10, 10), dtype=np.uint8)
|
| 760 |
+
>>> rr, cc = bezier_curve(1, 5, 5, -2, 8, 8, 2)
|
| 761 |
+
>>> img[rr, cc] = 1
|
| 762 |
+
>>> img
|
| 763 |
+
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 764 |
+
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
|
| 765 |
+
[0, 0, 0, 1, 1, 0, 0, 0, 0, 0],
|
| 766 |
+
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
|
| 767 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 768 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 769 |
+
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0],
|
| 770 |
+
[0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
|
| 771 |
+
[0, 0, 0, 0, 0, 0, 0, 1, 1, 0],
|
| 772 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
|
| 773 |
+
"""
|
| 774 |
+
return _bezier_curve(r0, c0, r1, c1, r2, c2, weight, shape)
|
| 775 |
+
|
| 776 |
+
|
| 777 |
+
def rectangle(start, end=None, extent=None, shape=None):
|
| 778 |
+
"""Generate coordinates of pixels within a rectangle.
|
| 779 |
+
|
| 780 |
+
Parameters
|
| 781 |
+
----------
|
| 782 |
+
start : tuple
|
| 783 |
+
Origin point of the rectangle, e.g., ``([plane,] row, column)``.
|
| 784 |
+
end : tuple
|
| 785 |
+
End point of the rectangle ``([plane,] row, column)``.
|
| 786 |
+
For a 2D matrix, the slice defined by the rectangle is
|
| 787 |
+
``[start:(end+1)]``.
|
| 788 |
+
Either `end` or `extent` must be specified.
|
| 789 |
+
extent : tuple
|
| 790 |
+
The extent (size) of the drawn rectangle. E.g.,
|
| 791 |
+
``([num_planes,] num_rows, num_cols)``.
|
| 792 |
+
Either `end` or `extent` must be specified.
|
| 793 |
+
A negative extent is valid, and will result in a rectangle
|
| 794 |
+
going along the opposite direction. If extent is negative, the
|
| 795 |
+
`start` point is not included.
|
| 796 |
+
shape : tuple, optional
|
| 797 |
+
Image shape used to determine the maximum bounds of the output
|
| 798 |
+
coordinates. This is useful for clipping rectangles that exceed
|
| 799 |
+
the image size. By default, no clipping is done.
|
| 800 |
+
|
| 801 |
+
Returns
|
| 802 |
+
-------
|
| 803 |
+
coords : array of int, shape (Ndim, Npoints)
|
| 804 |
+
The coordinates of all pixels in the rectangle.
|
| 805 |
+
|
| 806 |
+
Notes
|
| 807 |
+
-----
|
| 808 |
+
This function can be applied to N-dimensional images, by passing `start` and
|
| 809 |
+
`end` or `extent` as tuples of length N.
|
| 810 |
+
|
| 811 |
+
Examples
|
| 812 |
+
--------
|
| 813 |
+
>>> import numpy as np
|
| 814 |
+
>>> from skimage.draw import rectangle
|
| 815 |
+
>>> img = np.zeros((5, 5), dtype=np.uint8)
|
| 816 |
+
>>> start = (1, 1)
|
| 817 |
+
>>> extent = (3, 3)
|
| 818 |
+
>>> rr, cc = rectangle(start, extent=extent, shape=img.shape)
|
| 819 |
+
>>> img[rr, cc] = 1
|
| 820 |
+
>>> img
|
| 821 |
+
array([[0, 0, 0, 0, 0],
|
| 822 |
+
[0, 1, 1, 1, 0],
|
| 823 |
+
[0, 1, 1, 1, 0],
|
| 824 |
+
[0, 1, 1, 1, 0],
|
| 825 |
+
[0, 0, 0, 0, 0]], dtype=uint8)
|
| 826 |
+
|
| 827 |
+
|
| 828 |
+
>>> img = np.zeros((5, 5), dtype=np.uint8)
|
| 829 |
+
>>> start = (0, 1)
|
| 830 |
+
>>> end = (3, 3)
|
| 831 |
+
>>> rr, cc = rectangle(start, end=end, shape=img.shape)
|
| 832 |
+
>>> img[rr, cc] = 1
|
| 833 |
+
>>> img
|
| 834 |
+
array([[0, 1, 1, 1, 0],
|
| 835 |
+
[0, 1, 1, 1, 0],
|
| 836 |
+
[0, 1, 1, 1, 0],
|
| 837 |
+
[0, 1, 1, 1, 0],
|
| 838 |
+
[0, 0, 0, 0, 0]], dtype=uint8)
|
| 839 |
+
|
| 840 |
+
>>> import numpy as np
|
| 841 |
+
>>> from skimage.draw import rectangle
|
| 842 |
+
>>> img = np.zeros((6, 6), dtype=np.uint8)
|
| 843 |
+
>>> start = (3, 3)
|
| 844 |
+
>>>
|
| 845 |
+
>>> rr, cc = rectangle(start, extent=(2, 2))
|
| 846 |
+
>>> img[rr, cc] = 1
|
| 847 |
+
>>> rr, cc = rectangle(start, extent=(-2, 2))
|
| 848 |
+
>>> img[rr, cc] = 2
|
| 849 |
+
>>> rr, cc = rectangle(start, extent=(-2, -2))
|
| 850 |
+
>>> img[rr, cc] = 3
|
| 851 |
+
>>> rr, cc = rectangle(start, extent=(2, -2))
|
| 852 |
+
>>> img[rr, cc] = 4
|
| 853 |
+
>>> print(img)
|
| 854 |
+
[[0 0 0 0 0 0]
|
| 855 |
+
[0 3 3 2 2 0]
|
| 856 |
+
[0 3 3 2 2 0]
|
| 857 |
+
[0 4 4 1 1 0]
|
| 858 |
+
[0 4 4 1 1 0]
|
| 859 |
+
[0 0 0 0 0 0]]
|
| 860 |
+
|
| 861 |
+
"""
|
| 862 |
+
tl, br = _rectangle_slice(start=start, end=end, extent=extent)
|
| 863 |
+
|
| 864 |
+
if shape is not None:
|
| 865 |
+
n_dim = len(start)
|
| 866 |
+
br = np.minimum(shape[0:n_dim], br)
|
| 867 |
+
tl = np.maximum(np.zeros_like(shape[0:n_dim]), tl)
|
| 868 |
+
coords = np.meshgrid(*[np.arange(st, en) for st, en in zip(tuple(tl), tuple(br))])
|
| 869 |
+
return coords
|
| 870 |
+
|
| 871 |
+
|
| 872 |
+
@require("matplotlib", ">=3.3")
|
| 873 |
+
def rectangle_perimeter(start, end=None, extent=None, shape=None, clip=False):
|
| 874 |
+
"""Generate coordinates of pixels that are exactly around a rectangle.
|
| 875 |
+
|
| 876 |
+
Parameters
|
| 877 |
+
----------
|
| 878 |
+
start : tuple
|
| 879 |
+
Origin point of the inner rectangle, e.g., ``(row, column)``.
|
| 880 |
+
end : tuple
|
| 881 |
+
End point of the inner rectangle ``(row, column)``.
|
| 882 |
+
For a 2D matrix, the slice defined by inner the rectangle is
|
| 883 |
+
``[start:(end+1)]``.
|
| 884 |
+
Either `end` or `extent` must be specified.
|
| 885 |
+
extent : tuple
|
| 886 |
+
The extent (size) of the inner rectangle. E.g.,
|
| 887 |
+
``(num_rows, num_cols)``.
|
| 888 |
+
Either `end` or `extent` must be specified.
|
| 889 |
+
Negative extents are permitted. See `rectangle` to better
|
| 890 |
+
understand how they behave.
|
| 891 |
+
shape : tuple, optional
|
| 892 |
+
Image shape used to determine the maximum bounds of the output
|
| 893 |
+
coordinates. This is useful for clipping perimeters that exceed
|
| 894 |
+
the image size. By default, no clipping is done. Must be at least
|
| 895 |
+
length 2. Only the first two values are used to determine the extent of
|
| 896 |
+
the input image.
|
| 897 |
+
clip : bool, optional
|
| 898 |
+
Whether to clip the perimeter to the provided shape. If this is set
|
| 899 |
+
to True, the drawn figure will always be a closed polygon with all
|
| 900 |
+
edges visible.
|
| 901 |
+
|
| 902 |
+
Returns
|
| 903 |
+
-------
|
| 904 |
+
coords : array of int, shape (2, Npoints)
|
| 905 |
+
The coordinates of all pixels in the rectangle.
|
| 906 |
+
|
| 907 |
+
Examples
|
| 908 |
+
--------
|
| 909 |
+
.. testsetup::
|
| 910 |
+
>>> import pytest; _ = pytest.importorskip('matplotlib')
|
| 911 |
+
|
| 912 |
+
>>> import numpy as np
|
| 913 |
+
>>> from skimage.draw import rectangle_perimeter
|
| 914 |
+
>>> img = np.zeros((5, 6), dtype=np.uint8)
|
| 915 |
+
>>> start = (2, 3)
|
| 916 |
+
>>> end = (3, 4)
|
| 917 |
+
>>> rr, cc = rectangle_perimeter(start, end=end, shape=img.shape)
|
| 918 |
+
>>> img[rr, cc] = 1
|
| 919 |
+
>>> img
|
| 920 |
+
array([[0, 0, 0, 0, 0, 0],
|
| 921 |
+
[0, 0, 1, 1, 1, 1],
|
| 922 |
+
[0, 0, 1, 0, 0, 1],
|
| 923 |
+
[0, 0, 1, 0, 0, 1],
|
| 924 |
+
[0, 0, 1, 1, 1, 1]], dtype=uint8)
|
| 925 |
+
|
| 926 |
+
>>> img = np.zeros((5, 5), dtype=np.uint8)
|
| 927 |
+
>>> r, c = rectangle_perimeter(start, (10, 10), shape=img.shape, clip=True)
|
| 928 |
+
>>> img[r, c] = 1
|
| 929 |
+
>>> img
|
| 930 |
+
array([[0, 0, 0, 0, 0],
|
| 931 |
+
[0, 0, 1, 1, 1],
|
| 932 |
+
[0, 0, 1, 0, 1],
|
| 933 |
+
[0, 0, 1, 0, 1],
|
| 934 |
+
[0, 0, 1, 1, 1]], dtype=uint8)
|
| 935 |
+
|
| 936 |
+
"""
|
| 937 |
+
top_left, bottom_right = _rectangle_slice(start=start, end=end, extent=extent)
|
| 938 |
+
|
| 939 |
+
top_left -= 1
|
| 940 |
+
r = [top_left[0], top_left[0], bottom_right[0], bottom_right[0], top_left[0]]
|
| 941 |
+
c = [top_left[1], bottom_right[1], bottom_right[1], top_left[1], top_left[1]]
|
| 942 |
+
return polygon_perimeter(r, c, shape=shape, clip=clip)
|
| 943 |
+
|
| 944 |
+
|
| 945 |
+
def _rectangle_slice(start, end=None, extent=None):
|
| 946 |
+
"""Return the slice ``(top_left, bottom_right)`` of the rectangle.
|
| 947 |
+
|
| 948 |
+
Returns
|
| 949 |
+
-------
|
| 950 |
+
(top_left, bottom_right)
|
| 951 |
+
The slice you would need to select the region in the rectangle defined
|
| 952 |
+
by the parameters.
|
| 953 |
+
Select it like:
|
| 954 |
+
|
| 955 |
+
``rect[top_left[0]:bottom_right[0], top_left[1]:bottom_right[1]]``
|
| 956 |
+
"""
|
| 957 |
+
if end is None and extent is None:
|
| 958 |
+
raise ValueError("Either `end` or `extent` must be given.")
|
| 959 |
+
if end is not None and extent is not None:
|
| 960 |
+
raise ValueError("Cannot provide both `end` and `extent`.")
|
| 961 |
+
|
| 962 |
+
if extent is not None:
|
| 963 |
+
end = np.asarray(start) + np.asarray(extent)
|
| 964 |
+
top_left = np.minimum(start, end)
|
| 965 |
+
bottom_right = np.maximum(start, end)
|
| 966 |
+
|
| 967 |
+
top_left = np.round(top_left).astype(int)
|
| 968 |
+
bottom_right = np.round(bottom_right).astype(int)
|
| 969 |
+
|
| 970 |
+
if extent is None:
|
| 971 |
+
bottom_right += 1
|
| 972 |
+
|
| 973 |
+
return (top_left, bottom_right)
|
infer_4_47_1/lib/python3.10/site-packages/skimage/draw/draw3d.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from scipy.special import elliprg
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def ellipsoid(a, b, c, spacing=(1.0, 1.0, 1.0), levelset=False):
|
| 6 |
+
"""Generate ellipsoid for given semi-axis lengths.
|
| 7 |
+
|
| 8 |
+
The respective semi-axis lengths are given along three dimensions in
|
| 9 |
+
Cartesian coordinates. Each dimension may use a different grid spacing.
|
| 10 |
+
|
| 11 |
+
Parameters
|
| 12 |
+
----------
|
| 13 |
+
a : float
|
| 14 |
+
Length of semi-axis along x-axis.
|
| 15 |
+
b : float
|
| 16 |
+
Length of semi-axis along y-axis.
|
| 17 |
+
c : float
|
| 18 |
+
Length of semi-axis along z-axis.
|
| 19 |
+
spacing : 3-tuple of floats
|
| 20 |
+
Grid spacing in three spatial dimensions.
|
| 21 |
+
levelset : bool
|
| 22 |
+
If True, returns the level set for this ellipsoid (signed level
|
| 23 |
+
set about zero, with positive denoting interior) as np.float64.
|
| 24 |
+
False returns a binarized version of said level set.
|
| 25 |
+
|
| 26 |
+
Returns
|
| 27 |
+
-------
|
| 28 |
+
ellipsoid : (M, N, P) array
|
| 29 |
+
Ellipsoid centered in a correctly sized array for given `spacing`.
|
| 30 |
+
Boolean dtype unless `levelset=True`, in which case a float array is
|
| 31 |
+
returned with the level set above 0.0 representing the ellipsoid.
|
| 32 |
+
|
| 33 |
+
"""
|
| 34 |
+
if (a <= 0) or (b <= 0) or (c <= 0):
|
| 35 |
+
raise ValueError('Parameters a, b, and c must all be > 0')
|
| 36 |
+
|
| 37 |
+
offset = np.r_[1, 1, 1] * np.r_[spacing]
|
| 38 |
+
|
| 39 |
+
# Calculate limits, and ensure output volume is odd & symmetric
|
| 40 |
+
low = np.ceil(-np.r_[a, b, c] - offset)
|
| 41 |
+
high = np.floor(np.r_[a, b, c] + offset + 1)
|
| 42 |
+
|
| 43 |
+
for dim in range(3):
|
| 44 |
+
if (high[dim] - low[dim]) % 2 == 0:
|
| 45 |
+
low[dim] -= 1
|
| 46 |
+
num = np.arange(low[dim], high[dim], spacing[dim])
|
| 47 |
+
if 0 not in num:
|
| 48 |
+
low[dim] -= np.max(num[num < 0])
|
| 49 |
+
|
| 50 |
+
# Generate (anisotropic) spatial grid
|
| 51 |
+
x, y, z = np.mgrid[
|
| 52 |
+
low[0] : high[0] : spacing[0],
|
| 53 |
+
low[1] : high[1] : spacing[1],
|
| 54 |
+
low[2] : high[2] : spacing[2],
|
| 55 |
+
]
|
| 56 |
+
|
| 57 |
+
if not levelset:
|
| 58 |
+
arr = ((x / float(a)) ** 2 + (y / float(b)) ** 2 + (z / float(c)) ** 2) <= 1
|
| 59 |
+
else:
|
| 60 |
+
arr = ((x / float(a)) ** 2 + (y / float(b)) ** 2 + (z / float(c)) ** 2) - 1
|
| 61 |
+
|
| 62 |
+
return arr
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def ellipsoid_stats(a, b, c):
|
| 66 |
+
"""Calculate analytical volume and surface area of an ellipsoid.
|
| 67 |
+
|
| 68 |
+
The surface area of an ellipsoid is given by
|
| 69 |
+
|
| 70 |
+
.. math:: S=4\\pi b c R_G\\!\\left(1, \\frac{a^2}{b^2}, \\frac{a^2}{c^2}\\right)
|
| 71 |
+
|
| 72 |
+
where :math:`R_G` is Carlson's completely symmetric elliptic integral of
|
| 73 |
+
the second kind [1]_. The latter is implemented as
|
| 74 |
+
:py:func:`scipy.special.elliprg`.
|
| 75 |
+
|
| 76 |
+
Parameters
|
| 77 |
+
----------
|
| 78 |
+
a : float
|
| 79 |
+
Length of semi-axis along x-axis.
|
| 80 |
+
b : float
|
| 81 |
+
Length of semi-axis along y-axis.
|
| 82 |
+
c : float
|
| 83 |
+
Length of semi-axis along z-axis.
|
| 84 |
+
|
| 85 |
+
Returns
|
| 86 |
+
-------
|
| 87 |
+
vol : float
|
| 88 |
+
Calculated volume of ellipsoid.
|
| 89 |
+
surf : float
|
| 90 |
+
Calculated surface area of ellipsoid.
|
| 91 |
+
|
| 92 |
+
References
|
| 93 |
+
----------
|
| 94 |
+
.. [1] Paul Masson (2020). Surface Area of an Ellipsoid.
|
| 95 |
+
https://analyticphysics.com/Mathematical%20Methods/Surface%20Area%20of%20an%20Ellipsoid.htm
|
| 96 |
+
|
| 97 |
+
"""
|
| 98 |
+
if (a <= 0) or (b <= 0) or (c <= 0):
|
| 99 |
+
raise ValueError('Parameters a, b, and c must all be > 0')
|
| 100 |
+
|
| 101 |
+
# Volume
|
| 102 |
+
vol = 4 / 3.0 * np.pi * a * b * c
|
| 103 |
+
|
| 104 |
+
# Surface area
|
| 105 |
+
surf = 3 * vol * elliprg(1 / a**2, 1 / b**2, 1 / c**2)
|
| 106 |
+
|
| 107 |
+
return vol, surf
|
infer_4_47_1/lib/python3.10/site-packages/skimage/draw/draw_nd.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def _round_safe(coords):
|
| 5 |
+
"""Round coords while ensuring successive values are less than 1 apart.
|
| 6 |
+
|
| 7 |
+
When rounding coordinates for `line_nd`, we want coordinates that are less
|
| 8 |
+
than 1 apart (always the case, by design) to remain less than one apart.
|
| 9 |
+
However, NumPy rounds values to the nearest *even* integer, so:
|
| 10 |
+
|
| 11 |
+
>>> np.round([0.5, 1.5, 2.5, 3.5, 4.5])
|
| 12 |
+
array([0., 2., 2., 4., 4.])
|
| 13 |
+
|
| 14 |
+
So, for our application, we detect whether the above case occurs, and use
|
| 15 |
+
``np.floor`` if so. It is sufficient to detect that the first coordinate
|
| 16 |
+
falls on 0.5 and that the second coordinate is 1.0 apart, since we assume
|
| 17 |
+
by construction that the inter-point distance is less than or equal to 1
|
| 18 |
+
and that all successive points are equidistant.
|
| 19 |
+
|
| 20 |
+
Parameters
|
| 21 |
+
----------
|
| 22 |
+
coords : 1D array of float
|
| 23 |
+
The coordinates array. We assume that all successive values are
|
| 24 |
+
equidistant (``np.all(np.diff(coords) = coords[1] - coords[0])``)
|
| 25 |
+
and that this distance is no more than 1
|
| 26 |
+
(``np.abs(coords[1] - coords[0]) <= 1``).
|
| 27 |
+
|
| 28 |
+
Returns
|
| 29 |
+
-------
|
| 30 |
+
rounded : 1D array of int
|
| 31 |
+
The array correctly rounded for an indexing operation, such that no
|
| 32 |
+
successive indices will be more than 1 apart.
|
| 33 |
+
|
| 34 |
+
Examples
|
| 35 |
+
--------
|
| 36 |
+
>>> coords0 = np.array([0.5, 1.25, 2., 2.75, 3.5])
|
| 37 |
+
>>> _round_safe(coords0)
|
| 38 |
+
array([0, 1, 2, 3, 4])
|
| 39 |
+
>>> coords1 = np.arange(0.5, 8, 1)
|
| 40 |
+
>>> coords1
|
| 41 |
+
array([0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5])
|
| 42 |
+
>>> _round_safe(coords1)
|
| 43 |
+
array([0, 1, 2, 3, 4, 5, 6, 7])
|
| 44 |
+
"""
|
| 45 |
+
if len(coords) > 1 and coords[0] % 1 == 0.5 and coords[1] - coords[0] == 1:
|
| 46 |
+
_round_function = np.floor
|
| 47 |
+
else:
|
| 48 |
+
_round_function = np.round
|
| 49 |
+
return _round_function(coords).astype(int)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def line_nd(start, stop, *, endpoint=False, integer=True):
|
| 53 |
+
"""Draw a single-pixel thick line in n dimensions.
|
| 54 |
+
|
| 55 |
+
The line produced will be ndim-connected. That is, two subsequent
|
| 56 |
+
pixels in the line will be either direct or diagonal neighbors in
|
| 57 |
+
n dimensions.
|
| 58 |
+
|
| 59 |
+
Parameters
|
| 60 |
+
----------
|
| 61 |
+
start : array-like, shape (N,)
|
| 62 |
+
The start coordinates of the line.
|
| 63 |
+
stop : array-like, shape (N,)
|
| 64 |
+
The end coordinates of the line.
|
| 65 |
+
endpoint : bool, optional
|
| 66 |
+
Whether to include the endpoint in the returned line. Defaults
|
| 67 |
+
to False, which allows for easy drawing of multi-point paths.
|
| 68 |
+
integer : bool, optional
|
| 69 |
+
Whether to round the coordinates to integer. If True (default),
|
| 70 |
+
the returned coordinates can be used to directly index into an
|
| 71 |
+
array. `False` could be used for e.g. vector drawing.
|
| 72 |
+
|
| 73 |
+
Returns
|
| 74 |
+
-------
|
| 75 |
+
coords : tuple of arrays
|
| 76 |
+
The coordinates of points on the line.
|
| 77 |
+
|
| 78 |
+
Examples
|
| 79 |
+
--------
|
| 80 |
+
>>> lin = line_nd((1, 1), (5, 2.5), endpoint=False)
|
| 81 |
+
>>> lin
|
| 82 |
+
(array([1, 2, 3, 4]), array([1, 1, 2, 2]))
|
| 83 |
+
>>> im = np.zeros((6, 5), dtype=int)
|
| 84 |
+
>>> im[lin] = 1
|
| 85 |
+
>>> im
|
| 86 |
+
array([[0, 0, 0, 0, 0],
|
| 87 |
+
[0, 1, 0, 0, 0],
|
| 88 |
+
[0, 1, 0, 0, 0],
|
| 89 |
+
[0, 0, 1, 0, 0],
|
| 90 |
+
[0, 0, 1, 0, 0],
|
| 91 |
+
[0, 0, 0, 0, 0]])
|
| 92 |
+
>>> line_nd([2, 1, 1], [5, 5, 2.5], endpoint=True)
|
| 93 |
+
(array([2, 3, 4, 4, 5]), array([1, 2, 3, 4, 5]), array([1, 1, 2, 2, 2]))
|
| 94 |
+
"""
|
| 95 |
+
start = np.asarray(start)
|
| 96 |
+
stop = np.asarray(stop)
|
| 97 |
+
npoints = int(np.ceil(np.max(np.abs(stop - start))))
|
| 98 |
+
if endpoint:
|
| 99 |
+
npoints += 1
|
| 100 |
+
|
| 101 |
+
coords = np.linspace(start, stop, num=npoints, endpoint=endpoint).T
|
| 102 |
+
if integer:
|
| 103 |
+
for dim in range(len(start)):
|
| 104 |
+
coords[dim, :] = _round_safe(coords[dim, :])
|
| 105 |
+
|
| 106 |
+
coords = coords.astype(int)
|
| 107 |
+
|
| 108 |
+
return tuple(coords)
|
infer_4_47_1/lib/python3.10/site-packages/skimage/draw/tests/__init__.py
ADDED
|
File without changes
|
infer_4_47_1/lib/python3.10/site-packages/skimage/draw/tests/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (176 Bytes). View file
|
|
|
infer_4_47_1/lib/python3.10/site-packages/skimage/draw/tests/__pycache__/test_draw_nd.cpython-310.pyc
ADDED
|
Binary file (1.07 kB). View file
|
|
|
infer_4_47_1/lib/python3.10/site-packages/skimage/draw/tests/__pycache__/test_random_shapes.cpython-310.pyc
ADDED
|
Binary file (5.44 kB). View file
|
|
|
infer_4_47_1/lib/python3.10/site-packages/skimage/draw/tests/test_draw.py
ADDED
|
@@ -0,0 +1,1261 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from numpy.testing import assert_array_equal, assert_equal, assert_almost_equal
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
from skimage._shared.testing import run_in_parallel
|
| 6 |
+
from skimage._shared._dependency_checks import has_mpl
|
| 7 |
+
|
| 8 |
+
from skimage.draw import (
|
| 9 |
+
set_color,
|
| 10 |
+
line,
|
| 11 |
+
line_aa,
|
| 12 |
+
polygon,
|
| 13 |
+
polygon_perimeter,
|
| 14 |
+
disk,
|
| 15 |
+
circle_perimeter,
|
| 16 |
+
circle_perimeter_aa,
|
| 17 |
+
ellipse,
|
| 18 |
+
ellipse_perimeter,
|
| 19 |
+
_bezier_segment,
|
| 20 |
+
bezier_curve,
|
| 21 |
+
rectangle,
|
| 22 |
+
rectangle_perimeter,
|
| 23 |
+
)
|
| 24 |
+
from skimage.measure import regionprops
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def test_set_color():
|
| 28 |
+
img = np.zeros((10, 10))
|
| 29 |
+
|
| 30 |
+
rr, cc = line(0, 0, 0, 30)
|
| 31 |
+
set_color(img, (rr, cc), 1)
|
| 32 |
+
|
| 33 |
+
img_ = np.zeros((10, 10))
|
| 34 |
+
img_[0, :] = 1
|
| 35 |
+
|
| 36 |
+
assert_array_equal(img, img_)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def test_set_color_with_alpha():
|
| 40 |
+
img = np.zeros((10, 10))
|
| 41 |
+
|
| 42 |
+
rr, cc, alpha = line_aa(0, 0, 0, 30)
|
| 43 |
+
set_color(img, (rr, cc), 1, alpha=alpha)
|
| 44 |
+
|
| 45 |
+
# Wrong dimensionality color
|
| 46 |
+
with pytest.raises(ValueError):
|
| 47 |
+
set_color(img, (rr, cc), (255, 0, 0), alpha=alpha)
|
| 48 |
+
|
| 49 |
+
img = np.zeros((10, 10, 3))
|
| 50 |
+
|
| 51 |
+
rr, cc, alpha = line_aa(0, 0, 0, 30)
|
| 52 |
+
set_color(img, (rr, cc), (1, 0, 0), alpha=alpha)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
@run_in_parallel()
|
| 56 |
+
def test_line_horizontal():
|
| 57 |
+
img = np.zeros((10, 10))
|
| 58 |
+
|
| 59 |
+
rr, cc = line(0, 0, 0, 9)
|
| 60 |
+
img[rr, cc] = 1
|
| 61 |
+
|
| 62 |
+
img_ = np.zeros((10, 10))
|
| 63 |
+
img_[0, :] = 1
|
| 64 |
+
|
| 65 |
+
assert_array_equal(img, img_)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def test_line_vertical():
|
| 69 |
+
img = np.zeros((10, 10))
|
| 70 |
+
|
| 71 |
+
rr, cc = line(0, 0, 9, 0)
|
| 72 |
+
img[rr, cc] = 1
|
| 73 |
+
|
| 74 |
+
img_ = np.zeros((10, 10))
|
| 75 |
+
img_[:, 0] = 1
|
| 76 |
+
|
| 77 |
+
assert_array_equal(img, img_)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def test_line_reverse():
|
| 81 |
+
img = np.zeros((10, 10))
|
| 82 |
+
|
| 83 |
+
rr, cc = line(0, 9, 0, 0)
|
| 84 |
+
img[rr, cc] = 1
|
| 85 |
+
|
| 86 |
+
img_ = np.zeros((10, 10))
|
| 87 |
+
img_[0, :] = 1
|
| 88 |
+
|
| 89 |
+
assert_array_equal(img, img_)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def test_line_diag():
|
| 93 |
+
img = np.zeros((5, 5))
|
| 94 |
+
|
| 95 |
+
rr, cc = line(0, 0, 4, 4)
|
| 96 |
+
img[rr, cc] = 1
|
| 97 |
+
|
| 98 |
+
img_ = np.eye(5)
|
| 99 |
+
|
| 100 |
+
assert_array_equal(img, img_)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def test_line_aa_horizontal():
|
| 104 |
+
img = np.zeros((10, 10))
|
| 105 |
+
|
| 106 |
+
rr, cc, val = line_aa(0, 0, 0, 9)
|
| 107 |
+
set_color(img, (rr, cc), 1, alpha=val)
|
| 108 |
+
|
| 109 |
+
img_ = np.zeros((10, 10))
|
| 110 |
+
img_[0, :] = 1
|
| 111 |
+
|
| 112 |
+
assert_array_equal(img, img_)
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def test_line_aa_vertical():
|
| 116 |
+
img = np.zeros((10, 10))
|
| 117 |
+
|
| 118 |
+
rr, cc, val = line_aa(0, 0, 9, 0)
|
| 119 |
+
img[rr, cc] = val
|
| 120 |
+
|
| 121 |
+
img_ = np.zeros((10, 10))
|
| 122 |
+
img_[:, 0] = 1
|
| 123 |
+
|
| 124 |
+
assert_array_equal(img, img_)
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def test_line_aa_diagonal():
|
| 128 |
+
img = np.zeros((10, 10))
|
| 129 |
+
|
| 130 |
+
rr, cc, val = line_aa(0, 0, 9, 6)
|
| 131 |
+
img[rr, cc] = 1
|
| 132 |
+
|
| 133 |
+
# Check that each pixel belonging to line,
|
| 134 |
+
# also belongs to line_aa
|
| 135 |
+
r, c = line(0, 0, 9, 6)
|
| 136 |
+
for r_i, c_i in zip(r, c):
|
| 137 |
+
assert_equal(img[r_i, c_i], 1)
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def test_line_equal_aliasing_horizontally_vertically():
|
| 141 |
+
img0 = np.zeros((25, 25))
|
| 142 |
+
img1 = np.zeros((25, 25))
|
| 143 |
+
|
| 144 |
+
# Near-horizontal line
|
| 145 |
+
rr, cc, val = line_aa(10, 2, 12, 20)
|
| 146 |
+
img0[rr, cc] = val
|
| 147 |
+
|
| 148 |
+
# Near-vertical (transpose of prior)
|
| 149 |
+
rr, cc, val = line_aa(2, 10, 20, 12)
|
| 150 |
+
img1[rr, cc] = val
|
| 151 |
+
|
| 152 |
+
# Difference - should be zero
|
| 153 |
+
assert_array_equal(img0, img1.T)
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def test_polygon_rectangle():
|
| 157 |
+
img = np.zeros((10, 10), 'uint8')
|
| 158 |
+
poly = np.array(((1, 1), (4, 1), (4, 4), (1, 4), (1, 1)))
|
| 159 |
+
|
| 160 |
+
rr, cc = polygon(poly[:, 0], poly[:, 1])
|
| 161 |
+
img[rr, cc] = 1
|
| 162 |
+
|
| 163 |
+
img_ = np.zeros((10, 10), 'uint8')
|
| 164 |
+
img_[1:5, 1:5] = 1
|
| 165 |
+
|
| 166 |
+
assert_array_equal(img, img_)
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
def test_polygon_rectangle_angular():
|
| 170 |
+
img = np.zeros((10, 10), 'uint8')
|
| 171 |
+
poly = np.array(((0, 3), (4, 7), (7, 4), (3, 0), (0, 3)))
|
| 172 |
+
|
| 173 |
+
rr, cc = polygon(poly[:, 0], poly[:, 1])
|
| 174 |
+
img[rr, cc] = 1
|
| 175 |
+
|
| 176 |
+
img_ = np.array(
|
| 177 |
+
[
|
| 178 |
+
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
|
| 179 |
+
[0, 0, 1, 1, 1, 0, 0, 0, 0, 0],
|
| 180 |
+
[0, 1, 1, 1, 1, 1, 0, 0, 0, 0],
|
| 181 |
+
[1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
|
| 182 |
+
[0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
| 183 |
+
[0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
|
| 184 |
+
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
|
| 185 |
+
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
|
| 186 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 187 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 188 |
+
],
|
| 189 |
+
'uint8',
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
assert_array_equal(img, img_)
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
def test_polygon_parallelogram():
|
| 196 |
+
img = np.zeros((10, 10), 'uint8')
|
| 197 |
+
poly = np.array(((1, 1), (5, 1), (7, 6), (3, 6), (1, 1)))
|
| 198 |
+
|
| 199 |
+
rr, cc = polygon(poly[:, 0], poly[:, 1])
|
| 200 |
+
img[rr, cc] = 1
|
| 201 |
+
|
| 202 |
+
img_ = np.array(
|
| 203 |
+
[
|
| 204 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 205 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 206 |
+
[0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
|
| 207 |
+
[0, 1, 1, 1, 1, 1, 1, 0, 0, 0],
|
| 208 |
+
[0, 1, 1, 1, 1, 1, 1, 0, 0, 0],
|
| 209 |
+
[0, 1, 1, 1, 1, 1, 1, 0, 0, 0],
|
| 210 |
+
[0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
|
| 211 |
+
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
|
| 212 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 213 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 214 |
+
],
|
| 215 |
+
'uint8',
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
assert_array_equal(img, img_)
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
def test_polygon_exceed():
|
| 222 |
+
img = np.zeros((10, 10), 'uint8')
|
| 223 |
+
poly = np.array(((1, -1), (100, -1), (100, 100), (1, 100), (1, 1)))
|
| 224 |
+
|
| 225 |
+
rr, cc = polygon(poly[:, 0], poly[:, 1], img.shape)
|
| 226 |
+
img[rr, cc] = 1
|
| 227 |
+
|
| 228 |
+
img_ = np.zeros((10, 10))
|
| 229 |
+
img_[1:, :] = 1
|
| 230 |
+
|
| 231 |
+
assert_array_equal(img, img_)
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
def test_polygon_0d_input():
|
| 235 |
+
# Bug reported in #4938: 0d input causes segfault.
|
| 236 |
+
rr, cc = polygon(0, 1)
|
| 237 |
+
|
| 238 |
+
assert rr.size == cc.size == 1
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
def test_disk():
|
| 242 |
+
img = np.zeros((15, 15), 'uint8')
|
| 243 |
+
|
| 244 |
+
rr, cc = disk((7, 7), 6)
|
| 245 |
+
img[rr, cc] = 1
|
| 246 |
+
|
| 247 |
+
img_ = np.array(
|
| 248 |
+
[
|
| 249 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 250 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 251 |
+
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
|
| 252 |
+
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
|
| 253 |
+
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
| 254 |
+
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
| 255 |
+
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
| 256 |
+
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
| 257 |
+
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
| 258 |
+
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
| 259 |
+
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
| 260 |
+
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
|
| 261 |
+
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
|
| 262 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 263 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 264 |
+
]
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
assert_array_equal(img, img_)
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
def test_circle_perimeter_bresenham():
|
| 271 |
+
img = np.zeros((15, 15), 'uint8')
|
| 272 |
+
rr, cc = circle_perimeter(7, 7, 0, method='bresenham')
|
| 273 |
+
img[rr, cc] = 1
|
| 274 |
+
assert np.sum(img) == 1
|
| 275 |
+
assert img[7][7] == 1
|
| 276 |
+
|
| 277 |
+
img = np.zeros((17, 15), 'uint8')
|
| 278 |
+
rr, cc = circle_perimeter(7, 7, 7, method='bresenham')
|
| 279 |
+
img[rr, cc] = 1
|
| 280 |
+
img_ = np.array(
|
| 281 |
+
[
|
| 282 |
+
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
|
| 283 |
+
[0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0],
|
| 284 |
+
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
|
| 285 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
| 286 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
| 287 |
+
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
| 288 |
+
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
| 289 |
+
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
| 290 |
+
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
| 291 |
+
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
| 292 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
| 293 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
| 294 |
+
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
|
| 295 |
+
[0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0],
|
| 296 |
+
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
|
| 297 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 298 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 299 |
+
]
|
| 300 |
+
)
|
| 301 |
+
assert_array_equal(img, img_)
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
def test_circle_perimeter_bresenham_shape():
|
| 305 |
+
img = np.zeros((15, 20), 'uint8')
|
| 306 |
+
rr, cc = circle_perimeter(7, 10, 9, method='bresenham', shape=(15, 20))
|
| 307 |
+
img[rr, cc] = 1
|
| 308 |
+
shift = 5
|
| 309 |
+
img_ = np.zeros((15 + 2 * shift, 20), 'uint8')
|
| 310 |
+
rr, cc = circle_perimeter(7 + shift, 10, 9, method='bresenham', shape=None)
|
| 311 |
+
img_[rr, cc] = 1
|
| 312 |
+
assert_array_equal(img, img_[shift:-shift, :])
|
| 313 |
+
|
| 314 |
+
|
| 315 |
+
def test_circle_perimeter_andres():
|
| 316 |
+
img = np.zeros((15, 15), 'uint8')
|
| 317 |
+
rr, cc = circle_perimeter(7, 7, 0, method='andres')
|
| 318 |
+
img[rr, cc] = 1
|
| 319 |
+
assert np.sum(img) == 1
|
| 320 |
+
assert img[7][7] == 1
|
| 321 |
+
|
| 322 |
+
img = np.zeros((17, 15), 'uint8')
|
| 323 |
+
rr, cc = circle_perimeter(7, 7, 7, method='andres')
|
| 324 |
+
img[rr, cc] = 1
|
| 325 |
+
img_ = np.array(
|
| 326 |
+
[
|
| 327 |
+
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
|
| 328 |
+
[0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
|
| 329 |
+
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],
|
| 330 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
| 331 |
+
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
|
| 332 |
+
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
| 333 |
+
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
| 334 |
+
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
| 335 |
+
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
| 336 |
+
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
| 337 |
+
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
|
| 338 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
| 339 |
+
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],
|
| 340 |
+
[0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
|
| 341 |
+
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
|
| 342 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 343 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 344 |
+
]
|
| 345 |
+
)
|
| 346 |
+
assert_array_equal(img, img_)
|
| 347 |
+
|
| 348 |
+
|
| 349 |
+
def test_circle_perimeter_aa():
|
| 350 |
+
img = np.zeros((15, 15), 'uint8')
|
| 351 |
+
rr, cc, val = circle_perimeter_aa(7, 7, 0)
|
| 352 |
+
img[rr, cc] = 1
|
| 353 |
+
assert np.sum(img) == 1
|
| 354 |
+
assert img[7][7] == 1
|
| 355 |
+
|
| 356 |
+
img = np.zeros((17, 17), 'uint8')
|
| 357 |
+
rr, cc, val = circle_perimeter_aa(8, 8, 7)
|
| 358 |
+
img[rr, cc] = val * 255
|
| 359 |
+
# fmt: off
|
| 360 |
+
img_ = np.array(
|
| 361 |
+
[[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 362 |
+
[ 0, 0, 0, 0, 0, 82, 180, 236, 255, 236, 180, 82, 0, 0, 0, 0, 0],
|
| 363 |
+
[ 0, 0, 0, 0, 189, 172, 74, 18, 0, 18, 74, 172, 189, 0, 0, 0, 0],
|
| 364 |
+
[ 0, 0, 0, 229, 25, 0, 0, 0, 0, 0, 0, 0, 25, 229, 0, 0, 0],
|
| 365 |
+
[ 0, 0, 189, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 189, 0, 0],
|
| 366 |
+
[ 0, 82, 172, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 172, 82, 0],
|
| 367 |
+
[ 0, 180, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 74, 180, 0],
|
| 368 |
+
[ 0, 236, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 236, 0],
|
| 369 |
+
[ 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0],
|
| 370 |
+
[ 0, 236, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 236, 0],
|
| 371 |
+
[ 0, 180, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 74, 180, 0],
|
| 372 |
+
[ 0, 82, 172, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 172, 82, 0],
|
| 373 |
+
[ 0, 0, 189, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 189, 0, 0],
|
| 374 |
+
[ 0, 0, 0, 229, 25, 0, 0, 0, 0, 0, 0, 0, 25, 229, 0, 0, 0],
|
| 375 |
+
[ 0, 0, 0, 0, 189, 172, 74, 18, 0, 18, 74, 172, 189, 0, 0, 0, 0],
|
| 376 |
+
[ 0, 0, 0, 0, 0, 82, 180, 236, 255, 236, 180, 82, 0, 0, 0, 0, 0],
|
| 377 |
+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
|
| 378 |
+
)
|
| 379 |
+
# fmt: on
|
| 380 |
+
assert_array_equal(img, img_)
|
| 381 |
+
|
| 382 |
+
|
| 383 |
+
def test_circle_perimeter_aa_shape():
|
| 384 |
+
img = np.zeros((15, 20), 'uint8')
|
| 385 |
+
rr, cc, val = circle_perimeter_aa(7, 10, 9, shape=(15, 20))
|
| 386 |
+
img[rr, cc] = val * 255
|
| 387 |
+
|
| 388 |
+
shift = 5
|
| 389 |
+
img_ = np.zeros((15 + 2 * shift, 20), 'uint8')
|
| 390 |
+
rr, cc, val = circle_perimeter_aa(7 + shift, 10, 9, shape=None)
|
| 391 |
+
img_[rr, cc] = val * 255
|
| 392 |
+
assert_array_equal(img, img_[shift:-shift, :])
|
| 393 |
+
|
| 394 |
+
|
| 395 |
+
def test_ellipse_trivial():
|
| 396 |
+
img = np.zeros((2, 2), 'uint8')
|
| 397 |
+
rr, cc = ellipse(0.5, 0.5, 0.5, 0.5)
|
| 398 |
+
img[rr, cc] = 1
|
| 399 |
+
img_correct = np.array([[0, 0], [0, 0]])
|
| 400 |
+
assert_array_equal(img, img_correct)
|
| 401 |
+
|
| 402 |
+
img = np.zeros((2, 2), 'uint8')
|
| 403 |
+
rr, cc = ellipse(0.5, 0.5, 1.1, 1.1)
|
| 404 |
+
img[rr, cc] = 1
|
| 405 |
+
img_correct = np.array(
|
| 406 |
+
[
|
| 407 |
+
[1, 1],
|
| 408 |
+
[1, 1],
|
| 409 |
+
]
|
| 410 |
+
)
|
| 411 |
+
assert_array_equal(img, img_correct)
|
| 412 |
+
|
| 413 |
+
img = np.zeros((3, 3), 'uint8')
|
| 414 |
+
rr, cc = ellipse(1, 1, 0.9, 0.9)
|
| 415 |
+
img[rr, cc] = 1
|
| 416 |
+
img_correct = np.array(
|
| 417 |
+
[
|
| 418 |
+
[0, 0, 0],
|
| 419 |
+
[0, 1, 0],
|
| 420 |
+
[0, 0, 0],
|
| 421 |
+
]
|
| 422 |
+
)
|
| 423 |
+
assert_array_equal(img, img_correct)
|
| 424 |
+
|
| 425 |
+
img = np.zeros((3, 3), 'uint8')
|
| 426 |
+
rr, cc = ellipse(1, 1, 1.1, 1.1)
|
| 427 |
+
img[rr, cc] = 1
|
| 428 |
+
img_correct = np.array(
|
| 429 |
+
[
|
| 430 |
+
[0, 1, 0],
|
| 431 |
+
[1, 1, 1],
|
| 432 |
+
[0, 1, 0],
|
| 433 |
+
]
|
| 434 |
+
)
|
| 435 |
+
assert_array_equal(img, img_correct)
|
| 436 |
+
|
| 437 |
+
img = np.zeros((3, 3), 'uint8')
|
| 438 |
+
rr, cc = ellipse(1, 1, 1.5, 1.5)
|
| 439 |
+
img[rr, cc] = 1
|
| 440 |
+
img_correct = np.array(
|
| 441 |
+
[
|
| 442 |
+
[1, 1, 1],
|
| 443 |
+
[1, 1, 1],
|
| 444 |
+
[1, 1, 1],
|
| 445 |
+
]
|
| 446 |
+
)
|
| 447 |
+
assert_array_equal(img, img_correct)
|
| 448 |
+
|
| 449 |
+
|
| 450 |
+
def test_ellipse_generic():
|
| 451 |
+
img = np.zeros((4, 4), 'uint8')
|
| 452 |
+
rr, cc = ellipse(1.5, 1.5, 1.1, 1.7)
|
| 453 |
+
img[rr, cc] = 1
|
| 454 |
+
img_ = np.array(
|
| 455 |
+
[
|
| 456 |
+
[0, 0, 0, 0],
|
| 457 |
+
[1, 1, 1, 1],
|
| 458 |
+
[1, 1, 1, 1],
|
| 459 |
+
[0, 0, 0, 0],
|
| 460 |
+
]
|
| 461 |
+
)
|
| 462 |
+
assert_array_equal(img, img_)
|
| 463 |
+
|
| 464 |
+
img = np.zeros((5, 5), 'uint8')
|
| 465 |
+
rr, cc = ellipse(2, 2, 1.7, 1.7)
|
| 466 |
+
img[rr, cc] = 1
|
| 467 |
+
img_ = np.array(
|
| 468 |
+
[
|
| 469 |
+
[0, 0, 0, 0, 0],
|
| 470 |
+
[0, 1, 1, 1, 0],
|
| 471 |
+
[0, 1, 1, 1, 0],
|
| 472 |
+
[0, 1, 1, 1, 0],
|
| 473 |
+
[0, 0, 0, 0, 0],
|
| 474 |
+
]
|
| 475 |
+
)
|
| 476 |
+
assert_array_equal(img, img_)
|
| 477 |
+
|
| 478 |
+
img = np.zeros((10, 10), 'uint8')
|
| 479 |
+
rr, cc = ellipse(5, 5, 3, 4)
|
| 480 |
+
img[rr, cc] = 1
|
| 481 |
+
img_ = np.array(
|
| 482 |
+
[
|
| 483 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 484 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 485 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 486 |
+
[0, 0, 0, 1, 1, 1, 1, 1, 0, 0],
|
| 487 |
+
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
|
| 488 |
+
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
|
| 489 |
+
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
|
| 490 |
+
[0, 0, 0, 1, 1, 1, 1, 1, 0, 0],
|
| 491 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 492 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 493 |
+
]
|
| 494 |
+
)
|
| 495 |
+
assert_array_equal(img, img_)
|
| 496 |
+
|
| 497 |
+
img = np.zeros((10, 10), 'uint8')
|
| 498 |
+
rr, cc = ellipse(4.5, 5, 3.5, 4)
|
| 499 |
+
img[rr, cc] = 1
|
| 500 |
+
img_ = np.array(
|
| 501 |
+
[
|
| 502 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 503 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 504 |
+
[0, 0, 0, 1, 1, 1, 1, 1, 0, 0],
|
| 505 |
+
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
|
| 506 |
+
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
|
| 507 |
+
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
|
| 508 |
+
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
|
| 509 |
+
[0, 0, 0, 1, 1, 1, 1, 1, 0, 0],
|
| 510 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 511 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 512 |
+
]
|
| 513 |
+
)
|
| 514 |
+
assert_array_equal(img, img_)
|
| 515 |
+
|
| 516 |
+
img = np.zeros((15, 15), 'uint8')
|
| 517 |
+
rr, cc = ellipse(7, 7, 3, 7)
|
| 518 |
+
img[rr, cc] = 1
|
| 519 |
+
img_ = np.array(
|
| 520 |
+
[
|
| 521 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 522 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 523 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 524 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 525 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 526 |
+
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
| 527 |
+
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
|
| 528 |
+
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
|
| 529 |
+
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
|
| 530 |
+
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
|
| 531 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 532 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 533 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 534 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 535 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 536 |
+
]
|
| 537 |
+
)
|
| 538 |
+
assert_array_equal(img, img_)
|
| 539 |
+
|
| 540 |
+
|
| 541 |
+
def test_ellipse_with_shape():
|
| 542 |
+
img = np.zeros((15, 15), 'uint8')
|
| 543 |
+
|
| 544 |
+
rr, cc = ellipse(7, 7, 3, 10, shape=img.shape)
|
| 545 |
+
img[rr, cc] = 1
|
| 546 |
+
|
| 547 |
+
img_ = np.array(
|
| 548 |
+
[
|
| 549 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 550 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 551 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 552 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 553 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 554 |
+
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
| 555 |
+
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
| 556 |
+
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
| 557 |
+
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
| 558 |
+
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
| 559 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 560 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 561 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 562 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 563 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 564 |
+
]
|
| 565 |
+
)
|
| 566 |
+
|
| 567 |
+
assert_array_equal(img, img_)
|
| 568 |
+
|
| 569 |
+
img = np.zeros((10, 9, 3), 'uint8')
|
| 570 |
+
|
| 571 |
+
rr, cc = ellipse(7, 7, 3, 10, shape=img.shape)
|
| 572 |
+
img[rr, cc, 0] = 1
|
| 573 |
+
|
| 574 |
+
img_ = np.zeros_like(img)
|
| 575 |
+
img_[..., 0] = np.array(
|
| 576 |
+
[
|
| 577 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 578 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 579 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 580 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 581 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 582 |
+
[1, 1, 1, 1, 1, 1, 1, 1, 1],
|
| 583 |
+
[1, 1, 1, 1, 1, 1, 1, 1, 1],
|
| 584 |
+
[1, 1, 1, 1, 1, 1, 1, 1, 1],
|
| 585 |
+
[1, 1, 1, 1, 1, 1, 1, 1, 1],
|
| 586 |
+
[1, 1, 1, 1, 1, 1, 1, 1, 1],
|
| 587 |
+
],
|
| 588 |
+
)
|
| 589 |
+
|
| 590 |
+
assert_array_equal(img, img_)
|
| 591 |
+
|
| 592 |
+
|
| 593 |
+
def test_ellipse_negative():
|
| 594 |
+
rr, cc = ellipse(-3, -3, 1.7, 1.7)
|
| 595 |
+
rr_, cc_ = np.nonzero(
|
| 596 |
+
np.array(
|
| 597 |
+
[
|
| 598 |
+
[0, 0, 0, 0, 0],
|
| 599 |
+
[0, 1, 1, 1, 0],
|
| 600 |
+
[0, 1, 1, 1, 0],
|
| 601 |
+
[0, 1, 1, 1, 0],
|
| 602 |
+
[0, 0, 0, 0, 0],
|
| 603 |
+
]
|
| 604 |
+
)
|
| 605 |
+
)
|
| 606 |
+
|
| 607 |
+
assert_array_equal(rr, rr_ - 5)
|
| 608 |
+
assert_array_equal(cc, cc_ - 5)
|
| 609 |
+
|
| 610 |
+
|
| 611 |
+
def test_ellipse_rotation_symmetry():
|
| 612 |
+
img1 = np.zeros((150, 150), dtype=np.uint8)
|
| 613 |
+
img2 = np.zeros((150, 150), dtype=np.uint8)
|
| 614 |
+
for angle in range(0, 180, 15):
|
| 615 |
+
img1.fill(0)
|
| 616 |
+
rr, cc = ellipse(80, 70, 60, 40, rotation=np.deg2rad(angle))
|
| 617 |
+
img1[rr, cc] = 1
|
| 618 |
+
img2.fill(0)
|
| 619 |
+
rr, cc = ellipse(80, 70, 60, 40, rotation=np.deg2rad(angle + 180))
|
| 620 |
+
img2[rr, cc] = 1
|
| 621 |
+
assert_array_equal(img1, img2)
|
| 622 |
+
|
| 623 |
+
|
| 624 |
+
def test_ellipse_rotated():
|
| 625 |
+
img = np.zeros((1000, 1200), dtype=np.uint8)
|
| 626 |
+
for rot in range(0, 180, 10):
|
| 627 |
+
img.fill(0)
|
| 628 |
+
angle = np.deg2rad(rot)
|
| 629 |
+
rr, cc = ellipse(500, 600, 200, 400, rotation=angle)
|
| 630 |
+
img[rr, cc] = 1
|
| 631 |
+
# estimate orientation of ellipse
|
| 632 |
+
angle_estim_raw = regionprops(img)[0].orientation
|
| 633 |
+
angle_estim = np.round(angle_estim_raw, 3) % (np.pi / 2)
|
| 634 |
+
assert_almost_equal(angle_estim, angle % (np.pi / 2), 2)
|
| 635 |
+
|
| 636 |
+
|
| 637 |
+
def test_ellipse_perimeter_dot_zeroangle():
|
| 638 |
+
# dot, angle == 0
|
| 639 |
+
img = np.zeros((30, 15), 'uint8')
|
| 640 |
+
rr, cc = ellipse_perimeter(15, 7, 0, 0, 0)
|
| 641 |
+
img[rr, cc] = 1
|
| 642 |
+
assert np.sum(img) == 1
|
| 643 |
+
assert img[15][7] == 1
|
| 644 |
+
|
| 645 |
+
|
| 646 |
+
def test_ellipse_perimeter_dot_nzeroangle():
|
| 647 |
+
# dot, angle != 0
|
| 648 |
+
img = np.zeros((30, 15), 'uint8')
|
| 649 |
+
rr, cc = ellipse_perimeter(15, 7, 0, 0, 1)
|
| 650 |
+
img[rr, cc] = 1
|
| 651 |
+
assert np.sum(img) == 1
|
| 652 |
+
assert img[15][7] == 1
|
| 653 |
+
|
| 654 |
+
|
| 655 |
+
def test_ellipse_perimeter_flat_zeroangle():
|
| 656 |
+
# flat ellipse
|
| 657 |
+
img = np.zeros((20, 18), 'uint8')
|
| 658 |
+
img_ = np.zeros((20, 18), 'uint8')
|
| 659 |
+
rr, cc = ellipse_perimeter(6, 7, 0, 5, 0)
|
| 660 |
+
img[rr, cc] = 1
|
| 661 |
+
rr, cc = line(6, 2, 6, 12)
|
| 662 |
+
img_[rr, cc] = 1
|
| 663 |
+
assert_array_equal(img, img_)
|
| 664 |
+
|
| 665 |
+
|
| 666 |
+
def test_ellipse_perimeter_zeroangle():
|
| 667 |
+
# angle == 0
|
| 668 |
+
img = np.zeros((30, 15), 'uint8')
|
| 669 |
+
rr, cc = ellipse_perimeter(15, 7, 14, 6, 0)
|
| 670 |
+
img[rr, cc] = 1
|
| 671 |
+
img_ = np.array(
|
| 672 |
+
[
|
| 673 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 674 |
+
[0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
|
| 675 |
+
[0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
|
| 676 |
+
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
|
| 677 |
+
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
|
| 678 |
+
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
|
| 679 |
+
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
|
| 680 |
+
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
|
| 681 |
+
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
|
| 682 |
+
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
|
| 683 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
| 684 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
| 685 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0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| 708 |
+
|
| 709 |
+
def test_ellipse_perimeter_nzeroangle():
|
| 710 |
+
# angle != 0
|
| 711 |
+
img = np.zeros((30, 25), 'uint8')
|
| 712 |
+
rr, cc = ellipse_perimeter(15, 11, 12, 6, 1.1)
|
| 713 |
+
img[rr, cc] = 1
|
| 714 |
+
img_ = np.array(
|
| 715 |
+
[
|
| 716 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 717 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 718 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 719 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 720 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 721 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 722 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
|
| 723 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
|
| 724 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
|
| 725 |
+
[0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 738 |
+
[0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 739 |
+
[0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 740 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 741 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 742 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 743 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 744 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 745 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 746 |
+
]
|
| 747 |
+
)
|
| 748 |
+
assert_array_equal(img, img_)
|
| 749 |
+
|
| 750 |
+
|
| 751 |
+
def test_ellipse_perimeter_shape():
|
| 752 |
+
img = np.zeros((15, 20), 'uint8')
|
| 753 |
+
rr, cc = ellipse_perimeter(7, 10, 9, 9, 0, shape=(15, 20))
|
| 754 |
+
img[rr, cc] = 1
|
| 755 |
+
shift = 5
|
| 756 |
+
img_ = np.zeros((15 + 2 * shift, 20), 'uint8')
|
| 757 |
+
rr, cc = ellipse_perimeter(7 + shift, 10, 9, 9, 0, shape=None)
|
| 758 |
+
img_[rr, cc] = 1
|
| 759 |
+
assert_array_equal(img, img_[shift:-shift, :])
|
| 760 |
+
|
| 761 |
+
|
| 762 |
+
def test_bezier_segment_straight():
|
| 763 |
+
image = np.zeros((200, 200), dtype=int)
|
| 764 |
+
r0, r1, r2 = 50, 150, 150
|
| 765 |
+
c0, c1, c2 = 50, 50, 150
|
| 766 |
+
rr, cc = _bezier_segment(r0, c0, r1, c1, r2, c2, 0)
|
| 767 |
+
image[rr, cc] = 1
|
| 768 |
+
|
| 769 |
+
image2 = np.zeros((200, 200), dtype=int)
|
| 770 |
+
rr, cc = line(r0, c0, r2, c2)
|
| 771 |
+
image2[rr, cc] = 1
|
| 772 |
+
assert_array_equal(image, image2)
|
| 773 |
+
|
| 774 |
+
|
| 775 |
+
def test_bezier_segment_curved():
|
| 776 |
+
img = np.zeros((25, 25), 'uint8')
|
| 777 |
+
r0, c0 = 20, 20
|
| 778 |
+
r1, c1 = 20, 2
|
| 779 |
+
r2, c2 = 2, 2
|
| 780 |
+
rr, cc = _bezier_segment(r0, c0, r1, c1, r2, c2, 1)
|
| 781 |
+
img[rr, cc] = 1
|
| 782 |
+
img_ = np.array(
|
| 783 |
+
[
|
| 784 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 785 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 786 |
+
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 787 |
+
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 788 |
+
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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 0, 0, 0, 0, 0, 0, 0, 0],
|
| 801 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 802 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 803 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 804 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
|
| 805 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 806 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 807 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 808 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 809 |
+
]
|
| 810 |
+
)
|
| 811 |
+
assert_equal(img[r0, c0], 1)
|
| 812 |
+
assert_equal(img[r2, c2], 1)
|
| 813 |
+
assert_array_equal(img, img_)
|
| 814 |
+
|
| 815 |
+
|
| 816 |
+
def test_bezier_curve_straight():
|
| 817 |
+
image = np.zeros((200, 200), dtype=int)
|
| 818 |
+
r0, c0 = 50, 50
|
| 819 |
+
r1, c1 = 150, 50
|
| 820 |
+
r2, c2 = 150, 150
|
| 821 |
+
rr, cc = bezier_curve(r0, c0, r1, c1, r2, c2, 0)
|
| 822 |
+
image[rr, cc] = 1
|
| 823 |
+
|
| 824 |
+
image2 = np.zeros((200, 200), dtype=int)
|
| 825 |
+
rr, cc = line(r0, c0, r2, c2)
|
| 826 |
+
image2[rr, cc] = 1
|
| 827 |
+
assert_array_equal(image, image2)
|
| 828 |
+
|
| 829 |
+
|
| 830 |
+
def test_bezier_curved_weight_eq_1():
|
| 831 |
+
img = np.zeros((23, 8), 'uint8')
|
| 832 |
+
r0, c0 = 1, 1
|
| 833 |
+
r1, c1 = 11, 11
|
| 834 |
+
r2, c2 = 21, 1
|
| 835 |
+
rr, cc = bezier_curve(r0, c0, r1, c1, r2, c2, 1)
|
| 836 |
+
img[rr, cc] = 1
|
| 837 |
+
assert_equal(img[r0, c0], 1)
|
| 838 |
+
assert_equal(img[r2, c2], 1)
|
| 839 |
+
img_ = np.array(
|
| 840 |
+
[
|
| 841 |
+
[0, 0, 0, 0, 0, 0, 0, 0],
|
| 842 |
+
[0, 1, 0, 0, 0, 0, 0, 0],
|
| 843 |
+
[0, 0, 1, 0, 0, 0, 0, 0],
|
| 844 |
+
[0, 0, 0, 1, 0, 0, 0, 0],
|
| 845 |
+
[0, 0, 0, 0, 1, 0, 0, 0],
|
| 846 |
+
[0, 0, 0, 0, 1, 0, 0, 0],
|
| 847 |
+
[0, 0, 0, 0, 0, 1, 0, 0],
|
| 848 |
+
[0, 0, 0, 0, 0, 1, 0, 0],
|
| 849 |
+
[0, 0, 0, 0, 0, 0, 1, 0],
|
| 850 |
+
[0, 0, 0, 0, 0, 0, 1, 0],
|
| 851 |
+
[0, 0, 0, 0, 0, 0, 1, 0],
|
| 852 |
+
[0, 0, 0, 0, 0, 0, 1, 0],
|
| 853 |
+
[0, 0, 0, 0, 0, 0, 1, 0],
|
| 854 |
+
[0, 0, 0, 0, 0, 0, 1, 0],
|
| 855 |
+
[0, 0, 0, 0, 0, 0, 1, 0],
|
| 856 |
+
[0, 0, 0, 0, 0, 1, 0, 0],
|
| 857 |
+
[0, 0, 0, 0, 0, 1, 0, 0],
|
| 858 |
+
[0, 0, 0, 0, 1, 0, 0, 0],
|
| 859 |
+
[0, 0, 0, 0, 1, 0, 0, 0],
|
| 860 |
+
[0, 0, 0, 1, 0, 0, 0, 0],
|
| 861 |
+
[0, 0, 1, 0, 0, 0, 0, 0],
|
| 862 |
+
[0, 1, 0, 0, 0, 0, 0, 0],
|
| 863 |
+
[0, 0, 0, 0, 0, 0, 0, 0],
|
| 864 |
+
]
|
| 865 |
+
)
|
| 866 |
+
assert_equal(img, img_)
|
| 867 |
+
|
| 868 |
+
|
| 869 |
+
def test_bezier_curved_weight_neq_1():
|
| 870 |
+
img = np.zeros((23, 10), 'uint8')
|
| 871 |
+
r0, c0 = 1, 1
|
| 872 |
+
r1, c1 = 11, 11
|
| 873 |
+
r2, c2 = 21, 1
|
| 874 |
+
rr, cc = bezier_curve(r0, c0, r1, c1, r2, c2, 2)
|
| 875 |
+
img[rr, cc] = 1
|
| 876 |
+
assert_equal(img[r0, c0], 1)
|
| 877 |
+
assert_equal(img[r2, c2], 1)
|
| 878 |
+
img_ = np.array(
|
| 879 |
+
[
|
| 880 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 881 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 882 |
+
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
|
| 883 |
+
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
|
| 884 |
+
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
|
| 885 |
+
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
|
| 886 |
+
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
|
| 887 |
+
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
|
| 888 |
+
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
|
| 889 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
| 890 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
| 891 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
| 892 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
| 893 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
|
| 894 |
+
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
|
| 895 |
+
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
|
| 896 |
+
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
|
| 897 |
+
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
|
| 898 |
+
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
|
| 899 |
+
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
|
| 900 |
+
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
|
| 901 |
+
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 902 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
| 903 |
+
]
|
| 904 |
+
)
|
| 905 |
+
assert_equal(img, img_)
|
| 906 |
+
|
| 907 |
+
|
| 908 |
+
def test_bezier_curve_shape():
|
| 909 |
+
img = np.zeros((15, 20), 'uint8')
|
| 910 |
+
r0, c0 = 1, 5
|
| 911 |
+
r1, c1 = 6, 11
|
| 912 |
+
r2, c2 = 1, 14
|
| 913 |
+
rr, cc = bezier_curve(r0, c0, r1, c1, r2, c2, 2, shape=(15, 20))
|
| 914 |
+
img[rr, cc] = 1
|
| 915 |
+
shift = 5
|
| 916 |
+
img_ = np.zeros((15 + 2 * shift, 20), 'uint8')
|
| 917 |
+
r0, c0 = 1 + shift, 5
|
| 918 |
+
r1, c1 = 6 + shift, 11
|
| 919 |
+
r2, c2 = 1 + shift, 14
|
| 920 |
+
rr, cc = bezier_curve(r0, c0, r1, c1, r2, c2, 2, shape=None)
|
| 921 |
+
img_[rr, cc] = 1
|
| 922 |
+
assert_array_equal(img, img_[shift:-shift, :])
|
| 923 |
+
|
| 924 |
+
|
| 925 |
+
@pytest.mark.skipif(not has_mpl, reason="matplotlib not installed")
|
| 926 |
+
def test_polygon_perimeter():
|
| 927 |
+
expected = np.array([[1, 1, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]])
|
| 928 |
+
out = np.zeros_like(expected)
|
| 929 |
+
|
| 930 |
+
rr, cc = polygon_perimeter([0, 2, 2, 0], [0, 0, 3, 3])
|
| 931 |
+
|
| 932 |
+
out[rr, cc] = 1
|
| 933 |
+
assert_array_equal(out, expected)
|
| 934 |
+
|
| 935 |
+
out = np.zeros_like(expected)
|
| 936 |
+
rr, cc = polygon_perimeter(
|
| 937 |
+
[-1, -1, 3, 3], [-1, 4, 4, -1], shape=out.shape, clip=True
|
| 938 |
+
)
|
| 939 |
+
out[rr, cc] = 1
|
| 940 |
+
assert_array_equal(out, expected)
|
| 941 |
+
|
| 942 |
+
with pytest.raises(ValueError):
|
| 943 |
+
polygon_perimeter([0], [1], clip=True)
|
| 944 |
+
|
| 945 |
+
|
| 946 |
+
@pytest.mark.skipif(not has_mpl, reason="matplotlib not installed")
|
| 947 |
+
def test_polygon_perimeter_outside_image():
|
| 948 |
+
rr, cc = polygon_perimeter([-1, -1, 3, 3], [-1, 4, 4, -1], shape=(3, 4))
|
| 949 |
+
assert_equal(len(rr), 0)
|
| 950 |
+
assert_equal(len(cc), 0)
|
| 951 |
+
|
| 952 |
+
|
| 953 |
+
def test_rectangle_end():
|
| 954 |
+
expected = np.array(
|
| 955 |
+
[
|
| 956 |
+
[0, 1, 1, 1, 0],
|
| 957 |
+
[0, 1, 1, 1, 0],
|
| 958 |
+
[0, 1, 1, 1, 0],
|
| 959 |
+
[0, 1, 1, 1, 0],
|
| 960 |
+
[0, 0, 0, 0, 0],
|
| 961 |
+
],
|
| 962 |
+
dtype=np.uint8,
|
| 963 |
+
)
|
| 964 |
+
start = (0, 1)
|
| 965 |
+
end = (3, 3)
|
| 966 |
+
img = np.zeros((5, 5), dtype=np.uint8)
|
| 967 |
+
rr, cc = rectangle(start, end=end, shape=img.shape)
|
| 968 |
+
img[rr, cc] = 1
|
| 969 |
+
assert_array_equal(img, expected)
|
| 970 |
+
|
| 971 |
+
# Swap start and end
|
| 972 |
+
img = np.zeros((5, 5), dtype=np.uint8)
|
| 973 |
+
rr, cc = rectangle(end=start, start=end, shape=img.shape)
|
| 974 |
+
img[rr, cc] = 1
|
| 975 |
+
assert_array_equal(img, expected)
|
| 976 |
+
|
| 977 |
+
# Bottom left and top right
|
| 978 |
+
img = np.zeros((5, 5), dtype=np.uint8)
|
| 979 |
+
rr, cc = rectangle(start=(3, 1), end=(0, 3), shape=img.shape)
|
| 980 |
+
img[rr, cc] = 1
|
| 981 |
+
assert_array_equal(img, expected)
|
| 982 |
+
|
| 983 |
+
img = np.zeros((5, 5), dtype=np.uint8)
|
| 984 |
+
rr, cc = rectangle(end=(3, 1), start=(0, 3), shape=img.shape)
|
| 985 |
+
img[rr, cc] = 1
|
| 986 |
+
assert_array_equal(img, expected)
|
| 987 |
+
|
| 988 |
+
|
| 989 |
+
def test_rectangle_float_input():
|
| 990 |
+
expected = np.array(
|
| 991 |
+
[
|
| 992 |
+
[0, 1, 1, 1, 0],
|
| 993 |
+
[0, 1, 1, 1, 0],
|
| 994 |
+
[0, 1, 1, 1, 0],
|
| 995 |
+
[0, 1, 1, 1, 0],
|
| 996 |
+
[0, 0, 0, 0, 0],
|
| 997 |
+
],
|
| 998 |
+
dtype=np.uint8,
|
| 999 |
+
)
|
| 1000 |
+
start = (0.2, 0.8)
|
| 1001 |
+
end = (3.1, 2.9)
|
| 1002 |
+
img = np.zeros((5, 5), dtype=np.uint8)
|
| 1003 |
+
rr, cc = rectangle(start, end=end, shape=img.shape)
|
| 1004 |
+
img[rr, cc] = 1
|
| 1005 |
+
assert_array_equal(img, expected)
|
| 1006 |
+
|
| 1007 |
+
# Swap start and end
|
| 1008 |
+
img = np.zeros((5, 5), dtype=np.uint8)
|
| 1009 |
+
rr, cc = rectangle(end=start, start=end, shape=img.shape)
|
| 1010 |
+
img[rr, cc] = 1
|
| 1011 |
+
assert_array_equal(img, expected)
|
| 1012 |
+
|
| 1013 |
+
# Bottom left and top right
|
| 1014 |
+
img = np.zeros((5, 5), dtype=np.uint8)
|
| 1015 |
+
rr, cc = rectangle(start=(3.1, 0.8), end=(0.2, 2.9), shape=img.shape)
|
| 1016 |
+
img[rr, cc] = 1
|
| 1017 |
+
assert_array_equal(img, expected)
|
| 1018 |
+
|
| 1019 |
+
img = np.zeros((5, 5), dtype=np.uint8)
|
| 1020 |
+
rr, cc = rectangle(end=(3.1, 0.8), start=(0.2, 2.9), shape=img.shape)
|
| 1021 |
+
img[rr, cc] = 1
|
| 1022 |
+
assert_array_equal(img, expected)
|
| 1023 |
+
|
| 1024 |
+
|
| 1025 |
+
def test_rectangle_extent():
|
| 1026 |
+
expected = np.array(
|
| 1027 |
+
[
|
| 1028 |
+
[0, 0, 0, 0, 0],
|
| 1029 |
+
[0, 1, 1, 1, 0],
|
| 1030 |
+
[0, 1, 1, 1, 0],
|
| 1031 |
+
[0, 1, 1, 1, 0],
|
| 1032 |
+
[0, 0, 0, 0, 0],
|
| 1033 |
+
],
|
| 1034 |
+
dtype=np.uint8,
|
| 1035 |
+
)
|
| 1036 |
+
start = (1, 1)
|
| 1037 |
+
extent = (3, 3)
|
| 1038 |
+
img = np.zeros((5, 5), dtype=np.uint8)
|
| 1039 |
+
rr, cc = rectangle(start, extent=extent, shape=img.shape)
|
| 1040 |
+
img[rr, cc] = 1
|
| 1041 |
+
assert_array_equal(img, expected)
|
| 1042 |
+
|
| 1043 |
+
img = np.zeros((5, 5, 3), dtype=np.uint8)
|
| 1044 |
+
rr, cc = rectangle(start, extent=extent, shape=img.shape)
|
| 1045 |
+
img[rr, cc, 0] = 1
|
| 1046 |
+
expected_2 = np.zeros_like(img)
|
| 1047 |
+
expected_2[..., 0] = expected
|
| 1048 |
+
assert_array_equal(img, expected_2)
|
| 1049 |
+
|
| 1050 |
+
|
| 1051 |
+
@pytest.mark.skipif(not has_mpl, reason="matplotlib not installed")
|
| 1052 |
+
def test_rectangle_extent_negative():
|
| 1053 |
+
# These two tests should be done together.
|
| 1054 |
+
expected = np.array(
|
| 1055 |
+
[
|
| 1056 |
+
[0, 0, 0, 0, 0, 0],
|
| 1057 |
+
[0, 0, 1, 1, 1, 1],
|
| 1058 |
+
[0, 0, 1, 2, 2, 1],
|
| 1059 |
+
[0, 0, 1, 1, 1, 1],
|
| 1060 |
+
[0, 0, 0, 0, 0, 0],
|
| 1061 |
+
],
|
| 1062 |
+
dtype=np.uint8,
|
| 1063 |
+
)
|
| 1064 |
+
|
| 1065 |
+
start = (3, 5)
|
| 1066 |
+
extent = (-1, -2)
|
| 1067 |
+
img = np.zeros(expected.shape, dtype=np.uint8)
|
| 1068 |
+
rr, cc = rectangle_perimeter(start, extent=extent, shape=img.shape)
|
| 1069 |
+
img[rr, cc] = 1
|
| 1070 |
+
|
| 1071 |
+
rr, cc = rectangle(start, extent=extent, shape=img.shape)
|
| 1072 |
+
img[rr, cc] = 2
|
| 1073 |
+
assert_array_equal(img, expected)
|
| 1074 |
+
|
| 1075 |
+
# Ensure that rr and cc have no overlap
|
| 1076 |
+
img = np.zeros(expected.shape, dtype=np.uint8)
|
| 1077 |
+
rr, cc = rectangle(start, extent=extent, shape=img.shape)
|
| 1078 |
+
img[rr, cc] = 2
|
| 1079 |
+
|
| 1080 |
+
rr, cc = rectangle_perimeter(start, extent=extent, shape=img.shape)
|
| 1081 |
+
img[rr, cc] = 1
|
| 1082 |
+
assert_array_equal(img, expected)
|
| 1083 |
+
|
| 1084 |
+
|
| 1085 |
+
@pytest.mark.skipif(not has_mpl, reason="matplotlib not installed")
|
| 1086 |
+
def test_rectangle_perimiter():
|
| 1087 |
+
expected = np.array(
|
| 1088 |
+
[
|
| 1089 |
+
[0, 0, 0, 0, 0, 0],
|
| 1090 |
+
[0, 0, 1, 1, 1, 1],
|
| 1091 |
+
[0, 0, 1, 0, 0, 1],
|
| 1092 |
+
[0, 0, 1, 1, 1, 1],
|
| 1093 |
+
[0, 0, 0, 0, 0, 0],
|
| 1094 |
+
],
|
| 1095 |
+
dtype=np.uint8,
|
| 1096 |
+
)
|
| 1097 |
+
start = (2, 3)
|
| 1098 |
+
end = (2, 4)
|
| 1099 |
+
img = np.zeros(expected.shape, dtype=np.uint8)
|
| 1100 |
+
# Test that the default parameter is indeed end
|
| 1101 |
+
rr, cc = rectangle_perimeter(start, end, shape=img.shape)
|
| 1102 |
+
img[rr, cc] = 1
|
| 1103 |
+
assert_array_equal(img, expected)
|
| 1104 |
+
|
| 1105 |
+
# Swap start and end
|
| 1106 |
+
img = np.zeros(expected.shape, dtype=np.uint8)
|
| 1107 |
+
rr, cc = rectangle_perimeter(end=start, start=end, shape=img.shape)
|
| 1108 |
+
img[rr, cc] = 1
|
| 1109 |
+
assert_array_equal(img, expected)
|
| 1110 |
+
|
| 1111 |
+
img = np.zeros(expected.shape, dtype=np.uint8)
|
| 1112 |
+
start = (2, 3)
|
| 1113 |
+
extent = (1, 2)
|
| 1114 |
+
rr, cc = rectangle_perimeter(start, extent=extent, shape=img.shape)
|
| 1115 |
+
img[rr, cc] = 1
|
| 1116 |
+
assert_array_equal(img, expected)
|
| 1117 |
+
|
| 1118 |
+
|
| 1119 |
+
@pytest.mark.skipif(not has_mpl, reason="matplotlib not installed")
|
| 1120 |
+
def test_rectangle_perimiter_clip_bottom_right():
|
| 1121 |
+
# clip=False
|
| 1122 |
+
expected = np.array(
|
| 1123 |
+
[
|
| 1124 |
+
[0, 0, 0, 0, 0],
|
| 1125 |
+
[0, 1, 1, 1, 1],
|
| 1126 |
+
[0, 1, 0, 0, 0],
|
| 1127 |
+
[0, 1, 0, 0, 0],
|
| 1128 |
+
[0, 1, 0, 0, 0],
|
| 1129 |
+
],
|
| 1130 |
+
dtype=np.uint8,
|
| 1131 |
+
)
|
| 1132 |
+
img = np.zeros((5, 5), dtype=np.uint8)
|
| 1133 |
+
start = (2, 2)
|
| 1134 |
+
extent = (10, 10)
|
| 1135 |
+
rr, cc = rectangle_perimeter(start, extent=extent, shape=img.shape, clip=False)
|
| 1136 |
+
img[rr, cc] = 1
|
| 1137 |
+
assert_array_equal(img, expected)
|
| 1138 |
+
|
| 1139 |
+
# clip=True
|
| 1140 |
+
expected = np.array(
|
| 1141 |
+
[
|
| 1142 |
+
[0, 0, 0, 0, 0],
|
| 1143 |
+
[0, 1, 1, 1, 1],
|
| 1144 |
+
[0, 1, 0, 0, 1],
|
| 1145 |
+
[0, 1, 0, 0, 1],
|
| 1146 |
+
[0, 1, 1, 1, 1],
|
| 1147 |
+
],
|
| 1148 |
+
dtype=np.uint8,
|
| 1149 |
+
)
|
| 1150 |
+
img = np.zeros((5, 5), dtype=np.uint8)
|
| 1151 |
+
rr, cc = rectangle_perimeter(start, extent=extent, shape=img.shape, clip=True)
|
| 1152 |
+
img[rr, cc] = 1
|
| 1153 |
+
assert_array_equal(img, expected)
|
| 1154 |
+
|
| 1155 |
+
|
| 1156 |
+
@pytest.mark.skipif(not has_mpl, reason="matplotlib not installed")
|
| 1157 |
+
def test_rectangle_perimiter_clip_top_left():
|
| 1158 |
+
# clip=False
|
| 1159 |
+
expected = np.array(
|
| 1160 |
+
[
|
| 1161 |
+
[0, 0, 0, 1, 0],
|
| 1162 |
+
[0, 0, 0, 1, 0],
|
| 1163 |
+
[0, 0, 0, 1, 0],
|
| 1164 |
+
[1, 1, 1, 1, 0],
|
| 1165 |
+
[0, 0, 0, 0, 0],
|
| 1166 |
+
],
|
| 1167 |
+
dtype=np.uint8,
|
| 1168 |
+
)
|
| 1169 |
+
img = np.zeros((5, 5), dtype=np.uint8)
|
| 1170 |
+
start = (-5, -5)
|
| 1171 |
+
end = (2, 2)
|
| 1172 |
+
rr, cc = rectangle_perimeter(start, end=end, shape=img.shape, clip=False)
|
| 1173 |
+
img[rr, cc] = 1
|
| 1174 |
+
assert_array_equal(img, expected)
|
| 1175 |
+
|
| 1176 |
+
# clip=True
|
| 1177 |
+
expected = np.array(
|
| 1178 |
+
[
|
| 1179 |
+
[1, 1, 1, 1, 0],
|
| 1180 |
+
[1, 0, 0, 1, 0],
|
| 1181 |
+
[1, 0, 0, 1, 0],
|
| 1182 |
+
[1, 1, 1, 1, 0],
|
| 1183 |
+
[0, 0, 0, 0, 0],
|
| 1184 |
+
],
|
| 1185 |
+
dtype=np.uint8,
|
| 1186 |
+
)
|
| 1187 |
+
img = np.zeros((5, 5), dtype=np.uint8)
|
| 1188 |
+
rr, cc = rectangle_perimeter(start, end=end, shape=img.shape, clip=True)
|
| 1189 |
+
img[rr, cc] = 1
|
| 1190 |
+
assert_array_equal(img, expected)
|
| 1191 |
+
|
| 1192 |
+
|
| 1193 |
+
@pytest.mark.skipif(not has_mpl, reason="matplotlib not installed")
|
| 1194 |
+
def test_rectangle_perimiter_clip_top_right():
|
| 1195 |
+
expected = np.array(
|
| 1196 |
+
[
|
| 1197 |
+
[0, 1, 1, 1, 1],
|
| 1198 |
+
[0, 1, 0, 0, 1],
|
| 1199 |
+
[0, 1, 0, 0, 1],
|
| 1200 |
+
[0, 1, 1, 1, 1],
|
| 1201 |
+
[0, 0, 0, 0, 0],
|
| 1202 |
+
],
|
| 1203 |
+
dtype=np.uint8,
|
| 1204 |
+
)
|
| 1205 |
+
img = np.zeros((5, 5), dtype=np.uint8)
|
| 1206 |
+
start = (-10, 2)
|
| 1207 |
+
end = (2, 10)
|
| 1208 |
+
rr, cc = rectangle_perimeter(start, end=end, shape=img.shape, clip=True)
|
| 1209 |
+
img[rr, cc] = 1
|
| 1210 |
+
assert_array_equal(img, expected)
|
| 1211 |
+
|
| 1212 |
+
expected = np.array(
|
| 1213 |
+
[
|
| 1214 |
+
[0, 1, 0, 0, 0],
|
| 1215 |
+
[0, 1, 0, 0, 0],
|
| 1216 |
+
[0, 1, 0, 0, 0],
|
| 1217 |
+
[0, 1, 1, 1, 1],
|
| 1218 |
+
[0, 0, 0, 0, 0],
|
| 1219 |
+
],
|
| 1220 |
+
dtype=np.uint8,
|
| 1221 |
+
)
|
| 1222 |
+
img = np.zeros((5, 5), dtype=np.uint8)
|
| 1223 |
+
rr, cc = rectangle_perimeter(start, end=end, shape=img.shape, clip=False)
|
| 1224 |
+
img[rr, cc] = 1
|
| 1225 |
+
assert_array_equal(img, expected)
|
| 1226 |
+
|
| 1227 |
+
|
| 1228 |
+
@pytest.mark.skipif(not has_mpl, reason="matplotlib not installed")
|
| 1229 |
+
def test_rectangle_perimiter_clip_bottom_left():
|
| 1230 |
+
expected = np.array(
|
| 1231 |
+
[
|
| 1232 |
+
[0, 0, 0, 0, 0],
|
| 1233 |
+
[1, 1, 1, 0, 0],
|
| 1234 |
+
[1, 0, 1, 0, 0],
|
| 1235 |
+
[1, 0, 1, 0, 0],
|
| 1236 |
+
[1, 1, 1, 0, 0],
|
| 1237 |
+
],
|
| 1238 |
+
dtype=np.uint8,
|
| 1239 |
+
)
|
| 1240 |
+
img = np.zeros((5, 5), dtype=np.uint8)
|
| 1241 |
+
start = (2, -3)
|
| 1242 |
+
end = (10, 1)
|
| 1243 |
+
rr, cc = rectangle_perimeter(start, end=end, shape=img.shape, clip=True)
|
| 1244 |
+
img[rr, cc] = 1
|
| 1245 |
+
assert_array_equal(img, expected)
|
| 1246 |
+
|
| 1247 |
+
expected = np.array(
|
| 1248 |
+
[
|
| 1249 |
+
[0, 0, 0, 0, 0],
|
| 1250 |
+
[1, 1, 1, 0, 0],
|
| 1251 |
+
[0, 0, 1, 0, 0],
|
| 1252 |
+
[0, 0, 1, 0, 0],
|
| 1253 |
+
[0, 0, 1, 0, 0],
|
| 1254 |
+
],
|
| 1255 |
+
dtype=np.uint8,
|
| 1256 |
+
)
|
| 1257 |
+
|
| 1258 |
+
img = np.zeros((5, 5), dtype=np.uint8)
|
| 1259 |
+
rr, cc = rectangle_perimeter(start, end=end, shape=img.shape, clip=False)
|
| 1260 |
+
img[rr, cc] = 1
|
| 1261 |
+
assert_array_equal(img, expected)
|
infer_4_47_1/lib/python3.10/site-packages/skimage/draw/tests/test_draw3d.py
ADDED
|
@@ -0,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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from skimage._shared.testing import assert_array_equal, assert_allclose
|
| 3 |
+
|
| 4 |
+
from skimage.draw import ellipsoid, ellipsoid_stats, rectangle
|
| 5 |
+
from skimage._shared import testing
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def test_ellipsoid_sign_parameters1():
|
| 9 |
+
with testing.raises(ValueError):
|
| 10 |
+
ellipsoid(-1, 2, 2)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def test_ellipsoid_sign_parameters2():
|
| 14 |
+
with testing.raises(ValueError):
|
| 15 |
+
ellipsoid(0, 2, 2)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def test_ellipsoid_sign_parameters3():
|
| 19 |
+
with testing.raises(ValueError):
|
| 20 |
+
ellipsoid(-3, -2, 2)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def test_ellipsoid_bool():
|
| 24 |
+
test = ellipsoid(2, 2, 2)[1:-1, 1:-1, 1:-1]
|
| 25 |
+
test_anisotropic = ellipsoid(2, 2, 4, spacing=(1.0, 1.0, 2.0))
|
| 26 |
+
test_anisotropic = test_anisotropic[1:-1, 1:-1, 1:-1]
|
| 27 |
+
|
| 28 |
+
expected = np.array(
|
| 29 |
+
[
|
| 30 |
+
[
|
| 31 |
+
[0, 0, 0, 0, 0],
|
| 32 |
+
[0, 0, 0, 0, 0],
|
| 33 |
+
[0, 0, 1, 0, 0],
|
| 34 |
+
[0, 0, 0, 0, 0],
|
| 35 |
+
[0, 0, 0, 0, 0],
|
| 36 |
+
],
|
| 37 |
+
[
|
| 38 |
+
[0, 0, 0, 0, 0],
|
| 39 |
+
[0, 1, 1, 1, 0],
|
| 40 |
+
[0, 1, 1, 1, 0],
|
| 41 |
+
[0, 1, 1, 1, 0],
|
| 42 |
+
[0, 0, 0, 0, 0],
|
| 43 |
+
],
|
| 44 |
+
[
|
| 45 |
+
[0, 0, 1, 0, 0],
|
| 46 |
+
[0, 1, 1, 1, 0],
|
| 47 |
+
[1, 1, 1, 1, 1],
|
| 48 |
+
[0, 1, 1, 1, 0],
|
| 49 |
+
[0, 0, 1, 0, 0],
|
| 50 |
+
],
|
| 51 |
+
[
|
| 52 |
+
[0, 0, 0, 0, 0],
|
| 53 |
+
[0, 1, 1, 1, 0],
|
| 54 |
+
[0, 1, 1, 1, 0],
|
| 55 |
+
[0, 1, 1, 1, 0],
|
| 56 |
+
[0, 0, 0, 0, 0],
|
| 57 |
+
],
|
| 58 |
+
[
|
| 59 |
+
[0, 0, 0, 0, 0],
|
| 60 |
+
[0, 0, 0, 0, 0],
|
| 61 |
+
[0, 0, 1, 0, 0],
|
| 62 |
+
[0, 0, 0, 0, 0],
|
| 63 |
+
[0, 0, 0, 0, 0],
|
| 64 |
+
],
|
| 65 |
+
]
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
assert_array_equal(test, expected.astype(bool))
|
| 69 |
+
assert_array_equal(test_anisotropic, expected.astype(bool))
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def test_ellipsoid_levelset():
|
| 73 |
+
test = ellipsoid(2, 2, 2, levelset=True)[1:-1, 1:-1, 1:-1]
|
| 74 |
+
test_anisotropic = ellipsoid(2, 2, 4, spacing=(1.0, 1.0, 2.0), levelset=True)
|
| 75 |
+
test_anisotropic = test_anisotropic[1:-1, 1:-1, 1:-1]
|
| 76 |
+
|
| 77 |
+
expected = np.array(
|
| 78 |
+
[
|
| 79 |
+
[
|
| 80 |
+
[2.0, 1.25, 1.0, 1.25, 2.0],
|
| 81 |
+
[1.25, 0.5, 0.25, 0.5, 1.25],
|
| 82 |
+
[1.0, 0.25, 0.0, 0.25, 1.0],
|
| 83 |
+
[1.25, 0.5, 0.25, 0.5, 1.25],
|
| 84 |
+
[2.0, 1.25, 1.0, 1.25, 2.0],
|
| 85 |
+
],
|
| 86 |
+
[
|
| 87 |
+
[1.25, 0.5, 0.25, 0.5, 1.25],
|
| 88 |
+
[0.5, -0.25, -0.5, -0.25, 0.5],
|
| 89 |
+
[0.25, -0.5, -0.75, -0.5, 0.25],
|
| 90 |
+
[0.5, -0.25, -0.5, -0.25, 0.5],
|
| 91 |
+
[1.25, 0.5, 0.25, 0.5, 1.25],
|
| 92 |
+
],
|
| 93 |
+
[
|
| 94 |
+
[1.0, 0.25, 0.0, 0.25, 1.0],
|
| 95 |
+
[0.25, -0.5, -0.75, -0.5, 0.25],
|
| 96 |
+
[0.0, -0.75, -1.0, -0.75, 0.0],
|
| 97 |
+
[0.25, -0.5, -0.75, -0.5, 0.25],
|
| 98 |
+
[1.0, 0.25, 0.0, 0.25, 1.0],
|
| 99 |
+
],
|
| 100 |
+
[
|
| 101 |
+
[1.25, 0.5, 0.25, 0.5, 1.25],
|
| 102 |
+
[0.5, -0.25, -0.5, -0.25, 0.5],
|
| 103 |
+
[0.25, -0.5, -0.75, -0.5, 0.25],
|
| 104 |
+
[0.5, -0.25, -0.5, -0.25, 0.5],
|
| 105 |
+
[1.25, 0.5, 0.25, 0.5, 1.25],
|
| 106 |
+
],
|
| 107 |
+
[
|
| 108 |
+
[2.0, 1.25, 1.0, 1.25, 2.0],
|
| 109 |
+
[1.25, 0.5, 0.25, 0.5, 1.25],
|
| 110 |
+
[1.0, 0.25, 0.0, 0.25, 1.0],
|
| 111 |
+
[1.25, 0.5, 0.25, 0.5, 1.25],
|
| 112 |
+
[2.0, 1.25, 1.0, 1.25, 2.0],
|
| 113 |
+
],
|
| 114 |
+
]
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
assert_allclose(test, expected)
|
| 118 |
+
assert_allclose(test_anisotropic, expected)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def test_ellipsoid_stats():
|
| 122 |
+
# Test comparison values generated by Wolfram Alpha
|
| 123 |
+
vol, surf = ellipsoid_stats(6, 10, 16)
|
| 124 |
+
assert_allclose(1280 * np.pi, vol, atol=1e-6)
|
| 125 |
+
assert_allclose(1383.2828269179892359787, surf, atol=1e-6)
|
| 126 |
+
|
| 127 |
+
# Test when a <= b <= c does not hold
|
| 128 |
+
vol, surf = ellipsoid_stats(16, 6, 10)
|
| 129 |
+
assert_allclose(1280 * np.pi, vol, atol=1e-6)
|
| 130 |
+
assert_allclose(1383.2828269179892359787, surf, atol=1e-6)
|
| 131 |
+
|
| 132 |
+
# Larger test to ensure reliability over broad range
|
| 133 |
+
vol, surf = ellipsoid_stats(17, 27, 169)
|
| 134 |
+
assert_allclose(103428 * np.pi, vol, atol=1e-4)
|
| 135 |
+
assert_allclose(37426.253635465972897880, surf, atol=1e-6)
|
| 136 |
+
|
| 137 |
+
# For equal a, b, c values
|
| 138 |
+
vol, surf = ellipsoid_stats(10, 10, 10)
|
| 139 |
+
assert_allclose(4000 * np.pi / 3, vol, atol=1e-6)
|
| 140 |
+
assert_allclose(400 * np.pi, surf, atol=1e-6)
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def test_rect_3d_extent():
|
| 144 |
+
expected = np.array(
|
| 145 |
+
[
|
| 146 |
+
[
|
| 147 |
+
[0, 0, 1, 1, 1],
|
| 148 |
+
[0, 0, 1, 1, 1],
|
| 149 |
+
[0, 0, 0, 0, 0],
|
| 150 |
+
[0, 0, 0, 0, 0],
|
| 151 |
+
[0, 0, 0, 0, 0],
|
| 152 |
+
],
|
| 153 |
+
[
|
| 154 |
+
[0, 0, 1, 1, 1],
|
| 155 |
+
[0, 0, 1, 1, 1],
|
| 156 |
+
[0, 0, 0, 0, 0],
|
| 157 |
+
[0, 0, 0, 0, 0],
|
| 158 |
+
[0, 0, 0, 0, 0],
|
| 159 |
+
],
|
| 160 |
+
[
|
| 161 |
+
[0, 0, 1, 1, 1],
|
| 162 |
+
[0, 0, 1, 1, 1],
|
| 163 |
+
[0, 0, 0, 0, 0],
|
| 164 |
+
[0, 0, 0, 0, 0],
|
| 165 |
+
[0, 0, 0, 0, 0],
|
| 166 |
+
],
|
| 167 |
+
[
|
| 168 |
+
[0, 0, 1, 1, 1],
|
| 169 |
+
[0, 0, 1, 1, 1],
|
| 170 |
+
[0, 0, 0, 0, 0],
|
| 171 |
+
[0, 0, 0, 0, 0],
|
| 172 |
+
[0, 0, 0, 0, 0],
|
| 173 |
+
],
|
| 174 |
+
],
|
| 175 |
+
dtype=np.uint8,
|
| 176 |
+
)
|
| 177 |
+
img = np.zeros((4, 5, 5), dtype=np.uint8)
|
| 178 |
+
start = (0, 0, 2)
|
| 179 |
+
extent = (5, 2, 3)
|
| 180 |
+
pp, rr, cc = rectangle(start, extent=extent, shape=img.shape)
|
| 181 |
+
img[pp, rr, cc] = 1
|
| 182 |
+
assert_array_equal(img, expected)
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
def test_rect_3d_end():
|
| 186 |
+
expected = np.array(
|
| 187 |
+
[
|
| 188 |
+
[
|
| 189 |
+
[0, 0, 0, 0, 0],
|
| 190 |
+
[0, 0, 0, 0, 0],
|
| 191 |
+
[0, 0, 0, 0, 0],
|
| 192 |
+
[0, 0, 0, 0, 0],
|
| 193 |
+
[0, 0, 0, 0, 0],
|
| 194 |
+
],
|
| 195 |
+
[
|
| 196 |
+
[0, 0, 1, 1, 0],
|
| 197 |
+
[0, 0, 1, 1, 0],
|
| 198 |
+
[0, 0, 1, 1, 0],
|
| 199 |
+
[0, 0, 0, 0, 0],
|
| 200 |
+
[0, 0, 0, 0, 0],
|
| 201 |
+
],
|
| 202 |
+
[
|
| 203 |
+
[0, 0, 1, 1, 0],
|
| 204 |
+
[0, 0, 1, 1, 0],
|
| 205 |
+
[0, 0, 1, 1, 0],
|
| 206 |
+
[0, 0, 0, 0, 0],
|
| 207 |
+
[0, 0, 0, 0, 0],
|
| 208 |
+
],
|
| 209 |
+
[
|
| 210 |
+
[0, 0, 1, 1, 0],
|
| 211 |
+
[0, 0, 1, 1, 0],
|
| 212 |
+
[0, 0, 1, 1, 0],
|
| 213 |
+
[0, 0, 0, 0, 0],
|
| 214 |
+
[0, 0, 0, 0, 0],
|
| 215 |
+
],
|
| 216 |
+
],
|
| 217 |
+
dtype=np.uint8,
|
| 218 |
+
)
|
| 219 |
+
img = np.zeros((4, 5, 5), dtype=np.uint8)
|
| 220 |
+
start = (1, 0, 2)
|
| 221 |
+
end = (3, 2, 3)
|
| 222 |
+
pp, rr, cc = rectangle(start, end=end, shape=img.shape)
|
| 223 |
+
img[pp, rr, cc] = 1
|
| 224 |
+
assert_array_equal(img, expected)
|
infer_4_47_1/lib/python3.10/site-packages/skimage/draw/tests/test_draw_nd.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from skimage.draw import line_nd
|
| 2 |
+
from skimage._shared.testing import assert_equal
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def test_empty_line():
|
| 6 |
+
coords = line_nd((1, 1, 1), (1, 1, 1))
|
| 7 |
+
assert len(coords) == 3
|
| 8 |
+
assert all(len(c) == 0 for c in coords)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def test_zero_line():
|
| 12 |
+
coords = line_nd((-1, -1), (2, 2))
|
| 13 |
+
assert_equal(coords, [[-1, 0, 1], [-1, 0, 1]])
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def test_no_round():
|
| 17 |
+
coords = line_nd((0.5, 0), (2.5, 0), integer=False, endpoint=True)
|
| 18 |
+
assert_equal(coords, [[0.5, 1.5, 2.5], [0, 0, 0]])
|
infer_4_47_1/lib/python3.10/site-packages/skimage/draw/tests/test_polygon2mask.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
from skimage import draw
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
image_shape = (512, 512)
|
| 7 |
+
polygon = np.array(
|
| 8 |
+
[[80, 111, 146, 234, 407, 300, 187, 45], [465, 438, 499, 380, 450, 287, 210, 167]]
|
| 9 |
+
).T
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def test_polygon2mask():
|
| 13 |
+
mask = draw.polygon2mask(image_shape, polygon)
|
| 14 |
+
assert mask.shape == image_shape
|
| 15 |
+
assert mask.sum() == 57653
|
infer_4_47_1/lib/python3.10/site-packages/skimage/draw/tests/test_random_shapes.py
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from skimage._shared import testing
|
| 5 |
+
from skimage._shared._warnings import expected_warnings
|
| 6 |
+
from skimage.draw import random_shapes
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def test_generates_color_images_with_correct_shape():
|
| 10 |
+
image, _ = random_shapes((128, 128), max_shapes=10)
|
| 11 |
+
assert image.shape == (128, 128, 3)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def test_generates_gray_images_with_correct_shape():
|
| 15 |
+
image, _ = random_shapes(
|
| 16 |
+
(4567, 123), min_shapes=3, max_shapes=20, channel_axis=None
|
| 17 |
+
)
|
| 18 |
+
assert image.shape == (4567, 123)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def test_generates_gray_images_with_correct_shape_deprecated_multichannel():
|
| 22 |
+
image, _ = random_shapes(
|
| 23 |
+
(4567, 123), min_shapes=3, max_shapes=20, channel_axis=None
|
| 24 |
+
)
|
| 25 |
+
assert image.shape == (4567, 123)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
@pytest.mark.parametrize('channel_axis', [None, 0, 1, 2])
|
| 29 |
+
def test_generated_shape_for_channel_axis(channel_axis):
|
| 30 |
+
shape = (128, 64)
|
| 31 |
+
num_channels = 5
|
| 32 |
+
|
| 33 |
+
image, _ = random_shapes(
|
| 34 |
+
shape,
|
| 35 |
+
num_channels=num_channels,
|
| 36 |
+
min_shapes=3,
|
| 37 |
+
max_shapes=10,
|
| 38 |
+
channel_axis=channel_axis,
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
if channel_axis is None:
|
| 42 |
+
expected_shape = shape
|
| 43 |
+
else:
|
| 44 |
+
expected_shape = tuple(np.insert(shape, channel_axis, num_channels))
|
| 45 |
+
|
| 46 |
+
assert image.shape == expected_shape
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def test_generates_correct_bounding_boxes_for_rectangles():
|
| 50 |
+
image, labels = random_shapes((128, 128), max_shapes=1, shape='rectangle', rng=42)
|
| 51 |
+
|
| 52 |
+
assert len(labels) == 1
|
| 53 |
+
label, bbox = labels[0]
|
| 54 |
+
assert label == 'rectangle', label
|
| 55 |
+
|
| 56 |
+
crop = image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]]
|
| 57 |
+
|
| 58 |
+
# The crop is filled.
|
| 59 |
+
assert (crop >= 0).all() and (crop < 255).all()
|
| 60 |
+
|
| 61 |
+
# The crop is complete.
|
| 62 |
+
image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]] = 255
|
| 63 |
+
assert (image == 255).all()
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def test_generates_correct_bounding_boxes_for_triangles():
|
| 67 |
+
image, labels = random_shapes((128, 128), max_shapes=1, shape='triangle', rng=42)
|
| 68 |
+
assert len(labels) == 1
|
| 69 |
+
label, bbox = labels[0]
|
| 70 |
+
assert label == 'triangle', label
|
| 71 |
+
|
| 72 |
+
crop = image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]]
|
| 73 |
+
|
| 74 |
+
# The crop is filled.
|
| 75 |
+
assert (crop >= 0).any() and (crop < 255).any()
|
| 76 |
+
|
| 77 |
+
# The crop is complete.
|
| 78 |
+
image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]] = 255
|
| 79 |
+
assert (image == 255).all()
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def test_generates_correct_bounding_boxes_for_circles():
|
| 83 |
+
image, labels = random_shapes(
|
| 84 |
+
(43, 44), max_shapes=1, min_size=20, max_size=20, shape='circle', rng=42
|
| 85 |
+
)
|
| 86 |
+
assert len(labels) == 1
|
| 87 |
+
label, bbox = labels[0]
|
| 88 |
+
assert label == 'circle', label
|
| 89 |
+
|
| 90 |
+
crop = image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]]
|
| 91 |
+
|
| 92 |
+
# The crop is filled.
|
| 93 |
+
assert (crop >= 0).any() and (crop < 255).any()
|
| 94 |
+
|
| 95 |
+
# The crop is complete.
|
| 96 |
+
image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]] = 255
|
| 97 |
+
assert (image == 255).all()
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def test_generates_correct_bounding_boxes_for_ellipses():
|
| 101 |
+
image, labels = random_shapes(
|
| 102 |
+
(43, 44), max_shapes=1, min_size=20, max_size=20, shape='ellipse', rng=42
|
| 103 |
+
)
|
| 104 |
+
assert len(labels) == 1
|
| 105 |
+
label, bbox = labels[0]
|
| 106 |
+
assert label == 'ellipse', label
|
| 107 |
+
|
| 108 |
+
crop = image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]]
|
| 109 |
+
|
| 110 |
+
# The crop is filled.
|
| 111 |
+
assert (crop >= 0).any() and (crop < 255).any()
|
| 112 |
+
|
| 113 |
+
# The crop is complete.
|
| 114 |
+
image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]] = 255
|
| 115 |
+
assert (image == 255).all()
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def test_generate_circle_throws_when_size_too_small():
|
| 119 |
+
with testing.raises(ValueError):
|
| 120 |
+
random_shapes((64, 128), max_shapes=1, min_size=1, max_size=1, shape='circle')
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def test_generate_ellipse_throws_when_size_too_small():
|
| 124 |
+
with testing.raises(ValueError):
|
| 125 |
+
random_shapes((64, 128), max_shapes=1, min_size=1, max_size=1, shape='ellipse')
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def test_generate_triangle_throws_when_size_too_small():
|
| 129 |
+
with testing.raises(ValueError):
|
| 130 |
+
random_shapes((128, 64), max_shapes=1, min_size=1, max_size=1, shape='triangle')
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def test_can_generate_one_by_one_rectangle():
|
| 134 |
+
image, labels = random_shapes(
|
| 135 |
+
(50, 128), max_shapes=1, min_size=1, max_size=1, shape='rectangle'
|
| 136 |
+
)
|
| 137 |
+
assert len(labels) == 1
|
| 138 |
+
_, bbox = labels[0]
|
| 139 |
+
crop = image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]]
|
| 140 |
+
|
| 141 |
+
# rgb
|
| 142 |
+
assert np.shape(crop) == (1, 1, 3) and np.any(crop >= 1) and np.any(crop < 255)
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def test_throws_when_intensity_range_out_of_range():
|
| 146 |
+
with testing.raises(ValueError):
|
| 147 |
+
random_shapes(
|
| 148 |
+
(1000, 1234), max_shapes=1, channel_axis=None, intensity_range=(0, 256)
|
| 149 |
+
)
|
| 150 |
+
with testing.raises(ValueError):
|
| 151 |
+
random_shapes((2, 2), max_shapes=1, intensity_range=((-1, 255),))
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def test_returns_empty_labels_and_white_image_when_cannot_fit_shape():
|
| 155 |
+
# The circle will never fit this.
|
| 156 |
+
with expected_warnings(['Could not fit']):
|
| 157 |
+
image, labels = random_shapes(
|
| 158 |
+
(10000, 10000), max_shapes=1, min_size=10000, shape='circle'
|
| 159 |
+
)
|
| 160 |
+
assert len(labels) == 0
|
| 161 |
+
assert (image == 255).all()
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def test_random_shapes_is_reproducible_with_seed():
|
| 165 |
+
random_seed = 42
|
| 166 |
+
labels = []
|
| 167 |
+
for _ in range(5):
|
| 168 |
+
_, label = random_shapes((128, 128), max_shapes=5, rng=random_seed)
|
| 169 |
+
labels.append(label)
|
| 170 |
+
assert all(other == labels[0] for other in labels[1:])
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
def test_generates_white_image_when_intensity_range_255():
|
| 174 |
+
image, labels = random_shapes(
|
| 175 |
+
(128, 128), max_shapes=3, intensity_range=((255, 255),), rng=42
|
| 176 |
+
)
|
| 177 |
+
assert len(labels) > 0
|
| 178 |
+
assert (image == 255).all()
|
infer_4_47_1/lib/python3.10/site-packages/skimage/feature/_texture.cpython-310-x86_64-linux-gnu.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:91efbe3cf5cda615657a302daea1bbeb1ed270401c795fa85dd7871656f6dcdc
|
| 3 |
+
size 398936
|
infer_4_47_1/lib/python3.10/site-packages/skimage/morphology/__pycache__/_util.cpython-310.pyc
ADDED
|
Binary file (11.1 kB). View file
|
|
|
infer_4_47_1/lib/python3.10/site-packages/skimage/morphology/__pycache__/gray.cpython-310.pyc
ADDED
|
Binary file (23.9 kB). View file
|
|
|
infer_4_47_1/lib/python3.10/site-packages/skimage/morphology/_convex_hull.cpython-310-x86_64-linux-gnu.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cd110bbb9d540f7168ffe264050861f0629facfed8ef40e52ff57e2e8c0db7ef
|
| 3 |
+
size 236312
|
infer_4_47_1/lib/python3.10/site-packages/skimage/morphology/_grayreconstruct.cpython-310-x86_64-linux-gnu.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3fe6f109ed45a049f330ef30144c19422287b6e2bf209150e70a58d4f90cb6c1
|
| 3 |
+
size 294480
|
infer_4_47_1/lib/python3.10/site-packages/skimage/morphology/ball_decompositions.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4f9eb51f361fd7d7d22d342dec7bb98178a21aa9c944507f25898b8ca213e54d
|
| 3 |
+
size 431
|
infer_4_47_1/lib/python3.10/site-packages/skimage/morphology/tests/__pycache__/test_skeletonize.cpython-310.pyc
ADDED
|
Binary file (13.2 kB). View file
|
|
|
infer_4_47_1/lib/python3.10/site-packages/skimage/registration/__init__.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Image registration algorithms, e.g., optical flow or phase cross correlation."""
|
| 2 |
+
|
| 3 |
+
import lazy_loader as _lazy
|
| 4 |
+
|
| 5 |
+
__getattr__, __dir__, __all__ = _lazy.attach_stub(__name__, __file__)
|
infer_4_47_1/lib/python3.10/site-packages/skimage/registration/__init__.pyi
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Explicitly setting `__all__` is necessary for type inference engines
|
| 2 |
+
# to know which symbols are exported. See
|
| 3 |
+
# https://peps.python.org/pep-0484/#stub-files
|
| 4 |
+
|
| 5 |
+
__all__ = ['optical_flow_ilk', 'optical_flow_tvl1', 'phase_cross_correlation']
|
| 6 |
+
|
| 7 |
+
from ._optical_flow import optical_flow_tvl1, optical_flow_ilk
|
| 8 |
+
from ._phase_cross_correlation import phase_cross_correlation
|
infer_4_47_1/lib/python3.10/site-packages/skimage/registration/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (386 Bytes). View file
|
|
|
infer_4_47_1/lib/python3.10/site-packages/skimage/registration/__pycache__/_masked_phase_cross_correlation.cpython-310.pyc
ADDED
|
Binary file (8.97 kB). View file
|
|
|
infer_4_47_1/lib/python3.10/site-packages/skimage/registration/__pycache__/_optical_flow.cpython-310.pyc
ADDED
|
Binary file (12.3 kB). View file
|
|
|
infer_4_47_1/lib/python3.10/site-packages/skimage/registration/__pycache__/_phase_cross_correlation.cpython-310.pyc
ADDED
|
Binary file (15.1 kB). View file
|
|
|
infer_4_47_1/lib/python3.10/site-packages/skimage/registration/_masked_phase_cross_correlation.py
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Implementation of the masked normalized cross-correlation.
|
| 3 |
+
|
| 4 |
+
Based on the following publication:
|
| 5 |
+
D. Padfield. Masked object registration in the Fourier domain.
|
| 6 |
+
IEEE Transactions on Image Processing (2012)
|
| 7 |
+
|
| 8 |
+
and the author's original MATLAB implementation, available on this website:
|
| 9 |
+
http://www.dirkpadfield.com/
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
from functools import partial
|
| 13 |
+
|
| 14 |
+
import numpy as np
|
| 15 |
+
import scipy.fft as fftmodule
|
| 16 |
+
from scipy.fft import next_fast_len
|
| 17 |
+
|
| 18 |
+
from .._shared.utils import _supported_float_type
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def _masked_phase_cross_correlation(
|
| 22 |
+
reference_image, moving_image, reference_mask, moving_mask=None, overlap_ratio=0.3
|
| 23 |
+
):
|
| 24 |
+
"""Masked image translation registration by masked normalized
|
| 25 |
+
cross-correlation.
|
| 26 |
+
|
| 27 |
+
Parameters
|
| 28 |
+
----------
|
| 29 |
+
reference_image : ndarray
|
| 30 |
+
Reference image.
|
| 31 |
+
moving_image : ndarray
|
| 32 |
+
Image to register. Must be same dimensionality as ``reference_image``,
|
| 33 |
+
but not necessarily the same size.
|
| 34 |
+
reference_mask : ndarray
|
| 35 |
+
Boolean mask for ``reference_image``. The mask should evaluate
|
| 36 |
+
to ``True`` (or 1) on valid pixels. ``reference_mask`` should
|
| 37 |
+
have the same shape as ``reference_image``.
|
| 38 |
+
moving_mask : ndarray or None, optional
|
| 39 |
+
Boolean mask for ``moving_image``. The mask should evaluate to ``True``
|
| 40 |
+
(or 1) on valid pixels. ``moving_mask`` should have the same shape
|
| 41 |
+
as ``moving_image``. If ``None``, ``reference_mask`` will be used.
|
| 42 |
+
overlap_ratio : float, optional
|
| 43 |
+
Minimum allowed overlap ratio between images. The correlation for
|
| 44 |
+
translations corresponding with an overlap ratio lower than this
|
| 45 |
+
threshold will be ignored. A lower `overlap_ratio` leads to smaller
|
| 46 |
+
maximum translation, while a higher `overlap_ratio` leads to greater
|
| 47 |
+
robustness against spurious matches due to small overlap between
|
| 48 |
+
masked images.
|
| 49 |
+
|
| 50 |
+
Returns
|
| 51 |
+
-------
|
| 52 |
+
shifts : ndarray
|
| 53 |
+
Shift vector (in pixels) required to register ``moving_image``
|
| 54 |
+
with ``reference_image``. Axis ordering is consistent with numpy.
|
| 55 |
+
|
| 56 |
+
References
|
| 57 |
+
----------
|
| 58 |
+
.. [1] Dirk Padfield. Masked Object Registration in the Fourier Domain.
|
| 59 |
+
IEEE Transactions on Image Processing, vol. 21(5),
|
| 60 |
+
pp. 2706-2718 (2012). :DOI:`10.1109/TIP.2011.2181402`
|
| 61 |
+
.. [2] D. Padfield. "Masked FFT registration". In Proc. Computer Vision and
|
| 62 |
+
Pattern Recognition, pp. 2918-2925 (2010).
|
| 63 |
+
:DOI:`10.1109/CVPR.2010.5540032`
|
| 64 |
+
|
| 65 |
+
"""
|
| 66 |
+
if moving_mask is None:
|
| 67 |
+
if reference_image.shape != moving_image.shape:
|
| 68 |
+
raise ValueError(
|
| 69 |
+
"Input images have different shapes, moving_mask must "
|
| 70 |
+
"be explicitly set."
|
| 71 |
+
)
|
| 72 |
+
moving_mask = reference_mask.astype(bool)
|
| 73 |
+
|
| 74 |
+
# We need masks to be of the same size as their respective images
|
| 75 |
+
for im, mask in [(reference_image, reference_mask), (moving_image, moving_mask)]:
|
| 76 |
+
if im.shape != mask.shape:
|
| 77 |
+
raise ValueError("Image sizes must match their respective mask sizes.")
|
| 78 |
+
|
| 79 |
+
xcorr = cross_correlate_masked(
|
| 80 |
+
moving_image,
|
| 81 |
+
reference_image,
|
| 82 |
+
moving_mask,
|
| 83 |
+
reference_mask,
|
| 84 |
+
axes=tuple(range(moving_image.ndim)),
|
| 85 |
+
mode='full',
|
| 86 |
+
overlap_ratio=overlap_ratio,
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# Generalize to the average of multiple equal maxima
|
| 90 |
+
maxima = np.stack(np.nonzero(xcorr == xcorr.max()), axis=1)
|
| 91 |
+
center = np.mean(maxima, axis=0)
|
| 92 |
+
shifts = center - np.array(reference_image.shape) + 1
|
| 93 |
+
|
| 94 |
+
# The mismatch in size will impact the center location of the
|
| 95 |
+
# cross-correlation
|
| 96 |
+
size_mismatch = np.array(moving_image.shape) - np.array(reference_image.shape)
|
| 97 |
+
|
| 98 |
+
return -shifts + (size_mismatch / 2)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def cross_correlate_masked(
|
| 102 |
+
arr1, arr2, m1, m2, mode='full', axes=(-2, -1), overlap_ratio=0.3
|
| 103 |
+
):
|
| 104 |
+
"""
|
| 105 |
+
Masked normalized cross-correlation between arrays.
|
| 106 |
+
|
| 107 |
+
Parameters
|
| 108 |
+
----------
|
| 109 |
+
arr1 : ndarray
|
| 110 |
+
First array.
|
| 111 |
+
arr2 : ndarray
|
| 112 |
+
Seconds array. The dimensions of `arr2` along axes that are not
|
| 113 |
+
transformed should be equal to that of `arr1`.
|
| 114 |
+
m1 : ndarray
|
| 115 |
+
Mask of `arr1`. The mask should evaluate to `True`
|
| 116 |
+
(or 1) on valid pixels. `m1` should have the same shape as `arr1`.
|
| 117 |
+
m2 : ndarray
|
| 118 |
+
Mask of `arr2`. The mask should evaluate to `True`
|
| 119 |
+
(or 1) on valid pixels. `m2` should have the same shape as `arr2`.
|
| 120 |
+
mode : {'full', 'same'}, optional
|
| 121 |
+
'full':
|
| 122 |
+
This returns the convolution at each point of overlap. At
|
| 123 |
+
the end-points of the convolution, the signals do not overlap
|
| 124 |
+
completely, and boundary effects may be seen.
|
| 125 |
+
'same':
|
| 126 |
+
The output is the same size as `arr1`, centered with respect
|
| 127 |
+
to the `‘full’` output. Boundary effects are less prominent.
|
| 128 |
+
axes : tuple of ints, optional
|
| 129 |
+
Axes along which to compute the cross-correlation.
|
| 130 |
+
overlap_ratio : float, optional
|
| 131 |
+
Minimum allowed overlap ratio between images. The correlation for
|
| 132 |
+
translations corresponding with an overlap ratio lower than this
|
| 133 |
+
threshold will be ignored. A lower `overlap_ratio` leads to smaller
|
| 134 |
+
maximum translation, while a higher `overlap_ratio` leads to greater
|
| 135 |
+
robustness against spurious matches due to small overlap between
|
| 136 |
+
masked images.
|
| 137 |
+
|
| 138 |
+
Returns
|
| 139 |
+
-------
|
| 140 |
+
out : ndarray
|
| 141 |
+
Masked normalized cross-correlation.
|
| 142 |
+
|
| 143 |
+
Raises
|
| 144 |
+
------
|
| 145 |
+
ValueError : if correlation `mode` is not valid, or array dimensions along
|
| 146 |
+
non-transformation axes are not equal.
|
| 147 |
+
|
| 148 |
+
References
|
| 149 |
+
----------
|
| 150 |
+
.. [1] Dirk Padfield. Masked Object Registration in the Fourier Domain.
|
| 151 |
+
IEEE Transactions on Image Processing, vol. 21(5),
|
| 152 |
+
pp. 2706-2718 (2012). :DOI:`10.1109/TIP.2011.2181402`
|
| 153 |
+
.. [2] D. Padfield. "Masked FFT registration". In Proc. Computer Vision and
|
| 154 |
+
Pattern Recognition, pp. 2918-2925 (2010).
|
| 155 |
+
:DOI:`10.1109/CVPR.2010.5540032`
|
| 156 |
+
"""
|
| 157 |
+
if mode not in {'full', 'same'}:
|
| 158 |
+
raise ValueError(f"Correlation mode '{mode}' is not valid.")
|
| 159 |
+
|
| 160 |
+
fixed_image = np.asarray(arr1)
|
| 161 |
+
moving_image = np.asarray(arr2)
|
| 162 |
+
float_dtype = _supported_float_type((fixed_image.dtype, moving_image.dtype))
|
| 163 |
+
if float_dtype.kind == 'c':
|
| 164 |
+
raise ValueError("complex-valued arr1, arr2 are not supported")
|
| 165 |
+
|
| 166 |
+
fixed_image = fixed_image.astype(float_dtype)
|
| 167 |
+
fixed_mask = np.array(m1, dtype=bool)
|
| 168 |
+
moving_image = moving_image.astype(float_dtype)
|
| 169 |
+
moving_mask = np.array(m2, dtype=bool)
|
| 170 |
+
eps = np.finfo(float_dtype).eps
|
| 171 |
+
|
| 172 |
+
# Array dimensions along non-transformation axes should be equal.
|
| 173 |
+
all_axes = set(range(fixed_image.ndim))
|
| 174 |
+
for axis in all_axes - set(axes):
|
| 175 |
+
if fixed_image.shape[axis] != moving_image.shape[axis]:
|
| 176 |
+
raise ValueError(
|
| 177 |
+
f'Array shapes along non-transformation axes should be '
|
| 178 |
+
f'equal, but dimensions along axis {axis} are not.'
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
# Determine final size along transformation axes
|
| 182 |
+
# Note that it might be faster to compute Fourier transform in a slightly
|
| 183 |
+
# larger shape (`fast_shape`). Then, after all fourier transforms are done,
|
| 184 |
+
# we slice back to`final_shape` using `final_slice`.
|
| 185 |
+
final_shape = list(arr1.shape)
|
| 186 |
+
for axis in axes:
|
| 187 |
+
final_shape[axis] = fixed_image.shape[axis] + moving_image.shape[axis] - 1
|
| 188 |
+
final_shape = tuple(final_shape)
|
| 189 |
+
final_slice = tuple([slice(0, int(sz)) for sz in final_shape])
|
| 190 |
+
|
| 191 |
+
# Extent transform axes to the next fast length (i.e. multiple of 3, 5, or
|
| 192 |
+
# 7)
|
| 193 |
+
fast_shape = tuple([next_fast_len(final_shape[ax]) for ax in axes])
|
| 194 |
+
|
| 195 |
+
# We use the new scipy.fft because they allow leaving the transform axes
|
| 196 |
+
# unchanged which was not possible with scipy.fftpack's
|
| 197 |
+
# fftn/ifftn in older versions of SciPy.
|
| 198 |
+
# E.g. arr shape (2, 3, 7), transform along axes (0, 1) with shape (4, 4)
|
| 199 |
+
# results in arr_fft shape (4, 4, 7)
|
| 200 |
+
fft = partial(fftmodule.fftn, s=fast_shape, axes=axes)
|
| 201 |
+
_ifft = partial(fftmodule.ifftn, s=fast_shape, axes=axes)
|
| 202 |
+
|
| 203 |
+
def ifft(x):
|
| 204 |
+
return _ifft(x).real
|
| 205 |
+
|
| 206 |
+
fixed_image[np.logical_not(fixed_mask)] = 0.0
|
| 207 |
+
moving_image[np.logical_not(moving_mask)] = 0.0
|
| 208 |
+
|
| 209 |
+
# N-dimensional analog to rotation by 180deg is flip over all relevant axes.
|
| 210 |
+
# See [1] for discussion.
|
| 211 |
+
rotated_moving_image = _flip(moving_image, axes=axes)
|
| 212 |
+
rotated_moving_mask = _flip(moving_mask, axes=axes)
|
| 213 |
+
|
| 214 |
+
fixed_fft = fft(fixed_image)
|
| 215 |
+
rotated_moving_fft = fft(rotated_moving_image)
|
| 216 |
+
fixed_mask_fft = fft(fixed_mask.astype(float_dtype))
|
| 217 |
+
rotated_moving_mask_fft = fft(rotated_moving_mask.astype(float_dtype))
|
| 218 |
+
|
| 219 |
+
# Calculate overlap of masks at every point in the convolution.
|
| 220 |
+
# Locations with high overlap should not be taken into account.
|
| 221 |
+
number_overlap_masked_px = ifft(rotated_moving_mask_fft * fixed_mask_fft)
|
| 222 |
+
number_overlap_masked_px[:] = np.round(number_overlap_masked_px)
|
| 223 |
+
number_overlap_masked_px[:] = np.fmax(number_overlap_masked_px, eps)
|
| 224 |
+
masked_correlated_fixed_fft = ifft(rotated_moving_mask_fft * fixed_fft)
|
| 225 |
+
masked_correlated_rotated_moving_fft = ifft(fixed_mask_fft * rotated_moving_fft)
|
| 226 |
+
|
| 227 |
+
numerator = ifft(rotated_moving_fft * fixed_fft)
|
| 228 |
+
numerator -= (
|
| 229 |
+
masked_correlated_fixed_fft
|
| 230 |
+
* masked_correlated_rotated_moving_fft
|
| 231 |
+
/ number_overlap_masked_px
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
fixed_squared_fft = fft(np.square(fixed_image))
|
| 235 |
+
fixed_denom = ifft(rotated_moving_mask_fft * fixed_squared_fft)
|
| 236 |
+
fixed_denom -= np.square(masked_correlated_fixed_fft) / number_overlap_masked_px
|
| 237 |
+
fixed_denom[:] = np.fmax(fixed_denom, 0.0)
|
| 238 |
+
|
| 239 |
+
rotated_moving_squared_fft = fft(np.square(rotated_moving_image))
|
| 240 |
+
moving_denom = ifft(fixed_mask_fft * rotated_moving_squared_fft)
|
| 241 |
+
moving_denom -= (
|
| 242 |
+
np.square(masked_correlated_rotated_moving_fft) / number_overlap_masked_px
|
| 243 |
+
)
|
| 244 |
+
moving_denom[:] = np.fmax(moving_denom, 0.0)
|
| 245 |
+
|
| 246 |
+
denom = np.sqrt(fixed_denom * moving_denom)
|
| 247 |
+
|
| 248 |
+
# Slice back to expected convolution shape.
|
| 249 |
+
numerator = numerator[final_slice]
|
| 250 |
+
denom = denom[final_slice]
|
| 251 |
+
number_overlap_masked_px = number_overlap_masked_px[final_slice]
|
| 252 |
+
|
| 253 |
+
if mode == 'same':
|
| 254 |
+
_centering = partial(_centered, newshape=fixed_image.shape, axes=axes)
|
| 255 |
+
denom = _centering(denom)
|
| 256 |
+
numerator = _centering(numerator)
|
| 257 |
+
number_overlap_masked_px = _centering(number_overlap_masked_px)
|
| 258 |
+
|
| 259 |
+
# Pixels where `denom` is very small will introduce large
|
| 260 |
+
# numbers after division. To get around this problem,
|
| 261 |
+
# we zero-out problematic pixels.
|
| 262 |
+
tol = 1e3 * eps * np.max(np.abs(denom), axis=axes, keepdims=True)
|
| 263 |
+
nonzero_indices = denom > tol
|
| 264 |
+
|
| 265 |
+
# explicitly set out dtype for compatibility with SciPy < 1.4, where
|
| 266 |
+
# fftmodule will be numpy.fft which always uses float64 dtype.
|
| 267 |
+
out = np.zeros_like(denom, dtype=float_dtype)
|
| 268 |
+
out[nonzero_indices] = numerator[nonzero_indices] / denom[nonzero_indices]
|
| 269 |
+
np.clip(out, a_min=-1, a_max=1, out=out)
|
| 270 |
+
|
| 271 |
+
# Apply overlap ratio threshold
|
| 272 |
+
number_px_threshold = overlap_ratio * np.max(
|
| 273 |
+
number_overlap_masked_px, axis=axes, keepdims=True
|
| 274 |
+
)
|
| 275 |
+
out[number_overlap_masked_px < number_px_threshold] = 0.0
|
| 276 |
+
|
| 277 |
+
return out
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
def _centered(arr, newshape, axes):
|
| 281 |
+
"""Return the center `newshape` portion of `arr`, leaving axes not
|
| 282 |
+
in `axes` untouched."""
|
| 283 |
+
newshape = np.asarray(newshape)
|
| 284 |
+
currshape = np.array(arr.shape)
|
| 285 |
+
|
| 286 |
+
slices = [slice(None, None)] * arr.ndim
|
| 287 |
+
|
| 288 |
+
for ax in axes:
|
| 289 |
+
startind = (currshape[ax] - newshape[ax]) // 2
|
| 290 |
+
endind = startind + newshape[ax]
|
| 291 |
+
slices[ax] = slice(startind, endind)
|
| 292 |
+
|
| 293 |
+
return arr[tuple(slices)]
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
def _flip(arr, axes=None):
|
| 297 |
+
"""Reverse array over many axes. Generalization of arr[::-1] for many
|
| 298 |
+
dimensions. If `axes` is `None`, flip along all axes."""
|
| 299 |
+
if axes is None:
|
| 300 |
+
reverse = [slice(None, None, -1)] * arr.ndim
|
| 301 |
+
else:
|
| 302 |
+
reverse = [slice(None, None, None)] * arr.ndim
|
| 303 |
+
for axis in axes:
|
| 304 |
+
reverse[axis] = slice(None, None, -1)
|
| 305 |
+
|
| 306 |
+
return arr[tuple(reverse)]
|
infer_4_47_1/lib/python3.10/site-packages/skimage/registration/_optical_flow.py
ADDED
|
@@ -0,0 +1,429 @@
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""TV-L1 optical flow algorithm implementation."""
|
| 2 |
+
|
| 3 |
+
from functools import partial
|
| 4 |
+
from itertools import combinations_with_replacement
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
from scipy import ndimage as ndi
|
| 8 |
+
|
| 9 |
+
from .._shared.filters import gaussian as gaussian_filter
|
| 10 |
+
from .._shared.utils import _supported_float_type
|
| 11 |
+
from ..transform import warp
|
| 12 |
+
from ._optical_flow_utils import _coarse_to_fine, _get_warp_points
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def _tvl1(
|
| 16 |
+
reference_image,
|
| 17 |
+
moving_image,
|
| 18 |
+
flow0,
|
| 19 |
+
attachment,
|
| 20 |
+
tightness,
|
| 21 |
+
num_warp,
|
| 22 |
+
num_iter,
|
| 23 |
+
tol,
|
| 24 |
+
prefilter,
|
| 25 |
+
):
|
| 26 |
+
"""TV-L1 solver for optical flow estimation.
|
| 27 |
+
|
| 28 |
+
Parameters
|
| 29 |
+
----------
|
| 30 |
+
reference_image : ndarray, shape (M, N[, P[, ...]])
|
| 31 |
+
The first grayscale image of the sequence.
|
| 32 |
+
moving_image : ndarray, shape (M, N[, P[, ...]])
|
| 33 |
+
The second grayscale image of the sequence.
|
| 34 |
+
flow0 : ndarray, shape (image0.ndim, M, N[, P[, ...]])
|
| 35 |
+
Initialization for the vector field.
|
| 36 |
+
attachment : float
|
| 37 |
+
Attachment parameter. The smaller this parameter is,
|
| 38 |
+
the smoother is the solutions.
|
| 39 |
+
tightness : float
|
| 40 |
+
Tightness parameter. It should have a small value in order to
|
| 41 |
+
maintain attachment and regularization parts in
|
| 42 |
+
correspondence.
|
| 43 |
+
num_warp : int
|
| 44 |
+
Number of times moving_image is warped.
|
| 45 |
+
num_iter : int
|
| 46 |
+
Number of fixed point iteration.
|
| 47 |
+
tol : float
|
| 48 |
+
Tolerance used as stopping criterion based on the L² distance
|
| 49 |
+
between two consecutive values of (u, v).
|
| 50 |
+
prefilter : bool
|
| 51 |
+
Whether to prefilter the estimated optical flow before each
|
| 52 |
+
image warp.
|
| 53 |
+
|
| 54 |
+
Returns
|
| 55 |
+
-------
|
| 56 |
+
flow : ndarray, shape (image0.ndim, M, N[, P[, ...]])
|
| 57 |
+
The estimated optical flow components for each axis.
|
| 58 |
+
|
| 59 |
+
"""
|
| 60 |
+
|
| 61 |
+
dtype = reference_image.dtype
|
| 62 |
+
grid = np.meshgrid(
|
| 63 |
+
*[np.arange(n, dtype=dtype) for n in reference_image.shape],
|
| 64 |
+
indexing='ij',
|
| 65 |
+
sparse=True,
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# dt corresponds to tau in [3]_, i.e. the time step
|
| 69 |
+
dt = 0.5 / reference_image.ndim
|
| 70 |
+
reg_num_iter = 2
|
| 71 |
+
f0 = attachment * tightness
|
| 72 |
+
f1 = dt / tightness
|
| 73 |
+
tol *= reference_image.size
|
| 74 |
+
|
| 75 |
+
flow_current = flow_previous = flow0
|
| 76 |
+
|
| 77 |
+
g = np.zeros((reference_image.ndim,) + reference_image.shape, dtype=dtype)
|
| 78 |
+
proj = np.zeros(
|
| 79 |
+
(
|
| 80 |
+
reference_image.ndim,
|
| 81 |
+
reference_image.ndim,
|
| 82 |
+
)
|
| 83 |
+
+ reference_image.shape,
|
| 84 |
+
dtype=dtype,
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
s_g = [
|
| 88 |
+
slice(None),
|
| 89 |
+
] * g.ndim
|
| 90 |
+
s_p = [
|
| 91 |
+
slice(None),
|
| 92 |
+
] * proj.ndim
|
| 93 |
+
s_d = [
|
| 94 |
+
slice(None),
|
| 95 |
+
] * (proj.ndim - 2)
|
| 96 |
+
|
| 97 |
+
for _ in range(num_warp):
|
| 98 |
+
if prefilter:
|
| 99 |
+
flow_current = ndi.median_filter(
|
| 100 |
+
flow_current, [1] + reference_image.ndim * [3]
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
image1_warp = warp(
|
| 104 |
+
moving_image, _get_warp_points(grid, flow_current), mode='edge'
|
| 105 |
+
)
|
| 106 |
+
grad = np.array(np.gradient(image1_warp))
|
| 107 |
+
NI = (grad * grad).sum(0)
|
| 108 |
+
NI[NI == 0] = 1
|
| 109 |
+
|
| 110 |
+
rho_0 = image1_warp - reference_image - (grad * flow_current).sum(0)
|
| 111 |
+
|
| 112 |
+
for _ in range(num_iter):
|
| 113 |
+
# Data term
|
| 114 |
+
|
| 115 |
+
rho = rho_0 + (grad * flow_current).sum(0)
|
| 116 |
+
|
| 117 |
+
idx = abs(rho) <= f0 * NI
|
| 118 |
+
|
| 119 |
+
flow_auxiliary = flow_current
|
| 120 |
+
|
| 121 |
+
flow_auxiliary[:, idx] -= rho[idx] * grad[:, idx] / NI[idx]
|
| 122 |
+
|
| 123 |
+
idx = ~idx
|
| 124 |
+
srho = f0 * np.sign(rho[idx])
|
| 125 |
+
flow_auxiliary[:, idx] -= srho * grad[:, idx]
|
| 126 |
+
|
| 127 |
+
# Regularization term
|
| 128 |
+
flow_current = flow_auxiliary.copy()
|
| 129 |
+
|
| 130 |
+
for idx in range(reference_image.ndim):
|
| 131 |
+
s_p[0] = idx
|
| 132 |
+
for _ in range(reg_num_iter):
|
| 133 |
+
for ax in range(reference_image.ndim):
|
| 134 |
+
s_g[0] = ax
|
| 135 |
+
s_g[ax + 1] = slice(0, -1)
|
| 136 |
+
g[tuple(s_g)] = np.diff(flow_current[idx], axis=ax)
|
| 137 |
+
s_g[ax + 1] = slice(None)
|
| 138 |
+
|
| 139 |
+
norm = np.sqrt((g**2).sum(0))[np.newaxis, ...]
|
| 140 |
+
norm *= f1
|
| 141 |
+
norm += 1.0
|
| 142 |
+
proj[idx] -= dt * g
|
| 143 |
+
proj[idx] /= norm
|
| 144 |
+
|
| 145 |
+
# d will be the (negative) divergence of proj[idx]
|
| 146 |
+
d = -proj[idx].sum(0)
|
| 147 |
+
for ax in range(reference_image.ndim):
|
| 148 |
+
s_p[1] = ax
|
| 149 |
+
s_p[ax + 2] = slice(0, -1)
|
| 150 |
+
s_d[ax] = slice(1, None)
|
| 151 |
+
d[tuple(s_d)] += proj[tuple(s_p)]
|
| 152 |
+
s_p[ax + 2] = slice(None)
|
| 153 |
+
s_d[ax] = slice(None)
|
| 154 |
+
|
| 155 |
+
flow_current[idx] = flow_auxiliary[idx] + d
|
| 156 |
+
|
| 157 |
+
flow_previous -= flow_current # The difference as stopping criteria
|
| 158 |
+
if (flow_previous * flow_previous).sum() < tol:
|
| 159 |
+
break
|
| 160 |
+
|
| 161 |
+
flow_previous = flow_current
|
| 162 |
+
|
| 163 |
+
return flow_current
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
def optical_flow_tvl1(
|
| 167 |
+
reference_image,
|
| 168 |
+
moving_image,
|
| 169 |
+
*,
|
| 170 |
+
attachment=15,
|
| 171 |
+
tightness=0.3,
|
| 172 |
+
num_warp=5,
|
| 173 |
+
num_iter=10,
|
| 174 |
+
tol=1e-4,
|
| 175 |
+
prefilter=False,
|
| 176 |
+
dtype=np.float32,
|
| 177 |
+
):
|
| 178 |
+
r"""Coarse to fine optical flow estimator.
|
| 179 |
+
|
| 180 |
+
The TV-L1 solver is applied at each level of the image
|
| 181 |
+
pyramid. TV-L1 is a popular algorithm for optical flow estimation
|
| 182 |
+
introduced by Zack et al. [1]_, improved in [2]_ and detailed in [3]_.
|
| 183 |
+
|
| 184 |
+
Parameters
|
| 185 |
+
----------
|
| 186 |
+
reference_image : ndarray, shape (M, N[, P[, ...]])
|
| 187 |
+
The first grayscale image of the sequence.
|
| 188 |
+
moving_image : ndarray, shape (M, N[, P[, ...]])
|
| 189 |
+
The second grayscale image of the sequence.
|
| 190 |
+
attachment : float, optional
|
| 191 |
+
Attachment parameter (:math:`\lambda` in [1]_). The smaller
|
| 192 |
+
this parameter is, the smoother the returned result will be.
|
| 193 |
+
tightness : float, optional
|
| 194 |
+
Tightness parameter (:math:`\theta` in [1]_). It should have
|
| 195 |
+
a small value in order to maintain attachment and
|
| 196 |
+
regularization parts in correspondence.
|
| 197 |
+
num_warp : int, optional
|
| 198 |
+
Number of times moving_image is warped.
|
| 199 |
+
num_iter : int, optional
|
| 200 |
+
Number of fixed point iteration.
|
| 201 |
+
tol : float, optional
|
| 202 |
+
Tolerance used as stopping criterion based on the L² distance
|
| 203 |
+
between two consecutive values of (u, v).
|
| 204 |
+
prefilter : bool, optional
|
| 205 |
+
Whether to prefilter the estimated optical flow before each
|
| 206 |
+
image warp. When True, a median filter with window size 3
|
| 207 |
+
along each axis is applied. This helps to remove potential
|
| 208 |
+
outliers.
|
| 209 |
+
dtype : dtype, optional
|
| 210 |
+
Output data type: must be floating point. Single precision
|
| 211 |
+
provides good results and saves memory usage and computation
|
| 212 |
+
time compared to double precision.
|
| 213 |
+
|
| 214 |
+
Returns
|
| 215 |
+
-------
|
| 216 |
+
flow : ndarray, shape (image0.ndim, M, N[, P[, ...]])
|
| 217 |
+
The estimated optical flow components for each axis.
|
| 218 |
+
|
| 219 |
+
Notes
|
| 220 |
+
-----
|
| 221 |
+
Color images are not supported.
|
| 222 |
+
|
| 223 |
+
References
|
| 224 |
+
----------
|
| 225 |
+
.. [1] Zach, C., Pock, T., & Bischof, H. (2007, September). A
|
| 226 |
+
duality based approach for realtime TV-L 1 optical flow. In Joint
|
| 227 |
+
pattern recognition symposium (pp. 214-223). Springer, Berlin,
|
| 228 |
+
Heidelberg. :DOI:`10.1007/978-3-540-74936-3_22`
|
| 229 |
+
.. [2] Wedel, A., Pock, T., Zach, C., Bischof, H., & Cremers,
|
| 230 |
+
D. (2009). An improved algorithm for TV-L 1 optical flow. In
|
| 231 |
+
Statistical and geometrical approaches to visual motion analysis
|
| 232 |
+
(pp. 23-45). Springer, Berlin, Heidelberg.
|
| 233 |
+
:DOI:`10.1007/978-3-642-03061-1_2`
|
| 234 |
+
.. [3] Pérez, J. S., Meinhardt-Llopis, E., & Facciolo,
|
| 235 |
+
G. (2013). TV-L1 optical flow estimation. Image Processing On
|
| 236 |
+
Line, 2013, 137-150. :DOI:`10.5201/ipol.2013.26`
|
| 237 |
+
|
| 238 |
+
Examples
|
| 239 |
+
--------
|
| 240 |
+
>>> from skimage.color import rgb2gray
|
| 241 |
+
>>> from skimage.data import stereo_motorcycle
|
| 242 |
+
>>> from skimage.registration import optical_flow_tvl1
|
| 243 |
+
>>> image0, image1, disp = stereo_motorcycle()
|
| 244 |
+
>>> # --- Convert the images to gray level: color is not supported.
|
| 245 |
+
>>> image0 = rgb2gray(image0)
|
| 246 |
+
>>> image1 = rgb2gray(image1)
|
| 247 |
+
>>> flow = optical_flow_tvl1(image1, image0)
|
| 248 |
+
|
| 249 |
+
"""
|
| 250 |
+
|
| 251 |
+
solver = partial(
|
| 252 |
+
_tvl1,
|
| 253 |
+
attachment=attachment,
|
| 254 |
+
tightness=tightness,
|
| 255 |
+
num_warp=num_warp,
|
| 256 |
+
num_iter=num_iter,
|
| 257 |
+
tol=tol,
|
| 258 |
+
prefilter=prefilter,
|
| 259 |
+
)
|
| 260 |
+
|
| 261 |
+
if np.dtype(dtype) != _supported_float_type(dtype):
|
| 262 |
+
msg = f"dtype={dtype} is not supported. Try 'float32' or 'float64.'"
|
| 263 |
+
raise ValueError(msg)
|
| 264 |
+
|
| 265 |
+
return _coarse_to_fine(reference_image, moving_image, solver, dtype=dtype)
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
def _ilk(reference_image, moving_image, flow0, radius, num_warp, gaussian, prefilter):
|
| 269 |
+
"""Iterative Lucas-Kanade (iLK) solver for optical flow estimation.
|
| 270 |
+
|
| 271 |
+
Parameters
|
| 272 |
+
----------
|
| 273 |
+
reference_image : ndarray, shape (M, N[, P[, ...]])
|
| 274 |
+
The first grayscale image of the sequence.
|
| 275 |
+
moving_image : ndarray, shape (M, N[, P[, ...]])
|
| 276 |
+
The second grayscale image of the sequence.
|
| 277 |
+
flow0 : ndarray, shape (reference_image.ndim, M, N[, P[, ...]])
|
| 278 |
+
Initialization for the vector field.
|
| 279 |
+
radius : int
|
| 280 |
+
Radius of the window considered around each pixel.
|
| 281 |
+
num_warp : int
|
| 282 |
+
Number of times moving_image is warped.
|
| 283 |
+
gaussian : bool
|
| 284 |
+
if True, a gaussian kernel is used for the local
|
| 285 |
+
integration. Otherwise, a uniform kernel is used.
|
| 286 |
+
prefilter : bool
|
| 287 |
+
Whether to prefilter the estimated optical flow before each
|
| 288 |
+
image warp. This helps to remove potential outliers.
|
| 289 |
+
|
| 290 |
+
Returns
|
| 291 |
+
-------
|
| 292 |
+
flow : ndarray, shape (reference_image.ndim, M, N[, P[, ...]])
|
| 293 |
+
The estimated optical flow components for each axis.
|
| 294 |
+
|
| 295 |
+
"""
|
| 296 |
+
dtype = reference_image.dtype
|
| 297 |
+
ndim = reference_image.ndim
|
| 298 |
+
size = 2 * radius + 1
|
| 299 |
+
|
| 300 |
+
if gaussian:
|
| 301 |
+
sigma = ndim * (size / 4,)
|
| 302 |
+
filter_func = partial(gaussian_filter, sigma=sigma, mode='mirror')
|
| 303 |
+
else:
|
| 304 |
+
filter_func = partial(ndi.uniform_filter, size=ndim * (size,), mode='mirror')
|
| 305 |
+
|
| 306 |
+
flow = flow0
|
| 307 |
+
# For each pixel location (i, j), the optical flow X = flow[:, i, j]
|
| 308 |
+
# is the solution of the ndim x ndim linear system
|
| 309 |
+
# A[i, j] * X = b[i, j]
|
| 310 |
+
A = np.zeros(reference_image.shape + (ndim, ndim), dtype=dtype)
|
| 311 |
+
b = np.zeros(reference_image.shape + (ndim, 1), dtype=dtype)
|
| 312 |
+
|
| 313 |
+
grid = np.meshgrid(
|
| 314 |
+
*[np.arange(n, dtype=dtype) for n in reference_image.shape],
|
| 315 |
+
indexing='ij',
|
| 316 |
+
sparse=True,
|
| 317 |
+
)
|
| 318 |
+
|
| 319 |
+
for _ in range(num_warp):
|
| 320 |
+
if prefilter:
|
| 321 |
+
flow = ndi.median_filter(flow, (1,) + ndim * (3,))
|
| 322 |
+
|
| 323 |
+
moving_image_warp = warp(
|
| 324 |
+
moving_image, _get_warp_points(grid, flow), mode='edge'
|
| 325 |
+
)
|
| 326 |
+
grad = np.stack(np.gradient(moving_image_warp), axis=0)
|
| 327 |
+
error_image = (grad * flow).sum(axis=0) + reference_image - moving_image_warp
|
| 328 |
+
|
| 329 |
+
# Local linear systems creation
|
| 330 |
+
for i, j in combinations_with_replacement(range(ndim), 2):
|
| 331 |
+
A[..., i, j] = A[..., j, i] = filter_func(grad[i] * grad[j])
|
| 332 |
+
|
| 333 |
+
for i in range(ndim):
|
| 334 |
+
b[..., i, 0] = filter_func(grad[i] * error_image)
|
| 335 |
+
|
| 336 |
+
# Don't consider badly conditioned linear systems
|
| 337 |
+
idx = abs(np.linalg.det(A)) < 1e-14
|
| 338 |
+
A[idx] = np.eye(ndim, dtype=dtype)
|
| 339 |
+
b[idx] = 0
|
| 340 |
+
|
| 341 |
+
# Solve the local linear systems
|
| 342 |
+
flow = np.moveaxis(np.linalg.solve(A, b)[..., 0], ndim, 0)
|
| 343 |
+
|
| 344 |
+
return flow
|
| 345 |
+
|
| 346 |
+
|
| 347 |
+
def optical_flow_ilk(
|
| 348 |
+
reference_image,
|
| 349 |
+
moving_image,
|
| 350 |
+
*,
|
| 351 |
+
radius=7,
|
| 352 |
+
num_warp=10,
|
| 353 |
+
gaussian=False,
|
| 354 |
+
prefilter=False,
|
| 355 |
+
dtype=np.float32,
|
| 356 |
+
):
|
| 357 |
+
"""Coarse to fine optical flow estimator.
|
| 358 |
+
|
| 359 |
+
The iterative Lucas-Kanade (iLK) solver is applied at each level
|
| 360 |
+
of the image pyramid. iLK [1]_ is a fast and robust alternative to
|
| 361 |
+
TVL1 algorithm although less accurate for rendering flat surfaces
|
| 362 |
+
and object boundaries (see [2]_).
|
| 363 |
+
|
| 364 |
+
Parameters
|
| 365 |
+
----------
|
| 366 |
+
reference_image : ndarray, shape (M, N[, P[, ...]])
|
| 367 |
+
The first grayscale image of the sequence.
|
| 368 |
+
moving_image : ndarray, shape (M, N[, P[, ...]])
|
| 369 |
+
The second grayscale image of the sequence.
|
| 370 |
+
radius : int, optional
|
| 371 |
+
Radius of the window considered around each pixel.
|
| 372 |
+
num_warp : int, optional
|
| 373 |
+
Number of times moving_image is warped.
|
| 374 |
+
gaussian : bool, optional
|
| 375 |
+
If True, a Gaussian kernel is used for the local
|
| 376 |
+
integration. Otherwise, a uniform kernel is used.
|
| 377 |
+
prefilter : bool, optional
|
| 378 |
+
Whether to prefilter the estimated optical flow before each
|
| 379 |
+
image warp. When True, a median filter with window size 3
|
| 380 |
+
along each axis is applied. This helps to remove potential
|
| 381 |
+
outliers.
|
| 382 |
+
dtype : dtype, optional
|
| 383 |
+
Output data type: must be floating point. Single precision
|
| 384 |
+
provides good results and saves memory usage and computation
|
| 385 |
+
time compared to double precision.
|
| 386 |
+
|
| 387 |
+
Returns
|
| 388 |
+
-------
|
| 389 |
+
flow : ndarray, shape (reference_image.ndim, M, N[, P[, ...]])
|
| 390 |
+
The estimated optical flow components for each axis.
|
| 391 |
+
|
| 392 |
+
Notes
|
| 393 |
+
-----
|
| 394 |
+
- The implemented algorithm is described in **Table2** of [1]_.
|
| 395 |
+
- Color images are not supported.
|
| 396 |
+
|
| 397 |
+
References
|
| 398 |
+
----------
|
| 399 |
+
.. [1] Le Besnerais, G., & Champagnat, F. (2005, September). Dense
|
| 400 |
+
optical flow by iterative local window registration. In IEEE
|
| 401 |
+
International Conference on Image Processing 2005 (Vol. 1,
|
| 402 |
+
pp. I-137). IEEE. :DOI:`10.1109/ICIP.2005.1529706`
|
| 403 |
+
.. [2] Plyer, A., Le Besnerais, G., & Champagnat,
|
| 404 |
+
F. (2016). Massively parallel Lucas Kanade optical flow for
|
| 405 |
+
real-time video processing applications. Journal of Real-Time
|
| 406 |
+
Image Processing, 11(4), 713-730. :DOI:`10.1007/s11554-014-0423-0`
|
| 407 |
+
|
| 408 |
+
Examples
|
| 409 |
+
--------
|
| 410 |
+
>>> from skimage.color import rgb2gray
|
| 411 |
+
>>> from skimage.data import stereo_motorcycle
|
| 412 |
+
>>> from skimage.registration import optical_flow_ilk
|
| 413 |
+
>>> reference_image, moving_image, disp = stereo_motorcycle()
|
| 414 |
+
>>> # --- Convert the images to gray level: color is not supported.
|
| 415 |
+
>>> reference_image = rgb2gray(reference_image)
|
| 416 |
+
>>> moving_image = rgb2gray(moving_image)
|
| 417 |
+
>>> flow = optical_flow_ilk(moving_image, reference_image)
|
| 418 |
+
|
| 419 |
+
"""
|
| 420 |
+
|
| 421 |
+
solver = partial(
|
| 422 |
+
_ilk, radius=radius, num_warp=num_warp, gaussian=gaussian, prefilter=prefilter
|
| 423 |
+
)
|
| 424 |
+
|
| 425 |
+
if np.dtype(dtype) != _supported_float_type(dtype):
|
| 426 |
+
msg = f"dtype={dtype} is not supported. Try 'float32' or 'float64.'"
|
| 427 |
+
raise ValueError(msg)
|
| 428 |
+
|
| 429 |
+
return _coarse_to_fine(reference_image, moving_image, solver, dtype=dtype)
|
infer_4_47_1/lib/python3.10/site-packages/skimage/registration/_optical_flow_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
| 6 |
+
from ..transform import pyramid_reduce
|
| 7 |
+
from ..util.dtype import _convert
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def _get_warp_points(grid, flow):
|
| 11 |
+
"""Compute warp point coordinates.
|
| 12 |
+
|
| 13 |
+
Parameters
|
| 14 |
+
----------
|
| 15 |
+
grid : iterable
|
| 16 |
+
The sparse grid to be warped (obtained using
|
| 17 |
+
``np.meshgrid(..., sparse=True)).``)
|
| 18 |
+
flow : ndarray
|
| 19 |
+
The warping motion field.
|
| 20 |
+
|
| 21 |
+
Returns
|
| 22 |
+
-------
|
| 23 |
+
out : ndarray
|
| 24 |
+
The warp point coordinates.
|
| 25 |
+
|
| 26 |
+
"""
|
| 27 |
+
out = flow.copy()
|
| 28 |
+
for idx, g in enumerate(grid):
|
| 29 |
+
out[idx, ...] += g
|
| 30 |
+
return out
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def _resize_flow(flow, shape):
|
| 34 |
+
"""Rescale the values of the vector field (u, v) to the desired shape.
|
| 35 |
+
|
| 36 |
+
The values of the output vector field are scaled to the new
|
| 37 |
+
resolution.
|
| 38 |
+
|
| 39 |
+
Parameters
|
| 40 |
+
----------
|
| 41 |
+
flow : ndarray
|
| 42 |
+
The motion field to be processed.
|
| 43 |
+
shape : iterable
|
| 44 |
+
Couple of integers representing the output shape.
|
| 45 |
+
|
| 46 |
+
Returns
|
| 47 |
+
-------
|
| 48 |
+
rflow : ndarray
|
| 49 |
+
The resized and rescaled motion field.
|
| 50 |
+
|
| 51 |
+
"""
|
| 52 |
+
|
| 53 |
+
scale = [n / o for n, o in zip(shape, flow.shape[1:])]
|
| 54 |
+
scale_factor = np.array(scale, dtype=flow.dtype)
|
| 55 |
+
|
| 56 |
+
for _ in shape:
|
| 57 |
+
scale_factor = scale_factor[..., np.newaxis]
|
| 58 |
+
|
| 59 |
+
rflow = scale_factor * ndi.zoom(
|
| 60 |
+
flow, [1] + scale, order=0, mode='nearest', prefilter=False
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
return rflow
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def _get_pyramid(I, downscale=2.0, nlevel=10, min_size=16):
|
| 67 |
+
"""Construct image pyramid.
|
| 68 |
+
|
| 69 |
+
Parameters
|
| 70 |
+
----------
|
| 71 |
+
I : ndarray
|
| 72 |
+
The image to be preprocessed (Grayscale or RGB).
|
| 73 |
+
downscale : float
|
| 74 |
+
The pyramid downscale factor.
|
| 75 |
+
nlevel : int
|
| 76 |
+
The maximum number of pyramid levels.
|
| 77 |
+
min_size : int
|
| 78 |
+
The minimum size for any dimension of the pyramid levels.
|
| 79 |
+
|
| 80 |
+
Returns
|
| 81 |
+
-------
|
| 82 |
+
pyramid : list[ndarray]
|
| 83 |
+
The coarse to fine images pyramid.
|
| 84 |
+
|
| 85 |
+
"""
|
| 86 |
+
|
| 87 |
+
pyramid = [I]
|
| 88 |
+
size = min(I.shape)
|
| 89 |
+
count = 1
|
| 90 |
+
|
| 91 |
+
while (count < nlevel) and (size > downscale * min_size):
|
| 92 |
+
J = pyramid_reduce(pyramid[-1], downscale, channel_axis=None)
|
| 93 |
+
pyramid.append(J)
|
| 94 |
+
size = min(J.shape)
|
| 95 |
+
count += 1
|
| 96 |
+
|
| 97 |
+
return pyramid[::-1]
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def _coarse_to_fine(
|
| 101 |
+
I0, I1, solver, downscale=2, nlevel=10, min_size=16, dtype=np.float32
|
| 102 |
+
):
|
| 103 |
+
"""Generic coarse to fine solver.
|
| 104 |
+
|
| 105 |
+
Parameters
|
| 106 |
+
----------
|
| 107 |
+
I0 : ndarray
|
| 108 |
+
The first grayscale image of the sequence.
|
| 109 |
+
I1 : ndarray
|
| 110 |
+
The second grayscale image of the sequence.
|
| 111 |
+
solver : callable
|
| 112 |
+
The solver applied at each pyramid level.
|
| 113 |
+
downscale : float
|
| 114 |
+
The pyramid downscale factor.
|
| 115 |
+
nlevel : int
|
| 116 |
+
The maximum number of pyramid levels.
|
| 117 |
+
min_size : int
|
| 118 |
+
The minimum size for any dimension of the pyramid levels.
|
| 119 |
+
dtype : dtype
|
| 120 |
+
Output data type.
|
| 121 |
+
|
| 122 |
+
Returns
|
| 123 |
+
-------
|
| 124 |
+
flow : ndarray
|
| 125 |
+
The estimated optical flow components for each axis.
|
| 126 |
+
|
| 127 |
+
"""
|
| 128 |
+
|
| 129 |
+
if I0.shape != I1.shape:
|
| 130 |
+
raise ValueError("Input images should have the same shape")
|
| 131 |
+
|
| 132 |
+
if np.dtype(dtype).char not in 'efdg':
|
| 133 |
+
raise ValueError("Only floating point data type are valid" " for optical flow")
|
| 134 |
+
|
| 135 |
+
pyramid = list(
|
| 136 |
+
zip(
|
| 137 |
+
_get_pyramid(_convert(I0, dtype), downscale, nlevel, min_size),
|
| 138 |
+
_get_pyramid(_convert(I1, dtype), downscale, nlevel, min_size),
|
| 139 |
+
)
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
# Initialization to 0 at coarsest level.
|
| 143 |
+
flow = np.zeros((pyramid[0][0].ndim,) + pyramid[0][0].shape, dtype=dtype)
|
| 144 |
+
|
| 145 |
+
flow = solver(pyramid[0][0], pyramid[0][1], flow)
|
| 146 |
+
|
| 147 |
+
for J0, J1 in pyramid[1:]:
|
| 148 |
+
flow = solver(J0, J1, _resize_flow(flow, J0.shape))
|
| 149 |
+
|
| 150 |
+
return flow
|
infer_4_47_1/lib/python3.10/site-packages/skimage/registration/_phase_cross_correlation.py
ADDED
|
@@ -0,0 +1,420 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Port of Manuel Guizar's code from:
|
| 3 |
+
http://www.mathworks.com/matlabcentral/fileexchange/18401-efficient-subpixel-image-registration-by-cross-correlation
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import itertools
|
| 7 |
+
import warnings
|
| 8 |
+
|
| 9 |
+
import numpy as np
|
| 10 |
+
from scipy.fft import fftn, ifftn, fftfreq
|
| 11 |
+
from scipy import ndimage as ndi
|
| 12 |
+
|
| 13 |
+
from ._masked_phase_cross_correlation import _masked_phase_cross_correlation
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def _upsampled_dft(data, upsampled_region_size, upsample_factor=1, axis_offsets=None):
|
| 17 |
+
"""
|
| 18 |
+
Upsampled DFT by matrix multiplication.
|
| 19 |
+
|
| 20 |
+
This code is intended to provide the same result as if the following
|
| 21 |
+
operations were performed:
|
| 22 |
+
- Embed the array "data" in an array that is ``upsample_factor`` times
|
| 23 |
+
larger in each dimension. ifftshift to bring the center of the
|
| 24 |
+
image to (1,1).
|
| 25 |
+
- Take the FFT of the larger array.
|
| 26 |
+
- Extract an ``[upsampled_region_size]`` region of the result, starting
|
| 27 |
+
with the ``[axis_offsets+1]`` element.
|
| 28 |
+
|
| 29 |
+
It achieves this result by computing the DFT in the output array without
|
| 30 |
+
the need to zeropad. Much faster and memory efficient than the zero-padded
|
| 31 |
+
FFT approach if ``upsampled_region_size`` is much smaller than
|
| 32 |
+
``data.size * upsample_factor``.
|
| 33 |
+
|
| 34 |
+
Parameters
|
| 35 |
+
----------
|
| 36 |
+
data : array
|
| 37 |
+
The input data array (DFT of original data) to upsample.
|
| 38 |
+
upsampled_region_size : integer or tuple of integers, optional
|
| 39 |
+
The size of the region to be sampled. If one integer is provided, it
|
| 40 |
+
is duplicated up to the dimensionality of ``data``.
|
| 41 |
+
upsample_factor : integer, optional
|
| 42 |
+
The upsampling factor. Defaults to 1.
|
| 43 |
+
axis_offsets : tuple of integers, optional
|
| 44 |
+
The offsets of the region to be sampled. Defaults to None (uses
|
| 45 |
+
image center)
|
| 46 |
+
|
| 47 |
+
Returns
|
| 48 |
+
-------
|
| 49 |
+
output : ndarray
|
| 50 |
+
The upsampled DFT of the specified region.
|
| 51 |
+
"""
|
| 52 |
+
# if people pass in an integer, expand it to a list of equal-sized sections
|
| 53 |
+
if not hasattr(upsampled_region_size, "__iter__"):
|
| 54 |
+
upsampled_region_size = [
|
| 55 |
+
upsampled_region_size,
|
| 56 |
+
] * data.ndim
|
| 57 |
+
else:
|
| 58 |
+
if len(upsampled_region_size) != data.ndim:
|
| 59 |
+
raise ValueError(
|
| 60 |
+
"shape of upsampled region sizes must be equal "
|
| 61 |
+
"to input data's number of dimensions."
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
if axis_offsets is None:
|
| 65 |
+
axis_offsets = [
|
| 66 |
+
0,
|
| 67 |
+
] * data.ndim
|
| 68 |
+
else:
|
| 69 |
+
if len(axis_offsets) != data.ndim:
|
| 70 |
+
raise ValueError(
|
| 71 |
+
"number of axis offsets must be equal to input "
|
| 72 |
+
"data's number of dimensions."
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
im2pi = 1j * 2 * np.pi
|
| 76 |
+
|
| 77 |
+
dim_properties = list(zip(data.shape, upsampled_region_size, axis_offsets))
|
| 78 |
+
|
| 79 |
+
for n_items, ups_size, ax_offset in dim_properties[::-1]:
|
| 80 |
+
kernel = (np.arange(ups_size) - ax_offset)[:, None] * fftfreq(
|
| 81 |
+
n_items, upsample_factor
|
| 82 |
+
)
|
| 83 |
+
kernel = np.exp(-im2pi * kernel)
|
| 84 |
+
# use kernel with same precision as the data
|
| 85 |
+
kernel = kernel.astype(data.dtype, copy=False)
|
| 86 |
+
|
| 87 |
+
# Equivalent to:
|
| 88 |
+
# data[i, j, k] = kernel[i, :] @ data[j, k].T
|
| 89 |
+
data = np.tensordot(kernel, data, axes=(1, -1))
|
| 90 |
+
return data
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def _compute_phasediff(cross_correlation_max):
|
| 94 |
+
"""
|
| 95 |
+
Compute global phase difference between the two images (should be
|
| 96 |
+
zero if images are non-negative).
|
| 97 |
+
|
| 98 |
+
Parameters
|
| 99 |
+
----------
|
| 100 |
+
cross_correlation_max : complex
|
| 101 |
+
The complex value of the cross correlation at its maximum point.
|
| 102 |
+
"""
|
| 103 |
+
return np.arctan2(cross_correlation_max.imag, cross_correlation_max.real)
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def _compute_error(cross_correlation_max, src_amp, target_amp):
|
| 107 |
+
"""
|
| 108 |
+
Compute RMS error metric between ``src_image`` and ``target_image``.
|
| 109 |
+
|
| 110 |
+
Parameters
|
| 111 |
+
----------
|
| 112 |
+
cross_correlation_max : complex
|
| 113 |
+
The complex value of the cross correlation at its maximum point.
|
| 114 |
+
src_amp : float
|
| 115 |
+
The normalized average image intensity of the source image
|
| 116 |
+
target_amp : float
|
| 117 |
+
The normalized average image intensity of the target image
|
| 118 |
+
"""
|
| 119 |
+
amp = src_amp * target_amp
|
| 120 |
+
if amp == 0:
|
| 121 |
+
warnings.warn(
|
| 122 |
+
"Could not determine RMS error between images with the normalized "
|
| 123 |
+
f"average intensities {src_amp!r} and {target_amp!r}. Either the "
|
| 124 |
+
"reference or moving image may be empty.",
|
| 125 |
+
UserWarning,
|
| 126 |
+
stacklevel=3,
|
| 127 |
+
)
|
| 128 |
+
with np.errstate(invalid="ignore"):
|
| 129 |
+
error = 1.0 - cross_correlation_max * cross_correlation_max.conj() / amp
|
| 130 |
+
return np.sqrt(np.abs(error))
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def _disambiguate_shift(reference_image, moving_image, shift):
|
| 134 |
+
"""Determine the correct real-space shift based on periodic shift.
|
| 135 |
+
|
| 136 |
+
When determining a translation shift from phase cross-correlation in
|
| 137 |
+
Fourier space, the shift is only correct to within a period of the image
|
| 138 |
+
size along each axis, resulting in $2^n$ possible shifts, where $n$ is the
|
| 139 |
+
number of dimensions of the image. This function checks the
|
| 140 |
+
cross-correlation in real space for each of those shifts, and returns the
|
| 141 |
+
one with the highest cross-correlation.
|
| 142 |
+
|
| 143 |
+
The strategy we use is to perform the shift on the moving image *using the
|
| 144 |
+
'grid-wrap' mode* in `scipy.ndimage`. The moving image's original borders
|
| 145 |
+
then define $2^n$ quadrants, which we cross-correlate with the reference
|
| 146 |
+
image in turn using slicing. The entire operation is thus $O(2^n + m)$,
|
| 147 |
+
where $m$ is the number of pixels in the image (and typically dominates).
|
| 148 |
+
|
| 149 |
+
Parameters
|
| 150 |
+
----------
|
| 151 |
+
reference_image : numpy array
|
| 152 |
+
The reference (non-moving) image.
|
| 153 |
+
moving_image : numpy array
|
| 154 |
+
The moving image: applying the shift to this image overlays it on the
|
| 155 |
+
reference image. Must be the same shape as the reference image.
|
| 156 |
+
shift : ndarray
|
| 157 |
+
The shift to apply to each axis of the moving image, *modulo* image
|
| 158 |
+
size. The length of ``shift`` must be equal to ``moving_image.ndim``.
|
| 159 |
+
|
| 160 |
+
Returns
|
| 161 |
+
-------
|
| 162 |
+
real_shift : ndarray
|
| 163 |
+
The shift disambiguated in real space.
|
| 164 |
+
"""
|
| 165 |
+
shape = reference_image.shape
|
| 166 |
+
positive_shift = [shift_i % s for shift_i, s in zip(shift, shape)]
|
| 167 |
+
negative_shift = [shift_i - s for shift_i, s in zip(positive_shift, shape)]
|
| 168 |
+
subpixel = np.any(np.array(shift) % 1 != 0)
|
| 169 |
+
interp_order = 3 if subpixel else 0
|
| 170 |
+
shifted = ndi.shift(moving_image, shift, mode='grid-wrap', order=interp_order)
|
| 171 |
+
indices = np.round(positive_shift).astype(int)
|
| 172 |
+
splits_per_dim = [(slice(0, i), slice(i, None)) for i in indices]
|
| 173 |
+
max_corr = -1.0
|
| 174 |
+
max_slice = None
|
| 175 |
+
for test_slice in itertools.product(*splits_per_dim):
|
| 176 |
+
reference_tile = np.reshape(reference_image[test_slice], -1)
|
| 177 |
+
moving_tile = np.reshape(shifted[test_slice], -1)
|
| 178 |
+
corr = -1.0
|
| 179 |
+
if reference_tile.size > 2:
|
| 180 |
+
# In the case of zero std, np.corrcoef returns NaN and warns
|
| 181 |
+
# about division by zero. This is expected and handled below.
|
| 182 |
+
with warnings.catch_warnings():
|
| 183 |
+
warnings.filterwarnings("ignore", category=RuntimeWarning)
|
| 184 |
+
corr = np.corrcoef(reference_tile, moving_tile)[0, 1]
|
| 185 |
+
if corr > max_corr:
|
| 186 |
+
max_corr = corr
|
| 187 |
+
max_slice = test_slice
|
| 188 |
+
if max_slice is None:
|
| 189 |
+
warnings.warn(
|
| 190 |
+
f"Could not determine real-space shift for periodic shift {shift!r} "
|
| 191 |
+
f"as requested by `disambiguate=True` (disambiguation is degenerate).",
|
| 192 |
+
stacklevel=3,
|
| 193 |
+
)
|
| 194 |
+
return shift
|
| 195 |
+
real_shift_acc = []
|
| 196 |
+
for sl, pos_shift, neg_shift in zip(max_slice, positive_shift, negative_shift):
|
| 197 |
+
real_shift_acc.append(pos_shift if sl.stop is None else neg_shift)
|
| 198 |
+
|
| 199 |
+
return np.array(real_shift_acc)
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
def phase_cross_correlation(
|
| 203 |
+
reference_image,
|
| 204 |
+
moving_image,
|
| 205 |
+
*,
|
| 206 |
+
upsample_factor=1,
|
| 207 |
+
space="real",
|
| 208 |
+
disambiguate=False,
|
| 209 |
+
reference_mask=None,
|
| 210 |
+
moving_mask=None,
|
| 211 |
+
overlap_ratio=0.3,
|
| 212 |
+
normalization="phase",
|
| 213 |
+
):
|
| 214 |
+
"""Efficient subpixel image translation registration by cross-correlation.
|
| 215 |
+
|
| 216 |
+
This code gives the same precision as the FFT upsampled cross-correlation
|
| 217 |
+
in a fraction of the computation time and with reduced memory requirements.
|
| 218 |
+
It obtains an initial estimate of the cross-correlation peak by an FFT and
|
| 219 |
+
then refines the shift estimation by upsampling the DFT only in a small
|
| 220 |
+
neighborhood of that estimate by means of a matrix-multiply DFT [1]_.
|
| 221 |
+
|
| 222 |
+
Parameters
|
| 223 |
+
----------
|
| 224 |
+
reference_image : array
|
| 225 |
+
Reference image.
|
| 226 |
+
moving_image : array
|
| 227 |
+
Image to register. Must be same dimensionality as
|
| 228 |
+
``reference_image``.
|
| 229 |
+
upsample_factor : int, optional
|
| 230 |
+
Upsampling factor. Images will be registered to within
|
| 231 |
+
``1 / upsample_factor`` of a pixel. For example
|
| 232 |
+
``upsample_factor == 20`` means the images will be registered
|
| 233 |
+
within 1/20th of a pixel. Default is 1 (no upsampling).
|
| 234 |
+
Not used if any of ``reference_mask`` or ``moving_mask`` is not None.
|
| 235 |
+
space : string, one of "real" or "fourier", optional
|
| 236 |
+
Defines how the algorithm interprets input data. "real" means
|
| 237 |
+
data will be FFT'd to compute the correlation, while "fourier"
|
| 238 |
+
data will bypass FFT of input data. Case insensitive. Not
|
| 239 |
+
used if any of ``reference_mask`` or ``moving_mask`` is not
|
| 240 |
+
None.
|
| 241 |
+
disambiguate : bool
|
| 242 |
+
The shift returned by this function is only accurate *modulo* the
|
| 243 |
+
image shape, due to the periodic nature of the Fourier transform. If
|
| 244 |
+
this parameter is set to ``True``, the *real* space cross-correlation
|
| 245 |
+
is computed for each possible shift, and the shift with the highest
|
| 246 |
+
cross-correlation within the overlapping area is returned.
|
| 247 |
+
reference_mask : ndarray
|
| 248 |
+
Boolean mask for ``reference_image``. The mask should evaluate
|
| 249 |
+
to ``True`` (or 1) on valid pixels. ``reference_mask`` should
|
| 250 |
+
have the same shape as ``reference_image``.
|
| 251 |
+
moving_mask : ndarray or None, optional
|
| 252 |
+
Boolean mask for ``moving_image``. The mask should evaluate to ``True``
|
| 253 |
+
(or 1) on valid pixels. ``moving_mask`` should have the same shape
|
| 254 |
+
as ``moving_image``. If ``None``, ``reference_mask`` will be used.
|
| 255 |
+
overlap_ratio : float, optional
|
| 256 |
+
Minimum allowed overlap ratio between images. The correlation for
|
| 257 |
+
translations corresponding with an overlap ratio lower than this
|
| 258 |
+
threshold will be ignored. A lower `overlap_ratio` leads to smaller
|
| 259 |
+
maximum translation, while a higher `overlap_ratio` leads to greater
|
| 260 |
+
robustness against spurious matches due to small overlap between
|
| 261 |
+
masked images. Used only if one of ``reference_mask`` or
|
| 262 |
+
``moving_mask`` is not None.
|
| 263 |
+
normalization : {"phase", None}
|
| 264 |
+
The type of normalization to apply to the cross-correlation. This
|
| 265 |
+
parameter is unused when masks (`reference_mask` and `moving_mask`) are
|
| 266 |
+
supplied.
|
| 267 |
+
|
| 268 |
+
Returns
|
| 269 |
+
-------
|
| 270 |
+
shift : ndarray
|
| 271 |
+
Shift vector (in pixels) required to register ``moving_image``
|
| 272 |
+
with ``reference_image``. Axis ordering is consistent with
|
| 273 |
+
the axis order of the input array.
|
| 274 |
+
error : float
|
| 275 |
+
Translation invariant normalized RMS error between
|
| 276 |
+
``reference_image`` and ``moving_image``. For masked cross-correlation
|
| 277 |
+
this error is not available and NaN is returned.
|
| 278 |
+
phasediff : float
|
| 279 |
+
Global phase difference between the two images (should be
|
| 280 |
+
zero if images are non-negative). For masked cross-correlation
|
| 281 |
+
this phase difference is not available and NaN is returned.
|
| 282 |
+
|
| 283 |
+
Notes
|
| 284 |
+
-----
|
| 285 |
+
The use of cross-correlation to estimate image translation has a long
|
| 286 |
+
history dating back to at least [2]_. The "phase correlation"
|
| 287 |
+
method (selected by ``normalization="phase"``) was first proposed in [3]_.
|
| 288 |
+
Publications [1]_ and [2]_ use an unnormalized cross-correlation
|
| 289 |
+
(``normalization=None``). Which form of normalization is better is
|
| 290 |
+
application-dependent. For example, the phase correlation method works
|
| 291 |
+
well in registering images under different illumination, but is not very
|
| 292 |
+
robust to noise. In a high noise scenario, the unnormalized method may be
|
| 293 |
+
preferable.
|
| 294 |
+
|
| 295 |
+
When masks are provided, a masked normalized cross-correlation algorithm is
|
| 296 |
+
used [5]_, [6]_.
|
| 297 |
+
|
| 298 |
+
References
|
| 299 |
+
----------
|
| 300 |
+
.. [1] Manuel Guizar-Sicairos, Samuel T. Thurman, and James R. Fienup,
|
| 301 |
+
"Efficient subpixel image registration algorithms,"
|
| 302 |
+
Optics Letters 33, 156-158 (2008). :DOI:`10.1364/OL.33.000156`
|
| 303 |
+
.. [2] P. Anuta, Spatial registration of multispectral and multitemporal
|
| 304 |
+
digital imagery using fast Fourier transform techniques, IEEE Trans.
|
| 305 |
+
Geosci. Electron., vol. 8, no. 4, pp. 353–368, Oct. 1970.
|
| 306 |
+
:DOI:`10.1109/TGE.1970.271435`.
|
| 307 |
+
.. [3] C. D. Kuglin D. C. Hines. The phase correlation image alignment
|
| 308 |
+
method, Proceeding of IEEE International Conference on Cybernetics
|
| 309 |
+
and Society, pp. 163-165, New York, NY, USA, 1975, pp. 163–165.
|
| 310 |
+
.. [4] James R. Fienup, "Invariant error metrics for image reconstruction"
|
| 311 |
+
Optics Letters 36, 8352-8357 (1997). :DOI:`10.1364/AO.36.008352`
|
| 312 |
+
.. [5] Dirk Padfield. Masked Object Registration in the Fourier Domain.
|
| 313 |
+
IEEE Transactions on Image Processing, vol. 21(5),
|
| 314 |
+
pp. 2706-2718 (2012). :DOI:`10.1109/TIP.2011.2181402`
|
| 315 |
+
.. [6] D. Padfield. "Masked FFT registration". In Proc. Computer Vision and
|
| 316 |
+
Pattern Recognition, pp. 2918-2925 (2010).
|
| 317 |
+
:DOI:`10.1109/CVPR.2010.5540032`
|
| 318 |
+
"""
|
| 319 |
+
if (reference_mask is not None) or (moving_mask is not None):
|
| 320 |
+
shift = _masked_phase_cross_correlation(
|
| 321 |
+
reference_image, moving_image, reference_mask, moving_mask, overlap_ratio
|
| 322 |
+
)
|
| 323 |
+
return shift, np.nan, np.nan
|
| 324 |
+
|
| 325 |
+
# images must be the same shape
|
| 326 |
+
if reference_image.shape != moving_image.shape:
|
| 327 |
+
raise ValueError("images must be same shape")
|
| 328 |
+
|
| 329 |
+
# assume complex data is already in Fourier space
|
| 330 |
+
if space.lower() == 'fourier':
|
| 331 |
+
src_freq = reference_image
|
| 332 |
+
target_freq = moving_image
|
| 333 |
+
# real data needs to be fft'd.
|
| 334 |
+
elif space.lower() == 'real':
|
| 335 |
+
src_freq = fftn(reference_image)
|
| 336 |
+
target_freq = fftn(moving_image)
|
| 337 |
+
else:
|
| 338 |
+
raise ValueError('space argument must be "real" of "fourier"')
|
| 339 |
+
|
| 340 |
+
# Whole-pixel shift - Compute cross-correlation by an IFFT
|
| 341 |
+
shape = src_freq.shape
|
| 342 |
+
image_product = src_freq * target_freq.conj()
|
| 343 |
+
if normalization == "phase":
|
| 344 |
+
eps = np.finfo(image_product.real.dtype).eps
|
| 345 |
+
image_product /= np.maximum(np.abs(image_product), 100 * eps)
|
| 346 |
+
elif normalization is not None:
|
| 347 |
+
raise ValueError("normalization must be either phase or None")
|
| 348 |
+
cross_correlation = ifftn(image_product)
|
| 349 |
+
|
| 350 |
+
# Locate maximum
|
| 351 |
+
maxima = np.unravel_index(
|
| 352 |
+
np.argmax(np.abs(cross_correlation)), cross_correlation.shape
|
| 353 |
+
)
|
| 354 |
+
midpoint = np.array([np.fix(axis_size / 2) for axis_size in shape])
|
| 355 |
+
|
| 356 |
+
float_dtype = image_product.real.dtype
|
| 357 |
+
|
| 358 |
+
shift = np.stack(maxima).astype(float_dtype, copy=False)
|
| 359 |
+
shift[shift > midpoint] -= np.array(shape)[shift > midpoint]
|
| 360 |
+
|
| 361 |
+
if upsample_factor == 1:
|
| 362 |
+
src_amp = np.sum(np.real(src_freq * src_freq.conj()))
|
| 363 |
+
src_amp /= src_freq.size
|
| 364 |
+
target_amp = np.sum(np.real(target_freq * target_freq.conj()))
|
| 365 |
+
target_amp /= target_freq.size
|
| 366 |
+
CCmax = cross_correlation[maxima]
|
| 367 |
+
# If upsampling > 1, then refine estimate with matrix multiply DFT
|
| 368 |
+
else:
|
| 369 |
+
# Initial shift estimate in upsampled grid
|
| 370 |
+
upsample_factor = np.array(upsample_factor, dtype=float_dtype)
|
| 371 |
+
shift = np.round(shift * upsample_factor) / upsample_factor
|
| 372 |
+
upsampled_region_size = np.ceil(upsample_factor * 1.5)
|
| 373 |
+
# Center of output array at dftshift + 1
|
| 374 |
+
dftshift = np.fix(upsampled_region_size / 2.0)
|
| 375 |
+
# Matrix multiply DFT around the current shift estimate
|
| 376 |
+
sample_region_offset = dftshift - shift * upsample_factor
|
| 377 |
+
cross_correlation = _upsampled_dft(
|
| 378 |
+
image_product.conj(),
|
| 379 |
+
upsampled_region_size,
|
| 380 |
+
upsample_factor,
|
| 381 |
+
sample_region_offset,
|
| 382 |
+
).conj()
|
| 383 |
+
# Locate maximum and map back to original pixel grid
|
| 384 |
+
maxima = np.unravel_index(
|
| 385 |
+
np.argmax(np.abs(cross_correlation)), cross_correlation.shape
|
| 386 |
+
)
|
| 387 |
+
CCmax = cross_correlation[maxima]
|
| 388 |
+
|
| 389 |
+
maxima = np.stack(maxima).astype(float_dtype, copy=False)
|
| 390 |
+
maxima -= dftshift
|
| 391 |
+
|
| 392 |
+
shift += maxima / upsample_factor
|
| 393 |
+
|
| 394 |
+
src_amp = np.sum(np.real(src_freq * src_freq.conj()))
|
| 395 |
+
target_amp = np.sum(np.real(target_freq * target_freq.conj()))
|
| 396 |
+
|
| 397 |
+
# If its only one row or column the shift along that dimension has no
|
| 398 |
+
# effect. We set to zero.
|
| 399 |
+
for dim in range(src_freq.ndim):
|
| 400 |
+
if shape[dim] == 1:
|
| 401 |
+
shift[dim] = 0
|
| 402 |
+
|
| 403 |
+
if disambiguate:
|
| 404 |
+
if space.lower() != 'real':
|
| 405 |
+
reference_image = ifftn(reference_image)
|
| 406 |
+
moving_image = ifftn(moving_image)
|
| 407 |
+
shift = _disambiguate_shift(reference_image, moving_image, shift)
|
| 408 |
+
|
| 409 |
+
# Redirect user to masked_phase_cross_correlation if NaNs are observed
|
| 410 |
+
if np.isnan(CCmax) or np.isnan(src_amp) or np.isnan(target_amp):
|
| 411 |
+
raise ValueError(
|
| 412 |
+
"NaN values found, please remove NaNs from your "
|
| 413 |
+
"input data or use the `reference_mask`/`moving_mask` "
|
| 414 |
+
"keywords, eg: "
|
| 415 |
+
"phase_cross_correlation(reference_image, moving_image, "
|
| 416 |
+
"reference_mask=~np.isnan(reference_image), "
|
| 417 |
+
"moving_mask=~np.isnan(moving_image))"
|
| 418 |
+
)
|
| 419 |
+
|
| 420 |
+
return shift, _compute_error(CCmax, src_amp, target_amp), _compute_phasediff(CCmax)
|
infer_4_47_1/lib/python3.10/site-packages/skimage/registration/tests/__init__.py
ADDED
|
File without changes
|
infer_4_47_1/lib/python3.10/site-packages/skimage/registration/tests/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (184 Bytes). View file
|
|
|
infer_4_47_1/lib/python3.10/site-packages/skimage/registration/tests/__pycache__/test_ilk.cpython-310.pyc
ADDED
|
Binary file (2.75 kB). View file
|
|
|
infer_4_47_1/lib/python3.10/site-packages/skimage/registration/tests/__pycache__/test_masked_phase_cross_correlation.cpython-310.pyc
ADDED
|
Binary file (7.47 kB). View file
|
|
|
infer_4_47_1/lib/python3.10/site-packages/skimage/registration/tests/__pycache__/test_phase_cross_correlation.cpython-310.pyc
ADDED
|
Binary file (7.79 kB). View file
|
|
|
infer_4_47_1/lib/python3.10/site-packages/skimage/registration/tests/__pycache__/test_tvl1.cpython-310.pyc
ADDED
|
Binary file (3.67 kB). View file
|
|
|
infer_4_47_1/lib/python3.10/site-packages/skimage/registration/tests/test_ilk.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from skimage._shared.utils import _supported_float_type
|
| 5 |
+
from skimage.registration import optical_flow_ilk
|
| 6 |
+
from .test_tvl1 import _sin_flow_gen
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
@pytest.mark.parametrize('dtype', [np.float16, np.float32, np.float64])
|
| 10 |
+
@pytest.mark.parametrize('gaussian', [True, False])
|
| 11 |
+
@pytest.mark.parametrize('prefilter', [True, False])
|
| 12 |
+
def test_2d_motion(dtype, gaussian, prefilter):
|
| 13 |
+
# Generate synthetic data
|
| 14 |
+
rng = np.random.default_rng(0)
|
| 15 |
+
image0 = rng.normal(size=(256, 256))
|
| 16 |
+
gt_flow, image1 = _sin_flow_gen(image0)
|
| 17 |
+
image1 = image1.astype(dtype, copy=False)
|
| 18 |
+
float_dtype = _supported_float_type(dtype)
|
| 19 |
+
# Estimate the flow
|
| 20 |
+
flow = optical_flow_ilk(
|
| 21 |
+
image0, image1, gaussian=gaussian, prefilter=prefilter, dtype=float_dtype
|
| 22 |
+
)
|
| 23 |
+
assert flow.dtype == _supported_float_type(dtype)
|
| 24 |
+
# Assert that the average absolute error is less then half a pixel
|
| 25 |
+
assert abs(flow - gt_flow).mean() < 0.5
|
| 26 |
+
|
| 27 |
+
if dtype != float_dtype:
|
| 28 |
+
with pytest.raises(ValueError):
|
| 29 |
+
optical_flow_ilk(
|
| 30 |
+
image0, image1, gaussian=gaussian, prefilter=prefilter, dtype=dtype
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
@pytest.mark.parametrize('gaussian', [True, False])
|
| 35 |
+
@pytest.mark.parametrize('prefilter', [True, False])
|
| 36 |
+
def test_3d_motion(gaussian, prefilter):
|
| 37 |
+
# Generate synthetic data
|
| 38 |
+
rng = np.random.default_rng(123)
|
| 39 |
+
image0 = rng.normal(size=(50, 55, 60))
|
| 40 |
+
gt_flow, image1 = _sin_flow_gen(image0, npics=3)
|
| 41 |
+
# Estimate the flow
|
| 42 |
+
flow = optical_flow_ilk(
|
| 43 |
+
image0, image1, radius=5, gaussian=gaussian, prefilter=prefilter
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# Assert that the average absolute error is less then half a pixel
|
| 47 |
+
assert abs(flow - gt_flow).mean() < 0.5
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def test_no_motion_2d():
|
| 51 |
+
rng = np.random.default_rng(0)
|
| 52 |
+
img = rng.normal(size=(256, 256))
|
| 53 |
+
|
| 54 |
+
flow = optical_flow_ilk(img, img)
|
| 55 |
+
|
| 56 |
+
assert np.all(flow == 0)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def test_no_motion_3d():
|
| 60 |
+
rng = np.random.default_rng(0)
|
| 61 |
+
img = rng.normal(size=(64, 64, 64))
|
| 62 |
+
|
| 63 |
+
flow = optical_flow_ilk(img, img)
|
| 64 |
+
|
| 65 |
+
assert np.all(flow == 0)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def test_optical_flow_dtype():
|
| 69 |
+
# Generate synthetic data
|
| 70 |
+
rng = np.random.default_rng(0)
|
| 71 |
+
image0 = rng.normal(size=(256, 256))
|
| 72 |
+
gt_flow, image1 = _sin_flow_gen(image0)
|
| 73 |
+
# Estimate the flow at double precision
|
| 74 |
+
flow_f64 = optical_flow_ilk(image0, image1, dtype='float64')
|
| 75 |
+
|
| 76 |
+
assert flow_f64.dtype == 'float64'
|
| 77 |
+
|
| 78 |
+
# Estimate the flow at single precision
|
| 79 |
+
flow_f32 = optical_flow_ilk(image0, image1, dtype='float32')
|
| 80 |
+
|
| 81 |
+
assert flow_f32.dtype == 'float32'
|
| 82 |
+
|
| 83 |
+
# Assert that floating point precision does not affect the quality
|
| 84 |
+
# of the estimated flow
|
| 85 |
+
|
| 86 |
+
assert abs(flow_f64 - flow_f32).mean() < 1e-3
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def test_incompatible_shapes():
|
| 90 |
+
rng = np.random.default_rng(0)
|
| 91 |
+
I0 = rng.normal(size=(256, 256))
|
| 92 |
+
I1 = rng.normal(size=(255, 256))
|
| 93 |
+
with pytest.raises(ValueError):
|
| 94 |
+
u, v = optical_flow_ilk(I0, I1)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def test_wrong_dtype():
|
| 98 |
+
rng = np.random.default_rng(0)
|
| 99 |
+
img = rng.normal(size=(256, 256))
|
| 100 |
+
with pytest.raises(ValueError):
|
| 101 |
+
u, v = optical_flow_ilk(img, img, dtype='int')
|
infer_4_47_1/lib/python3.10/site-packages/skimage/registration/tests/test_masked_phase_cross_correlation.py
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
from numpy.testing import (
|
| 4 |
+
assert_almost_equal,
|
| 5 |
+
assert_array_almost_equal,
|
| 6 |
+
assert_array_equal,
|
| 7 |
+
assert_array_less,
|
| 8 |
+
assert_equal,
|
| 9 |
+
)
|
| 10 |
+
from scipy.ndimage import fourier_shift, shift as real_shift
|
| 11 |
+
import scipy.fft as fft
|
| 12 |
+
|
| 13 |
+
from skimage._shared.testing import fetch
|
| 14 |
+
from skimage._shared.utils import _supported_float_type
|
| 15 |
+
from skimage.data import camera, brain
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
from skimage.io import imread
|
| 19 |
+
from skimage.registration._masked_phase_cross_correlation import (
|
| 20 |
+
_masked_phase_cross_correlation as masked_register_translation,
|
| 21 |
+
cross_correlate_masked,
|
| 22 |
+
)
|
| 23 |
+
from skimage.registration import phase_cross_correlation
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def test_masked_registration_vs_phase_cross_correlation():
|
| 27 |
+
"""masked_register_translation should give the same results as
|
| 28 |
+
phase_cross_correlation in the case of trivial masks."""
|
| 29 |
+
reference_image = camera()
|
| 30 |
+
shift = (-7, 12)
|
| 31 |
+
shifted = np.real(fft.ifft2(fourier_shift(fft.fft2(reference_image), shift)))
|
| 32 |
+
trivial_mask = np.ones_like(reference_image)
|
| 33 |
+
|
| 34 |
+
nonmasked_result, *_ = phase_cross_correlation(reference_image, shifted)
|
| 35 |
+
masked_result = masked_register_translation(
|
| 36 |
+
reference_image, shifted, reference_mask=trivial_mask, overlap_ratio=1 / 10
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
assert_equal(nonmasked_result, masked_result)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def test_masked_registration_random_masks():
|
| 43 |
+
"""masked_register_translation should be able to register translations
|
| 44 |
+
between images even with random masks."""
|
| 45 |
+
# See random number generator for reproducible results
|
| 46 |
+
np.random.seed(23)
|
| 47 |
+
|
| 48 |
+
reference_image = camera()
|
| 49 |
+
shift = (-7, 12)
|
| 50 |
+
shifted = np.real(fft.ifft2(fourier_shift(fft.fft2(reference_image), shift)))
|
| 51 |
+
|
| 52 |
+
# Random masks with 75% of pixels being valid
|
| 53 |
+
ref_mask = np.random.choice([True, False], reference_image.shape, p=[3 / 4, 1 / 4])
|
| 54 |
+
shifted_mask = np.random.choice([True, False], shifted.shape, p=[3 / 4, 1 / 4])
|
| 55 |
+
|
| 56 |
+
measured_shift = masked_register_translation(
|
| 57 |
+
reference_image, shifted, reference_mask=ref_mask, moving_mask=shifted_mask
|
| 58 |
+
)
|
| 59 |
+
assert_equal(measured_shift, -np.array(shift))
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def test_masked_registration_3d_contiguous_mask():
|
| 63 |
+
"""masked_register_translation should be able to register translations
|
| 64 |
+
between volumes with contiguous masks."""
|
| 65 |
+
ref_vol = brain()[:, ::2, ::2]
|
| 66 |
+
|
| 67 |
+
offset = (1, -5, 10)
|
| 68 |
+
|
| 69 |
+
# create square mask
|
| 70 |
+
ref_mask = np.zeros_like(ref_vol, dtype=bool)
|
| 71 |
+
ref_mask[:-2, 75:100, 75:100] = True
|
| 72 |
+
ref_shifted = real_shift(ref_vol, offset)
|
| 73 |
+
|
| 74 |
+
measured_offset = masked_register_translation(
|
| 75 |
+
ref_vol, ref_shifted, reference_mask=ref_mask, moving_mask=ref_mask
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
assert_equal(offset, -np.array(measured_offset))
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def test_masked_registration_random_masks_non_equal_sizes():
|
| 82 |
+
"""masked_register_translation should be able to register
|
| 83 |
+
translations between images that are not the same size even
|
| 84 |
+
with random masks."""
|
| 85 |
+
# See random number generator for reproducible results
|
| 86 |
+
np.random.seed(23)
|
| 87 |
+
|
| 88 |
+
reference_image = camera()
|
| 89 |
+
shift = (-7, 12)
|
| 90 |
+
shifted = np.real(fft.ifft2(fourier_shift(fft.fft2(reference_image), shift)))
|
| 91 |
+
|
| 92 |
+
# Crop the shifted image
|
| 93 |
+
shifted = shifted[64:-64, 64:-64]
|
| 94 |
+
|
| 95 |
+
# Random masks with 75% of pixels being valid
|
| 96 |
+
ref_mask = np.random.choice([True, False], reference_image.shape, p=[3 / 4, 1 / 4])
|
| 97 |
+
shifted_mask = np.random.choice([True, False], shifted.shape, p=[3 / 4, 1 / 4])
|
| 98 |
+
|
| 99 |
+
measured_shift = masked_register_translation(
|
| 100 |
+
reference_image,
|
| 101 |
+
shifted,
|
| 102 |
+
reference_mask=np.ones_like(ref_mask),
|
| 103 |
+
moving_mask=np.ones_like(shifted_mask),
|
| 104 |
+
)
|
| 105 |
+
assert_equal(measured_shift, -np.array(shift))
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def test_masked_registration_padfield_data():
|
| 109 |
+
"""Masked translation registration should behave like in the original
|
| 110 |
+
publication"""
|
| 111 |
+
# Test translated from MATLABimplementation `MaskedFFTRegistrationTest`
|
| 112 |
+
# file. You can find the source code here:
|
| 113 |
+
# http://www.dirkpadfield.com/Home/MaskedFFTRegistrationCode.zip
|
| 114 |
+
|
| 115 |
+
shifts = [(75, 75), (-130, 130), (130, 130)]
|
| 116 |
+
for xi, yi in shifts:
|
| 117 |
+
fixed_image = imread(fetch(f'registration/tests/data/OriginalX{xi}Y{yi}.png'))
|
| 118 |
+
moving_image = imread(
|
| 119 |
+
fetch(f'registration/tests/data/TransformedX{xi}Y{yi}.png')
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
# Valid pixels are 1
|
| 123 |
+
fixed_mask = fixed_image != 0
|
| 124 |
+
moving_mask = moving_image != 0
|
| 125 |
+
|
| 126 |
+
# Note that shifts in x and y and shifts in cols and rows
|
| 127 |
+
shift_y, shift_x = masked_register_translation(
|
| 128 |
+
fixed_image,
|
| 129 |
+
moving_image,
|
| 130 |
+
reference_mask=fixed_mask,
|
| 131 |
+
moving_mask=moving_mask,
|
| 132 |
+
overlap_ratio=0.1,
|
| 133 |
+
)
|
| 134 |
+
# Note: by looking at the test code from Padfield's
|
| 135 |
+
# MaskedFFTRegistrationCode repository, the
|
| 136 |
+
# shifts were not xi and yi, but xi and -yi
|
| 137 |
+
assert_equal((shift_x, shift_y), (-xi, yi))
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
@pytest.mark.parametrize('dtype', [np.float16, np.float32, np.float64])
|
| 141 |
+
def test_cross_correlate_masked_output_shape(dtype):
|
| 142 |
+
"""Masked normalized cross-correlation should return a shape
|
| 143 |
+
of N + M + 1 for each transform axis."""
|
| 144 |
+
shape1 = (15, 4, 5)
|
| 145 |
+
shape2 = (6, 12, 7)
|
| 146 |
+
expected_full_shape = tuple(np.array(shape1) + np.array(shape2) - 1)
|
| 147 |
+
expected_same_shape = shape1
|
| 148 |
+
|
| 149 |
+
arr1 = np.zeros(shape1, dtype=dtype)
|
| 150 |
+
arr2 = np.zeros(shape2, dtype=dtype)
|
| 151 |
+
# Trivial masks
|
| 152 |
+
m1 = np.ones_like(arr1)
|
| 153 |
+
m2 = np.ones_like(arr2)
|
| 154 |
+
|
| 155 |
+
float_dtype = _supported_float_type(dtype)
|
| 156 |
+
|
| 157 |
+
full_xcorr = cross_correlate_masked(arr1, arr2, m1, m2, axes=(0, 1, 2), mode='full')
|
| 158 |
+
assert_equal(full_xcorr.shape, expected_full_shape)
|
| 159 |
+
assert full_xcorr.dtype == float_dtype
|
| 160 |
+
|
| 161 |
+
same_xcorr = cross_correlate_masked(arr1, arr2, m1, m2, axes=(0, 1, 2), mode='same')
|
| 162 |
+
assert_equal(same_xcorr.shape, expected_same_shape)
|
| 163 |
+
assert same_xcorr.dtype == float_dtype
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
def test_cross_correlate_masked_test_against_mismatched_dimensions():
|
| 167 |
+
"""Masked normalized cross-correlation should raise an error if array
|
| 168 |
+
dimensions along non-transformation axes are mismatched."""
|
| 169 |
+
shape1 = (23, 1, 1)
|
| 170 |
+
shape2 = (6, 2, 2)
|
| 171 |
+
|
| 172 |
+
arr1 = np.zeros(shape1)
|
| 173 |
+
arr2 = np.zeros(shape2)
|
| 174 |
+
|
| 175 |
+
# Trivial masks
|
| 176 |
+
m1 = np.ones_like(arr1)
|
| 177 |
+
m2 = np.ones_like(arr2)
|
| 178 |
+
|
| 179 |
+
with pytest.raises(ValueError):
|
| 180 |
+
cross_correlate_masked(arr1, arr2, m1, m2, axes=(1, 2))
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
def test_cross_correlate_masked_output_range():
|
| 184 |
+
"""Masked normalized cross-correlation should return between 1 and -1."""
|
| 185 |
+
# See random number generator for reproducible results
|
| 186 |
+
np.random.seed(23)
|
| 187 |
+
|
| 188 |
+
# Array dimensions must match along non-transformation axes, in
|
| 189 |
+
# this case
|
| 190 |
+
# axis 0
|
| 191 |
+
shape1 = (15, 4, 5)
|
| 192 |
+
shape2 = (15, 12, 7)
|
| 193 |
+
|
| 194 |
+
# Initial array ranges between -5 and 5
|
| 195 |
+
arr1 = 10 * np.random.random(shape1) - 5
|
| 196 |
+
arr2 = 10 * np.random.random(shape2) - 5
|
| 197 |
+
|
| 198 |
+
# random masks
|
| 199 |
+
m1 = np.random.choice([True, False], arr1.shape)
|
| 200 |
+
m2 = np.random.choice([True, False], arr2.shape)
|
| 201 |
+
|
| 202 |
+
xcorr = cross_correlate_masked(arr1, arr2, m1, m2, axes=(1, 2))
|
| 203 |
+
|
| 204 |
+
# No assert array less or equal, so we add an eps
|
| 205 |
+
# Also could not find an `assert_array_greater`, Use (-xcorr) instead
|
| 206 |
+
eps = np.finfo(float).eps
|
| 207 |
+
assert_array_less(xcorr, 1 + eps)
|
| 208 |
+
assert_array_less(-xcorr, 1 + eps)
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
def test_cross_correlate_masked_side_effects():
|
| 212 |
+
"""Masked normalized cross-correlation should not modify the inputs."""
|
| 213 |
+
shape1 = (2, 2, 2)
|
| 214 |
+
shape2 = (2, 2, 2)
|
| 215 |
+
|
| 216 |
+
arr1 = np.zeros(shape1)
|
| 217 |
+
arr2 = np.zeros(shape2)
|
| 218 |
+
|
| 219 |
+
# Trivial masks
|
| 220 |
+
m1 = np.ones_like(arr1)
|
| 221 |
+
m2 = np.ones_like(arr2)
|
| 222 |
+
|
| 223 |
+
for arr in (arr1, arr2, m1, m2):
|
| 224 |
+
arr.setflags(write=False)
|
| 225 |
+
|
| 226 |
+
cross_correlate_masked(arr1, arr2, m1, m2)
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
def test_cross_correlate_masked_over_axes():
|
| 230 |
+
"""Masked normalized cross-correlation over axes should be
|
| 231 |
+
equivalent to a loop over non-transform axes."""
|
| 232 |
+
# See random number generator for reproducible results
|
| 233 |
+
np.random.seed(23)
|
| 234 |
+
|
| 235 |
+
arr1 = np.random.random((8, 8, 5))
|
| 236 |
+
arr2 = np.random.random((8, 8, 5))
|
| 237 |
+
|
| 238 |
+
m1 = np.random.choice([True, False], arr1.shape)
|
| 239 |
+
m2 = np.random.choice([True, False], arr2.shape)
|
| 240 |
+
|
| 241 |
+
# Loop over last axis
|
| 242 |
+
with_loop = np.empty_like(arr1, dtype=complex)
|
| 243 |
+
for index in range(arr1.shape[-1]):
|
| 244 |
+
with_loop[:, :, index] = cross_correlate_masked(
|
| 245 |
+
arr1[:, :, index],
|
| 246 |
+
arr2[:, :, index],
|
| 247 |
+
m1[:, :, index],
|
| 248 |
+
m2[:, :, index],
|
| 249 |
+
axes=(0, 1),
|
| 250 |
+
mode='same',
|
| 251 |
+
)
|
| 252 |
+
|
| 253 |
+
over_axes = cross_correlate_masked(arr1, arr2, m1, m2, axes=(0, 1), mode='same')
|
| 254 |
+
|
| 255 |
+
assert_array_almost_equal(with_loop, over_axes)
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
def test_cross_correlate_masked_autocorrelation_trivial_masks():
|
| 259 |
+
"""Masked normalized cross-correlation between identical arrays
|
| 260 |
+
should reduce to an autocorrelation even with random masks."""
|
| 261 |
+
# See random number generator for reproducible results
|
| 262 |
+
np.random.seed(23)
|
| 263 |
+
|
| 264 |
+
arr1 = camera()
|
| 265 |
+
|
| 266 |
+
# Random masks with 75% of pixels being valid
|
| 267 |
+
m1 = np.random.choice([True, False], arr1.shape, p=[3 / 4, 1 / 4])
|
| 268 |
+
m2 = np.random.choice([True, False], arr1.shape, p=[3 / 4, 1 / 4])
|
| 269 |
+
|
| 270 |
+
xcorr = cross_correlate_masked(
|
| 271 |
+
arr1, arr1, m1, m2, axes=(0, 1), mode='same', overlap_ratio=0
|
| 272 |
+
).real
|
| 273 |
+
max_index = np.unravel_index(np.argmax(xcorr), xcorr.shape)
|
| 274 |
+
|
| 275 |
+
# Autocorrelation should have maximum in center of array
|
| 276 |
+
# uint8 inputs will be processed in float32, so reduce decimal to 5
|
| 277 |
+
assert_almost_equal(xcorr.max(), 1, decimal=5)
|
| 278 |
+
assert_array_equal(max_index, np.array(arr1.shape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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import itertools
|
| 2 |
+
import warnings
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pytest
|
| 6 |
+
from numpy.testing import assert_allclose
|
| 7 |
+
from scipy.ndimage import fourier_shift
|
| 8 |
+
import scipy.fft as fft
|
| 9 |
+
|
| 10 |
+
from skimage import img_as_float
|
| 11 |
+
from skimage._shared._warnings import expected_warnings
|
| 12 |
+
from skimage._shared.testing import assert_stacklevel
|
| 13 |
+
from skimage._shared.utils import _supported_float_type
|
| 14 |
+
from skimage.data import camera, binary_blobs, eagle
|
| 15 |
+
from skimage.registration._phase_cross_correlation import (
|
| 16 |
+
phase_cross_correlation,
|
| 17 |
+
_upsampled_dft,
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@pytest.mark.parametrize('normalization', [None, 'phase'])
|
| 22 |
+
def test_correlation(normalization):
|
| 23 |
+
reference_image = fft.fftn(camera())
|
| 24 |
+
shift = (-7, 12)
|
| 25 |
+
shifted_image = fourier_shift(reference_image, shift)
|
| 26 |
+
|
| 27 |
+
# pixel precision
|
| 28 |
+
result, _, _ = phase_cross_correlation(
|
| 29 |
+
reference_image, shifted_image, space="fourier", normalization=normalization
|
| 30 |
+
)
|
| 31 |
+
assert_allclose(result[:2], -np.array(shift))
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
@pytest.mark.parametrize('normalization', ['nonexisting'])
|
| 35 |
+
def test_correlation_invalid_normalization(normalization):
|
| 36 |
+
reference_image = fft.fftn(camera())
|
| 37 |
+
shift = (-7, 12)
|
| 38 |
+
shifted_image = fourier_shift(reference_image, shift)
|
| 39 |
+
|
| 40 |
+
# pixel precision
|
| 41 |
+
with pytest.raises(ValueError):
|
| 42 |
+
phase_cross_correlation(
|
| 43 |
+
reference_image, shifted_image, space="fourier", normalization=normalization
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
@pytest.mark.parametrize('normalization', [None, 'phase'])
|
| 48 |
+
def test_subpixel_precision(normalization):
|
| 49 |
+
reference_image = fft.fftn(camera())
|
| 50 |
+
subpixel_shift = (-2.4, 1.32)
|
| 51 |
+
shifted_image = fourier_shift(reference_image, subpixel_shift)
|
| 52 |
+
|
| 53 |
+
# subpixel precision
|
| 54 |
+
result, _, _ = phase_cross_correlation(
|
| 55 |
+
reference_image,
|
| 56 |
+
shifted_image,
|
| 57 |
+
upsample_factor=100,
|
| 58 |
+
space="fourier",
|
| 59 |
+
normalization=normalization,
|
| 60 |
+
)
|
| 61 |
+
assert_allclose(result[:2], -np.array(subpixel_shift), atol=0.05)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
@pytest.mark.parametrize('dtype', [np.float16, np.float32, np.float64])
|
| 65 |
+
def test_real_input(dtype):
|
| 66 |
+
reference_image = camera().astype(dtype, copy=False)
|
| 67 |
+
subpixel_shift = (-2.4, 1.32)
|
| 68 |
+
shifted_image = fourier_shift(fft.fftn(reference_image), subpixel_shift)
|
| 69 |
+
shifted_image = fft.ifftn(shifted_image).real.astype(dtype, copy=False)
|
| 70 |
+
|
| 71 |
+
# subpixel precision
|
| 72 |
+
result, error, diffphase = phase_cross_correlation(
|
| 73 |
+
reference_image, shifted_image, upsample_factor=100
|
| 74 |
+
)
|
| 75 |
+
assert result.dtype == _supported_float_type(dtype)
|
| 76 |
+
assert_allclose(result[:2], -np.array(subpixel_shift), atol=0.05)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def test_size_one_dimension_input():
|
| 80 |
+
# take a strip of the input image
|
| 81 |
+
reference_image = fft.fftn(camera()[:, 15]).reshape((-1, 1))
|
| 82 |
+
subpixel_shift = (-2.4, 4)
|
| 83 |
+
shifted_image = fourier_shift(reference_image, subpixel_shift)
|
| 84 |
+
|
| 85 |
+
# subpixel precision
|
| 86 |
+
result, error, diffphase = phase_cross_correlation(
|
| 87 |
+
reference_image, shifted_image, upsample_factor=20, space="fourier"
|
| 88 |
+
)
|
| 89 |
+
assert_allclose(result[:2], -np.array((-2.4, 0)), atol=0.05)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def test_3d_input():
|
| 93 |
+
phantom = img_as_float(binary_blobs(length=32, n_dim=3))
|
| 94 |
+
reference_image = fft.fftn(phantom)
|
| 95 |
+
shift = (-2.0, 1.0, 5.0)
|
| 96 |
+
shifted_image = fourier_shift(reference_image, shift)
|
| 97 |
+
|
| 98 |
+
result, error, diffphase = phase_cross_correlation(
|
| 99 |
+
reference_image, shifted_image, space="fourier"
|
| 100 |
+
)
|
| 101 |
+
assert_allclose(result, -np.array(shift), atol=0.05)
|
| 102 |
+
|
| 103 |
+
# subpixel precision now available for 3-D data
|
| 104 |
+
|
| 105 |
+
subpixel_shift = (-2.3, 1.7, 5.4)
|
| 106 |
+
shifted_image = fourier_shift(reference_image, subpixel_shift)
|
| 107 |
+
result, error, diffphase = phase_cross_correlation(
|
| 108 |
+
reference_image, shifted_image, upsample_factor=100, space="fourier"
|
| 109 |
+
)
|
| 110 |
+
assert_allclose(result, -np.array(subpixel_shift), atol=0.05)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def test_unknown_space_input():
|
| 114 |
+
image = np.ones((5, 5))
|
| 115 |
+
with pytest.raises(ValueError):
|
| 116 |
+
phase_cross_correlation(image, image, space="frank")
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def test_wrong_input():
|
| 120 |
+
# Dimensionality mismatch
|
| 121 |
+
image = np.ones((5, 5, 1))
|
| 122 |
+
template = np.ones((5, 5))
|
| 123 |
+
with pytest.raises(ValueError):
|
| 124 |
+
phase_cross_correlation(template, image)
|
| 125 |
+
|
| 126 |
+
# Size mismatch
|
| 127 |
+
image = np.ones((5, 5))
|
| 128 |
+
template = np.ones((4, 4))
|
| 129 |
+
with pytest.raises(ValueError):
|
| 130 |
+
phase_cross_correlation(template, image)
|
| 131 |
+
|
| 132 |
+
# NaN values in data
|
| 133 |
+
image = np.ones((5, 5))
|
| 134 |
+
image[0][0] = np.nan
|
| 135 |
+
template = np.ones((5, 5))
|
| 136 |
+
with expected_warnings(
|
| 137 |
+
[
|
| 138 |
+
r"invalid value encountered in true_divide"
|
| 139 |
+
+ r"|"
|
| 140 |
+
+ r"invalid value encountered in divide"
|
| 141 |
+
+ r"|\A\Z"
|
| 142 |
+
]
|
| 143 |
+
):
|
| 144 |
+
with pytest.raises(ValueError):
|
| 145 |
+
phase_cross_correlation(template, image)
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
def test_4d_input_pixel():
|
| 149 |
+
phantom = img_as_float(binary_blobs(length=32, n_dim=4))
|
| 150 |
+
reference_image = fft.fftn(phantom)
|
| 151 |
+
shift = (-2.0, 1.0, 5.0, -3)
|
| 152 |
+
shifted_image = fourier_shift(reference_image, shift)
|
| 153 |
+
result, error, diffphase = phase_cross_correlation(
|
| 154 |
+
reference_image, shifted_image, space="fourier"
|
| 155 |
+
)
|
| 156 |
+
assert_allclose(result, -np.array(shift), atol=0.05)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
def test_4d_input_subpixel():
|
| 160 |
+
phantom = img_as_float(binary_blobs(length=32, n_dim=4))
|
| 161 |
+
reference_image = fft.fftn(phantom)
|
| 162 |
+
subpixel_shift = (-2.3, 1.7, 5.4, -3.2)
|
| 163 |
+
shifted_image = fourier_shift(reference_image, subpixel_shift)
|
| 164 |
+
result, error, diffphase = phase_cross_correlation(
|
| 165 |
+
reference_image, shifted_image, upsample_factor=10, space="fourier"
|
| 166 |
+
)
|
| 167 |
+
assert_allclose(result, -np.array(subpixel_shift), atol=0.05)
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
def test_mismatch_upsampled_region_size():
|
| 171 |
+
with pytest.raises(ValueError):
|
| 172 |
+
_upsampled_dft(np.ones((4, 4)), upsampled_region_size=[3, 2, 1, 4])
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
def test_mismatch_offsets_size():
|
| 176 |
+
with pytest.raises(ValueError):
|
| 177 |
+
_upsampled_dft(np.ones((4, 4)), 3, axis_offsets=[3, 2, 1, 4])
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
@pytest.mark.parametrize(
|
| 181 |
+
('shift0', 'shift1'),
|
| 182 |
+
itertools.product((100, -100, 350, -350), (100, -100, 350, -350)),
|
| 183 |
+
)
|
| 184 |
+
def test_disambiguate_2d(shift0, shift1):
|
| 185 |
+
image = eagle()[500:, 900:] # use a highly textured part of image
|
| 186 |
+
shift = (shift0, shift1)
|
| 187 |
+
origin0 = []
|
| 188 |
+
for s in shift:
|
| 189 |
+
if s > 0:
|
| 190 |
+
origin0.append(0)
|
| 191 |
+
else:
|
| 192 |
+
origin0.append(-s)
|
| 193 |
+
origin1 = np.array(origin0) + shift
|
| 194 |
+
slice0 = tuple(slice(o, o + 450) for o in origin0)
|
| 195 |
+
slice1 = tuple(slice(o, o + 450) for o in origin1)
|
| 196 |
+
reference = image[slice0]
|
| 197 |
+
moving = image[slice1]
|
| 198 |
+
computed_shift, _, _ = phase_cross_correlation(
|
| 199 |
+
reference,
|
| 200 |
+
moving,
|
| 201 |
+
disambiguate=True,
|
| 202 |
+
)
|
| 203 |
+
np.testing.assert_equal(shift, computed_shift)
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
def test_invalid_value_in_division_warnings():
|
| 207 |
+
"""Regression test for https://github.com/scikit-image/scikit-image/issues/7146."""
|
| 208 |
+
im1 = np.zeros((100, 100))
|
| 209 |
+
im1[50, 50] = 1
|
| 210 |
+
im2 = np.zeros((100, 100))
|
| 211 |
+
im2[60, 60] = 1
|
| 212 |
+
with warnings.catch_warnings():
|
| 213 |
+
warnings.simplefilter("error")
|
| 214 |
+
phase_cross_correlation(im1, im2, disambiguate=True)
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
@pytest.mark.parametrize('disambiguate', [True, False])
|
| 218 |
+
def test_disambiguate_zero_shift(disambiguate):
|
| 219 |
+
"""When the shift is 0, disambiguation becomes degenerate.
|
| 220 |
+
|
| 221 |
+
Some quadrants become size 0, which prevents computation of
|
| 222 |
+
cross-correlation. This test ensures that nothing bad happens in that
|
| 223 |
+
scenario.
|
| 224 |
+
"""
|
| 225 |
+
image = camera()
|
| 226 |
+
computed_shift, _, _ = phase_cross_correlation(
|
| 227 |
+
image,
|
| 228 |
+
image,
|
| 229 |
+
disambiguate=disambiguate,
|
| 230 |
+
)
|
| 231 |
+
assert isinstance(computed_shift, np.ndarray)
|
| 232 |
+
np.testing.assert_array_equal(computed_shift, np.array((0.0, 0.0)))
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
@pytest.mark.parametrize('null_images', [(1, 0), (0, 1), (0, 0)])
|
| 236 |
+
def test_disambiguate_empty_image(null_images):
|
| 237 |
+
"""When the image is empty, disambiguation becomes degenerate."""
|
| 238 |
+
image = camera()
|
| 239 |
+
with pytest.warns(UserWarning) as records:
|
| 240 |
+
shift, error, phasediff = phase_cross_correlation(
|
| 241 |
+
image * null_images[0], image * null_images[1], disambiguate=True
|
| 242 |
+
)
|
| 243 |
+
assert_stacklevel(records, offset=-3)
|
| 244 |
+
np.testing.assert_array_equal(shift, np.array([0.0, 0.0]))
|
| 245 |
+
assert np.isnan(error)
|
| 246 |
+
assert phasediff == 0.0
|
| 247 |
+
|
| 248 |
+
# Check warnings
|
| 249 |
+
assert len(records) == 2
|
| 250 |
+
assert "Could not determine real-space shift" in records[0].message.args[0]
|
| 251 |
+
assert "Could not determine RMS error between images" in records[1].message.args[0]
|
infer_4_47_1/lib/python3.10/site-packages/skimage/registration/tests/test_tvl1.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from skimage._shared.utils import _supported_float_type
|
| 5 |
+
from skimage.registration import optical_flow_tvl1
|
| 6 |
+
from skimage.transform import warp
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def _sin_flow_gen(image0, max_motion=4.5, npics=5):
|
| 10 |
+
"""Generate a synthetic ground truth optical flow with a sinusoid as
|
| 11 |
+
first component.
|
| 12 |
+
|
| 13 |
+
Parameters
|
| 14 |
+
----------
|
| 15 |
+
image0: ndarray
|
| 16 |
+
The base image to be warped.
|
| 17 |
+
max_motion: float
|
| 18 |
+
Maximum flow magnitude.
|
| 19 |
+
npics: int
|
| 20 |
+
Number of sinusoid pics.
|
| 21 |
+
|
| 22 |
+
Returns
|
| 23 |
+
-------
|
| 24 |
+
flow, image1 : ndarray
|
| 25 |
+
The synthetic ground truth optical flow with a sinusoid as
|
| 26 |
+
first component and the corresponding warped image.
|
| 27 |
+
|
| 28 |
+
"""
|
| 29 |
+
grid = np.meshgrid(*[np.arange(n) for n in image0.shape], indexing='ij')
|
| 30 |
+
grid = np.stack(grid)
|
| 31 |
+
gt_flow = np.zeros_like(grid, dtype=float)
|
| 32 |
+
gt_flow[0, ...] = max_motion * np.sin(grid[0] / grid[0].max() * npics * np.pi)
|
| 33 |
+
image1 = warp(image0, grid - gt_flow, mode='edge')
|
| 34 |
+
return gt_flow, image1
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@pytest.mark.parametrize('dtype', [np.float16, np.float32, np.float64])
|
| 38 |
+
def test_2d_motion(dtype):
|
| 39 |
+
# Generate synthetic data
|
| 40 |
+
rng = np.random.default_rng(0)
|
| 41 |
+
image0 = rng.normal(size=(256, 256))
|
| 42 |
+
gt_flow, image1 = _sin_flow_gen(image0)
|
| 43 |
+
image1 = image1.astype(dtype, copy=False)
|
| 44 |
+
float_dtype = _supported_float_type(dtype)
|
| 45 |
+
# Estimate the flow
|
| 46 |
+
flow = optical_flow_tvl1(image0, image1, attachment=5, dtype=float_dtype)
|
| 47 |
+
assert flow.dtype == float_dtype
|
| 48 |
+
# Assert that the average absolute error is less then half a pixel
|
| 49 |
+
assert abs(flow - gt_flow).mean() < 0.5
|
| 50 |
+
|
| 51 |
+
if dtype != float_dtype:
|
| 52 |
+
with pytest.raises(ValueError):
|
| 53 |
+
optical_flow_tvl1(image0, image1, attachment=5, dtype=dtype)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def test_3d_motion():
|
| 57 |
+
# Generate synthetic data
|
| 58 |
+
rng = np.random.default_rng(0)
|
| 59 |
+
image0 = rng.normal(size=(100, 100, 100))
|
| 60 |
+
gt_flow, image1 = _sin_flow_gen(image0)
|
| 61 |
+
# Estimate the flow
|
| 62 |
+
flow = optical_flow_tvl1(image0, image1, attachment=10)
|
| 63 |
+
# Assert that the average absolute error is less then half a pixel
|
| 64 |
+
assert abs(flow - gt_flow).mean() < 0.5
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def test_no_motion_2d():
|
| 68 |
+
rng = np.random.default_rng(0)
|
| 69 |
+
img = rng.normal(size=(256, 256))
|
| 70 |
+
|
| 71 |
+
flow = optical_flow_tvl1(img, img)
|
| 72 |
+
|
| 73 |
+
assert np.all(flow == 0)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def test_no_motion_3d():
|
| 77 |
+
rng = np.random.default_rng(0)
|
| 78 |
+
img = rng.normal(size=(64, 64, 64))
|
| 79 |
+
|
| 80 |
+
flow = optical_flow_tvl1(img, img)
|
| 81 |
+
|
| 82 |
+
assert np.all(flow == 0)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def test_optical_flow_dtype():
|
| 86 |
+
# Generate synthetic data
|
| 87 |
+
rng = np.random.default_rng(0)
|
| 88 |
+
image0 = rng.normal(size=(256, 256))
|
| 89 |
+
gt_flow, image1 = _sin_flow_gen(image0)
|
| 90 |
+
# Estimate the flow at double precision
|
| 91 |
+
flow_f64 = optical_flow_tvl1(image0, image1, attachment=5, dtype=np.float64)
|
| 92 |
+
|
| 93 |
+
assert flow_f64.dtype == np.float64
|
| 94 |
+
|
| 95 |
+
# Estimate the flow at single precision
|
| 96 |
+
flow_f32 = optical_flow_tvl1(image0, image1, attachment=5, dtype=np.float32)
|
| 97 |
+
|
| 98 |
+
assert flow_f32.dtype == np.float32
|
| 99 |
+
|
| 100 |
+
# Assert that floating point precision does not affect the quality
|
| 101 |
+
# of the estimated flow
|
| 102 |
+
|
| 103 |
+
assert np.abs(flow_f64 - flow_f32).mean() < 1e-3
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def test_incompatible_shapes():
|
| 107 |
+
rng = np.random.default_rng(0)
|
| 108 |
+
I0 = rng.normal(size=(256, 256))
|
| 109 |
+
I1 = rng.normal(size=(128, 256))
|
| 110 |
+
with pytest.raises(ValueError):
|
| 111 |
+
u, v = optical_flow_tvl1(I0, I1)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def test_wrong_dtype():
|
| 115 |
+
rng = np.random.default_rng(0)
|
| 116 |
+
img = rng.normal(size=(256, 256))
|
| 117 |
+
with pytest.raises(ValueError):
|
| 118 |
+
u, v = optical_flow_tvl1(img, img, dtype=np.int64)
|
infer_4_47_1/lib/python3.10/site-packages/skimage/transform/__init__.pyi
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Explicitly setting `__all__` is necessary for type inference engines
|
| 2 |
+
# to know which symbols are exported. See
|
| 3 |
+
# https://peps.python.org/pep-0484/#stub-files
|
| 4 |
+
|
| 5 |
+
__all__ = [
|
| 6 |
+
'hough_circle',
|
| 7 |
+
'hough_ellipse',
|
| 8 |
+
'hough_line',
|
| 9 |
+
'probabilistic_hough_line',
|
| 10 |
+
'hough_circle_peaks',
|
| 11 |
+
'hough_line_peaks',
|
| 12 |
+
'radon',
|
| 13 |
+
'iradon',
|
| 14 |
+
'iradon_sart',
|
| 15 |
+
'order_angles_golden_ratio',
|
| 16 |
+
'frt2',
|
| 17 |
+
'ifrt2',
|
| 18 |
+
'integral_image',
|
| 19 |
+
'integrate',
|
| 20 |
+
'warp',
|
| 21 |
+
'warp_coords',
|
| 22 |
+
'warp_polar',
|
| 23 |
+
'estimate_transform',
|
| 24 |
+
'matrix_transform',
|
| 25 |
+
'EuclideanTransform',
|
| 26 |
+
'SimilarityTransform',
|
| 27 |
+
'AffineTransform',
|
| 28 |
+
'ProjectiveTransform',
|
| 29 |
+
'EssentialMatrixTransform',
|
| 30 |
+
'FundamentalMatrixTransform',
|
| 31 |
+
'PolynomialTransform',
|
| 32 |
+
'PiecewiseAffineTransform',
|
| 33 |
+
'ThinPlateSplineTransform',
|
| 34 |
+
'swirl',
|
| 35 |
+
'resize',
|
| 36 |
+
'resize_local_mean',
|
| 37 |
+
'rotate',
|
| 38 |
+
'rescale',
|
| 39 |
+
'downscale_local_mean',
|
| 40 |
+
'pyramid_reduce',
|
| 41 |
+
'pyramid_expand',
|
| 42 |
+
'pyramid_gaussian',
|
| 43 |
+
'pyramid_laplacian',
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
from .hough_transform import (
|
| 47 |
+
hough_line,
|
| 48 |
+
hough_line_peaks,
|
| 49 |
+
probabilistic_hough_line,
|
| 50 |
+
hough_circle,
|
| 51 |
+
hough_circle_peaks,
|
| 52 |
+
hough_ellipse,
|
| 53 |
+
)
|
| 54 |
+
from .radon_transform import radon, iradon, iradon_sart, order_angles_golden_ratio
|
| 55 |
+
from .finite_radon_transform import frt2, ifrt2
|
| 56 |
+
from .integral import integral_image, integrate
|
| 57 |
+
from ._geometric import (
|
| 58 |
+
estimate_transform,
|
| 59 |
+
matrix_transform,
|
| 60 |
+
EuclideanTransform,
|
| 61 |
+
SimilarityTransform,
|
| 62 |
+
AffineTransform,
|
| 63 |
+
ProjectiveTransform,
|
| 64 |
+
FundamentalMatrixTransform,
|
| 65 |
+
EssentialMatrixTransform,
|
| 66 |
+
PolynomialTransform,
|
| 67 |
+
PiecewiseAffineTransform,
|
| 68 |
+
)
|
| 69 |
+
from ._thin_plate_splines import ThinPlateSplineTransform
|
| 70 |
+
from ._warps import (
|
| 71 |
+
swirl,
|
| 72 |
+
resize,
|
| 73 |
+
rotate,
|
| 74 |
+
rescale,
|
| 75 |
+
downscale_local_mean,
|
| 76 |
+
warp,
|
| 77 |
+
warp_coords,
|
| 78 |
+
warp_polar,
|
| 79 |
+
resize_local_mean,
|
| 80 |
+
)
|
| 81 |
+
from .pyramids import (
|
| 82 |
+
pyramid_reduce,
|
| 83 |
+
pyramid_expand,
|
| 84 |
+
pyramid_gaussian,
|
| 85 |
+
pyramid_laplacian,
|
| 86 |
+
)
|