python+opencv获取最小外接矩形
环境代码效果
环境
python版本:3.8 opencv版本:4.3.0.36
代码
from PIL
import Image
from pylab
import *
import cv2
import numpy
as np
im
= array
(Image
.open('./001.bmp'))
xs
= [190.5,167.5,144.5,128.5,114.5,100.5,97.5,82.5,71.5,62.5,45.5,33.5,23.5,10.5,9.5,9.5,18.5,20.5,14.5,17.5,16.5,25.5,26.5,37.5,50.5,61.5,75.5,80.5,82.5,83.5,107.5,131.5,137.5,176.5,193.5,200.5,203.5,217.5,239.5,254.5,270.5,288.5,304.5,327.5,341.5,363.5,377.5,388.5,397.5,402.5,388.5,381.5,374.5,375.5,367.5,363.5,358.5,363.5,353.5,340.5,339.5,335.5,326.5,311.5,279.5,239.5,211.5,190.5]
ys
= [374.13,366.13,363.13,361.13,365.13,361.13,357.13,362.13,373.13,376.13,369.13,365.13,354.13,342.13,329.13,317.13,311.13,296.13,283.13,272.13,258.13,247.13,216.13,184.13,158.13,129.13,98.13,82.13,52.13,42.13,34.13,31.13,15.13,10.12,13.13,26.13,31.13,22.13,22.13,28.13,41.13,54.13,65.13,81.13,92.13,97.13,112.13,148.13,176.13,192.13,204.13,213.13,223.13,245.13,255.13,269.13,281.13,296.13,302.13,313.13,325.13,338.13,358.13,381.13,391.13,390.13,380.13,374.13]
plt
.plot
(xs
, ys
, 'r*')
contour
= np
.vstack
((xs
, ys
))
contour
= np
.array
(contour
.T
, dtype
=np
.float32
)
rect
= cv2
.minAreaRect
(contour
)
box
= cv2
.boxPoints
(rect
)
box
= np
.int0
(box
)
plt
.plot
(box
[1], box
[0], color
='g')
plt
.plot
(box
[2], box
[1], color
='g')
plt
.plot
(box
[3], box
[2], color
='g')
plt
.plot
(box
[0], box
[3], color
='g')
title
("pic1.png")
imshow
(im
)
plt
.show
()
效果
转载请注明原文地址:https://tech.qufami.com/read-12192.html