Created
August 14, 2015 16:13
-
-
Save patricksnape/639a16dfd02adc92cefd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "import menpo3d.io as m3dio\nimport menpo.io as mio\nimport numpy as np\nimport mayavi.mlab as mlab\nimport cv2\n\nfrom menpo.image import Image\n\ntemplate = m3dio.import_mesh('/home/pts08/Dropbox/james/template.obj')\ntemplate_landmarks = mio.import_landmark_file('/home/pts08/Dropbox/james/template.ljson').lms", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "%matplotlib qt\n\ntemplate.view_landmarks()", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": true | |
}, | |
"cell_type": "code", | |
"source": "image = mio.import_image('/vol/atlas/databases/lfpw/trainset/image_0002.png')\nimage_landmarks = image.landmarks[None].lms", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "%matplotlib inline\n\nimage.view_widget()", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "rows = image.shape[0]\ncols = image.shape[1]\nmax_d = max(rows, cols)\ncamera_matrix = np.array([[max_d, 0, rows / 2.0],\n [0, max_d, cols / 2.0],\n [0, 0, 1.0]])\ndistortion_coeffs = np.zeros(4)\n\nconverged, r_vec, t_vec = cv2.solvePnP(template_landmarks.points, \n image_landmarks.points[:, ::-1], \n camera_matrix, \n distortion_coeffs)\n\nrotation_matrix = cv2.Rodrigues(r_vec)[0]\nprint(rotation_matrix)", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "img = np.array(image.as_PILImage())\n\nproject_matrix = np.hstack([rotation_matrix, t_vec])\nkp = camera_matrix.dot(project_matrix)\n\n# reproject object points - check validity of found projection matrix\nfor p in template_landmarks.points:\n X = np.append(p, [1])\n opt_p = kp.dot(X)\n opt_p_img = tuple((opt_p[:2] / opt_p[-1]).astype(np.int))\n\n cv2.circle(img, opt_p_img, 1, (0, 0, 255))", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "%matplotlib inline\n\nImage.init_from_rolled_channels(img).view()", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "python2", | |
"display_name": "Python 2", | |
"language": "python" | |
}, | |
"language_info": { | |
"mimetype": "text/x-python", | |
"nbconvert_exporter": "python", | |
"name": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.10", | |
"file_extension": ".py", | |
"codemirror_mode": { | |
"version": 2, | |
"name": "ipython" | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment