Skip to content

Commit 5bb2f56

Browse files
committed
update code—_66_to_70
1 parent a3ed861 commit 5bb2f56

24 files changed

+161
-109
lines changed

Diff for: README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ gray = cv2.cvtColor(src, cv.COLOR_BGR2GRAY)
2626

2727
<div align=center><img src=https://i.loli.net/2019/05/22/5ce4b2ae1e7ce86434.png width=120> <img src=https://i.loli.net/2019/05/22/5ce4b2ae220a248459.png width=120></div>
2828

29-
30-
29+
30+
3131
***More opencv4.0 tutorials plese follow the learning road as below*** 👇👇👇
3232

3333
## Learning Road ⛳️
@@ -104,3 +104,8 @@ code_062 | [Dilation and Erosion](python/code_062/opencv_062.py) | ❣️
104104
code_063 | [Structuring Element](python/code_063/opencv_063.py) | ✔️
105105
code_064 | [Opening Transformation](python/code_064/opencv_064.py) | ✏️
106106
code_065 | [Closing Transformation](python/code_065/opencv_065.py) | ✏️
107+
code_066 | [Application of Opening and Closing Operations](python/code_066/opencv_066.py) | ✏️
108+
code_067 | [Top Hat](python/code_067/opencv_067.py) | ✔️
109+
code_068 | [Black Hat](python/code_068/opencv_068.py) | ✔️
110+
code_069 | [Morph Gradient](python/code_069/opencv_069.py) | ✔️
111+
code_070 | [Contour based on Morph Gradient](python/code_070/opencv_070.py) | ✏️

Diff for: README_CN.md

+5
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,8 @@ code_062 | [膨胀和腐蚀](python/code_062/opencv_062.py) | ❣️
103103
code_063 | [结构元素](python/code_063/opencv_063.py) | ✔️
104104
code_064 | [开运算](python/code_064/opencv_064.py) | ✏️
105105
code_065 | [闭运算](python/code_065/opencv_065.py) | ✏️
106+
code_066 | [开闭运算的应用](python/code_066/opencv_066.py) | ✏️
107+
code_067 | [顶帽](python/code_067/opencv_067.py) | ✔️
108+
code_068 | [黑帽](python/code_068/opencv_068.py) | ✔️
109+
code_069 | [图像梯度](python/code_069/opencv_069.py) | ✔️
110+
code_070 | [基于梯度的轮廓发现](python/code_070/opencv_070.py) | ✏️

Diff for: python/README_EN.md

-107
This file was deleted.

Diff for: python/code_066/binary1.png

10.7 KB
Loading

Diff for: python/code_066/binary2.png

1003 Bytes
Loading

Diff for: python/code_066/close.png

2.18 KB
Loading

Diff for: python/code_066/fill.png

56 KB
Loading

Diff for: python/code_066/morph3.png

49.6 KB
Loading

Diff for: python/code_066/opencv_066.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import cv2 as cv
2+
import numpy as np
3+
4+
5+
def open_demo():
6+
src = cv.imread("fill.png")
7+
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
8+
cv.imshow("input", src)
9+
10+
# 图像二值化
11+
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
12+
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
13+
cv.imwrite("binary1.png", binary)
14+
cv.imshow("binary1", binary)
15+
16+
# 开操作
17+
se1 = cv.getStructuringElement(cv.MORPH_RECT, (15, 1))
18+
binary = cv.morphologyEx(binary, cv.MORPH_OPEN, se1)
19+
cv.imshow("binary", binary)
20+
cv.imwrite("binary2.png", binary)
21+
22+
# 提取轮廓
23+
out, contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
24+
for c in range(len(contours)):
25+
x, y, w, h = cv.boundingRect(contours[c])
26+
y = y - 10
27+
h = 12
28+
cv.rectangle(src, (x, y), (x+w, y+h), (0, 0, 255), 1, 8, 0)
29+
cv.imshow("result", src)
30+
31+
32+
def close_demo():
33+
src = cv.imread("morph3.png")
34+
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
35+
cv.imshow("input", src)
36+
37+
# 图像二值化
38+
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
39+
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
40+
41+
# 闭操作
42+
se = cv.getStructuringElement(cv.MORPH_ELLIPSE, (15, 15), (-1, -1))
43+
binary = cv.morphologyEx(binary, cv.MORPH_CLOSE, se)
44+
cv.imwrite("close.png", binary)
45+
cv.imshow("close", binary)
46+
47+
#open_demo()
48+
close_demo()
49+
cv.waitKey(0)
50+
cv.destroyAllWindows()

Diff for: python/code_067/binary2.png

