检测XKCD漫画面板的角点

2024-04-29 00:55:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试检测xkcd漫画的面板,以便将它们剪掉。我想检索随机的XKCD漫画(已实现),并可靠地知道面板的角在哪里。我的理想输出如下: ![enter image description here

其中我有一些或所有的角落面板。 我有一个包含整个漫画的数组,到目前为止,我的方法是找到大轮廓,如下所示:

    contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    conts=[]
    if len(contours) > 1:
        conts = sorted(contours, key=cv2.contourArea, reverse=True)[0]

然后使用cv2.APROXPOLYDP检测角点。然而,我的轮廓似乎还不够,因为我通常不会得到好的角,而且当我画轮廓时,它们看起来也不太好

良好的运行:

Good run (一些点落在角落上)

糟糕的一个:

bad run (女孩身上布满了圆点)

我想知道我的轮廓是否做错了什么,比如可能没有选择正确的模式(我选择了外部的RETR_,因为漫画面板是相当外部的)

漫画通常是灰度的,但有时不是,所以我将转换为灰度,并在轮廓检测之前使用二进制阈值

其他一些想法:

  1. 边缘检测是否更适合此任务

  2. 我还注意到我的数组在图片的边缘有边框,所以填充会有帮助吗

  3. 漫画有点变化无常,所以暴力手段会更好吗


Tags: 方法面板数组cv2xkcd边缘灰度漫画
1条回答
网友
1楼 · 发布于 2024-04-29 00:55:20

我的直觉是轮廓区域会告诉你路径有多复杂。 我会使用一个更简单的度量,比如轮廓的高度^{}

#!/usr/bin/env python
import cv2
import numpy as np

filename = 'boyfriend.png'
img = cv2.imread(filename)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

ret, dst = cv2.threshold(gray, 127 ,255,0)
# erode and negate to amplify edges
dst = cv2.erode(dst,None,iterations=2)
dst = (255-dst)

cv2.imshow('thresh', dst)

contours, hierarchy = cv2.findContours(dst, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

if len(contours) > 1:
    # print bounding box height per contour for debugging purposes
    print([cv2.boundingRect(cnt)[3] for cnt in contours])
    # filter contours where the bounding box height matches the image height
    conts = [cnt for cnt in contours if cv2.boundingRect(cnt)[3] == img.shape[0]]
    # preview
    img = cv2.drawContours(img, conts, -1, (0,255,0), 3)

cv2.imshow('img',img)
cv2.waitKey(0)

注意3轮廓边界框高度突出:

[127, 16, 16, 16, 16, 35, 15, 36, 16, 16, 16, 18, 17, 17, 220, 220, 220]

您可能希望选择性地将条件更改为阈值,而不是精确值。 e、 g

# if the contour bounding box height is > 3/4 of the image height
cv2.boundingRect(cnt)[3] > img.shape[0] * 0.75

上面的完整代码生成: xkcd panel detection

请注意,我使用了morphological filter来放大边。 它适用于具有这些迭代的图像,因为过滤器扩展框的轮廓足够大,但不会太多,以至于它们与文本/字符合并。 这可能是其他图像需要调整的地方

更新快速搜索后,我发现了一些潜在的有趣资源:

Kumiko

Kumiko, the Comics Cutter is a set of tools to compute useful information about comic book pages, panels, and more. Its main strength is to find out the locations of panels within a comic's page (image file). Kumiko can also compile information about panels for all pages in a comic book, and present it as one piece of data (JSON-formatted object). kumiko xkcd panel detection

Segmentation and indexation of complex objects in comic book images(与您的要求不同,但对下一步可能有用)

相关问题 更多 >