opencv抠出图像中的有角度的矩形框

本文介绍了使用OpenCV从图像中提取倾斜矩形框的两种策略:旋转抠图和透视变换抠图。其中,旋转方法由于采用仿射变换,计算量相对较小。
摘要由CSDN通过智能技术生成
opencv抠出图像中的有角度的矩形框:
import cv2
import numpy as np


def main():
    img = cv2.imread("F:/wangjiao/_LB37824S5AX055086_.jpg")
    cnt = np.array([
            [[215, 381]],
            [[1109, 452]],
            [[1102, 532]],
            [[209, 461]]
        ])
    # cnt = np.array([
    #         [[281, 573]],
    #         [[1054, 498]],
    #         [[1061, 566]],
    #         [[287, 641]]
    #     ])
    cv2.drawContours(img, [cnt], 0, (0, 255, 255), 2)
    rect = cv2.minAreaRect(cnt)
    print("rect: {}".format(rect))

    box = cv2.boxPoints(rect)
    box = np.int0(box)
    cv2.drawContours(img, [box], 0, (0, 0, 255), 2)

    # img_crop will the cropped rectangle, img_rot is the rotated image
    img_crop, img_rot = crop_rect(img, rect)
    cv2.imwrite("F:\wangjiao/rotate/img.jpg", img)
    cv2.imwrite("F:\wangjiao/rotate/cropped_img.jpg", img_crop)
    cv2.waitKey(0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值