用python识别高亮区域的文字
时间: 2023-02-12 21:25:40 浏览: 66
可以使用 OpenCV 库的 "template matching" 功能来识别高亮区域的文字。首先需要将高亮区域作为模板,然后在整个图像中进行匹配,最后找到匹配度最高的位置即为高亮文字的位置。代码如下:
```
import cv2
import numpy as np
# Load the image
img = cv2.imread("image.jpg")
# Convert the image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Load the template
template = cv2.imread("template.jpg", 0)
# Perform template matching
res = cv2.matchTemplate(gray, template, cv2.TM_CCOEFF_NORMED)
# Get the location of the highest match
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
# Draw a rectangle around the matched region
h, w = template.shape
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(img, top_left, bottom_right, (0, 0, 255), 2)
# Display the image
cv2.imshow("Result", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```