DAY 16
0

1.外接矩形
2.最小外接矩形
3.最小外接圓形
4.最小外接橢圓
5.最佳擬合直線
6.最小外接三角

## 外接矩形

``````#外接矩形
copy = img.copy()
x,y,w,h = cv.boundingRect(contours[i])
brcnt = np.array([[[x,y]],[[x+w,y]],[[x+w,y+h]],[[x,y+h]]])
cv.drawContours(copy,[brcnt],-1,(255,0,0),2)
a = './static/a_'+image_name
cv.imwrite(a,copy)
``````

## 最小外接矩形

``````#最小外接矩形
copy = img.copy()
rect = cv.minAreaRect(contours[i])
points = cv.boxPoints(rect)
repoint = np.array(points).reshape((-1,1,2)).astype(np.int32)
cv.drawContours(copy,[repoint],0,(255,0,0),2)
b = './static/b_'+image_name
cv.imwrite(b,copy)
``````

## 最小外接圓形

``````#最小外接圓形
copy = img.copy()
center = (int(x),int(y))
c = './static/c_'+image_name
cv.imwrite(c,copy)
``````

## 最小外接橢圓

``````#最小外接橢圓
copy = img.copy()
ellipse = cv.fitEllipse(contours[i])
cv.ellipse(copy,ellipse,(255,0,0),3)
d = './static/d_'+image_name
cv.imwrite(d,copy)

``````

## 最佳擬合直線

``````#最小擬合直線
copy = img.copy()
rows,cols = copy.shape[:2]
[vx,vy,x,y] = cv.fitLine(contours[i],cv.DIST_L2,0,0.01,0.01)
lefty = int(((-x*vy/vx)+y))
righty = int(((cols-x)*vy/vx)+y)
cv.line(copy,(cols-1,righty),(0,lefty),(255,0,0),2)
e = './static/e_'+image_name
cv.imwrite(e,copy)
``````

## 最小外接三角

``````#最小外接三角
copy = img.copy()
area,trgl = cv.minEnclosingTriangle(contours[i])
for i in range(0,3):
cv.line(copy,tuple(trgl[i][0]),tuple(trgl[(i+1)%3][0]),(255,0,0),2)
f = './static/f_'+image_name
cv.imwrite(f,copy)
``````