识别图片文本坐标;透视变换;输入点集坐标,输出凸包(最大多边形)顶点坐标

'''
    识别图片文本坐标
'''
import cv2
import pytesseract
from pytesseract import Output
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import numpy as np


def recoText(im):
    """
    识别字符并返回所识别的字符及它们的坐标
    :param im: 需要识别的图片
    :return data: 字符及它们在图片的位置
    """
    data = {}
    d = pytesseract.image_to_data(im, output_type=Output.DICT, lang='chi_sim')
    for i in range(len(d['text'])):
        if 0 < len(d['text'][i]):
            (x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])
            data[d['text'][i]] = ([d['left'][i], d['top'][i], d['width'][i], d['height'][i]])

            cv2.rectangle(im, (x, y), (x + w, y + h), (255, 0, 0), 1)
            # 使用cv2.putText不能显示中文,需要使用下面的代码代替
            cv2.putText(im, d['text'][i], (x, y-8), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (255, 0, 0), 1)

            pilimg = Image.fromarray(im)
            draw = ImageDraw.Draw(pilimg)
            # 参数1:字体文件路径,参数2:字体大小
            font = ImageFont.truetype("simhei.ttf", 15, encoding="utf-8")
            # 参数1:打印坐标,参数2:文本,参数3:字体颜色,参数4:字体
            draw.text((x, y - 10), d['text'][i], (255, 0, 0), font=font)
            im = cv2.cvtColor(np.array(pilimg), cv2.COLOR_RGB2BGR)

    cv2.imshow("recoText", im)
    return data


if __name__ == '__main__':
    img = cv2.imread('D:\python course/tuxiangshibie\data/3.jpg')
    # cv2.imshow("src", img)
    data = recoText(img)
    print(data)

    cv2.waitKey(0)
    cv2.destroyAllWindows()

计算透视投影矩阵:

import cv2
import numpy as np

pts = np.float32([[183,235],[643,465],[154,678],[477,675]])
pts1 = np.float32([[193,245],[653,475],[164,688],[487,685]])
M = cv2.getPerspectiveTransform(pts,pts1)
print(M)

求凸包坐标及其面积:

'''
    输入点集坐标,输出凸包顶点坐标
'''
# -*-coding:utf-8-*-
from math import sqrt

solution = []
def convex_hull(points: list):
    if len(points) <= 3:
        return points
    global solution
    points.sort(key=lambda x: x[0])  # 按照横坐标排序
    left_most = points[0]
    right_most = points[-1]
    solution.extend([left_most, right_most])

    helper(points, left_most, right_most, True)
    helper(points, left_most, right_most, False)
    return solution


def helper(points, left_most, right_most, upBool):
    """

    :param points:
    :param left_most:
    :param right_most:
    :param upBool: 上包为True
    :return:
    """
    global solution
    if len(points) <= 1:
        return
    l = line_helper(left_most, right_most)
    if upBool:
        up = []
        max_distance = 0
        max_point = ()
        for point in points:
            distance = 0 - (l[0] * point[0] + l[1] * point[1] + l[2]) / sqrt(l[0] * l[0] + l[1] * l[1])  # 与直线的距离
            if distance > 0:
                up.append(point)
                if distance > max_distance:
                    max_distance = distance
                    max_point = point
        if max_point != ():
            solution.append(max_point)
        helper(up, left_most, max_point, True)
        helper(up, max_point, right_most, True)
    else:
        down = []
        min_distance = 0
        min_point = ()
        for point in points:
            distance = 0 - (l[0] * point[0] + l[1] * point[1] + l[2]) / sqrt(l[0] * l[0] + l[1] * l[1])  # 与直线的距离
            if distance < 0:
                down.append(point)
                if distance < min_distance:
                    min_distance = distance
                    min_point = point
        if min_point != ():
            solution.append(min_point)
        helper(down, left_most, min_point, False)
        helper(down, min_point, right_most, False)


def line_helper(point1, point2):
    """
    计算距离
    :param point1:
    :param point2:
    :return:
    """
    if (point1[0] - point2[0]) != 0:
        m = (point1[1] - point2[1]) / (point1[0] - point2[0])
        c = point1[1] - m * point1[0]
        return [m, -1, c]
    else:
        return [1, 0, point1[0]]

def compute_polygon_area(points):  # 计算任意多边形的面积,顶点按照顺时针或者逆时针方向排列
    point_num = len(points)
    if(point_num < 3): return 0.0
    s = points[0][1] * (points[point_num-1][0] - points[1][0])
    #for i in range(point_num): # (int i = 1 i < point_num ++i):
    for i in range(1, point_num): # 有小伙伴发现一个bug,这里做了修改,但是没有测试,需要使用的亲请测试下,以免结果不正确。
        s += points[i][1] * (points[i-1][0] - points[(i+1)%point_num][0])
    return abs(s/2.0)


if __name__ == '__main__':
    input_points = [(182, 151), (219, 201), (241.5, 246.75), (253.25, 297), (207, 329), (341.5, 422.5), (622.5, 498.25)]
    print("Input:", input_points)
    rst = convex_hull(input_points)
    print("Output", rst)
    polygon = rst
    print('凸包面积是:', compute_polygon_area(polygon))

发布了22 篇原创文章 · 获赞 0 · 访问量 1252
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览