Update Final_Black_White_Patch_inversion.py
Browse files
Final_Black_White_Patch_inversion.py
CHANGED
|
@@ -1,28 +1,42 @@
|
|
| 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]
|
| 8 |
cnt = 0
|
| 9 |
-
for y in range((
|
| 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 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# Check if 40% pixels are black
|
| 21 |
-
if
|
| 22 |
-
cnt+=1
|
| 23 |
# new_roi = cv2.bitwise_not(roi)
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
return image_original
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def scan_image_inversion(image_original, image, filter_patch):
|
| 2 |
+
inversion_threshold = 0.4
|
| 3 |
W, H = image.shape[:2]
|
| 4 |
filter_size = filter_patch.shape
|
| 5 |
t = filter_size[0]
|
| 6 |
cnt = 0
|
| 7 |
+
for y in range((W-t)//t + 1):
|
| 8 |
roi = image[y*t:(y*t)+t, :]
|
| 9 |
if (roi.shape[0]<filter_patch.shape[0]):
|
|
|
|
| 10 |
break
|
| 11 |
+
# print(f"roi shape:{roi.shape}, filter_patch shape: {filter_patch.shape}")
|
| 12 |
+
width = roi.shape[1]
|
| 13 |
+
mid = int(width/2) if width%2==0 else int((width/2)+1)
|
| 14 |
+
roi_left = roi[:, :mid]
|
| 15 |
+
roi_right = roi[:, mid:]
|
| 16 |
+
result_left = cv2.bitwise_or(roi_left, filter_patch[:, :mid])
|
| 17 |
+
result_right = cv2.bitwise_or(roi_right, filter_patch[:, mid:])
|
| 18 |
+
cnt+=1
|
| 19 |
+
# Count black pixels in the result_left
|
| 20 |
+
total_pixel_count_left = result_left.size
|
| 21 |
+
black_pixels_count_left = total_pixel_count_left - cv2.countNonZero(result_left)
|
| 22 |
+
# Count black pixels in the result_right
|
| 23 |
+
total_pixel_count_right = result_right.size
|
| 24 |
+
black_pixels_count_right = total_pixel_count_right - cv2.countNonZero(result_right)
|
| 25 |
|
| 26 |
# Check if 40% pixels are black
|
| 27 |
+
if black_pixels_count_left >= total_pixel_count_left*inversion_threshold:
|
|
|
|
| 28 |
# new_roi = cv2.bitwise_not(roi)
|
| 29 |
+
new_roi_left = 255-roi_left
|
| 30 |
+
else:
|
| 31 |
+
new_roi_left = roi_left
|
| 32 |
+
|
| 33 |
+
if black_pixels_count_right >= total_pixel_count_right*inversion_threshold:
|
| 34 |
+
new_roi_right = 255-roi_right
|
| 35 |
+
else:
|
| 36 |
+
new_roi_right = roi_right
|
| 37 |
+
|
| 38 |
+
new_roi = np.concatenate((new_roi_left, new_roi_right), axis=1)
|
| 39 |
+
image_original[y*t:(y*t)+t, :] = new_roi
|
| 40 |
|
| 41 |
return image_original
|
| 42 |
|