Skip to content

Commit 1de7886

Browse files
committed
commit
1 parent 9ae5a74 commit 1de7886

18 files changed

+240
-1
lines changed

README.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
# OpenCV 4.0 Tutorial
2+
3+
## Introduction
4+
25
This repository contains source code of OpenCV Tutorial application, the environment is python3.0 and opencv4.0
36

4-
## Learning Road
7+
## Sample
8+
- Image load
9+
```python
10+
import cv2
11+
12+
src = cv2.imread("test.png")
13+
cv2.namedWindow("input", cv2.WINDOW_AUTOSIZE)
14+
cv2.imshow("input", src)
15+
cv2.waitKey(0)
16+
cv2.destroyAllWindows()
17+
```
18+
- Gray Image
19+
```python
20+
gray = cv2.cvtColor(src, cv.COLOR_BGR2GRAY)
21+
```
22+
---
23+
**More opencv4.0 tutorials plese flow the learning road as below** 👇👇👇
24+
25+
## Learning Road ⛳️⛳️⛳️
526
- ✔️ : **Basic**
627
- ✏️ : **Attention**
728
- ❣️ : **Important**
@@ -63,3 +84,8 @@ code_052 | [OpenCV之几何矩计算中心](python/code_052/opencv_052.py) |
6384
code_053 | [OpenCV之使用Hu矩阵实现轮廓匹配](python/code_053/opencv_053.py) | ✔️
6485
code_054 | [OpenCV之轮廓圆与椭圆拟合](python/code_054/opencv_054.py) | ✔️
6586
code_055 | [OpenCV之凸包检测](python/code_055/opencv_055.py) | ✏️
87+
code_056 | [OpenCV之直线拟合与极值点寻找](python/code_056/opencv_056.py) | ✔️
88+
code_057 | [OpenCV之点多边形测试](python/code_057/opencv_057.py) | ✔️
89+
code_058 | [OpenCV之寻找最大内接圆](python/code_058/opencv_058.py) | ✔️
90+
code_059 | [OpenCV之霍夫曼直线检测](python/code_059/opencv_059.py) | ✔️
91+
code_060 | [OpenCV之概率霍夫曼直线检测](python/code_060/opencv_060.py) | ❣️

python/code_056/canny_output.png

1.43 KB
Loading

python/code_056/contours_analysis.png

54.9 KB
Loading

python/code_056/opencv_056.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import cv2 as cv
2+
import numpy as np
3+
4+
5+
def canny_demo(image):
6+
t = 80
7+
canny_output = cv.Canny(image, t, t * 2)
8+
cv.imwrite("canny_output.png", canny_output)
9+
return canny_output
10+
11+
12+
src = cv.imread("twolines.png")
13+
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
14+
cv.imshow("input", src)
15+
16+
binary = canny_demo(src)
17+
k = np.ones((3, 3), dtype=np.uint8)
18+
binary = cv.morphologyEx(binary, cv.MORPH_DILATE, k)
19+
cv.imshow("binary", binary)
20+
21+
# 轮廓发现
22+
out, contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
23+
24+
# 直线拟合与极值点寻找
25+
for c in range(len(contours)):
26+
x, y, w, h = cv.boundingRect(contours[c])
27+
m = max(w, h)
28+
# if m < 30:
29+
# continue
30+
vx, vy, x0, y0 = cv.fitLine(contours[c], cv.DIST_L1, 0, 0.01, 0.01)
31+
k = vy/vx
32+
b = y0 - k*x0
33+
maxx = 0
34+
maxy = 0
35+
miny = 100000
36+
minx = 0
37+
for pt in contours[c]:
38+
px, py = pt[0]
39+
if maxy < py:
40+
maxy = py
41+
if miny > py:
42+
miny = py
43+
maxx = (maxy - b) / k
44+
minx = (miny - b) / k
45+
cv.line(src, (np.int32(maxx), np.int32(maxy)),
46+
(np.int32(minx), np.int32(miny)), (0, 0, 255), 2, 8, 0)
47+
48+
49+
# 显示
50+
cv.imshow("contours_analysis", src)
51+
cv.imwrite("contours_analysis.png", src)
52+
53+
cv.waitKey(0)
54+
cv.destroyAllWindows()
55+
56+

python/code_056/twolines.png

47 KB
Loading

python/code_057/contours_analysis.png

100 KB
Loading

python/code_057/mask.png

2.2 KB
Loading

python/code_057/opencv_057.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import cv2 as cv
2+
import numpy as np
3+
4+
src = cv.imread("mask.png")
5+
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
6+
cv.imshow("input", src)
7+
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
8+
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
9+
cv.imshow("binary", binary)
10+
11+
# 轮廓发现
12+
image = np.zeros(src.shape, dtype=np.float32)
13+
out, contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
14+
h, w = src.shape[:2]
15+
for row in range(h):
16+
for col in range(w):
17+
dist = cv.pointPolygonTest(contours[0], (col, row), True) # True的话返回点到轮廓的距离,False则返回+1,0,-1三个值,其中+1表示点在轮廓内部,0表示点在轮廓上,-1表示点在轮廓外
18+
if dist == 0:
19+
image[row, col] = (255, 255, 255)
20+
if dist > 0:
21+
image[row, col] = (255-dist, 0, 0)
22+
if dist < 0:
23+
image[row, col] = (0, 0, 255+dist)
24+
25+
dst = cv.convertScaleAbs(image) # 将像素点进行绝对值计算
26+
dst = np.uint8(dst)
27+
28+
# 显示
29+
cv.imshow("contours_analysis", dst)
30+
cv.imwrite("contours_analysis.png", dst)
31+
cv.waitKey(0)
32+
cv.destroyAllWindows()
33+
34+

