# 基于深度图的物块检测 (OpenCV)

1、图片读取

``````import cv2
import numpy as np

ref_path = 'C:Users/15989Desktopimgs/1.png'
cur_path = 'C:Users/15989Desktopimgs/201.png'

``````def norm(img):
img = (img - img.min()) / (img.max() - img.min())
img *= 255
return img``````

2、做帧差

``diff_frame = cv2.absdiff(cur_img, ref_img)``

3、选取感兴趣区域

``diff_frame_roi = diff_frame[200:, 200:540]``

4、将图片转成二值图

``frame_bin = cv2.inRange(diff_frame_roi, 20, 100)``

5、闭运算 (先膨胀，后腐蚀)，消除小型的黑洞

``````kernel = np.ones((3, 3), dtype=np.uint8)
closing = cv2.morphologyEx(frame_bin, cv2.MORPH_CLOSE, kernel)``````

6、寻找目标轮廓

``contours, _ = cv2.findContours(closing, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)``

7、找到包含轮廓的最小外接圆并求出对应半径，对轮廓半径进行过滤，计算符合要求的轮廓的最小外接矩形，对外接矩形的宽高及它们的比例进行过滤，选取合适大小的目标 (调参主要集中在这一环节)

``````x_obj, y_obj = 0, 0
w, h = 0, 0
xl, yl = [], []
wl, hl = [], []
for cont in contours:
rect = cv2.minAreaRect(cont)
x_obj, y_obj = rect[0]
w, h = rect[1]
if w/h > 1.5 or w/h < 0.5 or w*h < 500:
continue
else:
xl.append(x_obj)
yl.append(y_obj)
wl.append(w)
hl.append(h)``````

8、 得到目标最终位置，由于第3步中对图片进行了裁剪，因此需要将上一步得到的坐标进行转换，转换成原图坐标并在图片中框出目标所在位置

``````x_final = [xi + 200 for xi in xl]
y_final = [yi + 200 for yi in yl]
for x_f, y_f, w_f, h_f in zip(x_final, y_final, wl, hl):
cv2.rectangle(cur_img, (int(x_f - w_f / 2), int(y_f - h_f / 2)), (int(x_f + w_f / 2), int(y_f + h_f / 2)), (0, 0, 0), thickness=2)``````

THE END

)">