Skip to content

Commit 485df60

Browse files
committed
update 11-13
1 parent d439dad commit 485df60

File tree

20 files changed

+400
-146
lines changed

20 files changed

+400
-146
lines changed

.idea/workspace.xml

Lines changed: 232 additions & 136 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch13/13.VideoCapture_blue_object.py renamed to ch13-颜色空间转换/13.VideoCapture_blue_object.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@
99
# 换到 HSV
1010
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
1111
# 定蓝色的 值
12-
lower_blue = np.array([110, 50, 50])
13-
upper_blue = np.array([130, 255, 255])
12+
# lower_blue = np.array([110, 50, 50])
13+
# upper_blue = np.array([130, 255, 255])
14+
15+
#黑色
16+
lower_black = np.array([0, 0, 0])
17+
upper_black = np.array([180, 255, 30])
1418

1519
# 根据 值构建掩模
16-
mask = cv2.inRange(hsv, lower_blue, upper_blue)
20+
# mask = cv2.inRange(hsv, lower_blue, upper_blue)
21+
mask = cv2.inRange(hsv, lower_black, upper_black)
1722
# 对原图像和掩模位 算
1823
res = cv2.bitwise_and(frame, frame, mask=mask)
1924
# 显示图像
@@ -23,5 +28,5 @@
2328
k = cv2.waitKey(5) & 0xFF
2429
if k == 27:
2530
break
26-
# 关 窗口
31+
# 关闭窗口
2732
cv2.destroyAllWindows()
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@
1919
green=np.uint8([[[0,255,0]]])
2020
hsv_green=cv2.cvtColor(green,cv2.COLOR_BGR2HSV)
2121
print hsv_green
22-
#[[[60 255 255]]]
22+
#[[[60 255 255]]]
23+
24+
black=np.uint8([[[0,0,0]]])
25+
hsv_black=cv2.cvtColor(black,cv2.COLOR_BGR2HSV)
26+
print hsv_black
27+
#[[[0 0 0]]]

ch21-Contours/21-findContour.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
import cv2
55

66
# im = cv2.imread('test.jpg')#
7-
im = cv2.imread('../data/star.png')#contour.jpg
7+
# im = cv2.imread('../data/black-white-rect.png')#contour.jpg #
8+
im = cv2.imread('../data/chessboard.jpeg')
89
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
910

10-
ret,thresh = cv2.threshold(imgray,50,255,0)
11+
ret,thresh = cv2.threshold(imgray,0,25,0)
1112
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
1213
print "contours size: ", len(contours)
1314
#img = cv2.drawContour(img, contours, -1, (0,255,0), 3)
1415

15-
img = cv2.drawContours(image, contours, 3, (0,0,255), 3)
16+
img = cv2.drawContours(image, contours, 3, (255,0,0), 3)
1617

1718
cv2.namedWindow("contour.jpg",0)
1819
cv2.imshow("contour.jpg",img)

ch21-Contours/21-moments.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
import cv2
88
import numpy as np
9-
img = cv2.imread('../data/star.jpg',0)
9+
img = cv2.imread('../data/star.png',0)
1010
ret,thresh = cv2.threshold(img,127,255,0)
1111
# ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
12-
contours,hierarchy = cv2.findContours(thresh, 1, 2)
12+
# contours,hierarchy = cv2.findContours(thresh, 1, 2)
13+
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
1314
# contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
1415
cnt = contours[0]
1516
M = cv2.moments(cnt)

ch22/22.3.4.hsv_hist.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#-*-coding:utf8-*-#
2+
__author__ = 'play4fun'
3+
"""
4+
create time:15-11-8 下午4:44
5+
"""
6+
7+
8+
9+
import cv2
10+
import numpy as np
11+
from matplotlib import pyplot as plt
12+
img = cv2.imread('../data/home.jpg')
13+
cv2.imshow("src",img)
14+
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
15+
hist = cv2.calcHist( [hsv], [0, 1], None, [180, 256], [0, 180, 0, 256] )
16+
plt.imshow(hist,interpolation = 'nearest')
17+
plt.show()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#-*-coding:utf8-*-#
2+
__author__ = 'play4fun'
3+
"""
4+
create time:15-10-28 下午7:27
5+
"""
6+
7+
import cv2
8+
import numpy as np
9+
# filename = '../data/chessboard-3.png'
10+
filename = '../data/corner-detection.jpg'
11+
12+
img = cv2.imread(filename)
13+
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
14+
gray = np.float32(gray)
15+
16+
#输入图像必 是 float32 最后一个参数在 0.04 到 0.05 之间
17+
dst = cv2.cornerHarris(gray,2,3,0.04)
18+
#result is dilated for marking the corners, not important
19+
dst = cv2.dilate(dst,None)
20+
# Threshold for an optimal value, it may vary depending on the image.
21+
img[dst>0.01*dst.max()]=[0,0,255]
22+
23+
cv2.imshow('dst',img)
24+
if cv2.waitKey(0) & 0xff == 27:
25+
cv2.destroyAllWindows()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# -*-coding:utf8-*-#
2+
__author__ = 'play4fun'
3+
"""
4+
create time:15-10-29 上午7:47
5+
"""
6+
7+
import cv2
8+
import numpy as np
9+
10+
filename = '../data/chessboard-2.png'
11+
img = cv2.imread(filename)
12+
13+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
14+
# find Harris corners
15+
gray = np.float32(gray)
16+
dst = cv2.cornerHarris(gray, 2, 3, 0.04)
17+
dst = cv2.dilate(dst, None)
18+
ret, dst = cv2.threshold(dst, 0.01 * dst.max(), 255, 0)
19+
dst = np.uint8(dst)
20+
21+
# find centroids
22+
# connectedComponentsWithStats(InputArray image, OutputArray labels, OutputArray stats,
23+
# OutputArray centroids, int connectivity=8, int ltype=CV_32S)
24+
ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
25+
# define the criteria to stop and refine the corners
26+
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
27+
# Python: cv2.cornerSubPix(image, corners, winSize, zeroZone, criteria)
28+
# zeroZone – Half of the size of the dead region in the middle of the search zone
29+
# over which the summation in the formula below is not done. It is used sometimes
30+
# to avoid possible singularities of the autocorrelation matrix. The value of (-1,-1)
31+
# indicates that there is no such a size.
32+
# 返回值由 点坐标组成的一个数组 而 图像
33+
corners = cv2.cornerSubPix(gray, np.float32(centroids), (5, 5), (-1, -1), criteria)
34+
# Now draw them
35+
res = np.hstack((centroids, corners))
36+
# np.int0 可以用来省略小数点后 的数字,非四舍五入
37+
res = np.int0(res)
38+
img[res[:, 1], res[:, 0]] = [0, 0, 255]
39+
img[res[:, 3], res[:, 2]] = [0, 255, 0]
40+
41+
# cv2.imwrite('subpixel5.png',img)
42+
cv2.imshow('subpixel5.png', img)
43+
cv2.waitKey(0)
85.6 KB
Loading

0 commit comments

Comments
 (0)