gpt4 book ai didi

python - 如何从图像中删除文本图章?

转载 作者:行者123 更新时间:2023-12-02 17:06:43 25 4
gpt4 key购买 nike

我有这样的图片:

enter image description here

它具有随机分布在整个图像文件中的文本戳记。关于图像要记住的一些方面是:

  • 戳记中的文本始终相同。
  • 不透明。
  • 文本字体为黑色,因此与原始文本相比有一些显着差异。

  • 所以我的问题是;
  • 如何找到此文本图章?我猜,也许模板与容差匹配可能会有所帮助?
  • 尽管即使我找到了文本的确切位置,也如何摆脱它?我可以尝试找出随机背景,并做如下所述的事情。
  • 获取文本图章轮廓的边界框。
  • 然后取轮廓外的所有像素。
  • 从上一步中删除轮廓并用随机像素填充并添加一些模糊效果可以达到我期望的效果。
  • 最佳答案

    以下代码从图像中删除图章:

    inp_img = cv2.imread('stamp.jpg',cv2.IMREAD_GRAYSCALE)
    th,inp_img_thresh = cv2.threshold(255-inp_img,220,255,cv2.THRESH_BINARY)
    dilate = cv2.dilate(inp_img_thresh,np.ones((5,5),np.uint8))
    canny = cv2.Canny(dilate,0,255)
    _,contours,_ =
    cv2.findContours(canny,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    test_img = inp_img.copy()
    for c in contours:
    (x, y, w, h) = cv2.boundingRect(c)
    #print(x,y,w,h,test_img[y+h//2,x-w])
    test_img[y+3:y-2+h,x+3:x+w] = 240 #test_img[y+h//2,x-w]

    cv2.imwrite("stamp_removed.jpg",test_img)
    cv2.imshow("input image",inp_img)
    cv2.imshow("threshold",inp_img_thresh)
    cv2.imshow("output image",test_img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    Output Image With The Stamp Removed

    关于python - 如何从图像中删除文本图章?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53023658/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com