Spaces:
Running
Running
add wdth thres
Browse files
comic_panel_extractor/image_processor.py
CHANGED
|
@@ -552,41 +552,54 @@ class ImageProcessor:
|
|
| 552 |
cv2.imwrite(output_path, result)
|
| 553 |
return output_path
|
| 554 |
|
| 555 |
-
def remove_small_continuity_components(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 556 |
output_folder = output_folder or self.config.output_folder
|
|
|
|
| 557 |
# Load the image in grayscale
|
| 558 |
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
|
| 559 |
if img is None:
|
| 560 |
raise ValueError("Unable to load the image. Check the file path.")
|
| 561 |
|
| 562 |
height, width = img.shape
|
| 563 |
-
|
|
|
|
|
|
|
| 564 |
# Threshold to binary (invert if lines are black on white background)
|
| 565 |
_, binary = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY_INV)
|
| 566 |
-
|
| 567 |
# Perform connected component labeling (8-connectivity)
|
| 568 |
num_labels, labels, stats, _ = cv2.connectedComponentsWithStats(binary, connectivity=8)
|
| 569 |
-
|
| 570 |
-
# Create
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
for label in tqdm(range(1, num_labels), desc="Processing labels"):
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
# Invert back to original style
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
# Save the
|
| 586 |
output_path = self.get_output_path(output_folder, file_name)
|
| 587 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 588 |
return output_path
|
| 589 |
|
|
|
|
| 590 |
def connect_horizontal_vertical_gaps(self, image_path, file_name='connected_output.jpg', output_folder=None):
|
| 591 |
output_folder = output_folder or self.config.output_folder
|
| 592 |
|
|
|
|
| 552 |
cv2.imwrite(output_path, result)
|
| 553 |
return output_path
|
| 554 |
|
| 555 |
+
def remove_small_continuity_components(
|
| 556 |
+
self,
|
| 557 |
+
image_path,
|
| 558 |
+
file_name="remove_small_continuity_components.jpg",
|
| 559 |
+
output_folder=None,
|
| 560 |
+
):
|
| 561 |
output_folder = output_folder or self.config.output_folder
|
| 562 |
+
|
| 563 |
# Load the image in grayscale
|
| 564 |
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
|
| 565 |
if img is None:
|
| 566 |
raise ValueError("Unable to load the image. Check the file path.")
|
| 567 |
|
| 568 |
height, width = img.shape
|
| 569 |
+
min_height = height * self.config.min_height_ratio
|
| 570 |
+
min_width = width * self.config.min_width_ratio
|
| 571 |
+
|
| 572 |
# Threshold to binary (invert if lines are black on white background)
|
| 573 |
_, binary = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY_INV)
|
| 574 |
+
|
| 575 |
# Perform connected component labeling (8-connectivity)
|
| 576 |
num_labels, labels, stats, _ = cv2.connectedComponentsWithStats(binary, connectivity=8)
|
| 577 |
+
|
| 578 |
+
# Create output copies
|
| 579 |
+
cleaned_output = binary.copy()
|
| 580 |
+
debug_output = cv2.cvtColor(binary.copy(), cv2.COLOR_GRAY2BGR) # For visualizing removed components
|
| 581 |
+
|
| 582 |
for label in tqdm(range(1, num_labels), desc="Processing labels"):
|
| 583 |
+
x, y, w, h, area = stats[label]
|
| 584 |
+
|
| 585 |
+
# Filter out small components based on width and height
|
| 586 |
+
if h < min_height and w < min_width:
|
| 587 |
+
cleaned_output[labels == label] = 0
|
| 588 |
+
debug_output[labels == label] = [0, 0, 255] # Mark removed components in red
|
| 589 |
+
|
| 590 |
+
# Invert back to original style
|
| 591 |
+
final_result = cv2.bitwise_not(cleaned_output)
|
| 592 |
+
|
| 593 |
+
# Save the final and debug outputs
|
| 594 |
output_path = self.get_output_path(output_folder, file_name)
|
| 595 |
+
debug_path = self.get_output_path(output_folder, file_name.replace(".jpg", "_debug.jpg"))
|
| 596 |
+
|
| 597 |
+
cv2.imwrite(output_path, final_result)
|
| 598 |
+
cv2.imwrite(debug_path, debug_output)
|
| 599 |
+
|
| 600 |
return output_path
|
| 601 |
|
| 602 |
+
|
| 603 |
def connect_horizontal_vertical_gaps(self, image_path, file_name='connected_output.jpg', output_folder=None):
|
| 604 |
output_folder = output_folder or self.config.output_folder
|
| 605 |
|