def visualize_homo(img1, img2, kp1, kp2, matches, homo, mask):
h, w, d = img1.shape
pts = [[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]
pts = np.array(pts, dtype=np.float32).reshape((-1, 1, 2))
dst = cv.perspectiveTransform(pts, homo)
img2 = cv.polylines(img2, [np.int32(dst)], True, [255, 0, 0], 3, 8)
matches_mask = mask.ravel().tolist()
draw_params = dict(matchesMask=matches_mask,
singlePointColor=None,
matchColor=(0, 255, 0),
flags=2)
res = cv.drawMatches(img1, kp1, img2, kp2, matches, None, **draw_params)
return res
python类drawMatches()的实例源码
def drawMatches(self, MarkImage, SceneImage):
outImg = cv2.drawMatches(MarkImage,self.KP1,
SceneImage,self.KP2,
self.GoodMatches,None,**self.DrawParams)
return outImg
def visualize_matches(image1, keypoints1, image2, keypoints2, matches):
m_image = np.array([])
m_image = cv2.drawMatches(
image1, keypoints1,
image2, keypoints2,
[match[0] for match in matches],
m_image)
cv2.imshow(PROJ_WIN, m_image)
wait()
def main():
stream=urllib.urlopen(CAM_URL)
bytes=''
ts=time.time()
while True:
bytes+=stream.read(2048)
a = bytes.find('\xff\xd8')
b = bytes.find('\xff\xd9')
if a==-1 or b==-1:
continue
# Frame available
rtimestamp=time.time()
jpg = bytes[a:b+2]
bytes= bytes[b+2:]
img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.IMREAD_COLOR)
cv2.imshow('RAW',img)
#ORB to get corresponding points
kp, des = orb.detectAndCompute(img,None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des_ref,des)
matches = sorted(matches, key = lambda x:x.distance)
img3 = cv2.drawMatches(img_ref,kp_ref,img,kp,matches[:4], None,flags=2)
cv2.imshow('Matches',img3)
# pts_src = np.float32([[kp_ref[0].pt[0],kp_ref[0].pt[1]],[kp_ref[1].pt[0],kp_ref[1].pt[1]],[kp_ref[0].pt[0],kp_ref[0].pt[1]],[kp_ref[0].pt[0],kp_ref[0].pt[1]]
# Perspective Transform
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,50],[100,250]])
Tr_M = cv2.getAffineTransform(pts1,pts2)
oimg = cv2.warpAffine(img,Tr_M,(cols,rows))
cv2.imshow('Perspective Transform',oimg)
# Print lag
print(time.time()-ts)
ts=time.time()
if cv2.waitKey(1) == 27:
exit(0)
def drawMatches(self, MarkImage, SceneImage):
outImg = cv2.drawMatches(MarkImage,self.KP1,
SceneImage,self.KP2,
self.GoodMatches,None,**self.DrawParams)
return outImg
def debugMatches():
# Debug module.
# from matplotlib import pyplot as plt
markImage = cv2.imread('./clock.png')
sceneImage = cv2.imread('./clock_in_scene.png')
# Init PM.
pm = GetPMatrix(markImage)
# Get kp1, kp2, dst, goodMatches, [draw_params].
dst = pm.getMatches(sceneImage)
if dst is None:
exit()
# Draw circles and lines.
img3 = pm.drawMatches(markImage, sceneImage)
# # Get ret, mtx, dist, rvecs, tvecs
tmp = None
for i in range(30):
tmp = pm.getP(dst)
if tmp is None:
exit()
print i
mtx, dist, rvec, tvec = tmp
# Draw Box
h,w = markImage.shape[:2]
img3[:,w:] = pm.drawBox(img3[:,w:])
h2,w2 = sceneImage.shape[:2]
glP = pm.getGLP(w2, h2)
glM = pm.getGLM()
print 'mtx -------------'
print mtx
print 'dist ------------'
print dist
print 'rvec -----------'
print rvec
print 'tvec -----------'
print tvec
print 'glP ------------'
print glP
print 'glM ------------'
print glM
img3 = cv2.cvtColor(img3, cv2.COLOR_BGR2RGB)
plt.figure('Matches test.'), plt.imshow(img3)
def debugMatches():
# Debug module.
# from matplotlib import pyplot as plt
markImage = cv2.imread('./clock.png')
sceneImage = cv2.imread('./clock_in_scene.png')
# Init PM.
pm = GetPMatrix(markImage)
# Get kp1, kp2, dst, goodMatches, [draw_params].
dst = pm.getMatches(sceneImage)
if dst is None:
exit()
# Draw circles and lines.
img3 = pm.drawMatches(markImage, sceneImage)
# # Get ret, mtx, dist, rvecs, tvecs
tmp = None
for i in range(30):
tmp = pm.getP(dst)
if tmp is None:
exit()
print i
mtx, dist, rvec, tvec = tmp
# Draw Box
h,w = markImage.shape[:2]
img3[:,w:] = pm.drawBox(img3[:,w:])
h2,w2 = sceneImage.shape[:2]
glP = pm.getGLP(w2, h2)
glM = pm.getGLM()
print 'mtx -------------'
print mtx
print 'dist ------------'
print dist
print 'rvec -----------'
print rvec
print 'tvec -----------'
print tvec
print 'glP ------------'
print glP
print 'glM ------------'
print glM
img3 = cv2.cvtColor(img3, cv2.COLOR_BGR2RGB)
plt.figure('Matches test.'), plt.imshow(img3)