jkushwaha commited on
Commit
5404db8
·
verified ·
1 Parent(s): 36271e5

Update Final_Black_White_Patch_inversion.py

Browse files
Final_Black_White_Patch_inversion.py CHANGED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+
4
+ def scan_image(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((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
+
16
+ # Count black pixels in the result
17
+ total_pixel_count = result.size
18
+ black_pixels_count = total_pixel_count - cv2.countNonZero(result)
19
+
20
+ # Check if 40% pixels are black
21
+ if black_pixels_count >= total_pixel_count*0.4:
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
+ # Example usage
32
+ image = cv2.imread('fin.png', cv2.IMREAD_GRAYSCALE)
33
+ image_original = cv2.imread('fin.png', cv2.IMREAD_GRAYSCALE)
34
+ _, black_white_image = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
35
+ filter_size = 100
36
+ filter = np.zeros((filter_size, black_white_image.shape[1]), dtype=np.uint8)
37
+ inverted_image = scan_image(image_original, black_white_image, filter)