根据暗中心线使用python水平分割图像 [英] Split image horizontally with python based on dark center line
问题描述
目标是基于比周围其他区域更暗的垂直中心线,在python中水平分割图像(双报纸页面).
Goal is to horizontally split an image (double newspaper page) in python based on a vertical centerline that is darker than other areas around.
示例图片:
使用opencv(cv2
)进行初始裁剪时有些运气,并使用cv2.Canny
从黑色背景旋转双页,然后根据cv2.contourArea
对轮廓进行排序.
Had some luck using opencv (cv2
) for the initial crop and rotation of the double page from a black background using cv2.Canny
, and then sorting the contours based on cv2.contourArea
.
但是,现在我只是想找到一条中心线,然后将图像分成两个单独的图像.再次使用cv2.Canny
,我看到它可以识别该中心线,但是不确定如何识别该长的垂直线并将其用于分割图像:
But, now I'm just interested in finding a center line and then splitting the image into two separate images. Using cv2.Canny
again I see that it's able to identify that centerline, but not sure how to identify that long, vertical line and use that to split the image:
最终目标将是两个图像,如下所示:
End goal would be two images like the following:
任何建议都将受到欢迎.
Any suggestions would be most welcome.
推荐答案
首先,运行水平渐变,以便仅突出垂直边缘.您可以使用以下系数计算水平梯度:
First, run a horizontal gradient so you only accentuate vertical edges. You can calculate a horizontal gradient with these coefficients:
-1 0 1
-2 0 2
-1 0 1
然后计算垂直列的总和,您可以使用np.sum(array,axis=0)
,您将获得以下信息:
Then compute the sum of the vertical columns, you can use np.sum(array,axis=0)
and you will get this:
为了便于查看,我对其进行了重新整形-实际上它只有1像素高.希望您可以在中间看到Numpy argmax()
处的亮白线.当您只进行水平渐变时,效果也会更好,因为此刻我正在使用紫色和黄色图像,并增强了垂直和水平边缘.
I have re-shaped it for ease of viewing - it is actually only 1 pixel tall. Hopefully you can see the bright white line in the middle which you can find with Numpy argmax()
. It will also be better when you just do a horizontal gradient because at the moment I am using the purple and yellow image with vertical and horizontal edges enhanced.
请注意,此方法的灵感在于您说"要识别出较长的垂直中心线" ,其理由是一长行白色像素将加起来很大.请注意,我已经假设您的图像是歪斜的(因为您说这条线是垂直的),并且此方法在"vertical" 线会分布在几列的歪斜图像上可能效果不佳.
Note that the inspiration for this approach is that you said you "want to identify that long, vertical centerline" and the rationale is that a long line of white pixels will add up to a large sum. Note that I have assumed your image is de-skewed (since you said the line is vertical) and this method may not work so well on skew images where "vertical" line will be spread across several columns.
这篇关于根据暗中心线使用python水平分割图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!