Skip to content

Commit

Permalink
fix all
Browse files Browse the repository at this point in the history
  • Loading branch information
makelove committed Jul 14, 2017
1 parent f5e6d58 commit 4a97075
Show file tree
Hide file tree
Showing 30 changed files with 62 additions and 45 deletions.
2 changes: 2 additions & 0 deletions ch04-图片/4.1_imread_imshow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# img = cv2.imread('messi5.jpg', cv2.IMREAD_GRAYSCALE)# Load an color image in grayscale 灰度
img = cv2.imread('messi5.jpg',cv2.IMREAD_UNCHANGED)#包括图像的 alpha 通道

img = cv2.resize(img, (640, 480))

# img.I
# AttributeError: 'numpy.ndarray' object has no attribute 'I'

Expand Down
4 changes: 2 additions & 2 deletions ch21-轮廓Contours/21.4.3-形状匹配.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
ret, thresh = cv2.threshold(img1, 127, 255, 0)
ret, thresh2 = cv2.threshold(img2, 127, 255, 0)

contours, hierarchy = cv2.findContours(thresh, 2, 1)
image,contours, hierarchy = cv2.findContours(thresh, 2, 1)
cnt1 = contours[0]
contours, hierarchy = cv2.findContours(thresh2, 2, 1)
image,contours, hierarchy = cv2.findContours(thresh2, 2, 1)
cnt2 = contours[0]

ret = cv2.matchShapes(cnt1, cnt2, 1, 0.0)
Expand Down
Binary file added ch21-轮廓Contours/star.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ch21-轮廓Contours/star2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
import cv2
import numpy as np

filename = '../data/chessboard.jpeg'
filename = '../data/chessboard.png'
# filename = '../data/chessboard-3.png'
# filename = '../data/corner-detection.jpg'

img = cv2.imread(filename)
img = cv2.resize(img, (640, 480))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)

Expand All @@ -28,7 +29,7 @@
# Threshold for an optimal value, it may vary depending on the image.
img[dst > 0.01 * dst.max()] = [0, 0, 255]

cv2.namedWindow('dst', cv2.WINDOW_NORMAL)
cv2.imshow('dst', img)

cv2.waitKey(0)
cv2.destroyAllWindows()
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
import cv2
from matplotlib import pyplot as plt

filename = '../data/corner-detection.jpg'
# filename = '../data/corner-detection.jpg'
filename = '../data/blox.jpg'
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Expand Down
5 changes: 3 additions & 2 deletions ch33-介绍SURF/surf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import cv2
import matplotlib.pyplot as plt

img = cv2.imread('fly.png', 0)
img = cv2.imread('../data/butterfly.jpg', 0)
# Create SURF object. You can specify params here or later. # Here I set Hessian Threshold to 400
# surf = cv2.SURF(400)
surf = cv2.xfeatures2d.SURF_create(400)
Expand Down Expand Up @@ -47,7 +47,8 @@
print(surf.getExtended())
# False
# So we make it to True to get 128-dim descriptors.
surf.extended = True
# surf.extended = True
surf.setExtended(True)
kp, des = surf.detectAndCompute(img, None)
print(surf.descriptorSize())
# 128
Expand Down
2 changes: 1 addition & 1 deletion ch34-角点检测的FAST算法/fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('simple.jpg', 0)
img = cv2.imread('../data/blox.jpg', 0)

# Initiate FAST object with default values
fast = cv2.FastFeatureDetector_create()
Expand Down
Binary file added ch34-角点检测的FAST算法/fast_false.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ch34-角点检测的FAST算法/fast_true.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion ch35-BRIEF/brief.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('simple.jpg', 0)
img = cv2.imread('../data/blox.jpg', 0)

# Initiate FAST detector
star = cv2.xfeatures2d.StarDetector_create()
Expand Down
2 changes: 1 addition & 1 deletion ch36-ORB/orb.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('simple.jpg', 0)
img = cv2.imread('../data/blox.jpg', 0)

# Initiate ORB detector
orb = cv2.ORB_create()
Expand Down
7 changes: 4 additions & 3 deletions ch37-特征匹配/37.2-对ORB描述符进行蛮力匹配.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import cv2
import matplotlib.pyplot as plt

img1 = cv2.imread('box.png', 0) # queryImage
img2 = cv2.imread('box_in_scene.png', 0) # trainImage
img1 = cv2.imread('../data/box.png', 0) # queryImage
img2 = cv2.imread('../data/box_in_scene.png', 0) # trainImage

# Initiate ORB detector
orb = cv2.ORB_create()
Expand All @@ -36,6 +36,7 @@
# Sort them in the order of their distance.
matches = sorted(matches, key=lambda x: x.distance)
# Draw first 10 matches.
img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches[:10], flags=2) # 前10个匹配
# img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches[:10], flags=2) # 前10个匹配
img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches[:10], None,flags=2) # 前10个匹配

