opencv最小外接矩形

import cv2
import numpy as np
img1 = cv2.imread(r'D:\test\pigs\20200518132334816.jpg.jpg',0)
_,thresh = cv2.threshold(img1,200,255,0)
image,contours = cv2.findContours(thresh,2,1)
area = 0
for c in image:
    rect = cv2.minAreaRect(c)
    box = np.int0(cv2.boxPoints(rect))
    y_max = np.max(box[:,1])
    x_max = np.max(box[:,0])
    y_min = np.min(box[:, 1])
    x_min = np.min(box[:, 0])
    if (y_max - y_min) * (x_max - x_min) > area:
        area = (y_max - y_min) * (x_max - x_min)
        bbox = box
        yy_max = y_max
        xx_max = x_max
        yy_min = y_min
        xx_min = x_min
print(bbox)
cv2.drawContours(img1,[bbox],0,(100,100,100),4)
cv2.imshow('a',img1)
cv2.waitKey(0)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章