1.78 KB
Loading

Diff for: python/code_067/binary3.png

1.08 KB
Loading

Diff for: python/code_067/cells.png

15.4 KB
Loading

Diff for: python/code_067/morph02.png

9.27 KB
Loading

Diff for: python/code_067/opencv_067.py

+21
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("morph02.png")
5+
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
6+
cv.imshow("input", src)
7+
8+
# 高斯模糊去噪声
9+
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
10+
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
11+
12+
# 顶帽操作
13+
se = cv.getStructuringElement(cv.MORPH_RECT, (3, 3), (-1, -1))
14+
binary = cv.morphologyEx(binary, cv.MORPH_TOPHAT, se)
15+
16+
17+
cv.imshow("binary", binary)
18+
cv.imwrite("binary3.png", binary)
19+
20+
cv.waitKey(0)
21+
cv.destroyAllWindows()

Diff for: python/code_068/binary2.png

4.66 KB
Loading

Diff for: python/code_068/morph.png

9.27 KB
Loading

Diff for: python/code_068/opencv_068.py

+21
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("morph.png")
5+
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
6+
cv.imshow("input", src)
7+
8+
# 图像二值化
9+
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
10+
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
11+
12+
# 黑帽操作
13+
se = cv.getStructuringElement(cv.MORPH_RECT, (9, 9), (-1, -1))
14+
binary = cv.morphologyEx(binary, cv.MORPH_BLACKHAT, se)
15+
16+
17+
cv.imshow("black hat", binary)
18+
cv.imwrite("binary2.png", binary)
19+
20+
cv.waitKey(0)
21+
cv.destroyAllWindows()

Diff for: python/code_069/external.png

136 KB
Loading

Diff for: python/code_069/gradient.png

127 KB
Loading

Diff for: python/code_069/hist_01.jpg

35.4 KB
Loading

Diff for: python/code_069/interal.png

140 KB
Loading

Diff for: python/code_069/opencv_069.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import cv2 as cv
2+
3+
src = cv.imread("hist_01.jpg",0)
4+
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
5+
cv.imshow("input", src)
6+
7+
# 形态学梯度 - 基本梯度
8+
se = cv.getStructuringElement(cv.MORPH_RECT, (5, 5), (-1, -1))
9+
basic = cv.morphologyEx(src, cv.MORPH_GRADIENT, se)
10+
cv.imshow("basic gradient", basic)
11+
12+
# 外梯度
13+
dilate = cv.morphologyEx(src, cv.MORPH_DILATE, se)
14+
exteral = cv.subtract(dilate, src)
15+
cv.imshow("external gradient", exteral)
16+
17+
# 内梯度
18+
erode = cv.morphologyEx(src, cv.MORPH_ERODE, se)
19+
interal = cv.subtract(src, erode)
20+
cv.imshow("interal gradient", interal)
21+
22+
cv.imwrite("gradient.png", basic)
23+
cv.imwrite("external.png", exteral)
24+
cv.imwrite("interal.png", interal)
25+
cv.waitKey(0)
26+
cv.destroyAllWindows()

Diff for: python/code_070/kd02.png

407 KB
Loading

Diff for: python/code_070/opencv_070.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import cv2 as cv
2+
3+
src = cv.imread("kd02.png")
4+
src = cv.resize(src,(800,300),interpolation=cv.INTER_CUBIC)
5+
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
6+
cv.imshow("input", src)
7+
8+
# 形态学梯度 - 基本梯度
9+
se = cv.getStructuringElement(cv.MORPH_RECT, (3, 3), (-1, -1))
10+
basic = cv.morphologyEx(src, cv.MORPH_GRADIENT, se)
11+
cv.imshow("basic gradient", basic)
12+
13+
gray = cv.cvtColor(basic, cv.COLOR_BGR2GRAY)
14+
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
15+
cv.imshow("binary", binary)
16+
17+
se = cv.getStructuringElement(cv.MORPH_RECT, (1, 5), (-1, -1))
18+
binary = cv.morphologyEx(binary, cv.MORPH_DILATE, se)
19+
out, contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
20+
for c in range(len(contours)):
21+
x, y, w, h = cv.boundingRect(contours[c])
22+
area = cv.contourArea(contours[c])
23+
if area < 200:
24+
continue
25+
if h > (3*w) or h < 20:
26+
continue
27+
cv.rectangle(src, (x, y), (x+w, y+h), (0, 0, 255), 1, 8, 0)
28+
29+
cv.imshow("result", src)
30+
cv.waitKey(0)
31+
cv.destroyAllWindows()

0 commit comments

Comments
 (0)