我正在尝试使用 Python 构建一个字符识别程序。我坚持对轮廓进行排序。我正在使用此页面作为参考。
我设法使用以下代码找到轮廓:
mo_image = di_image.copy()
contour0 = cv2.findContours(mo_image.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
contours = [cv2.approxPolyDP(cnt,3,True) for cnt in contour0[0]]
Run Code Online (Sandbox Code Playgroud)
并使用这部分代码添加了边界矩形并分割图像:
maxArea = 0
rect=[]
for ctr in contours:
maxArea = max(maxArea,cv2.contourArea(ctr))
if img == "Food.jpg":
areaRatio = 0.05
elif img == "Plate.jpg":
areaRatio = 0.5
for ctr in contours:
if cv2.contourArea(ctr) > maxArea * areaRatio:
rect.append(cv2.boundingRect(cv2.approxPolyDP(ctr,1,True)))
symbols=[]
for i in rect:
x = i[0]
y = i[1]
w = i[2]
h = i[3]
p1 = (x,y)
p2 = (x+w,y+h)
cv2.rectangle(mo_image,p1,p2,255,2) …Run Code Online (Sandbox Code Playgroud) 我制作了一个星巴克标识探测器,但是当我画出应该包围徽标的折线时,我会得到这些奇怪的人工制品.
这是一个正确的结果:
以下是人工制品的一些例子:
我正在使用SIFT来检测关键点并绘制矩形,如OpenCV教程中所示,如下所示:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
sift = cv2.xfeatures2d.SIFT_create()
img1 = cv2.imread('logo.png', 0)
img1.resize(512, 512)
kp1, des1 = sift.detectAndCompute(img1, None)
while (True):
ret, frame = cap.read()
frame = findLogo(frame, kp1=kp1, des1=des1)
cv2.imshow("frame",frame)
if cv2.waitKey(1) & 0xFF == ord(' '):
break
cap.release()
out.release()
cv2.destroyAllWindows()
def findLogo(frame, kp1, des1):
MIN_MATCH_COUNT = 10
# Initiate SIFT detector
sift = cv2.xfeatures2d.SIFT_create()
img2 = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# find the keypoints and descriptors with SIFT
kp2, des2 …Run Code Online (Sandbox Code Playgroud)