【问题标题】:find rectangle in image and extract text inside of it to save it as new image在图像中找到矩形并提取其中的文本以将其保存为新图像
【发布时间】:2018-10-13 21:43:02
【问题描述】:

我是 OpenCV 的新手,所以我真的需要你的帮助。我有一堆这样的图片:

我需要检测图像上的矩形,从中提取文本部分并将其保存为新图像。

你能帮我解决这个问题吗?

谢谢!

【问题讨论】:

    标签: python opencv image-processing opencv3.0


    【解决方案1】:

    只是为了添加到 Danyals 的答案中,我添加了一个示例代码,其中包含用 cmets 编写的步骤。对于此图像,您甚至不需要对图像执行形态学打开。但通常对于图像中的这种噪点,建议使用它。干杯!

    import cv2
    import numpy as np
    
    # Read the image and create a blank mask
    img = cv2.imread('napis.jpg')
    h,w = img.shape[:2]
    mask = np.zeros((h,w), np.uint8)
    
    # Transform to gray colorspace and invert Otsu threshold the image
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
    
    # ***OPTIONAL FOR THIS IMAGE
    
    ### Perform opening (erosion followed by dilation)
    #kernel = np.ones((2,2),np.uint8)
    #opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
    
    # ***
    
    # Search for contours, select the biggest and draw it on the mask
    _, contours, hierarchy = cv2.findContours(thresh, # if you use opening then change "thresh" to "opening"
                                              cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
    cnt = max(contours, key=cv2.contourArea)
    cv2.drawContours(mask, [cnt], 0, 255, -1)
    
    # Perform a bitwise operation
    res = cv2.bitwise_and(img, img, mask=mask)
    
    ########### The result is a ROI with some noise
    ########### Clearing the noise
    
    # Create a new mask
    mask = np.zeros((h,w), np.uint8)
    
    # Transform the resulting image to gray colorspace and Otsu threshold the image 
    gray = cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    
    # Search for contours and select the biggest one again
    _, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
    cnt = max(contours, key=cv2.contourArea)
    
    # Draw it on the new mask and perform a bitwise operation again
    cv2.drawContours(mask, [cnt], 0, 255, -1)
    res = cv2.bitwise_and(img, img, mask=mask)
    
    # If you will use pytesseract it is wise to make an aditional white border
    # so that the letters arent on the borders
    x,y,w,h = cv2.boundingRect(cnt)
    cv2.rectangle(res,(x,y),(x+w,y+h),(255,255,255),1)
    
    # Crop the result
    final_image = res[y:y+h+1, x:x+w+1]
    
    # Display the result
    cv2.imshow('img', final_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    结果:

    【讨论】:

    • 你是救生员!非常感谢!
    【解决方案2】:

    一种方法(如果矩形大小可以预测的话)是:

    1. 将图像转换为黑白
    2. 反转图像
    3. 使用水平线/矩形对 (2) 中的图像执行形态学打开(我尝试使用 2x30)。
    4. 使用垂直线对 (2) 中的图像执行形态学打开(我尝试使用 15x2)。
    5. 从 (3) 和 (4) 添加图像。你现在应该只有一个白色的矩形。现在可以删除原始图像中所有对应的行和列,这些行和列在该图像中完全为零。

    【讨论】:

      猜你喜欢
      • 2012-12-02
      • 2017-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      相关资源
      最近更新 更多