引言
想法起源于控制3d打印机初始线径,能够监控平台首层高度是否合理。经过实际测量,不同的高度,精度需求为0.01mm,即程序应该能够区分之分辨率。
```python
%load_ext autoreload
%autoreload 2
import cv2
import os
import numpy as np
from utils import objectFound
```
```python
imgs = [os.path.join("../images/",x) for x in os.listdir("../images/") if x.find("re")>-1]
imgs
```
['../images/1re.jpg', '../images/2re.jpg', '../images/3re.jpg']
## 测量精度
* 单个2cm方块精度: 0.024
```python
img1 = cv2.imread(imgs[1])
img1.shape
```
(1840, 3264, 3)
```python
gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (9,9), 0)
# _, thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
_, thresh = cv2.threshold(blur, 150, 255, 0)
cv2.namedWindow("thresh", cv2.WINDOW_NORMAL)
cv2.imshow("thresh", thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
```python
ob = objectFound()
rects = ob.rectangle(img1.copy(), thresh, debug=True, mode='all')
```
```python
maxrate = 0
roiF = None
for index, rect in enumerate(rects):
#计算0-255比例, 0要尽可能多
x,y,w,h = rect
roi = thresh[y:y+h, x:x+w]
rate = (np.sum(roi==0)/(roi.shape[0]*roi.shape[1])) + (0.1-index*0.01) #排名奖励
&nb