\n", "
• You can use negative indexing in python. `v[-5:]` takes the last 5 elements of the vector `v`.
• \n", "
• Make sure that the elements in the column vector add up to 1.
• \n", "
• Be patient - the code might take a while to execute. If the text to the left of the cell below is `In[*]`, the code is still executing.
• \n", "
• It's okay if the result isn't that good - we will fix this later! (In fact, with larger window sizes the result is probably worse here)
• \n", "
\n", "
• Take a look at the function `np.percentile` to implement the threshold.
• \n", "
• There is an easy way to implement the threshold in python: `v[v>3] = 100` changes all entries in v that is greater than 3 to 100's. Try out the little code block below.
• \n", "
" ] }, { "cell_type": "code", "collapsed": false, "input": [ "vec = np.array([0,1,2,3,4,5,6])\n", "vec[vec>3] = 100\n", "print vec" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "threshold_percentile = 90\n", "\n", "for counter in range(len(window_sizes)):\n", " result = filtered_results3[counter,:,:].copy()\n", " \n", " # Find the threshold given threshold_percentile\n", " threshold = np.percentile(result, threshold_percentile)\n", " \n", " # TODO: Implement the thresholding on the matrix \"result\" \n", " # (change any entry above threshold to 1 and any entry below threshold to 0)\n", " \n", " \n", " plt.subplot(\"24{}\".format(counter+1))\n", " imshow(result, interpolation='none', cmap=cm.Greys_r)\n", " title('Thresholded window={}, threshold={}'.format(window_sizes[counter], round(threshold, -int(floor(log10(threshold))))))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##4(d) Bonus: Finding the peak##\n", "\n", "Although we have a nice approximation of where our finger can be and we can find the center of all the white pixels, sometimes it is just easier to find the position of the highest-value pixel after the filtering. The center of the finger should be close to this position.\n", "\n", "In the code below, find the position of the pixel with the highest value.\n", "\n", "Hints:\n", "
\n", "
• `a.argmax()` returns the maximum entry of the matrix `a`
• \n", "
• `a.shape` returns the height and width of matrix `a`
• \n", "
• `np.unravel_index` might help a lot :)
• \n", "
" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for counter in range(len(window_sizes)):\n", " result = filtered_results3[counter,:,:]\n", " \n", " # TODO: find x and y, the coordinates of the largest element in the matrix \"result\"\n", " x,y = \n", " \n", " print \"Peak for filtering with window size {} is at ({},{}).\".format(window_sizes[counter],x,y)" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }