Skip to content

Commit fb65478

Browse files
committed
update ch48
1 parent 485df60 commit fb65478

File tree

112 files changed

+752
-247
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+752
-247
lines changed

._.git

4 KB
Binary file not shown.

._.idea

4 KB
Binary file not shown.
4 KB
Binary file not shown.

.idea/._.name

4 KB
Binary file not shown.

.idea/.name

100644100755
File mode changed.

.idea/OpenCV-Python-Toturial.iml

100644100755
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

100644100755
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

100644100755
File mode changed.

.idea/vcs.xml

100644100755
File mode changed.

.idea/workspace.xml

100644100755
Lines changed: 452 additions & 242 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017/2/24 下午3:00
3+
# @Author : play4fun
4+
# @File : 48.2.2_仅有一个特征的数据.py
5+
# @Software: PyCharm
6+
7+
"""
8+
48.2.2_仅有一个特征的数据.py:
9+
"""
10+
11+
# 假 我们有一组数据 每个数据只有一个特征 1 维 。例如前 的 T 恤 我们只使用人们的 来决定 T 恤的大小。
12+
# 我们先来产生一些 机数据 并使用 Matplotlib 将它们绘制出来。
13+
14+
import numpy as np
15+
import cv2
16+
from matplotlib import pyplot as plt
17+
18+
x = np.random.randint(25, 100, 25)
19+
y = np.random.randint(175, 255, 25)
20+
z = np.hstack((x, y))
21+
z = z.reshape((50, 1))
22+
z = np.float32(z)
23+
plt.hist(z, 256, [0, 256]), plt.show()
24+
# 现在我们有一个 度为 50 取值范围为 0 到 255 的向 z。我已经将向 z 了 排 将它变成了一个列向 。当每个数据含有多个特征是 会很 有用。然后我们数据类型 换成 np.float32。
25+
26+
##
27+
28+
# 现在我们使用KMeans函数。在之前我们应先置好终止条件。我的终止条件是算法执10次代或者精确度epsilon=1.0。
29+
30+
# Define criteria = ( type, max_iter = 10 , epsilon = 1.0 )
31+
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
32+
# Set flags (Just to avoid line break in the code)
33+
34+
flags = cv2.KMEANS_RANDOM_CENTERS
35+
# Apply KMeans
36+
compactness, labels, centers = cv2.kmeans(z, 2, None, criteria, 10, flags)
37+
# 返回值有紧密度compactness,标志和中心。在本例中我的到的中心是60和207。标志的数目与测数据的多少是相同的每个数据会标上01等。取决与它们的中心是什么。现在我们可以根据它们的标志将把数据分两组。现在将A组数用红色示将B组数据用蓝色示心用色示。
38+
A = z[labels == 0]
39+
B = z[labels == 1]
40+
# Now plot 'A' in red, 'B' in blue, 'centers' in yellow
41+
plt.hist(A, 256, [0, 256], color='r')
42+
plt.hist(B, 256, [0, 256], color='b')
43+
plt.hist(centers, 32, [0, 256], color='y')
44+
plt.show()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017/2/24 下午3:17
3+
# @Author : play4fun
4+
# @File : 48.2.3_颜色量化.py
5+
# @Software: PyCharm
6+
7+
"""
8+
48.2.3_颜色量化.py:
9+
"""
10+
11+
# 色 化就是减少图片中 色数目的一个 程。为什么 减少图片中的 色呢 减少内存消耗 有些 备的 源有 只能显示很少的 色。在 种情 况下就 色 化。我们使用 K 值聚类的方法来 色 化。
12+
# 没有什么新的知 介绍了。现在有 3 个特征 R G B。所以我们 把图片数据变形成 Mx3 M 是图片中像素点的数目 的向 。聚类完成后 我们用聚类中心值替换与其同组的像素值 样结果图片就只含有指定数目的 色了。下 是代码
13+
# -*- coding: utf-8 -*-
14+
15+
import numpy as np
16+
import cv2
17+
18+
# img = cv2.imread('../data/home.jpg')
19+
img = cv2.imread('../data/opencv_logo.png')
20+
Z = img.reshape((-1, 3))
21+
# convert to np.float32
22+
Z = np.float32(Z)
23+
24+
25+
# define criteria, number of clusters(K) and apply kmeans()
26+
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
27+
# K = 8
28+
# K = 3
29+
K = 14
30+
ret, label, center = cv2.kmeans(Z, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
31+
32+
33+
# Now convert back into uint8, and make original image
34+
center = np.uint8(center)
35+
res = center[label.flatten()]
36+
res2 = res.reshape((img.shape))
37+
38+
cv2.imshow('res2', res2)
39+
cv2.waitKey(0)
40+
cv2.destroyAllWindows()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017/2/24 下午3:17
3+
# @Author : play4fun
4+
# @File : 48.2.3_颜色量化.py
5+
# @Software: PyCharm
6+
7+
"""
8+
48.2.3_颜色量化.py:
9+
"""
10+
11+
# 色 化就是减少图片中 色数目的一个 程。为什么 减少图片中的 色呢 减少内存消耗 有些 备的 源有 只能显示很少的 色。在 种情 况下就 色 化。我们使用 K 值聚类的方法来 色 化。
12+
# 没有什么新的知 介绍了。现在有 3 个特征 R G B。所以我们 把图片数据变形成 Mx3 M 是图片中像素点的数目 的向 。聚类完成后 我们用聚类中心值替换与其同组的像素值 样结果图片就只含有指定数目的 色了。下 是代码
13+
# -*- coding: utf-8 -*-
14+
15+
import numpy as np
16+
import cv2
17+
18+
# img = cv2.imread('../data/home.jpg')
19+
img = cv2.imread('../data/opencv_logo.png')
20+
Z = img.reshape((-1, 3))
21+
# convert to np.float32
22+
Z = np.float32(Z)
23+
24+
25+
# define criteria, number of clusters(K) and apply kmeans()
26+
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
27+
K = 8
28+
# K = 3
29+
# K = 14
30+
ret, label, center = cv2.kmeans(Z, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
31+
32+
#分离颜色
33+
for y in range(len(center)):
34+
a1 = []
35+
for i,x in enumerate(label.ravel()):
36+
if x==y:
37+
a1.append(list(Z[i]))
38+
else:
39+
a1.append([0,0,0])
40+
a2=np.array(a1)
41+
a3=a2.reshape((img.shape))
42+
cv2.imshow('res2'+str(y), a3)
43+
44+
#最大的色块
45+
46+
47+
48+
# # Now convert back into uint8, and make original image
49+
# center = np.uint8(center)
50+
# res = center[label.flatten()]
51+
# res2 = res.reshape((img.shape))
52+
53+
# cv2.imshow('res2', res2)
54+
# cv2.imshow('res2', a3)
55+
cv2.waitKey(0)
56+
cv2.destroyAllWindows()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017/2/24 下午3:14
3+
# @Author : play4fun
4+
# @File : 含有多个特征的数据.py
5+
# @Software: PyCharm
6+
7+
"""
8+
含有多个特征的数据.py:
9+
"""
10+
11+
# 在前 的 T 恤例子中我们只考 了 现在我们也把体 考 去 也 就是两个特征。
12+
# 在前一节我们的数据是一个单列向 。每一个特征 排列成一列 每一 对应一个测 样本。
13+
# 在本例中我们的测 数据 应 50x2 的向 其中包含 50 个人的 和 体 。第一列对应与 第二列对应与体 。第一 包含两个元素 第一个 是第一个人的 第二个是第一个人的体 。剩下的 对应与其他人的 和体 。
14+
15+
import numpy as np
16+
import cv2
17+
from matplotlib import pyplot as plt
18+
19+
X = np.random.randint(25, 50, (25, 2))
20+
Y = np.random.randint(60, 85, (25, 2))
21+
Z = np.vstack((X, Y))
22+
# convert to np.float32
23+
Z = np.float32(Z)
24+
25+
# define criteria and apply kmeans()
26+
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
27+
ret, label, center = cv2.kmeans(Z, 2, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
28+
# Now separate the data, Note the flatten()
29+
A = Z[label.ravel() == 0]
30+
B = Z[label.ravel() == 1]
31+
32+
# Plot the data
33+
plt.scatter(A[:, 0], A[:, 1])
34+
plt.scatter(B[:, 0], B[:, 1], c='r')
35+
plt.scatter(center[:, 0], center[:, 1], s=80, c='y', marker='s')
36+
plt.xlabel('Height'), plt.ylabel('Weight')
37+
plt.show()

OpenCV-Python-Toturial-中文版.pdf

100644100755
File mode changed.

README.md

100644100755
File mode changed.

README.md~

100644100755
File mode changed.

__init__.py

100644100755
File mode changed.

ch10/10.addWeighted.py

100644100755
File mode changed.

ch10/10.bitwise_and.py

100644100755
File mode changed.

ch11/11.getTickCount.py

100644100755
File mode changed.

ch11/11.useOptimized.py

100644100755
File mode changed.

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

100644100755
File mode changed.

ch13-颜色空间转换/13.cvtColor_flag.py

100644100755
File mode changed.

ch13-颜色空间转换/13.find_object_hsv.py

100644100755
File mode changed.

ch14/14.getAffineTransform.py

100644100755
File mode changed.

ch14/14.getPerspectiveTransform.py

100644100755
File mode changed.

ch14/14.getRotationMatrix2D.py

100644100755
File mode changed.

ch14/14.resize.py

100644100755
File mode changed.

ch14/14.wrapAffine.py

100644100755
File mode changed.

ch15/15.THRESH_OTSU.py

100644100755
File mode changed.

ch15/15.adaptiveThreshold.py

100644100755
File mode changed.

ch15/15.threshold.py

100644100755
File mode changed.

ch15/__init__.py

100644100755
File mode changed.

ch16/16.filter2D.py

100644100755
File mode changed.

ch17-形态学zhuan换/17.erode.py

100644100755
File mode changed.

ch18-图像梯度/19.Sobel.py

100644100755
File mode changed.

ch19-Canny-边缘检测/19.Canny.py

100644100755
File mode changed.

ch20-图像jin字塔/20.Apple_orange.py

100644100755
File mode changed.

ch20-图像jin字塔/__init__.py

100644100755
File mode changed.

ch21-Contours/21-findContour.py

100644100755
File mode changed.

ch21-Contours/21-moments.py

100644100755
File mode changed.
File renamed without changes.
File renamed without changes.
File renamed without changes.

ch22/hist-normalized-2.py renamed to ch22-直方图/hist-normalized-2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
# 对被掩盖的元素赋值,赋值为 0
2222
cdf = np.ma.filled(cdf_m,0).astype('uint8')
2323
img2 = cdf[img]
24-
cv2.imshow("img2",img2)
25-
cv2.waitKey(0)
24+
# cv2.imshow("img2",img2)
25+
# cv2.waitKey(0)
2626

2727
##
2828
#flatten() 将数组变成一维
File renamed without changes.

ch22-直方图/hist.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/usr/bin/env python
2+
3+
''' This is a sample for histogram plotting for RGB images and grayscale images for better understanding of colour distribution
4+
5+
Benefit : Learn how to draw histogram of images
6+
Get familier with cv2.calcHist, cv2.equalizeHist,cv2.normalize and some drawing functions
7+
8+
Level : Beginner or Intermediate
9+
10+
Functions : 1) hist_curve : returns histogram of an image drawn as curves
11+
2) hist_lines : return histogram of an image drawn as bins ( only for grayscale images )
12+
13+
Usage : python hist.py <image_file>
14+
15+
Abid Rahman 3/14/12 debug Gary Bradski
16+
'''
17+
18+
import cv2
19+
import numpy as np
20+
21+
bins = np.arange(256).reshape(256,1)
22+
23+
def hist_curve(im):
24+
h = np.zeros((300,256,3))
25+
if len(im.shape) == 2:
26+
color = [(255,255,255)]
27+
elif im.shape[2] == 3:
28+
color = [ (255,0,0),(0,255,0),(0,0,255) ]
29+
for ch, col in enumerate(color):
30+
hist_item = cv2.calcHist([im],[ch],None,[256],[0,256])
31+
cv2.normalize(hist_item,hist_item,0,255,cv2.NORM_MINMAX)
32+
hist=np.int32(np.around(hist_item))
33+
pts = np.int32(np.column_stack((bins,hist)))
34+
cv2.polylines(h,[pts],False,col)
35+
y=np.flipud(h)
36+
return y
37+
38+
def hist_lines(im):
39+
h = np.zeros((300,256,3))
40+
if len(im.shape)!=2:
41+
print "hist_lines applicable only for grayscale images"
42+
#print "so converting image to grayscale for representation"
43+
im = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
44+
hist_item = cv2.calcHist([im],[0],None,[256],[0,256])
45+
cv2.normalize(hist_item,hist_item,0,255,cv2.NORM_MINMAX)
46+
hist=np.int32(np.around(hist_item))
47+
for x,y in enumerate(hist):
48+
cv2.line(h,(x,0),(x,y),(255,255,255))
49+
y = np.flipud(h)
50+
return y
51+
52+
53+
if __name__ == '__main__':
54+
55+
import sys
56+
57+
if len(sys.argv)>1:
58+
fname = sys.argv[1]
59+
else :
60+
fname = '../data/lena.jpg'
61+
print "usage : python hist.py <image_file>"
62+
63+
im = cv2.imread(fname)
64+
65+
if im is None:
66+
print 'Failed to load image file:', fname
67+
sys.exit(1)
68+
69+
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
70+
71+
72+
print ''' Histogram plotting \n
73+
Keymap :\n
74+
a - show histogram for color image in curve mode \n
75+
b - show histogram in bin mode \n
76+
c - show equalized histogram (always in bin mode) \n
77+
d - show histogram for color image in curve mode \n
78+
e - show histogram for a normalized image in curve mode \n
79+
Esc - exit \n
80+
'''
81+
82+
cv2.imshow('image',im)
83+
while True:
84+
k = cv2.waitKey(0)&0xFF
85+
if k == ord('a'):
86+
curve = hist_curve(im)
87+
cv2.imshow('histogram',curve)
88+
cv2.imshow('image',im)
89+
print 'a'
90+
elif k == ord('b'):
91+
print 'b'
92+
lines = hist_lines(im)
93+
cv2.imshow('histogram',lines)
94+
cv2.imshow('image',gray)
95+
elif k == ord('c'):
96+
print 'c'
97+
equ = cv2.equalizeHist(gray)
98+
lines = hist_lines(equ)
99+
cv2.imshow('histogram',lines)
100+
cv2.imshow('image',equ)
101+
elif k == ord('d'):
102+
print 'd'
103+
curve = hist_curve(gray)
104+
cv2.imshow('histogram',curve)
105+
cv2.imshow('image',gray)
106+
elif k == ord('e'):
107+
print 'e'
108+
norm=np.ndarray((2,2))
109+
norm = cv2.normalize(gray,norm,alpha = 0,beta = 255,norm_type = cv2.NORM_MINMAX)
110+
111+
lines = hist_lines(norm)
112+
cv2.imshow('histogram',lines)
113+
cv2.imshow('image',norm)
114+
elif k == 27:
115+
print 'ESC'
116+
cv2.destroyAllWindows()
117+
break
118+
cv2.destroyAllWindows()
File renamed without changes.

ch23/fftshift-abs.py

100644100755
File mode changed.

ch23/fftshift.py

100644100755
File mode changed.

ch24/Multiple-Objects.py

100644100755
File mode changed.

ch24/matchTemplate.py

100644100755
File mode changed.

ch25/HoughLines.py

100644100755
File mode changed.

ch25/HoughLinesP.py

100644100755
File mode changed.

ch26-Hough圆环变换/HoughCircles.py

100644100755
File mode changed.

ch26-Hough圆环变换/__init__.py

100644100755
File mode changed.

ch27-分水岭算法图像分割/threshold.py

100644100755
File mode changed.

ch28-使用GrabCut算法进行交互式前景提取/grabCut.py

100644100755
File mode changed.

ch28-使用GrabCut算法进行交互式前景提取/grabCut2.py

100644100755
File mode changed.

ch30-Harris角点检测/cornerHarris.py

100644100755
File mode changed.

ch30-Harris角点检测/cornerSubPix.py

100644100755
File mode changed.

ch30-Harris角点检测/subpixel5.png

100644100755
File mode changed.

ch31-Shi-Tomasi角点检测/goodFeaturesToTrack.py

100644100755
File mode changed.

ch37-特征匹配/37.4-SIFT_match.py

100644100755
File mode changed.

ch4/4.1_imread_imshow.py

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
# Load an color image in grayscale
99
img = cv2.imread('messi5.jpg',0)
10-
10+
img.I
1111
cv2.namedWindow('image',cv2.WINDOW_NORMAL)
1212
cv2.imshow('image',img)
1313
cv2.waitKey(0)

ch4/4.imread_imshow_imwrite.py

100644100755
File mode changed.

ch4/4.matplotlib.py

100644100755
File mode changed.

ch4/messi5.jpg

100644100755
File mode changed.

ch4/messigray.png

100644100755
File mode changed.

ch5/5.VideoCapture.py

100644100755
File mode changed.

ch5/5.VideoPlay.py

100644100755
File mode changed.

ch5/5.VideoWriter.py

100644100755
File mode changed.

ch5/__init__.py

100644100755
File mode changed.

ch51-对xiang检测/51.CascadeClassifier.py

100644100755
File mode changed.

ch51-对xiang检测/51.CascadeClassifier.py~

100644100755
File mode changed.

ch6/6.draw.py

100644100755
File mode changed.

ch7/7.MouseCallback.py

100644100755
File mode changed.

ch7/7.draw_circle_rectangle.py

100644100755
File mode changed.

ch8/8.Trackbar_draw.py

100644100755
File mode changed.

ch8/8.createTrackbar.py

100644100755
File mode changed.

ch9/9.MakeBorder.py

100644100755
File mode changed.

ch9/9.img_roi.py

100644100755
File mode changed.

ch9/9.itemset.py

100644100755
File mode changed.

ch9/9.shape.py

100644100755
File mode changed.

ch9/9.split_color.py

100644100755
File mode changed.

data/._messi5.jpg

4 KB

data/OpenCV_Logo_with_text.png

100644100755
File mode changed.

data/black-white-rect.png

100644100755
File mode changed.

data/box.png

100644100755
File mode changed.

data/box_in_scene.png

100644100755
File mode changed.

data/chessboard-2.png

100644100755
File mode changed.

data/chessboard-3.png

100644100755
File mode changed.

data/chessboard.jpeg

100644100755
File mode changed.

data/contour.jpg

100644100755
File mode changed.

data/contrast75.png

100644100755
File mode changed.

data/corner-detection.jpg

100644100755
File mode changed.

data/home.jpg

100644100755
File mode changed.

data/kongjie_hezhao.jpg

100644100755
File mode changed.

data/lena.jpg

89.7 KB

data/mario.png

100644100755
File mode changed.

data/mario_coin.png

100644100755
File mode changed.

data/messi5.jpg

100644100755
File mode changed.

data/messi_face.jpg

100644100755
File mode changed.

data/ml.png

100644100755
File mode changed.

data/newmask.jpg

100644100755
File mode changed.

data/opencv_logo.png

100644100755
File mode changed.

data/sachin.jpg

100644100755
File mode changed.

data/star.png

100644100755
File mode changed.

data/sudoku-1.jpg

100644100755
File mode changed.

data/sudoku.jpg

100644100755
File mode changed.

data/water_coins.jpg

100644100755
File mode changed.

0 commit comments

Comments
 (0)