美文网首页OpenCV 基础入门数字图像处理入门
[计算机视觉基础] OpenCV_4 Draw and Put

[计算机视觉基础] OpenCV_4 Draw and Put

作者: 砥砺前行的人 | 来源:发表于2021-09-23 20:42 被阅读0次

    针对图片的处理中,有很大一部分需要要对图片中识别出的物体进行画圈或者画框,或者在物体旁表上文字用以表明物体的类型,这些就需要用到本文需要的一些基本的接口。本文会介绍关于绘制矩形,圆形,线,附加文字等。

    代码很简单:

    import cv2 as cv
    import numpy as np
    
    # 黑色幕布
    blank = np.zeros((500, 500, 3), dtype='uint8')
    
    # 绘制方形
    cv.rectangle(blank, (0, 0), (250, 250), (0, 255, 0), 5)
    
    # 绘制圆形
    cv.circle(blank, (250, 250), 50, (255, 0, 0), 5)
    
    # 绘制
    cv.line(blank, (0, 0), (250, 250), (0, 0, 255), 5)
    
    cv.putText(blank, "OpenCV", (0, 300), cv.FONT_HERSHEY_TRIPLEX, 1, (0, 255, 0))
    cv.imshow("result", blank)
    
    cv.waitKey(0)
    
    output.png
    cv.rectangle(img, pt1, pt2, color, thickness=None, lineType=None, shift=None)
    在给定的img上以左上角坐标pt1到右下角坐标pt2绘制矩形,color 为 RGB 的元组,thickness 为线的厚度,lineType 为线的类型。

    cv.circle(img, center, radius, color, thickness=None, lineType=None, shift=None)
    在给定的img上以半径为radius,圆心坐标为 center 绘制圆形,其他类似

    cv.line(img, pt1, pt2, color, thickness=None, lineType=None, shift=None)
    在给定的img上从pt1pt2 绘制直线,其他类似

    cv.putText(img, text, org, fontFace, fontScale, color, thickness=None, lineType=None, bottomLeftOrigin=None)
    在给定的img 上添加文本,org为文本框左下角的坐标,fontFace 为字体类型,fontScale 为字体缩放,其他类似,bottomLeftOrigin 决定坐标是否以左下角开始,如果 False 则为左上角。

    OCR 识别文字

    import cv2 as cv
    import pytesseract
    from pytesseract import Output
    
    frame = cv.imread("./ocr_need.png")
    
    data = pytesseract.image_to_data(frame, output_type=Output.DICT, lang='chi_sim')
    
    left = data["left"]
    top = data["top"]
    width = data["width"]
    height = data["height"]
    text = data["height"]
    word_num = data["word_num"]
    
    start_word = False
    pre_word_num = 1
    
    for index, tex in enumerate(text):
        if not tex or word_num[index] == 0:
            continue
        i = index
        if word_num[i] == 1:
            cv.rectangle(frame, (left[index], top[index]), (left[index] + width[index], top[index] + height[index]), (255, 0, 0), 1)
    
    cv.imshow("", frame)
    cv.waitKey(0)
    
    

    利用 pytesseract 模块实现对于视频设备采集到的视频流做文字识别并且将内容使用矩形框出来,效果如下:


    image.png

    相关文章

      网友评论

        本文标题:[计算机视觉基础] OpenCV_4 Draw and Put

        本文链接:https://www.haomeiwen.com/subject/uvpogltx.html