Hello everyone, I’m going to perform an expansion process on an image with black text on a white background and white text on a black background. However, if I use the traditional dilate function to expand it by 2 pixels, the white text on the black background will become very clear, but at the same time, the black text on the white background will become very thin, especially the relatively fine text will be severely damaged. I’m wondering if there’s any way to selectively expand this kind of image using Opencv, only processing the white text on the black blocks while keeping the black text on the white background unchanged. I’ve been searching online for a long time but haven’t found a suitable method. I hope experienced friends can give me some guidance. Thank you very much!
The following picture is a pattern used for testing
median filter, proportional to the maximum font size you can expect
that gives you an estimation of “background”, i.e. a mask.
you can either invert the picture wherever the mask is black, or wherever it’s white. then you have a picture with all the same background.
in that picture, you can apply your morphology operation.
if you need the black-background and white-background areas preserved instead of made the same, then apply the inversion step once more at this time.
you will see artefacts around where the background color changes. I have no idea how to handle the difference between “text” and “edge of a background change”. it’s both high-frequency content.
you might swap the median filter for something else that preserves morphology, such as connected components analysis followed by erasure of small features (assumed to be text). you could do that with findContours, or with two times connectedComponents (once for white, once for black). since your source is grayscale, not binarized, you’ll need to dilate each component mask slightly so it also captures the grayish edge pixels around any component.
a different approach might work better. that approach here is something I didn’t have to think about.
Thank you very much for your help. I have tried this method. This method can only process a fixed image. However, the graphics we come into contact with in daily life have various layout styles and forms. Therefore, the int ksize value of medianBlur is different. If it is not adjusted properly, some unnecessary contour traces will be left on the final result graphic. However, I need to only expand the white content on the black background and not change anything else. I must not add any content to the graphic. I need to open the picture and click the button to automatically process the image. I cannot look at the shape of the image and then gradually try to adjust the effect value by value. Thank you very much again.



