问题描述
我有漫画页面图片,例如 Link to image
我想从中提取所有带有边框的漫画作为单个图像。 我不打算手动做。我需要一些自动工具。
解决方法
我不知道任何工具,但是使用此脚本,您应该可以做到:
import cv2
import numpy as np
import imutils
img = "comic.jpg"
image = cv2.imread(img)
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
# blur
blurred = cv2.GaussianBlur(gray,(3,3),0)
# threshold it
(T,threshInv) = cv2.threshold(blurred,255,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
# find contours
cnts,cnts_hierarchy = cv2.findContours(threshInv.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
clone = image.copy()
cnts = sorted(cnts,key=cv2.contourArea,reverse=True) # order contours by area
for i,c in enumerate(cnts):
(x,y,w,h) = cv2.boundingRect(c)
area = cv2.contourArea(c)
extent = area / float(w * h)
crWidth = w / float(image.shape[1]) # width ratio of contour to image width
crHeight = h / float(image.shape[0]) # height ratio of contour to image height
# check if it's noise or a comic strip,change if necessary
if crWidth > 0.15 or crHeight > 0.15 or extent > 0.8:
# rotated bounding box
box = cv2.minAreaRect(c)
box = np.int0(cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)) # gives us a contour
warped = imutils.perspective.four_point_transform(clone,box.reshape(4,2))
cv2.imwrite(f'./image_{i}.png',warped)
else:
break