且构网

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

如何使用Python和OpenCV进行图像分割

更新时间:2022-06-16 23:29:55

关键是扩大(扩展)字母的轮廓以形成块.方法如下:

The key is to dilate (expand) the contours of the letters to form chunks. Here is how:

import cv2
import numpy as np

def process(img):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img_canny = cv2.Canny(img_gray, 0, 0)
    return cv2.dilate(img_canny, np.ones((5, 5)), iterations=20)

def draw_segments(img):
    contours, hierarchies = cv2.findContours(process(img), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    for cnt in contours:
        x, y, w, h = cv2.boundingRect(cnt)
        if w * h > 70000:
            cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 5)

img = cv2.imread("document.jpg")
draw_segments(img)
cv2.imshow("Image", img)
cv2.waitKey(0)

输出:

说明:

  1. 导入必要的库:

import cv2
import numpy as np

  1. 定义用于处理图像的函数,有关详细信息,请参见代码中的注释:

def process(img):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Convert to grayscale
    img_canny = cv2.Canny(img_gray, 0, 0) # Detect edges with canny edge detector
    return cv2.dilate(img_canny, np.ones((5, 5)), iterations=20) # Dilate edges to convert scattered contours that are close to each others into chunks

  1. 定义一个将接收图像的函数,并利用之前定义的 process 函数处理图像并找到其轮廓.然后它将遍历每个轮廓,如果轮廓的边界矩形的面积大于例如70000 (以消除停留文本),请在图像上绘制边界矩形:
  1. Define a function that will take in an image, and utilize the process function defined earlier to process the image, and find its contours. It will then loop through each contour, and if the contour's bounding rectangle has an area greater than, for example, 70000 (to eliminate the stay text), draw the bounding rectangle on the image:

def draw_segments(img):
    contours, hierarchies = cv2.findContours(process(img), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    for cnt in contours:
        x, y, w, h = cv2.boundingRect(cnt)
        if w * h > 70000:
            cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 5)

  1. 最后,读入图像,调用 draw_segments 函数并显示图像:
  1. Finally, read in the image, call the draw_segments function and display the image:

img = cv2.imread("document.jpg")
draw_segments(img)
cv2.imshow("Image", img)
cv2.waitKey(0)