diff --git a/.idea/UNI_Python.iml b/.idea/UNI_Python.iml
index adf23df..5854a2f 100644
--- a/.idea/UNI_Python.iml
+++ b/.idea/UNI_Python.iml
@@ -4,7 +4,7 @@
-
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 59b5b2e..c20271c 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -1,4 +1,4 @@
-
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 0d1fe52..cacf0ff 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -5,9 +5,10 @@
-
+
+
-
+
@@ -165,10 +166,21 @@
+
+
+
+
+ file://$PROJECT_DIR$/ha_08/loosen_janniclas_1540907_09.py
+ 63
+
+
+
+
+
-
-
+
+
\ No newline at end of file
diff --git a/ha_09/loosen_janniclas_1540907_10.py b/ha_09/loosen_janniclas_1540907_10.py
index 1f1f613..d5746ad 100644
--- a/ha_09/loosen_janniclas_1540907_10.py
+++ b/ha_09/loosen_janniclas_1540907_10.py
@@ -69,22 +69,31 @@ FLAG_gauss = 0
FLAG_uniform = 1
-def process_image_part(data_part, kernel):
+def process_image_part(data_part, kernel, padding):
y_part_size, x_part_size, _ = data_part.shape
- data_part_new = np.zeros_like(data_part)
- for i in range((kernel.shape[0] - 1) // 2, y_part_size - (kernel.shape[0] - 1) // 2):
- for j in range((kernel.shape[1] - 1) // 2, x_part_size - (kernel.shape[1] - 1) // 2):
+
+ data_part_new = np.zeros((data_part.shape[0] - padding[0], data_part.shape[1] - padding[1], 3))
+
+ pad_y, pad_x = padding
+ for i in range(pad_y, y_part_size - pad_y):
+ for j in range(pad_x, x_part_size - pad_x):
for k in range(3):
new_value = 0.0
for ii in range(kernel.shape[0]):
for jj in range(kernel.shape[1]):
- iii = ii - (kernel.shape[0] - 1) // 2
- jjj = jj - (kernel.shape[1] - 1) // 2
+ iii = ii - pad_y
+ jjj = jj - pad_x
new_value += kernel[ii, jj] * data_part[i + iii, j + jjj, k]
- data_part_new[i, j, k] = new_value
+ data_part_new[i - pad_y, j - pad_x, k] = new_value
return data_part_new
+def split_array(arr, n, overlap):
+ sub_array_length = (len(arr) + (n - 1) * overlap) // n
+ sub_arrays = [arr[i * (sub_array_length - overlap): i * (sub_array_length - overlap) + sub_array_length] for i in range(n)]
+ return np.array(sub_arrays)
+
+
def process_image(img, func=0, mode=0):
if isinstance(img, str):
img = Image.open(img)
@@ -95,15 +104,19 @@ def process_image(img, func=0, mode=0):
data = np.asarray(img, dtype=np.float64) / 255.0
if func == 1:
- kernel = uniform_kernel(3)
+ kernel = uniform_kernel(7)
else:
kernel = gauss_kernel(3)
+ padding = [(kernel.shape[0] // 2), kernel.shape[1] // 2]
+
if mode == 1: # Multithreading mode
num_threads = 16
- data_parts = np.array_split(data, num_threads, axis=0)
+
+ data_parts = split_array(data, num_threads, padding[0])
+
with multiprocessing.Pool(num_threads) as pool:
- data_new_parts = pool.starmap(process_image_part, zip(data_parts, [kernel]*num_threads))
+ data_new_parts = pool.starmap(process_image_part, zip(data_parts, [kernel]*num_threads, [padding]*num_threads))
data_new = np.concatenate(data_new_parts, axis=0)
elif mode == 2: # MPI parallel mode
@@ -111,7 +124,7 @@ def process_image(img, func=0, mode=0):
rank = comm.Get_rank()
size = comm.Get_size()
data_part = np.array_split(data, size, axis=0)[rank]
- data_new_part = process_image_part(data_part, kernel)
+ data_new_part = process_image_part(data_part, kernel, padding)
data_new_parts = comm.gather(data_new_part, root=0)
if rank == 0:
data_new = np.concatenate(data_new_parts, axis=0)
@@ -120,7 +133,7 @@ def process_image(img, func=0, mode=0):
data_new = comm.bcast(data_new, root=0)
else: # Default mode
- data_new = process_image_part(data, kernel)
+ data_new = process_image_part(data, kernel, padding)
data_new = data_new * 255.0
data_new = np.uint8(data_new)
@@ -128,6 +141,7 @@ def process_image(img, func=0, mode=0):
return Image.fromarray(data_new, mode="RGB")
+
if __name__ == '__main__':
print(monte_carlo_methode(1000, FLAG_default))
print(monte_carlo_methode(1000, FLAG_threaded))