Я работаю над проектом, где загружаю папки на страницах манги/манхва. Некоторые изображения содержат несколько панелей сцены, составленных в одно большое изображение. Я хочу разделить эти панели на отдельные изображения, но мой текущий код не делает этого точно - иногда он пропускает панели или неправильно объединяет их. < /P>
Я использую OpenCV для обнаружения и извлечения панелей. Ниже приведена основная часть моей функции extract_panels, которая выполняет разделение: < /p>
img = cv2.imread(temp_path, cv2.IMREAD_COLOR)
if img is None:
with open(log_file, 'a') as f:
f.write(f"Failed to load preprocessed image {temp_path} at {datetime.now()}\n")
os.remove(temp_path) if os.path.exists(temp_path) else None
return
try:
# Get image dimensions
img_height, img_width = img.shape[:2]
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Blur to reduce noise
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# Edge detection with Canny
edges = cv2.Canny(blurred, 50, 150)
# Dilate to connect panel borders
kernel = np.ones((15, 15), np.uint8)
dilated = cv2.dilate(edges, kernel, iterations=1)
# Find contours
contours, _ = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if not contours:
with open(log_file, 'a') as f:
f.write(f"No panels detected in {image_path} at {datetime.now()}\n")
os.remove(temp_path) if os.path.exists(temp_path) else None
return
# Filter and collect panels
panels = []
min_area = img_width * img_height * 0.02 # At least 2% of image area
min_width = img_width * 0.1 # At least 10% of image width
min_height = img_height * 0.1 # At least 10% of image height
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
area = w * h
aspect_ratio = w / h if h > 0 else 0
if (area > min_area and
w >= min_width and
h >= min_height and
0.2 < aspect_ratio < 5.0):
panels.append((x, y, w, h))
# Sort panels top-to-bottom, left-to-right
panels.sort(key=lambda p: (p[1], p[0]))
# Merge or discard adjacent panels
merged_panels = []
i = 0
while i < len(panels):
x, y, w, h = panels
if i + 1 < len(panels):
next_x, next_y, next_w, next_h = panels[i + 1]
gap = next_y - (y + h)
if abs(x - next_x) < 50 and 0
Любые советы по улучшению обнаружения контура, фильтрации или логики слияния будут оценены. В идеале я хотел бы более последовательные и чистые сокращения между панелями, даже когда они тесно упакованы или художественно граничат.
Подробнее здесь: https://stackoverflow.com/questions/796 ... using-open
Как я могу точно разделить составные панели манги/манхва на изображении, используя OpenCV? ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Как выполнить программно нечеткий поиск в pubchem, используя составные имена
Anonymous » » в форуме Python - 0 Ответы
- 11 Просмотры
-
Последнее сообщение Anonymous
-