jebin2 commited on
Commit
76634d5
Β·
1 Parent(s): 750d5b3

added support for black/white

Browse files
comic_panel_extractor/image_processor.py CHANGED
@@ -35,10 +35,13 @@ class ImageProcessor:
35
  # Convert to grayscale and binary
36
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
37
  _, binary = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV)
 
38
 
39
- # Dilate to strengthen borders
40
- kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
41
- dilated = cv2.dilate(binary, kernel, iterations=2)
 
 
42
 
43
  # Save intermediate results
44
  gray_path = f'{self.config.output_folder}/2_gray.jpg'
@@ -51,6 +54,27 @@ class ImageProcessor:
51
 
52
  return str(gray_path), str(binary_path), str(dilated_path)
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  def thin_image_borders(self, processed_image_path: str, output_filename: str = "5_thin_border.jpg") -> str:
55
  """
56
  Clean dilated image by thinning thick borders and removing hanging clusters.
 
35
  # Convert to grayscale and binary
36
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
37
  _, binary = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV)
38
+ binary, is_inverted = self.invert_if_black_dominates(binary)
39
 
40
+ if not is_inverted:
41
+ # Dilate to strengthen borders
42
+ kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
43
+ dilated = cv2.dilate(binary, kernel, iterations=2)
44
+ else: dilated = binary
45
 
46
  # Save intermediate results
47
  gray_path = f'{self.config.output_folder}/2_gray.jpg'
 
54
 
55
  return str(gray_path), str(binary_path), str(dilated_path)
56
 
57
+ def invert_if_black_dominates(self, binary):
58
+ # Threshold to binary image
59
+ _, binary = cv2.threshold(binary, 127, 255, cv2.THRESH_BINARY)
60
+
61
+ # Count black and white pixels
62
+ black_pixels = np.sum(binary == 0)
63
+ white_pixels = np.sum(binary == 255)
64
+
65
+ print(f"Black pixels: {black_pixels}, White pixels: {white_pixels}")
66
+
67
+ # If black dominates, invert
68
+ if black_pixels > white_pixels:
69
+ print("πŸ”„ Inverting image because black > white")
70
+ inverted = cv2.bitwise_not(binary)
71
+ else:
72
+ print("βœ… No inversion needed, white >= black")
73
+ inverted = binary
74
+
75
+ # Save result
76
+ return inverted, black_pixels > white_pixels
77
+
78
  def thin_image_borders(self, processed_image_path: str, output_filename: str = "5_thin_border.jpg") -> str:
79
  """
80
  Clean dilated image by thinning thick borders and removing hanging clusters.