前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >OpenCV旋转图像

OpenCV旋转图像

作者头像
用户6021899
发布2023-09-19 17:07:32
1540
发布2023-09-19 17:07:32
举报

OpenCV旋转图像 要用到这两个函数:

M = cv2.getRotationMatrix2D((cx, cy), angle, 1) # 旋转中心,角度degree,放大比例

image = cv2.warpAffine(image, M, die_image.shape)

示例程序

代码语言:javascript
复制
K = 10
resized = cv2.resize(copied, (rows // K, cols // K))
domain = connected_domain(resized, 20)  # 缩小图像,以便加速图像分割
for label in domain:
    if (area := len(domain[label])) < 1440000 // (K * K):
        continue  # 舍弃掉面积小的连通域

    # 找 连通域的左上角和右下角
    x1, y1, x3, y3 = find_corners(domain[label])
    row = int(x1 / (1330.0 / K))  # 1330 为die 重复间距
    col = int(y1 / (1330.0 / K))
    index = 3 * row + col

    #print(f"row={row}, col={col}")
    die_bin = MX_MY_BIN_list[index][2]
    if die_bin == "GOOD":  # only analyze good die
        # print(f"row={row}, col={col}")
        # print(MX_MY_BIN_list[index], x1, y1)

        die_image = img[x1 * K:x3 * K, y1 * K:y3 * K]
        # plt.imshow(die_image, cmap="gray")
        # plt.title("-".join(MX_MY_BIN_list[index]))
        # plt.show()
        row1, col1, row2, col2 = find_left_dummy_pads(die_image)

        # 计算旋转角度
        theta = 0
        if (delta_y := col2 - col1) != 0:  # 象鼻表达式的优先级很低,所有这里需要括号
            theta = atan((row2 - row1) / delta_y) / pi * 180
        #print(f" die 的旋转角度 theta = {theta} 度")
        angle = (theta - 90) if theta > 0 else (theta + 90)
        M = cv2.getRotationMatrix2D((0, 0), angle, 1)  # 旋转中心,角度degree,放大比例
        die_image = cv2.warpAffine(die_image, M, die_image.shape)

        # plt.imshow(die_image, cmap="gray")
        # plt.title("rotated die image")
        # plt.show()
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-09-13 21:36,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python可视化编程机器学习OpenCV 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档