資源簡介
AR、增強現實的視頻實現的完整代碼,用電腦攝像頭直接實現。親測可用

代碼片段和文件信息
#?Useful?links
#?http://www.pygame.org/wiki/OBJFileLoader
#?https://rdmilligan.wordpress.com/2015/10/15/augmented-reality-using-opencv-opengl-and-blender/
#?https://clara.io/library
#?TODO?->?Implement?command?line?arguments?(scale?model?and?object?to?be?projected)
#??????->?Refactor?and?organize?code?(proper?funcition?definition?and?separation?classes?error?handling...)
import?argparse
import?cv2
import?numpy?as?np
import?math
import?os
from?objloader_simple?import?*
#?Minimum?number?of?matches?that?have?to?be?found
#?to?consider?the?recognition?valid
MIN_MATCHES?=?10??
def?main():
????“““
????This?functions?loads?the?target?surface?image
????“““
????homography?=?None?
????#?matrix?of?camera?parameters?(made?up?but?works?quite?well?for?me)?
????camera_parameters?=?np.array([[800?0?320]?[0?800?240]?[0?0?1]])
????#?create?ORB?keypoint?detector
????orb?=?cv2.ORB_create()
????#?create?BFMatcher?object?based?on?hamming?distance??
????bf?=?cv2.BFMatcher(cv2.NORM_HAMMING?crossCheck=True)
????#?load?the?reference?surface?that?will?be?searched?in?the?video?stream
????dir_name?=?‘E:/PycharmProjects/augmented-reality/‘
????model?=?cv2.imread(os.path.join(dir_name?‘reference/front.jpg‘)?0)
????#?Compute?model?keypoints?and?its?descriptors
????kp_model?des_model?=?orb.detectAndCompute(model?None)
????#?Load?3D?model?from?OBJ?file
????obj?=?OBJ(os.path.join(dir_name?‘models/rat.obj‘)?swapyz=True)
????#?init?video?capture
????cap?=?cv2.VideoCapture(0)
????while?True:
????????#?read?the?current?frame
????????ret?frame?=?cap.read()
????????if?not?ret:
????????????print?(“Unable?to?capture?video“)
????????????return?
????????#?find?and?draw?the?keypoints?of?the?frame
????????kp_frame?des_frame?=?orb.detectAndCompute(frame?None)
????????#?match?frame?descriptors?with?model?descriptors
????????matches?=?bf.match(des_model?des_frame)
????????#?sort?them?in?the?order?of?their?distance
????????#?the?lower?the?distance?the?better?the?match
????????matches?=?sorted(matches?key=lambda?x:?x.distance)
????????#?compute?Homography?if?enough?matches?are?found
????????if?len(matches)?>?MIN_MATCHES:
????????????#?differenciate?between?source?points?and?destination?points
????????????src_pts?=?np.float32([kp_model[m.queryIdx].pt?for?m?in?matches]).reshape(-1?1?2)
????????????dst_pts?=?np.float32([kp_frame[m.trainIdx].pt?for?m?in?matches]).reshape(-1?1?2)
????????????#?compute?Homography
????????????homography?mask?=?cv2.findHomography(src_pts?dst_pts?cv2.RANSAC?5.0)
????????????if?args.rectangle:
????????????????#?Draw?a?rectangle?that?marks?the?found?model?in?the?frame
????????????????h?w?=?model.shape
????????????????pts?=?np.float32([[0?0]?[0?h?-?1]?[w?-?1?h?-?1]?[w?-?1?0]]).reshape(-1?1?2)
????????????????#?project?corners?into?frame
????????????????dst?=?cv2.perspectiveTransform(pts?homography)
????????????????#?connect?them?with?lines??
????????????????frame?=?cv2.polylines(frame?[np.int32(dst)]?True?255?3?cv2.LINE_
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????1203??2019-01-05?09:36??augmented-reality\.gitignore
?????文件????????501??2019-04-07?13:17??augmented-reality\.idea\augmented-reality-master.iml
?????文件????????138??2019-04-06?22:45??augmented-reality\.idea\encodings.xm
?????文件????????188??2019-04-07?13:17??augmented-reality\.idea\misc.xm
?????文件????????307??2019-04-06?22:45??augmented-reality\.idea\modules.xm
?????文件???????9393??2019-04-07?13:20??augmented-reality\.idea\workspace.xm
?????文件???????1077??2019-01-05?09:36??augmented-reality\LICENSE
?????文件??????73379??2019-01-05?09:36??augmented-reality\models\cow.obj
?????文件??????53540??2019-01-05?09:36??augmented-reality\models\fox.obj
?????文件???????6186??2019-01-05?09:36??augmented-reality\models\pirate-ship-fat.obj
?????文件??????48596??2019-01-05?09:36??augmented-reality\models\rat.obj
?????文件?????201102??2019-01-05?09:36??augmented-reality\models\wolf.obj
?????文件???????2627??2019-01-05?09:36??augmented-reality\README.md
?????文件?????161116??2019-04-06?22:59??augmented-reality\reference\front.jpg
?????文件???????7170??2019-04-07?13:21??augmented-reality\src\ar_main.py
?????文件???????1757??2019-04-06?22:55??augmented-reality\src\objloader_simple.py
?????文件???????1371??2019-04-07?13:17??augmented-reality\src\__pycache__\objloader_simple.cpython-35.pyc
?????文件???????1230??2019-04-06?22:55??augmented-reality\src\__pycache__\objloader_simple.cpython-37.pyc
?????目錄??????????0??2019-04-07?13:17??augmented-reality\src\__pycache__
?????目錄??????????0??2019-04-07?13:21??augmented-reality\.idea
?????目錄??????????0??2019-04-06?22:44??augmented-reality\models
?????目錄??????????0??2019-04-07?13:20??augmented-reality\reference
?????目錄??????????0??2019-04-07?13:21??augmented-reality\src
?????目錄??????????0??2019-04-06?22:45??augmented-reality
-----------?---------??----------?-----??----
???????????????570881????????????????????24
評論
共有 條評論