img-phy-sim 0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of img-phy-sim might be problematic. Click here for more details.

@@ -0,0 +1,400 @@
1
+ Metadata-Version: 2.1
2
+ Name: img-phy-sim
3
+ Version: 0.1
4
+ Summary: Physical Simulations on Images.
5
+ Home-page: https://github.com/xXAI-botXx/Image-Physics-Simulation
6
+ Download-URL: https://github.com/xXAI-botXx/Image-Physics-Simulation/archive/v_01.tar.gz
7
+ Author: Tobia Ippolito
8
+ Project-URL: Documentation, https://xxai-botxx.github.io/Image-Physics-Simulation/img_phy_sim
9
+ Project-URL: Source, https://github.com/xXAI-botXx/Image-Physics-Simulation
10
+ Keywords: Simulation,Computer-Vision,Physgen
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Programming Language :: Python :: 3.8
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Description-Content-Type: text/markdown
21
+ Requires-Dist: numpy
22
+ Requires-Dist: opencv-python
23
+ Requires-Dist: matplotlib
24
+ Requires-Dist: scikit-image
25
+
26
+ # **I**mage-**P**hysics-**S**imulation
27
+
28
+ This is a library for 2D Ray-Tracing on an image. For example the Physgen Dataset, [see here](https://huggingface.co/datasets/mspitzna/physicsgen).
29
+
30
+ Contents:
31
+ - [Installation](#installation)
32
+ - [Download Example Data](#download-example-data)
33
+ - [Library Overview](#library-overview)
34
+ - [Raytracing Computation](#raytracing-computation)
35
+ - [Raytracing Tutorial](#raytracing-tutorial)
36
+ - [Performance Test](#performance-test)
37
+
38
+ [> Documentation <](https://xxai-botxx.github.io/Image-Physics-Simulation/img_phy_sim.html)
39
+
40
+ <img src="./img_phy_sim/raytracing_example.png"></img>
41
+
42
+ <br><br>
43
+
44
+ ### Installation
45
+
46
+ This repo only need some basic libraries:
47
+ - `numpy`
48
+ - `matplotlib`
49
+ - `opencv-python`
50
+ - `scikit-image`
51
+
52
+ You can download / clone this repo and run the example notebook via following Python/Anaconda setup:
53
+ ```bash
54
+ conda create -n img-phy-sim python=3.13 pip -y
55
+ conda activate img-phy-sim
56
+ pip install numpy matplotlib opencv-python ipython jupyter shapely prime_printer datasets==3.6.0 scikit-image
57
+ pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
58
+ ```
59
+
60
+ You can also use this repo via [Python Package Index (PyPI)](https://pypi.org/) as a package:
61
+ ```bash
62
+ pip install img-phy-sim
63
+ ```
64
+
65
+ Here the instructions to use the package version of `ips` and an anconda setup:
66
+ ```bash
67
+ conda create -n img-phy-sim python=3.13 pip -y
68
+ conda activate img-phy-sim
69
+ pip install img-phy-sim
70
+ ```
71
+
72
+ To run the example code you still need:
73
+ ```bash
74
+ pip install prime_printer shapely datasets==3.6.0
75
+ pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
76
+ ```
77
+
78
+ <br><br>
79
+
80
+ ### Download Example Data
81
+
82
+ You can download Physgen data if wanted via the `physgen_dataset.py` using following commands:
83
+
84
+ ```bash
85
+ conda activate img-phy-sim
86
+ cd "D:\Informatik\Projekte\Image-Physics-Simulation" && D:
87
+ python physgen_dataset.py --output_real_path ./datasets/physgen_train_raw/real --output_osm_path ./datasets/physgen_train_raw/osm --variation sound_reflection --input_type osm --output_type standard --data_mode train
88
+ ```
89
+
90
+ ```bash
91
+ python physgen_dataset.py --output_real_path ./datasets/physgen_test_raw/real --output_osm_path ./datasets/physgen_test_raw/osm --variation sound_reflection --input_type osm --output_type standard --data_mode test
92
+ ```
93
+
94
+ ```bash
95
+ python physgen_dataset.py --output_real_path ./datasets/physgen_val_raw/real --output_osm_path ./datasets/physgen_val_raw/osm --variation sound_reflection --input_type osm --output_type standard --data_mode validation
96
+ ```
97
+
98
+ <br><br>
99
+
100
+ ### Library Overview
101
+
102
+ - Img-Phy-Sim (ips)
103
+ - `ray_tracing`
104
+ - `get_wall_map`: extracting a wall-map from an image
105
+ - `trace-beam`: calculates one ray
106
+ - `trace_beams`: load an image, extract the wall-map and trace multiple beams
107
+ - `draw_rays`: draw/export the rays as/inside an image
108
+ - `print_rays_info`: get some interesting informations about your rays
109
+ - `save`: save your rays as txt file
110
+ - `open`: load your saved rays txt file
111
+ - `get_linear_degree_range`: get a range for your beam-directions -> example beams between 0-360 with stepsize 10
112
+ - `merge_rays`: merge 2 rays to one 'object'
113
+ - `img`
114
+ - `open`: load an image via Open-CV
115
+ - `save`: save an image
116
+ - `imshow`: show an single image (without much features)
117
+ - `advanced_imshow`: show multiple images with many options
118
+ - `show_image_with_line_and_profile`: show an image with a red line + the values of the image on this line
119
+ - `plot_image_with_values`: plot an image with it's value plotted and averaged to see your image in values
120
+
121
+
122
+ That are not all functions but the ones which should be most useful. Check out the documentation for all functions.
123
+
124
+ <br><br>
125
+
126
+ ### Raytracing Computation
127
+
128
+ 1. A map is created which contains the vector of all "walls" (collision objects) in the image as a single degree value. This is done using the `ips.ray_tracing.get_wall_map`-function, which internally uses the canny-edge detection algorithm from OpenCV. The whole process is masked to get the edges for the given wall-values. In order to put the degree/direction values into the wall-map, we following the `Bresenham's line algorithm` to go from one point pixel-wise to the end-point of the edge and on the way we put the value of thes epixels and the pixels around, depending of the used *thickness*.
129
+ 2. The `ips.ray_tracing.trace_beam`-function goes from pixel to pixel and checks if the pixel-value is a "wall" (collision-object). If yes, a new beam is added using the wall-map to calculate the right direction-vector. To know, which pixel is the next pixel, we use the average vector between the direction vector (the target direction which is available since the beginning of a beam calculation) and a vector pointing to the closest point of the optimal way/vector. Why so complicated? Because we move per pixel and pixel does only know 8 directions, but we want to move more smooth in more directions. The direction vector from the beginning is helpful but will lead every update to the same pixel, because it does not use the information of the current position. THis is bad because so we will move very wrong if going only after the goal vector with our limited step-directions. And so we add the described "to the perfect line"-vector into the calculation weighted a bit less then the real direction (0.5).<br>The "perfect line" is calculated very easy. At the beginning of a beam we move in small steps (like 0.01) forward towards the target direction, ignoring pixel-wise approach, until we hit with x or y a boundary of the image.
130
+
131
+ > You may have noticed that we use a own and Bresenham's line algorithm for moving in the pixel-space, which have no further reason and we may only use one of them in future.
132
+
133
+
134
+
135
+ <br><br>
136
+
137
+ ### Raytracing Tutorial
138
+
139
+ [See also the example notebook 👀](./example/physgen.ipynb)
140
+
141
+ In general you need to do:
142
+ 1. **Load your Image** -> using `ips.img.open`
143
+ 2. **Calculate the Wall-Map** -> using `ips.ray_tracing.get_wall_map`
144
+ 3. **Calculate the Beams** -> using `ips.img.open`
145
+ 4. **Draw (Export) the Beams** -> using `ips.img.open`
146
+
147
+ Using this lib, this is reduced to:
148
+ 1. **Calculate the Beams** (including Wall-Map and loading your Image) -> using `ips.img.open`
149
+ 2. **Draw (Export) the Beams** -> using `ips.img.open`
150
+
151
+ See this example:
152
+
153
+ ```python
154
+ rays = ips.ray_tracing.trace_beams(rel_position=[0.5, 0.5],
155
+ img_src=img_src,
156
+ directions_in_degree=ips.ray_tracing.get_linear_degree_range(step_size=10),
157
+ wall_values=None,
158
+ wall_thickness=1,
159
+ img_border_also_collide=False,
160
+ reflexion_order=1,
161
+ should_scale_rays=True,
162
+ should_scale_img=True)
163
+ ips.ray_tracing.print_rays_info(rays)
164
+
165
+ ray_img = ips.ray_tracing.draw_rays(rays, detail_draw=False,
166
+ output_format="single_image",
167
+ img_background=img, ray_value=2, ray_thickness=1,
168
+ img_shape=(256, 256), dtype=float, standard_value=0,
169
+ should_scale_rays_to_image=True, original_max_width=None, original_max_height=None)
170
+ ips.img.imshow(ray_img, size=5)
171
+ ```
172
+
173
+ **Rays structure:**<br>
174
+ The Raytracing result is a list of rays, where every ray can consist of multiple beams which comes from reflections. One beam is a list of multiple points, where the first point is the start point and the last element is the end point.
175
+
176
+ Example:
177
+ ```text
178
+ [
179
+ [[start-point, ..., end-point], [start-point, ..., end-point]], # one reflection
180
+ [[start-point, ..., end-point]], # no reflection
181
+ # ...
182
+ ]
183
+ ```
184
+
185
+ <br><br>
186
+
187
+ Now let's go step by step how to apply ray-tracing to your image.
188
+
189
+ <br><br>
190
+
191
+ **1. Analyzing your image**<br>
192
+ First it is important you know the values of your image and which values should consider an object for collision. For that use the tools given with this package:
193
+ ```python
194
+ img = ips.img.open(src=img_src, should_scale=False, should_print=True)
195
+ ips.img.imshow(img, size=4, axis_off=False)
196
+ ips.img.show_image_with_line_and_profile(imgs=[img], axis='row', index=None, titles=None, figsize=(10, 8));
197
+ ```
198
+ If you see super small values then you might tried to scale your already scaled image.
199
+
200
+ <br><br>
201
+
202
+ **2. Get the Collision Ready**<br>
203
+ Next it is helpful to check if your collision is ready by running the wall-map by yourself (later you will use the wrapper, but here you can find the right params).
204
+
205
+ ```python
206
+ wall_map = ips.ray_tracing.get_wall_map(img, wall_values=None, thickness=0)
207
+ ips.img.imshow(wall_map, size=4, axis_off=False)
208
+ ips.img.show_image_with_line_and_profile(imgs=[wall_map], axis='row', index=None, titles=None, figsize=(10, 8));
209
+ ```
210
+
211
+ You can give `wall_values` a list of values, which you want to collide with.
212
+
213
+ <br><br>
214
+
215
+ **3. Let's start tracing the rays!**<br>
216
+ Now everything should be ready for tracing the rays. The following code include loading your image and creating the wall-map.
217
+
218
+ ```python
219
+ rays = ips.ray_tracing.trace_beams(rel_position=[0.5, 0.5],
220
+ img_src="./my_image.png",
221
+ directions_in_degree=ips.ray_tracing.get_linear_degree_range(start=0, stop=360, step_size=10),
222
+ wall_values=[0.0],
223
+ wall_thickness=0,
224
+ img_border_also_collide=False,
225
+ reflexion_order=1,
226
+ should_scale_rays=True,
227
+ should_scale_img=True)
228
+ ips.ray_tracing.print_rays_info(rays)
229
+ ```
230
+
231
+ Following features are included:
232
+ - Setting custom startposition for raytracing
233
+ - Adding custom beam shooting positions in degree (where 0° is the east/right of the image and 90° is south/bottom and so on)
234
+ - Setting reflexion order (how many maxium reflexions should be calculated)
235
+ - Setting if the border of the image should be reflective or not
236
+ - And setting if the input image should be scaled and if the rays itself should be scaled
237
+ - You can also set the "wall" object values, which should get detected as objects with collision. If set to None, the programm will find all clear edges.
238
+ - Setting if the rays should be in 0.0-1.0 range or the real image range
239
+ - Whether to scale the image or not
240
+
241
+ <br><br>
242
+
243
+ **4. Export your rays**<br>
244
+ At the end you might want to use your rays in an image. We provide you with a draw/export function with many flexibility.
245
+
246
+ Features are:
247
+ - Custom value of ray-traces
248
+ - Thickness of ray-traces
249
+ - Drawing on empty image or an existing image
250
+ - Given image-shape, dtype and fill-value (standard-value)
251
+ - Scaling rays to the given image
252
+ - Different Format Types
253
+ - One Image -> `single_image`
254
+ - Multiple Images (each ray on one image) -> `multiple_images`
255
+ - One Image and each channel is one ray -> `channels`
256
+ - Showing only the reflexions
257
+ - Give different values for different reflexion orders
258
+ ```
259
+ ray_img = ips.ray_tracing.draw_rays(rays, detail_draw=False,
260
+ output_format="single_image",
261
+ img_background=None, ray_value=2, ray_thickness=1,
262
+ img_shape=(256, 256), dtype=float, standard_value=0,
263
+ should_scale_rays_to_image=False, original_max_width=None, original_max_height=None,
264
+ show_only_reflections=False)
265
+ ips.img.imshow(ray_img, size=4)
266
+ ```
267
+
268
+ <br><br>
269
+
270
+ I hope this little tutorial could be helpful. Good luck with your project <3
271
+
272
+ <br><br>
273
+
274
+ ### Performance Test
275
+
276
+ <br>
277
+
278
+ [> See the notebook/code <](./example/physgen_performance.ipynb)
279
+
280
+ Executed with 50 random images.
281
+
282
+ Standard Settings were:
283
+ - test_amount=50
284
+ - step_size=10
285
+ - reflexion_order=3
286
+ - ray_scaling=True
287
+ - detail_draw=False
288
+ - output_format="channels"
289
+
290
+ <br>
291
+
292
+ Investigated Factors:
293
+ - `Ray Amount` (step_size)
294
+ - `Scaling of Rays` (ray_scaling)
295
+ - `Reflexion Order` (reflexion_order)
296
+ - `Detail Drawing of Rays` (detail_draw)
297
+
298
+
299
+ <br><br>
300
+
301
+ Experiment 1: Ray Amount
302
+
303
+ ```text
304
+ Number of experiments: 4
305
+
306
+ avg_time : mean=16.0064, std=23.6257, min=0.5744, max=56.7918, rel_change=351.22%
307
+ median_time : mean=15.4329, std=22.7908, min=0.5546, max=54.7799, rel_change=351.36%
308
+ var_time : mean=17.1176, std=29.2774, min=0.0076, max=67.8259, rel_change=396.19%
309
+ avg_compute_time : mean=15.1070, std=22.2322, min=0.5529, max=53.4815, rel_change=350.36%
310
+ median_compute_time : mean=14.5613, std=21.4404, min=0.5346, max=51.5723, rel_change=350.50%
311
+ var_compute_time : mean=14.7739, std=25.2362, min=0.0074, max=58.4825, rel_change=395.80%
312
+ avg_draw_time : mean=0.8994, std=1.3940, min=0.0215, max=3.3103, rel_change=365.67%
313
+ median_draw_time : mean=0.8373, std=1.2914, min=0.0207, max=3.0705, rel_change=364.22%
314
+ var_draw_time : mean=0.1525, std=0.2639, min=0.0000, max=0.6096, rel_change=399.67%
315
+
316
+ Overall trend in avg_time: increasing (1.7306e+01 change per experiment)
317
+ Conclusion: Performance changes significantly across experiments.
318
+ ```
319
+
320
+
321
+ <br><br>
322
+
323
+ Experiment 2: Ray Scaling
324
+
325
+ ```text
326
+ Number of experiments: 2
327
+
328
+ avg_time : mean=0.5744, std=0.0078, min=0.5666, max=0.5823, rel_change=2.73%
329
+ median_time : mean=0.5558, std=0.0080, min=0.5478, max=0.5637, rel_change=2.86%
330
+ var_time : mean=0.0078, std=0.0000, min=0.0078, max=0.0078, rel_change=0.16%
331
+ avg_compute_time : mean=0.5581, std=0.0024, min=0.5557, max=0.5605, rel_change=0.86%
332
+ median_compute_time : mean=0.5398, std=0.0026, min=0.5372, max=0.5423, rel_change=0.95%
333
+ var_compute_time : mean=0.0075, std=0.0000, min=0.0075, max=0.0075, rel_change=0.13%
334
+ avg_draw_time : mean=0.0163, std=0.0055, min=0.0108, max=0.0218, rel_change=66.90%
335
+ median_draw_time : mean=0.0160, std=0.0053, min=0.0106, max=0.0213, rel_change=66.86%
336
+ var_draw_time : mean=0.0000, std=0.0000, min=0.0000, max=0.0000, rel_change=67.83%
337
+
338
+ Overall trend in avg_time: decreasing (-1.5690e-02 change per experiment)
339
+ Conclusion: Performance changes slightly across experiments.
340
+ ```
341
+
342
+ <br><br>
343
+
344
+ Experiment 3: Reflexion Order
345
+
346
+ ```text
347
+ Number of experiments: 6
348
+
349
+ avg_time : mean=0.9300, std=0.7354, min=0.3231, max=2.4409, rel_change=227.71%
350
+ median_time : mean=0.8502, std=0.6128, min=0.3253, max=2.0917, rel_change=207.76%
351
+ var_time : mean=0.4635, std=0.9329, min=0.0002, max=2.5421, rel_change=548.45%
352
+ avg_compute_time : mean=0.9014, std=0.7194, min=0.3088, max=2.3798, rel_change=229.74%
353
+ median_compute_time : mean=0.8241, std=0.6002, min=0.3108, max=2.0401, rel_change=209.83%
354
+ var_compute_time : mean=0.4449, std=0.8953, min=0.0002, max=2.4396, rel_change=548.32%
355
+ avg_draw_time : mean=0.0286, std=0.0160, min=0.0143, max=0.0611, rel_change=163.74%
356
+ median_draw_time : mean=0.0266, std=0.0129, min=0.0144, max=0.0521, rel_change=142.21%
357
+ var_draw_time : mean=0.0002, std=0.0004, min=0.0000, max=0.0011, rel_change=550.06%
358
+
359
+ Overall trend in avg_time: increasing (3.7365e-01 change per experiment)
360
+ Conclusion: Performance changes significantly across experiments.
361
+ ```
362
+
363
+ <br><br>
364
+
365
+ Experiment 4: Detail Draw
366
+
367
+ ```text
368
+ Number of experiments: 2
369
+
370
+ avg_time : mean=0.6282, std=0.0515, min=0.5767, max=0.6796, rel_change=16.39%
371
+ median_time : mean=0.6101, std=0.0507, min=0.5593, max=0.6608, rel_change=16.63%
372
+ var_time : mean=0.0115, std=0.0040, min=0.0074, max=0.0155, rel_change=70.54%
373
+ avg_compute_time : mean=0.5572, std=0.0020, min=0.5552, max=0.5592, rel_change=0.70%
374
+ median_compute_time : mean=0.5430, std=0.0047, min=0.5383, max=0.5478, rel_change=1.75%
375
+ var_compute_time : mean=0.0074, std=0.0002, min=0.0072, max=0.0076, rel_change=5.32%
376
+ avg_draw_time : mean=0.0709, std=0.0495, min=0.0214, max=0.1204, rel_change=139.55%
377
+ median_draw_time : mean=0.0663, std=0.0454, min=0.0209, max=0.1117, rel_change=137.05%
378
+ var_draw_time : mean=0.0011, std=0.0011, min=0.0000, max=0.0022, rel_change=199.31%
379
+
380
+ Overall trend in avg_time: increasing (1.0292e-01 change per experiment)
381
+ Conclusion: Performance changes slightly across experiments.
382
+ ```
383
+
384
+ <br><br>
385
+
386
+ Summary:
387
+
388
+ The Stepsize/amount of rays have the biggest impact on the performance. The other parameters have rather a small impact.
389
+
390
+
391
+ | **Experiment** | **Number of Experiments** | **avg_time (mean ± std)** | **avg_compute_time (mean ± std)** | **avg_draw_time (mean ± std)** | **rel_change (avg_time)** | **Trend** | **Conclusion** |
392
+ | --- | --- | --- | --- | --- | --- | --- | --- |
393
+ | **1. Ray Amount** | 4 | 16.01 ± 23.63 s | 15.11 ± 22.23 s | 0.90 ± 1.39 s | **351.22 %** | Increasing (+17.31 s/exp) | Performance changes **significantly** |
394
+ | **2. Ray Scaling** | 2 | 0.57 ± 0.01 s | 0.56 ± 0.00 s | 0.016 ± 0.006 s | **2.73 %** | Decreasing (−0.016 s/exp) | Performance changes **slightly** |
395
+ | **3. Reflection Order** | 6 | 0.93 ± 0.74 s | 0.90 ± 0.72 s | 0.029 ± 0.016 s | **227.71 %** | Increasing (+0.37 s/exp) | Performance changes **significantly** |
396
+ | **4. Detail Draw** | 2 | 0.63 ± 0.05 s | 0.56 ± 0.00 s | 0.071 ± 0.050 s | **16.39 %** | Increasing (+0.10 s/exp) | Performance changes **slightly** |
397
+
398
+
399
+
400
+