plt.imshow(img3), plt.show()
2 changes: 1 addition & 1 deletion ch37-特征匹配/37.4-SIFT_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@
# img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, good[:10], img3, flags=2)

# cv2.drawMatchesKnn expects list of lists as matches.
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,flags=2)
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=2)

plt.imshow(img3), plt.show()
4 changes: 2 additions & 2 deletions ch37-特征匹配/37.5-FLANN匹配器.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import cv2
from matplotlib import pyplot as plt

img1 = cv2.imread('box.png', 0) # queryImage
img2 = cv2.imread('box_in_scene.png', 0) # trainImage
img1 = cv2.imread('../data/box.png', 0) # queryImage
img2 = cv2.imread('../data/box_in_scene.png', 0) # trainImage

# Initiate SIFT detector
# sift = cv2.SIFT()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from matplotlib import pyplot as plt

MIN_MATCH_COUNT = 10
img1 = cv2.imread('box.png', 0) # queryImage
img2 = cv2.imread('box_in_scene.png', 0) # trainImage
img1 = cv2.imread('../data/box.png', 0) # queryImage
img2 = cv2.imread('../data/box_in_scene.png', 0) # trainImage

# Initiate SIFT detector
sift = cv2.xfeatures2d.SIFT_create()
Expand Down Expand Up @@ -51,7 +51,7 @@
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
matchesMask = mask.ravel().tolist()
# 获得原图像的高和宽
h, w, d = img1.shape
h, w = img1.shape
# 使用得到的变换矩 对原图像的四个 变换 获得在目标图像上对应的坐标
pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2)
dst = cv2.perspectiveTransform(pts, M)
Expand All @@ -61,8 +61,7 @@
print("Not enough matches are found - %d/%d" % (len(good), MIN_MATCH_COUNT))
matchesMask = None


