0 目錄
1 opencv
1.1 cv2.minAreaRect
1.2 cv2.boxPoints
2 numpy
2.1 np.random.randint()
1 Opencv 篇
1.1 cv2.minAreaRect
函數原型
cv2.minAreaRect(Points) 生成最小外接矩形
其中points是點集,數據類型爲ndarray,array((x1,y1),(x2,y2),…,(xn,yn))
而minAreaRect就是求出在上述點集下的最小面積矩形
eg:
返回對象rect
其中 rect(0) = 中心點座標 (x_c, y_c) , rect(1) = 矩形寬高 (w, h), rect(2) = 角度 theta
實例
可以用作座標變換(x1,y1,x2,y2,x3,y3,x4,y4) -> (x_c, y_c, w, h, theta)
boxes = []
coordinate = np.array([[167., 203., 96., 132., 132., 96., 203., 167., 1.]])
for rect in coordinate:
box = np.int0(rect[:-1])
box = box.reshape([4, 2])
rect1 = cv2.minAreaRect(box)
print(rect1)
# rect1 = ((149.50001525878906, 149.50001525878906), (50.91169357299805, 100.40917205810547), -45.0)
1.2 cv2.boxPoints
根據minAreaRect的返回值,返回外接矩形的四個頂點座標。
借用1.1.2的圖,即返回box[0],box[1],box[2],box[3]四個點的座標。
實例:
可以用作座標變換(x_c, y_c, w, h, theta) -> (x1,y1,x2,y2,x3,y3,x4,y4)
boxes = []
coordinate = (x_c, y_c, w, h, theta)
for rect in coordinate:
box = cv2.boxPoints(((rect[0], rect[1]), (rect[2], rect[3]), rect[4]))
box = np.reshape(box, [-1, ])
boxes.append([box[0], box[1], box[2], box[3], box[4], box[5], box[6], box[7]])
2 Numpy 篇
2.1 np.random.randint( )
numpy.random.randint(low, high=None, size=None, dtype=‘l’)
low (inclusive) to high (exclusive)的隨機整數。
如果默認high=None,則取[0, low)
low: 從分佈中提取的最小的整數
high: 分佈中提取的最大值
size: int or tuple of ints, optional
輸出形態,例如(m,n,k),默認爲None,表示給出單值
實例
np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1],
[3, 2, 2, 0]])
color = (np.random.randint(255), np.random.randint(255), np.random.randint(255))