Update Final_Black_White_Patch_inversion.py
Browse files
Final_Black_White_Patch_inversion.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
|
| 4 |
-
def
|
| 5 |
W, H = image.shape[:2]
|
| 6 |
filter_size = filter_patch.shape
|
| 7 |
t = filter_size[0]
|
|
@@ -9,7 +9,7 @@ def scan_image(image_original, image, filter_patch):
|
|
| 9 |
for y in range((H-t)//t + 1):
|
| 10 |
roi = image[y*t:(y*t)+t, :]
|
| 11 |
if (roi.shape[0]<filter_patch.shape[0]):
|
| 12 |
-
print(">>>>")
|
| 13 |
break
|
| 14 |
result = cv2.bitwise_or(roi, filter_patch)
|
| 15 |
|
|
@@ -22,16 +22,15 @@ def scan_image(image_original, image, filter_patch):
|
|
| 22 |
cnt+=1
|
| 23 |
# new_roi = cv2.bitwise_not(roi)
|
| 24 |
new_roi = 255-roi
|
| 25 |
-
cv2.imwrite(f"black_patches/{cnt}.png", roi)
|
| 26 |
image_original[y*t:(y*t)+t, :] = new_roi
|
| 27 |
|
| 28 |
return image_original
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
|
| 4 |
+
def scan_image_inversion(image_original, image, filter_patch):
|
| 5 |
W, H = image.shape[:2]
|
| 6 |
filter_size = filter_patch.shape
|
| 7 |
t = filter_size[0]
|
|
|
|
| 9 |
for y in range((H-t)//t + 1):
|
| 10 |
roi = image[y*t:(y*t)+t, :]
|
| 11 |
if (roi.shape[0]<filter_patch.shape[0]):
|
| 12 |
+
print(f">>>>{roi.shape[0]<filter_patch.shape[0]}")
|
| 13 |
break
|
| 14 |
result = cv2.bitwise_or(roi, filter_patch)
|
| 15 |
|
|
|
|
| 22 |
cnt+=1
|
| 23 |
# new_roi = cv2.bitwise_not(roi)
|
| 24 |
new_roi = 255-roi
|
|
|
|
| 25 |
image_original[y*t:(y*t)+t, :] = new_roi
|
| 26 |
|
| 27 |
return image_original
|
| 28 |
|
| 29 |
+
def black_patch_inversion(input_image_path, out_image_path):
|
| 30 |
+
image = cv2.imread(input_image_path, cv2.IMREAD_GRAYSCALE)
|
| 31 |
+
image_original = cv2.imread(input_image_path, cv2.IMREAD_GRAYSCALE)
|
| 32 |
+
_, black_white_image = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
|
| 33 |
+
filter_size = 5
|
| 34 |
+
filter_patch = np.zeros((filter_size, black_white_image.shape[1]), dtype=np.uint8)
|
| 35 |
+
inverted_image = scan_image_inversion(image_original, black_white_image, filter_patch)
|
| 36 |
+
cv2.imwrite(out_image_path, inverted_image)
|