查看minAreaRect返回角度数值

import cv2
import os
import random
import numpy as np
if __name__ == '__main__':
    image = cv2.imread('min.png')
    for i in range(0,360):
        img_gt = image.copy()
        #img_gt = cv2.copyMakeBorder(img_gt,50,50,50,50,cv2.BORDER_CONSTANT,value = (0,0,0))
        M = cv2.getRotationMatrix2D((int(img_gt.shape[0] / 2), int(img_gt.shape[1] / 2)), i, 1)
        img_gt = cv2.warpAffine(img_gt, M, (int(img_gt.shape[0]), int(img_gt.shape[1])))
        img = img_gt.copy()
        img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        ret, img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
        contours, hi = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        rect = cv2.![在这里插入图片描述](https://img-blog.csdnimg.cn/867a980ef0794b509e440347f5713a78.png#pic_center)
(contours[0])
        box = cv2.boxPoints(rect)
        box = np.int0(box)
        box = box.reshape((-1, 1, 2))
        # cv2.circle(img, (box[0][0][0], box[0][0][1]), 5, (255, 255, 255), -1)
        # cv2.circle(img, (box[1][0][0], box[1][0][1]), 10, (255, 255, 255), -1)
        # cv2.circle(img, (box[2][0][0], box[2][0][1]), 15, (255, 255, 255), -1)
        # cv2.circle(img, (box[3][0][0], box[3][0][1]), 20, (255, 255, 255), -1)
        cv2.putText(img,'1',(box[0][0][0], box[0][0][1]), cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,255),2)
        cv2.putText(img, '2', (box[1][0][0], box[1][0][1]), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
        cv2.putText(img, '3', (box[2][0][0], box[2][0][1]), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
        cv2.putText(img, '4', (box[3][0][0], box[3][0][1]), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
        print("角度 == ",round(90 - rect[2]))
        if len(contours) > 2:
            print('有两个轮廓')
            break 
        cv2.imshow('' + str(i),img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

你可能感兴趣的:(opencv自学疑点,opencv)