Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

Python - Remove Black Pixels originatin from the border of an image

I am very new to Image processing and I am trying to cleanse pictures similar to picture 1 of the Black Pixels originating from the border of the Image.

Clipped Image of a Character using PyMuPDF

The Images are clipped Characters from a PDF which I try to process with tesseract to retieve the character. I already searched in Stackoverflow for answers, but only found resolutions to get rid of black borders. I need to overwrite all the black pixels from the corners with white pixels, so tesseract can correctly recognize the character.

I cannot alter the Bounding Boxes used to clip the Characters, since the characters are centered in different ares of the BoundingBox and if i Cut the BoundingBox, i would cut some Characters like seen below

Clipped Image of Character with BoundingBox adjusted to fit before seen Image

My first guess would have been to recursively track down pixels with a certain threshhold of black in them, but I am scared of computing time in that case and wouldn't really know where and how to start, except for using two two-dimensional arrays, one with the pixels, and one with an indicator whether i already worked on that pixel or not.

Help would be greatly appreciated.

Edit: some more pictures of cases, where black pixels from the edge need to be cleared:

enter image description hereenter image description hereenter image description hereenter image description hereenter image description here

Edit: Code-Snippet to create Border Image:

    @staticmethod
    def __get_border_image(image: Image) -> Image:
        data = numpy.asarray(image)

        border = cv2.copyMakeBorder(data, top=5, bottom=5, left=5, right=5, borderType=cv2.BORDER_CONSTANT)

        return Image.fromarray(border)
question from:https://stackoverflow.com/questions/65885414/python-remove-black-pixels-originatin-from-the-border-of-an-image

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try like this:

  • artificially add a 1px wide black border all around the edge
  • flood-fill with white all black pixels starting at top-left corner
  • remove the 1px border from the first step (if necessary)

The point of adding the border is to allow the white to "flow" all around all edges of the image and reach any black items touching the edge.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...