Python中检测线条的倾斜度

Cla*_*lil -2 python django opencv image-recognition handwriting

我正在创建一个系统,该系统将能够注册手写文本的人,稍后我将不得不分析该图像并检测书写是升序、降序还是直写。通过笔迹学,我将能够创建该人的个人资料,但我不知道如何分析该图像。

使用Python和Django的系统,我只需要读取图像即可进行分析。有人对如何做有建议吗?

倾向的例子

Thi*_* B. 5

一个可能的解决方案是使用minAreaRect()为您提供角度的 。一旦你有了它,只需设置你的阈值来判断文字是否是['Ascending', 'Descending', 'Level']

write = cv2.imread('your_image.png', cv2.IMREAD_COLOR)
write_gray = cv2.cvtColor(write, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(write_gray, 150, 255, cv2.THRESH_BINARY_INV)
# dilate the write
elem = cv2.getStructuringElement(cv2.MORPH_RECT, (4, 4), (2, 2))
dilat = cv2.dilate(thresh, elem, iterations=1)
contours, hierarchy = cv2.findContours(dilat, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    if cv2.contourArea(cnt) > 1600: # only keep the writing text
        box = cv2.minAreaRect(cnt)
        (pos, size, angle) = box
        box = cv2.boxPoints(box)
        box = np.int0(box)
        cv2.drawContours(write, [box], 0, (0,255,0), 2)
        angle = angle if size[0] > size[1] else angle + 90
        pos = (cnt[cnt[:, :, 0].argmin()][0][0], cnt[cnt[:, :, 1].argmin()][0][1])
        #print(angle)
        if -2 <= angle <= 2:
            cv2.putText(write, 'Level', pos, cv2.FONT_HERSHEY_SIMPLEX, 0.7, (190, 123, 68), 2)
        elif angle < -2:
            cv2.putText(write, 'Ascending', pos, cv2.FONT_HERSHEY_SIMPLEX, 0.7, (190, 123, 68), 2)
        elif 2 < angle:
            cv2.putText(write, 'Descending', pos, cv2.FONT_HERSHEY_SIMPLEX, 0.7, (190, 123, 68), 2)

cv2.imshow('resultat', write)
Run Code Online (Sandbox Code Playgroud)

你会得到这样的东西: 结果