#最后我再绘制 inliers 如果能成功的找到目标图像的话 或者匹配的关 点 如果失败。
# 最后我再绘制 inliers 如果能成功的找到目标图像的话 或者匹配的关 点 如果失败。
draw_params = dict(matchColor=(0, 255, 0), # draw matches in green color
singlePointColor=None,
matchesMask=matchesMask, # draw only inliers
Expand All @@ -71,4 +70,4 @@
img3 = cv2.drawMatches(img1, kp1, img2, kp2, good, None, **draw_params)

plt.imshow(img3, 'gray'), plt.show()
#复杂图像中被找到的目标图像被标记成白色
# 复杂图像中被找到的目标图像被标记成白色
10 changes: 6 additions & 4 deletions ch39-视频分析-Meanshift和Camshift/Camshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@
# Draw it on image
pts = cv2.boxPoints(ret)
pts = np.int0(pts)
img2 = cv2.polylines(frame, [pts], True, 255, 2)
print('len pts:', len(pts),pts)
img2 = cv2.polylines(frame, [pts], True, (255, 0, 0), 2)

cv2.imshow('img2', img2)
k = cv2.waitKey(60) & 0xff
k = cv2.waitKey(1) # & 0xff
if k == 27:
break
else:
cv2.imwrite(chr(k) + ".jpg", img2)
# else:
# cv2.imwrite(chr(k) + ".jpg", img2)
else:
break
cv2.destroyAllWindows()
Expand Down
19 changes: 12 additions & 7 deletions ch39-视频分析-Meanshift和Camshift/Meanshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
而汽车由远及近 在视觉上 是一个 渐变大的过程
固定的窗口是不 合适的。所以我们需要根据目标的大小和角度来对窗口的大小和角度进行修订
http://docs.opencv.org/3.2.0/db/df8/tutorial_py_meanshift.html
"""

import numpy as np
import cv2

cap = cv2.VideoCapture('slow.flv')
cap = cv2.VideoCapture('../data/slow.flv')

# take first frame of the video
ret, frame = cap.read()
Expand All @@ -26,7 +27,7 @@
# set up the ROI for tracking
roi = frame[r:r + h, c:c + w]
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
#将低亮度的值忽略掉
# 将低亮度的值忽略掉
mask = cv2.inRange(hsv_roi, np.array((0., 60., 32.)), np.array((180., 255., 255.)))
roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0, 180])

Expand All @@ -37,22 +38,26 @@

while True:
ret, frame = cap.read()
if ret == True:
if ret is True:
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
# apply meanshift to get the new location
ret, track_window = cv2.meanShift(dst, track_window, term_crit)

# Draw it on image
x, y, w, h = track_window
print(track_window)
img2 = cv2.rectangle(frame, (x, y), (x + w, y + h), 255, 2)
cv2.imshow('img2', img2)
k = cv2.waitKey(60) & 0xff


k = cv2.waitKey(60) # & 0xff
if k == 27:
break
else:
cv2.imwrite(chr(k) + ".jpg", img2)
# else:
# cv2.imwrite(chr(k) + ".jpg", img2)
else:
break
cv2.destroyAllWindows()
cap.release()

#不正常
Binary file added ch39-视频分析-Meanshift和Camshift/q.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ch39-视频分析-Meanshift和Camshift/s.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ch39-视频分析-Meanshift和Camshift/ÿ.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import numpy as np
import cv2

cap = cv2.VideoCapture('slow.flv')
cap = cv2.VideoCapture('../data/slow.flv')

# params for ShiTomasi corner detection
feature_params = dict(maxCorners=100,
Expand Down Expand Up @@ -55,7 +55,8 @@
frame = cv2.circle(frame, (a, b), 5, color[i].tolist(), -1)
img = cv2.add(frame, mask)
cv2.imshow('frame', img)
k = cv2.waitKey(30) & 0xff

k = cv2.waitKey(30) #& 0xff
if k == 27:
break
# Now update the previous frame and previous points
Expand Down
7 changes: 5 additions & 2 deletions ch40-光流/40.4-OpenCV中的稠密光流.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import numpy as np

cap = cv2.VideoCapture("../data/vtest.avi")
# cap = cv2.VideoCapture("../data/slow.flv")
ret, frame1 = cap.read()

prvs = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
Expand All @@ -26,8 +27,10 @@
hsv[..., 0] = ang * 180 / np.pi / 2
hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
cv2.imshow('frame2', bgr)
k = cv2.waitKey(30) & 0xff

cv2.imshow('frame2', frame2)
cv2.imshow('flow', bgr)
k = cv2.waitKey(1) & 0xff
if k == 27:
break
elif k == ord('s'):
Expand Down
4 changes: 2 additions & 2 deletions ch41-背景减除/41.2-BackgroundSubtractorMOG.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
import cv2

cap = cv2.VideoCapture('../data/vtest.avi')
fgbg = cv2.bgsegm.createBackgroundSubtractorMOG() #TODO
fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()
# 可选参数 比如 进行建模场景的时间长度 高斯混合成分的数量-阈值等

while True:
ret, frame = cap.read()
fgmask = fgbg.apply(frame)

cv2.imshow('frame', fgmask)
k = cv2.waitKey(30) & 0xff
k = cv2.waitKey(1) #& 0xff
if k == 27:
break
cap.release()
Expand Down
2 changes: 1 addition & 1 deletion ch41-背景减除/41.3-BackgroundSubtractorMOG2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import numpy as np
import cv2

cap = cv2.VideoCapture('vtest.avi')
cap = cv2.VideoCapture('../data/vtest.avi')
fgbg = cv2.createBackgroundSubtractorMOG2()
while True:
ret, frame = cap.read()
Expand Down
6 changes: 3 additions & 3 deletions ch41-背景减除/41.4-BackgroundSubtractorGMG.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
import numpy as np
import cv2

cap = cv2.VideoCapture('vtest.avi')
cap = cv2.VideoCapture('../data/vtest.avi')
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
fgbg = cv2.createBackgroundSubtractorGMG()
fgbg = cv2.bgsegm.createBackgroundSubtractorGMG()
while True:
ret, frame = cap.read()
fgmask = fgbg.apply(frame)
fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)

cv2.imshow('frame', fgmask)
k = cv2.waitKey(30) & 0xff
k = cv2.waitKey(1) # & 0xff
if k == 27:
break
cap.release()
Expand Down
3 changes: 2 additions & 1 deletion ch42-摄像机标定/42.2.1-设置-findChessboardCorners.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.
images = glob.glob('*.jpg')
images = glob.glob('../data/left*.jpg')
images += glob.glob('../data/right*.jpg')
for fname in images:
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Expand Down
2 changes: 1 addition & 1 deletion ch43-姿势估计/calib3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def draw_cube(img, corners, imgpts):
'''
很 常一样我们 加 图像。搜寻 7x6 的格子 如果发现 我们就把它 优化到亚像素级。然后使用函数:cv2.solvePnPRansac() 来 算旋 和变 换。但我们有了变换矩 之后 我们就可以利用它们将 些坐标 点映射到图 像平 中去。简单来 我们在图像平 上找到了与 3D 空 中的点 3,0,0 ,(0,3,0),(0,0,3) 相对应的点。然后我们就可以使用我们的函数 draw() 从图像 上的第一个 点开始绘制 接 些点的直线了。搞定
'''
for fname in glob.glob('left*.jpg'):
for fname in glob.glob('../data/left*.jpg'):
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, (7, 6), None)
Expand Down

0 comments on commit 4a97075

Please sign in to comment.