问题
Goal is to horizontally split an image (double newspaper page) in python based on a vertical centerline that is darker than other areas around.
Example image:
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
.
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.
回答1:
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
Then compute the sum of the vertical columns, you can use np.sum(array,axis=0)
and you will get this:
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.
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.
来源:https://stackoverflow.com/questions/54443968/split-image-horizontally-with-python-based-on-dark-center-line