MCPcopy Create free account
hub / github.com/JimmyHHua/opencv_tutorials / open_demo

Function open_demo

python/code_066/opencv_066.py:5–29  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

3
4
5def 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
32def close_demo():

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected