jkushwaha commited on
Commit
b66b75a
·
verified ·
1 Parent(s): 85d3215

Update Final_Black_White_Patch_inversion.py

Browse files
Files changed (1) hide show
  1. Final_Black_White_Patch_inversion.py +10 -11
Final_Black_White_Patch_inversion.py CHANGED
@@ -1,7 +1,7 @@
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]
@@ -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
- # 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)
 
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)