Skip to content

Commit 95e7388

Browse files
committed
10-25
1 parent 73622d9 commit 95e7388

37 files changed

+877
-147
lines changed

.idea/workspace.xml

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

ch14/14.resize.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
res=cv2.resize(img,(2*width,2*height),interpolation=cv2.INTER_CUBIC)
1414

1515
while(1):
16-
cv2.imshow('res',res)
17-
cv2.imshow('img',img)
16+
cv2.imshow('resize',res)
17+
cv2.imshow('src img',img)
1818
if cv2.waitKey(1) & 0xFF == 27:
1919
break
2020
cv2.destroyAllWindows()

ch19-Canny-bian缘检测/19.Canny.py renamed to ch19-Canny-边缘检测/19.Canny.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import numpy as np
55
from matplotlib import pyplot as plt
66

7-
img = cv2.imread('messi5.jpg',0)
7+
img = cv2.imread('../data/messi5.jpg',0)
88
edges = cv2.Canny(img,100,200)
99

1010
plt.subplot(121),plt.imshow(img,cmap = 'gray')

ch20-Contours/21-findContour.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

ch21-Contours/21-findContour.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import numpy as np
4+
import cv2
5+
6+
# im = cv2.imread('test.jpg')#
7+
im = cv2.imread('../data/star.png')#contour.jpg
8+
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
9+
10+
ret,thresh = cv2.threshold(imgray,50,255,0)
11+
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
12+
print "contours size: ", len(contours)
13+
#img = cv2.drawContour(img, contours, -1, (0,255,0), 3)
14+
15+
img = cv2.drawContours(image, contours, 3, (0,0,255), 3)
16+
17+
cv2.namedWindow("contour.jpg",0)
18+
cv2.imshow("contour.jpg",img)
19+
cv2.waitKey(0)

ch21-Contours/21-moments.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#-*-coding:utf8-*-#
2+
__author__ = 'play4fun'
3+
"""
4+
create time:15-10-23 下午12:56
5+
"""
6+
7+
import cv2
8+
import numpy as np
9+
img = cv2.imread('../data/star.jpg',0)
10+
ret,thresh = cv2.threshold(img,127,255,0)
11+
# ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
12+
contours,hierarchy = cv2.findContours(thresh, 1, 2)
13+
# contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
14+
cnt = contours[0]
15+
M = cv2.moments(cnt)
16+
print M

ch22/hist-1.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#-*-coding:utf8-*-#
2+
__author__ = 'play4fun'
3+
"""
4+
create time:15-10-24 下午5:17
5+
"""
6+
7+
import cv2
8+
import numpy as np
9+
from matplotlib import pyplot as plt
10+
img = cv2.imread('../data/home.jpg',0)
11+
plt.hist(img.ravel(),256,[0,256]);
12+
plt.show()

ch22/hist-bgr.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#-*-coding:utf8-*-#
2+
__author__ = 'play4fun'
3+
"""
4+
create time:15-10-24 下午5:19
5+
"""
6+
7+
import cv2
8+
import numpy as np
9+
from matplotlib import pyplot as plt
10+
img = cv2.imread('../data/home.jpg')
11+
color = ('b','g','r')
12+
# 对一个列表或数组既要遍历索引又要遍历元素时
13+
# 使用内置 enumerrate 函数会有更加直接 优美的做法
14+
#enumerate 会将数组或列表组成一个索引序列。
15+
# 使我们再获取索引和索引内容的时候更加方便
16+
for i,col in enumerate(color):
17+
histr = cv2.calcHist([img],[i],None,[256],[0,256])
18+
plt.plot(histr,color = col)
19+
plt.xlim([0,256])
20+
plt.show()

ch22/hist-normalized-2.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#-*-coding:utf8-*-#
2+
__author__ = 'play4fun'
3+
"""
4+
create time:15-10-24 下午5:26
5+
"""
6+
7+
import cv2
8+
import numpy as np
9+
from matplotlib import pyplot as plt
10+
11+
img = cv2.imread('../data/contrast75.png',0)
12+
#flatten() 将数组变成一维
13+
hist,bins = np.histogram(img.flatten(),256,[0,256])
14+
#计算累积分布图
15+
cdf = hist.cumsum()
16+
17+
##
18+
# 构建 Numpy 掩模数组 cdf 为原数组 当数组元素为 0 时 掩盖(计算时被忽略
19+
cdf_m = np.ma.masked_equal(cdf,0)
20+
cdf_m = (cdf_m - cdf_m.min())*255/(cdf_m.max()-cdf_m.min())
21+
# 对被掩盖的元素赋值,赋值为 0
22+
cdf = np.ma.filled(cdf_m,0).astype('uint8')
23+
img2 = cdf[img]
24+
cv2.imshow("img2",img2)
25+
cv2.waitKey(0)
26+
27+
##
28+
#flatten() 将数组变成一维
29+
hist,bins = np.histogram(img2.flatten(),256,[0,256])
30+
#计算累积分布图
31+
cdf = hist.cumsum()
32+
cdf_normalized = cdf * hist.max()/ cdf.max()
33+
34+
plt.plot(cdf_normalized, color = 'b')
35+
plt.hist(img.flatten(),256,[0,256], color = 'r')
36+
plt.xlim([0,256])
37+
plt.legend(('cdf','histogram'), loc = 'upper left')
38+
plt.show()

ch22/hist-normalized.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#-*-coding:utf8-*-#
2+
__author__ = 'play4fun'
3+
"""
4+
create time:15-10-24 下午5:26
5+
"""
6+
7+
import cv2
8+
import numpy as np
9+
from matplotlib import pyplot as plt
10+
11+
img = cv2.imread('../data/contrast75.png',0)
12+
#flatten() 将数组变成一维
13+
hist,bins = np.histogram(img.flatten(),256,[0,256])
14+
#计算累积分布图
15+
cdf = hist.cumsum()
16+
cdf_normalized = cdf * hist.max()/ cdf.max()
17+
18+
plt.plot(cdf_normalized, color = 'b')
19+
plt.hist(img.flatten(),256,[0,256], color = 'r')
20+
plt.xlim([0,256])
21+
plt.legend(('cdf','histogram'), loc = 'upper left')
22+
plt.show()

0 commit comments

Comments
 (0)