{"id":6908,"date":"2019-05-17T11:17:28","date_gmt":"2019-05-17T02:17:28","guid":{"rendered":"http:\/\/www.gisdeveloper.co.kr\/?p=6908"},"modified":"2020-05-28T10:32:41","modified_gmt":"2020-05-28T01:32:41","slug":"python%ea%b3%bc-opencv-39-%ec%9e%90%ec%84%b8-%ec%b6%94%ec%a0%95pose-estimation","status":"publish","type":"post","link":"http:\/\/www.gisdeveloper.co.kr\/?p=6908","title":{"rendered":"Python\uacfc OpenCV \u2013 44 : \uc790\uc138 \ucd94\uc815(Pose Estimation)"},"content":{"rendered":"<p>\uc774 \uae00\uc758 \uc6d0\ubb38\uc740 https:\/\/opencv-python-tutroals.readthedocs.io\/en\/latest\/py_tutorials\/py_calib3d\/py_pose\/py_pose.html \uc785\ub2c8\ub2e4.<\/p>\n<p>\uc774\uc804 \uae00\uc740 \uce74\uba54\ub77c \ubcf4\uc815\uc5d0 \ub300\ud55c \ub0b4\uc6a9\uc73c\ub85c, \uce74\uba54\ub77c \uba54\ud2b8\ub9ad\uc2a4\uc640 \uc65c\uace1 \uacc4\uc218 \ub4f1\uc744 \uad6c\ud588\uc2b5\ub2c8\ub2e4. \uc8fc\uc5b4\uc9c4 \ud328\ud134 \uc774\ubbf8\uc9c0\ub97c \ud1b5\ud574 \uc6b0\ub9ac\ub294 \uc774\ubbf8\uc9c0 \uc548\uc758 \ud328\ud134\uc758 \uc790\uc138\ub97c \uacc4\uc0b0\ud558\uae30 \uc704\ud55c \uc815\ubcf4\ub97c \uc774\uc6a9\ud560 \uc218 \uc788\uace0 \uac1d\uccb4\uac00 \uacf5\uac04\uc0c1\uc5d0 \uc5b4\ub5bb\uac8c \ub193\uc5ec\uc788\ub294\uc9c0 \ud30c\uc545\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \ud3c9\uba74 \uac1d\uccb4\uc5d0 \ub300\ud574\uc11c\ub294, Z\uac12\uc744 0\uc73c\ub85c \uac00\uc815\ud55c\ub2e4\uba74.. \uc774\ubbf8\uc9c0\uc758 \uc774\ub7ec\ud55c \uac1d\uccb4\uc5d0 \ub300\ud55c \uc790\uc138 \ubb38\uc81c\ub294 \uce74\uba54\ub77c\uac00 \uacf5\uac04 \uc0c1\uc5d0 \uc5b4\ub5bb\uac8c \uc704\uce58\ud574 \uc788\ub294\uc9c0\uc758 \ubb38\uc81c\uac00 \ub429\ub2c8\ub2e4. \uadf8\ub798\uc11c, \ub9cc\uc57d \uacf5\uac04 \uc0c1\uc5d0 \uac1d\uccb4\uac00 \uc5b4\ub5bb\uac8c \ub193\uc5ec \uc788\ub294\uc9c0\ub97c \uc548\ub2e4\uba74, 3\ucc28\uc6d0 \ud6a8\uacfc\ub97c \uc2dc\ubbac\ub808\uc774\uc158\ud558\uae30 \uc704\ud574 \uc774\ubbf8\uc9c0 \uc0c1\uc5d0 2\ucc28\uc6d0 \ub3c4\ud615\uc744 \uadf8\ub824 \ub123\uc744 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<p>\uc6b0\ub9ac\uc758 \ubaa9\ud45c\ub294 \uccb4\uc2a4\ud310\uc758 \uccab\ubc88\uc9f8 \ucf54\ub108 \uc9c0\uc810 \uc704\uc5d0 3\ucc28\uc6d0 \uc88c\ud45c\ucd95(X, Y, Z \ucd95)\uc744 \uadf8\ub824 \ub123\ub294 \uac83\uc785\ub2c8\ub2e4. X \uce21\uc740 \ud30c\ub791\uc0c9\uc73c\ub85c, Y\ucd95\uc740 \ucd08\ub85d\uc0c9\uc73c\ub85c Z\ucd95\uc740 \ube68\uac04\uc0c9\uc73c\ub85c \uadf8\ub824 \ubd05\uc2dc\ub2e4. \uadf8\ub798\uc11c \ud6a8\uacfc\uba74\uc5d0\uc11c \ubcfc \ub54c, Z\ucd95\uc740 \uccb4\uc2a4\ud310 \uc0c1\uc5d0 \uc218\uc9c1\uc73c\ub85c \ub290\uaef4\uc838\uc57c \ud569\ub2c8\ub2e4.<\/p>\n<p>\uac00\uc7a5 \uba3c\uc800, \uc774\uc804 \uce74\uba54\ub77c \ubcf4\uc815 \uacb0\uacfc\ub85c\ubd80\ud130 \uce74\uba54\ub77c \ud589\ub82c\uacfc \uc65c\uace1\uacc4\uc218\ub97c \uacc4\uc0b0\ud574 \ubd05\uc2dc\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nimport numpy as np\r\nimport cv2\r\nimport glob\r\n\r\ncriteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)\r\nobjp = np.zeros((6*7,3), np.float32)\r\nobjp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)\r\n\r\nobjpoints = []\r\nimgpoints = []\r\n\r\nimages = glob.glob('.\/data\/chess\/*.jpg')\r\n\r\nfor fname in images:\r\n    img = cv2.imread(fname)\r\n    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\r\n\r\n    ret, corners = cv2.findChessboardCorners(gray, (7,6),None)\r\n\r\n    if ret == True:\r\n        objpoints.append(objp)\r\n\r\n        corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)\r\n        imgpoints.append(corners2)\r\n\r\nret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)\r\n<\/pre>\n<p>draw\ub77c\ub294 \uc0ac\uc6a9\uc790 \uc815\uc758 \ud568\uc218\ub97c \ub9cc\ub4e4\ud150\ub370, \uc774 \ud568\uc218\ub294 cv2.findChessboardCorners() \ud568\uc218\ub97c \ud1b5\ud574 \uad6c\ud55c \uccb4\uc2a4\ud310\uc758 \ucf54\ub108\uc810\uacfc \ucd95\uc758 \ud3ec\uc778\ud2b8\ub97c \uc778\uc790\ub85c \ubc1b\uc544 3\ucc28\uc6d0 \ucd95\uc744 \uadf8\ub9bd\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\ndef draw(img, corners, imgpts):\r\n    corner = tuple(corners[0].ravel())\r\n    img = cv2.line(img, corner, tuple(imgpts[0].ravel()), (255,0,0), 5)\r\n    img = cv2.line(img, corner, tuple(imgpts[1].ravel()), (0,255,0), 5)\r\n    img = cv2.line(img, corner, tuple(imgpts[2].ravel()), (0,0,255), 5)\r\n    return img\r\n<\/pre>\n<p>\ucd95\uc758 \ud3ec\uc778\ud2b8\ub97c \uc704\ud55c \ubcc0\uc218\ub97c \uc815\uc758\ud560 \uac83\uc778\ub370, \uc774 \ubcc0\uc218\ub294 \ucd95\uc744 \uadf8\ub9ac\uae30 \uc704\ud55c 3\ucc28\uc6d0 \uacf5\uac04 \uc0c1\uc758 \ud3ec\uc778\ud2b8\uc785\ub2c8\ub2e4. \uae38\uc774 3(\ub2e8\uc704\ub294 \uccb4\uc2a4\ubcf4\ub4dc\uc758 \ud06c\uae30\ub97c \uae30\ubc18\uc73c\ub85c \ud568)\ub9cc\ud07c\uc744 \ucd95\uc758 \uae38\uc774\ub85c \uc815\ud569\ub2c8\ub2e4. \uadf8\ub798\uc11c X\ucd95\uc758 \uc120\uc740 (0,0,0)-(3,0,0)\uc73c\ub85c, Y\ucd95\uc758 \uc120\uc740 (0,0,0)-(0,3,0)\uc73c\ub85c, Z\ucd95\uc740 (0,0,0)-(0,0,-3)\uc778\ub370, \uc74c\uc218\uc778 \uc774\uc720\ub294 \uce74\uba54\ub77c\uc758 \ubc29\ud5a5\uc744 \ub098\ud0c0\ub0b4\uae30 \uc704\ud568\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\naxis = np.float32([[3,0,0], [0,3,0], [0,0,-3]]).reshape(-1,3)\r\n<\/pre>\n<p>\uc790, \uc774\uc81c \uac01 \uc774\ubbf8\uc9c0\ub97c \ub85c\ub4dc\ud558\uace0 \uc774\ubbf8 \uc0c1\uc758 \uccb4\uc2a4\ud310\uc758 7&#215;6 \uadf8\ub9ac\ub4dc\ub97c \ucc3e\uc2b5\ub2c8\ub2e4. \ub9cc\uc57d \ubc1c\uacac\ub418\uba74, \uc774\ub97c Subcorner \ud53d\uc140\ub85c \uc815\uc81c\ud569\ub2c8\ub2e4. cv2.solvePnPRansac() \ud568\uc218\ub97c \uc0ac\uc6a9\ud574 \ud68c\uc804\uacfc \uc774\ub3d9\uc744 \uacc4\uc0b0\ud569\ub2c8\ub2e4. \uc77c\ub2e8 \uc774\ub7ec\ud55c \ubcc0\ud658 \ud589\ub82c\uc774 \uacc4\uc0b0\ub418\uba74, \uc774\ub97c \uc774\uc6a9\ud574 \uc774\ubbf8\uc9c0 \ud3c9\uba74 \uc0c1\uc758 \ucd95\uc758 \ud3ec\uc778\ud2b8\ub4e4\uc744 \ud22c\uc601\ud569\ub2c8\ub2e4. \uac04\ub2e8\ud788 \ub9d0\ud574\uc11c, 3\ucc28\uc6d0 \uc0c1\uc758 \uc88c\ud45c (3,0,0), (0,3,0), (0,0,3)\uc5d0 \ud574\ub2f9\ud558\ub294 \uc774\ubbf8\uc9c0 \uc0c1\uc758 \uc88c\ud45c\ub97c \uc5bb\ub294 \uac83\uc785\ub2c8\ub2e4. \uc774 3\uac1c\uc758 \uc88c\ud45c\uac00 \uc5bb\uc5b4\uc84c\ub2e4\uba74 \uc55e\uc11c \uc815\uc758\ud55c draw \ud568\uc218\ub97c \ud1b5\ud574 \uadf8\ub9bd\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nfor fname in images:\r\n    img = cv2.imread(fname)\r\n    gray = cv2.cvtColor(img,  cv2.COLOR_BGR2GRAY)\r\n    ret, corners = cv2.findChessboardCorners(gray, (7,6),None)\r\n\r\n    if ret == True:\r\n        corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)\r\n\r\n        # Find the rotation and translation vectors.\r\n        _, rvecs, tvecs, inliers = cv2.solvePnPRansac(objp, corners2, mtx, dist)\r\n\r\n        # project 3D points to image plane\r\n        imgpts, jac = cv2.projectPoints(axis, rvecs, tvecs, mtx, dist)\r\n\r\n        img = draw(img, corners2, imgpts)\r\n        cv2.imshow('img',img)\r\n        k = cv2.waitKey(500)\r\n<\/pre>\n<p>\uc544\ub798 \uadf8\ub9bc\uc740 \uc704 \uc608\uc81c\uc758 \uc2e4\ud589 \uc2dc \ud45c\uc2dc\ub418\ub294 \uc601\uc0c1 \uc911 \ud558\ub098\uc785\ub2c8\ub2e4.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gisdeveloper.co.kr\/wp-content\/uploads\/2019\/05\/PoseEst1.png\" alt=\"\" width=\"643\" height=\"513\" class=\"aligncenter size-full wp-image-6910\" \/><\/p>\n<p>\uc774\uc81c, 3\ucc28\uc6d0 \ucd95\uc744 \ud30c\uc545\ud588\uc73c\ub2c8, draw \ud568\uc218\ub97c \ubcc0\ud615\ud574\uc11c 3\ucc28\uc6d0 \ud050\ube0c\ub97c \uc774\ubbf8\uc9c0\uc5d0 \ud45c\uc2dc\ud558\uaca0\uc2b5\ub2c8\ub2e4. \uc218\uc815\ub41c draw \ud568\uc218\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\ndef draw(img, corners, imgpts):\r\n    imgpts = np.int32(imgpts).reshape(-1,2)\r\n\r\n    # draw ground floor in green\r\n    img = cv2.drawContours(img, [imgpts[:4]],-1,(0,255,0),-3)\r\n\r\n    # draw pillars in blue color\r\n    for i,j in zip(range(4),range(4,8)):\r\n        img = cv2.line(img, tuple(imgpts[i]),tuple(imgpts[j]),(255),3)\r\n\r\n    # draw top layer in red color\r\n    img = cv2.drawContours(img, [imgpts[4:]],-1,(0,0,255),3)\r\n\r\n    return img\r\n<\/pre>\n<p>\ubcc0\uacbd\ub41c draw \ud568\uc218\uc5d0 \ub9de\uac8c \uae30\uc874\uc758 axis \ubcc0\uc218\ub3c4 \ud050\ube0c\ub97c \uad6c\uc131\ud558\ub294 8\uac1c\uc758 \ubaa8\uc11c\ub9ac \uc88c\ud45c\uc5d0 \ub9de\uac8c \ubcc0\uacbd\ub429\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\naxis = np.float32([[0,0,0], [0,3,0], [3,3,0], [3,0,0],\r\n                   [0,0,-3],[0,3,-3],[3,3,-3],[3,0,-3]])\r\n<\/pre>\n<p>\uc2e4\ud589\ud574\ubcf4\uba74, \uadf8 \uacb0\uacfc \uc911 \ud558\ub098\uc758 \uc774\ubbf8\uc9c0\ub294 \uc544\ub798\uc640 \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gisdeveloper.co.kr\/wp-content\/uploads\/2019\/05\/PoseEst2.png\" alt=\"\" width=\"643\" height=\"513\" class=\"aligncenter size-full wp-image-6913\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uc774 \uae00\uc758 \uc6d0\ubb38\uc740 https:\/\/opencv-python-tutroals.readthedocs.io\/en\/latest\/py_tutorials\/py_calib3d\/py_pose\/py_pose.html \uc785\ub2c8\ub2e4. \uc774\uc804 \uae00\uc740 \uce74\uba54\ub77c \ubcf4\uc815\uc5d0 \ub300\ud55c \ub0b4\uc6a9\uc73c\ub85c, \uce74\uba54\ub77c \uba54\ud2b8\ub9ad\uc2a4\uc640 \uc65c\uace1 \uacc4\uc218 \ub4f1\uc744 \uad6c\ud588\uc2b5\ub2c8\ub2e4. \uc8fc\uc5b4\uc9c4 \ud328\ud134 \uc774\ubbf8\uc9c0\ub97c \ud1b5\ud574 \uc6b0\ub9ac\ub294 \uc774\ubbf8\uc9c0 \uc548\uc758 \ud328\ud134\uc758 \uc790\uc138\ub97c \uacc4\uc0b0\ud558\uae30 \uc704\ud55c \uc815\ubcf4\ub97c \uc774\uc6a9\ud560 \uc218 \uc788\uace0 \uac1d\uccb4\uac00 \uacf5\uac04\uc0c1\uc5d0 \uc5b4\ub5bb\uac8c \ub193\uc5ec\uc788\ub294\uc9c0 \ud30c\uc545\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \ud3c9\uba74 \uac1d\uccb4\uc5d0 \ub300\ud574\uc11c\ub294, Z\uac12\uc744 0\uc73c\ub85c \uac00\uc815\ud55c\ub2e4\uba74.. \uc774\ubbf8\uc9c0\uc758 \uc774\ub7ec\ud55c \uac1d\uccb4\uc5d0 \ub300\ud55c \uc790\uc138 \ubb38\uc81c\ub294 \uce74\uba54\ub77c\uac00 \uacf5\uac04 \uc0c1\uc5d0 \uc5b4\ub5bb\uac8c &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.gisdeveloper.co.kr\/?p=6908\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Python\uacfc OpenCV \u2013 44 : \uc790\uc138 \ucd94\uc815(Pose Estimation)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[130,131,1],"tags":[],"class_list":["post-6908","post","type-post","status-publish","format-standard","hentry","category-opencv","category-python","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/6908","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6908"}],"version-history":[{"count":8,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/6908\/revisions"}],"predecessor-version":[{"id":9418,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/6908\/revisions\/9418"}],"wp:attachment":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6908"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}