Skip to content

Commit 2af5a1f

Browse files
committed
add 71~75
1 parent 5bb2f56 commit 2af5a1f

25 files changed

+319
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,8 @@ code_067 | [Top Hat](python/code_067/opencv_067.py) | ✔️
109109
code_068 | [Black Hat](python/code_068/opencv_068.py) | ✔️
110110
code_069 | [Morph Gradient](python/code_069/opencv_069.py) | ✔️
111111
code_070 | [Contour based on Morph Gradient](python/code_070/opencv_070.py) | ✏️
112+
code_071 | [Hit and Miss](python/code_071/opencv_071.py) | ✔️
113+
code_072 | [Defect Detecting-1](python/code_072/opencv_072.py) | ✔️
114+
code_073 | [Defect Detecting-2](python/code_073/opencv_073.py) | ✔️
115+
code_074 | [Extract the Maximum Contour and Coding Key Points](python/code_074/opencv_074.py) | ✔️
116+
code_075 | [Image Inpainting](python/code_075/opencv_075.py) | ✔️

README_CN.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,8 @@ code_067 | [顶帽](python/code_067/opencv_067.py) | ✔️
108108
code_068 | [黑帽](python/code_068/opencv_068.py) | ✔️
109109
code_069 | [图像梯度](python/code_069/opencv_069.py) | ✔️
110110
code_070 | [基于梯度的轮廓发现](python/code_070/opencv_070.py) | ✏️
111+
code_071 | [击中击不中](python/code_071/opencv_071.py) | ✔️
112+
code_072 | [缺陷检测1](python/code_072/opencv_072.py) | ✔️
113+
code_073 | [缺陷检测2](python/code_073/opencv_073.py) | ✔️
114+
code_074 | [提取最大轮廓和编码关键点](python/code_074/opencv_074.py) | ✔️
115+
code_075 | [图像修复](python/code_075/opencv_075.py) | ✔️

python/code_071/binary2.png

2.62 KB
Loading

python/code_071/cross.png

205 KB
Loading

python/code_071/opencv_071.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import cv2 as cv
2+
import numpy as np
3+
4+
src = cv.imread("cross.png")
5+
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
6+
cv.imshow("input", src)
7+
8+
# Binary image
9+
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
10+
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
11+
12+
# Hit and Miss
13+
se = cv.getStructuringElement(cv.MORPH_CROSS, (12, 12))
14+
binary = cv.morphologyEx(binary, cv.MORPH_HITMISS, se)
15+
16+
17+
cv.imshow("hit miss", binary)
18+
cv.imwrite("binary2.png", binary)
19+
20+
cv.waitKey(0)
21+
cv.destroyAllWindows()

python/code_072/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#### 二值图像分析 – 缺陷检测
2+
3+
1. 观察图像与提取图像ROI对象轮廓外接矩形与轮廓.
4+
```python
5+
# Get contours
6+
_, contours, hierarchy = cv.findContours(binary, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
7+
height, width = src.shape[:2]
8+
for c in range(len(contours)):
9+
x, y, w, h = cv.boundingRect(contours[c])
10+
area = cv.contourArea(contours[c])
11+
if h > (height//2):
12+
continue
13+
if area < 150:
14+
continue
15+
cv.rectangle(src, (x, y), (x+w, y+h), (0, 0, 255), 1, 8, 0)
16+
cv.drawContours(src, contours, c, (0, 255, 0), 2, 8)
17+
```
18+
<img src=ce_02.jpg width=300>
19+
<img src=binary2.png width=300>
20+
21+
2. 对于得到的刀片外接矩形,首先需要通过排序,确定他们的编号.
22+
23+
👀 [代码Code](../code_073/opencv_073.py)
24+
25+
```
26+
# 排序轮廓
27+
rects = sorted(rects, key = lambda x:x[1])
28+
```
29+
3. 根据模板进行相减得到与模板不同的区域,对这些区域进行形态学操作,去掉边缘细微差异,最终就得到了可以检出的缺陷或者划痕刀片。
30+
31+
```python
32+
# 获取模板
33+
def get_template(binary, boxes):
34+
x, y, w, h = boxes[0]
35+
roi = binary[y:y+h, x:x+w]
36+
return roi
37+
38+
# 缺陷检测函数
39+
def detect_defect(binary, boxes, tpl):
40+
height, width = tpl.shape
41+
index = 1
42+
defect_rois = []
43+
# 发现缺失
44+
for x, y, w, h in boxes:
45+
roi = binary[y:y + h, x:x + w]
46+
roi = cv.resize(roi, (width, height))
47+
mask = cv.subtract(tpl, roi)
48+
se = cv.getStructuringElement(cv.MORPH_RECT, (5, 5), (-1, -1))
49+
mask = cv.morphologyEx(mask, cv.MORPH_OPEN, se)
50+
ret, mask = cv.threshold(mask, 0, 255, cv.THRESH_BINARY)
51+
count = 0
52+
for row in range(height):
53+
for col in range(width):
54+
pv = mask[row, col]
55+
if pv == 255:
56+
count += 1
57+
if count > 0:
58+
defect_rois.append([x, y, w, h])
59+
cv.imwrite("mask%d.png"%index, mask)
60+
index += 1
61+
return defect_rois
62+
63+
```
64+
65+
<img src=../code_073/ce_02.jpg width=300>
66+
<img src=../code_073/binary2.png width=300>

python/code_072/binary2.png

240 KB
Loading

python/code_072/ce_02.jpg

27.2 KB
Loading

python/code_072/opencv_072.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import cv2 as cv
2+
import numpy as np
3+
4+
src = cv.imread("ce_02.jpg")
5+
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
6+
cv.imshow("input", src)
7+
8+
# get binary
9+
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
10+
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
11+
12+
se = cv.getStructuringElement(cv.MORPH_RECT, (3, 3), (-1, -1))
13+
binary = cv.morphologyEx(binary, cv.MORPH_OPEN, se)
14+
cv.imshow("binary", binary)
15+
16+
# Get contours
17+
_, contours, hierarchy = cv.findContours(binary, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
18+
height, width = src.shape[:2]
19+
for c in range(len(contours)):
20+
x, y, w, h = cv.boundingRect(contours[c])
21+
area = cv.contourArea(contours[c])
22+
if h > (height//2):
23+
continue
24+
if area < 150:
25+
continue
26+
cv.rectangle(src, (x, y), (x+w, y+h), (0, 0, 255), 1, 8, 0)
27+
cv.drawContours(src, contours, c, (0, 255, 0), 2, 8)
28+
29+
cv.imshow("result", src)
30+
cv.imwrite("binary2.png", src)
31+
32+
cv.waitKey(0)
33+
cv.destroyAllWindows()

python/code_073/binary2.png

248 KB
Loading

0 commit comments

Comments
 (0)