且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

OpenCV - 从图像中删除文本

更新时间:2023-12-05 15:31:52

阈值化以制作白色区域的蒙版,然后修复将适用于该图像的大多数情况.

Thresholding to make a mask of the whiter areas and then inpainting will work for most cases in this image.

img = cv2.imread('ultrasound.png')
mask = cv2.threshold(img, 210, 255, cv2.THRESH_BINARY)[1][:,:,0]
dst = cv2.inpaint(img, mask, 7, cv2.INPAINT_NS)

这是面具:

这是修复后的图像:

请注意,阈值蒙版并不准确,包括没有字母的较亮区域.但更重要的是,如果蒙版不包括需要去除的区域,例如中间十字的暗影,则尤其存在问题.这是该区域的放大图.

Notice the thresholding mask is not exact, and includes lighter regions where there are no letters. But more importantly, there is especially an issue if the mask does not include regions that need to be removed, such as the dark shadows of the crosses in the middle. Here's a zoom-in of that region.

遮罩只是白色区域,不覆盖暗区域.对于此类阈值不够的问题,可以手动调整掩码.在这里,我使用蒙版中的原始十字架并移动以覆盖阴影,并且修复效果要好得多.(同样,如果需要,可以手动去除不应该包含在掩码中的区域)

The mask is just of the white region, and doesn't cover the dark areas. For problems like this where thresholding will not be enough, the mask can be adjusted manually. Here I take the original crosses in the mask and shift to also cover the shadows, and the inpainting is much better. (Similarly, if needed, the areas that shouldn't be included in the mask can be manually removed)

crosses = mask[235:267,290:320] | mask[233:265,288:318]
mask[235:267,290:318] = crosses
dst = cv2.inpaint(img, mask, 7, cv2.INPAINT_NS)