| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
# Get contours
_, contours, hierarchy = cv.findContours(binary, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
height, width = src.shape[:2]
for c in range(len(contours)):
x, y, w, h = cv.boundingRect(contours[c])
area = cv.contourArea(contours[c])
if h > (height//2):
continue
if area < 150:
continue
cv.rectangle(src, (x, y), (x+w, y+h), (0, 0, 255), 1, 8, 0)
cv.drawContours(src, contours, c, (0, 255, 0), 2, 8)👀 代码Code
# 排序轮廓 rects = sorted(rects, key = lambda x:x[1])
# 获取模板
def get_template(binary, boxes):
x, y, w, h = boxes[0]
roi = binary[y:y+h, x:x+w]
return roi
# 缺陷检测函数
def detect_defect(binary, boxes, tpl):
height, width = tpl.shape
index = 1
defect_rois = []
# 发现缺失
for x, y, w, h in boxes:
roi = binary[y:y + h, x:x + w]
roi = cv.resize(roi, (width, height))
mask = cv.subtract(tpl, roi)
se = cv.getStructuringElement(cv.MORPH_RECT, (5, 5), (-1, -1))
mask = cv.morphologyEx(mask, cv.MORPH_OPEN, se)
ret, mask = cv.threshold(mask, 0, 255, cv.THRESH_BINARY)
count = 0
for row in range(height):
for col in range(width):
pv = mask[row, col]
if pv == 255:
count += 1
if count > 0:
defect_rois.append([x, y, w, h])
cv.imwrite("mask%d.png"%index, mask)
index += 1
return defect_rois| Back | FazBrowse Home | New Git URL |