python/code_058/drawing.jpg

31.7 KB
Loading

python/code_058/opencv_058.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import cv2 as cv
2+
import numpy as np
3+
# Create an image
4+
r = 100
5+
src = np.zeros((4*r, 4*r), dtype=np.uint8)
6+
# Create a sequence of points to make a contour
7+
vert = [None]*6
8+
vert[0] = (3*r//2, int(1.34*r))
9+
vert[1] = (1*r, 2*r)
10+
vert[2] = (3*r//2, int(2.866*r))
11+
vert[3] = (5*r//2, int(2.866*r))
12+
vert[4] = (3*r, 2*r)
13+
vert[5] = (5*r//2, int(1.34*r))
14+
# Draw it in src
15+
for i in range(6):
16+
#cv.putText(src,str(i),vert[i],cv.FONT_HERSHEY_SIMPLEX, .8,( 255 ), 2)
17+
cv.line(src, vert[i], vert[(i+1)%6], ( 255 ), 3)
18+
# Get the contours
19+
_, contours, _ = cv.findContours(src, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
20+
21+
# Calculate the distances to the contour
22+
raw_dist = np.empty(src.shape, dtype=np.float32)
23+
for i in range(src.shape[0]):
24+
for j in range(src.shape[1]):
25+
raw_dist[i,j] = cv.pointPolygonTest(contours[0], (j,i), True)
26+
27+
# 获取最大值即内接圆半径,中心点坐标
28+
minVal, maxVal, _, maxDistPt = cv.minMaxLoc(raw_dist)
29+
minVal = abs(minVal)
30+
maxVal = abs(maxVal)
31+
32+
# Depicting the distances graphically
33+
drawing = np.zeros((src.shape[0], src.shape[1], 3), dtype=np.uint8)
34+
for i in range(src.shape[0]):
35+
for j in range(src.shape[1]):
36+
if raw_dist[i,j] < 0:
37+
drawing[i,j,0] = 255 - abs(raw_dist[i,j]) * 255 / minVal
38+
elif raw_dist[i,j] > 0:
39+
drawing[i,j,2] = 255 - raw_dist[i,j] * 255 / maxVal
40+
else:
41+
drawing[i,j,0] = 255
42+
drawing[i,j,1] = 255
43+
drawing[i,j,2] = 255
44+
45+
src_t = np.stack((src,)*3,axis=2)
46+
# max inner circle
47+
cv.circle(drawing,maxDistPt, np.int(maxVal),(255,255,255), 1, cv.LINE_8, 0)
48+
cv.imshow('Source', src)
49+
cv.imshow('Distance and inscribed circle', drawing)
50+
cv.imwrite('drawing.jpg', np.hstack((src_t,drawing)))
51+
52+
cv.waitKey(0)
53+
cv.destroyAllWindows()

python/code_059/canny_output.png

21 KB
Loading

python/code_059/contours_analysis.png

345 KB
Loading

python/code_059/opencv_059.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import cv2 as cv
2+
import numpy as np
3+
4+
5+
def canny_demo(image):
6+
t = 80
7+
canny_output = cv.Canny(image, t, t * 2)
8+
cv.imwrite("canny_output.png", canny_output)
9+
return canny_output
10+
11+
12+
src = cv.imread("sudoku.png")
13+
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
14+
cv.imshow("input", src)
15+
16+
binary = canny_demo(src)
17+
cv.imshow("binary", binary)
18+
19+
lines = cv.HoughLines(binary, 1, np.pi / 180, 150, None, 0, 0)
20+
if lines is not None:
21+
for i in range(0, len(lines)):
22+
rho = lines[i][0][0]
23+
theta = lines[i][0][1]
24+
a = np.cos(theta)
25+
b = np.sin(theta)
26+
x0 = a * rho
27+
y0 = b * rho
28+
pt1 = (int(x0 + 1000 * (-b)), int(y0 + 1000 * (a)))
29+
pt2 = (int(x0 - 1000 * (-b)), int(y0 - 1000 * (a)))
30+
cv.line(src, pt1, pt2, (0, 0, 255), 3, cv.LINE_AA)
31+
32+
33+
# 显示
34+
cv.imshow("hough line demo", src)
35+
cv.imwrite("contours_analysis.png", src)
36+
cv.waitKey(0)
37+
cv.destroyAllWindows()
38+
39+

python/code_059/sudoku.png

245 KB
Loading

python/code_060/canny_output.png

2.5 KB
Loading

python/code_060/contours_analysis.png

16 KB
Loading

python/code_060/morph01.png

6.46 KB
Loading

python/code_060/opencv_060.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import cv2 as cv
2+
import numpy as np
3+
4+
5+
def canny_demo(image):
6+
t = 80
7+
canny_output = cv.Canny(image, t, t * 2)
8+
cv.imwrite("canny_output.png", canny_output)
9+
return canny_output
10+
11+
12+
src = cv.imread("morph01.png")
13+
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
14+
cv.imshow("input", src)
15+
binary = canny_demo(src)
16+
cv.imshow("binary", binary)
17+
18+
linesP = cv.HoughLinesP(binary, 1, np.pi / 180, 55, None, 50, 10)
19+
cv.HoughLinesP
20+
if linesP is not None:
21+
for i in range(0, len(linesP)):
22+
l = linesP[i][0]
23+
cv.line(src, (l[0], l[1]), (l[2], l[3]), (255, 0, 0), 1, cv.LINE_AA)
24+
25+
# 显示
26+
cv.imshow("hough line demo", src)
27+
cv.imwrite("contours_analysis.png", src)
28+
cv.waitKey(0)
29+
cv.destroyAllWindows()
30+
31+

0 commit comments

Comments
 (0)