Skip to content

Instantly share code, notes, and snippets.

@se7oluti0n
Created November 15, 2017 10:31
Show Gist options
  • Save se7oluti0n/40fcd95286d383a1e222fd79a2131025 to your computer and use it in GitHub Desktop.
Save se7oluti0n/40fcd95286d383a1e222fd79a2131025 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import cv2\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## you can take an analytic derivative of a Gaussian in X or Y and use that filter"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"simA = cv2.imread('input/simA.jpg', cv2.IMREAD_GRAYSCALE)\n",
"plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def calculateGradients(img, ksize, axis=0):\n",
" if axis == 1:\n",
" grad = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize)\n",
" else:\n",
" grad = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize)\n",
" \n",
" grad = cv2.normalize(grad, grad, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)\n",
" return grad"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gradx = calculateGradients(simA, 3, axis=1)\n",
"plt.imshow(gradx)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def harrisScoreFunction(Ix, Iy, weights, alpha, norm=True):\n",
" Ixx = Ix ** 2\n",
" Ixy = Ix * Iy\n",
" Iyx = Iy * Ix\n",
" Iyy = Iy ** 2\n",
" \n",
" im_rows, im_cols = Ix.shape\n",
" w_rows, w_cols = weights.shape\n",
" \n",
" Rs = np.zeros(Ix.shape)\n",
" \n",
" for i in range(w_rows / 2, im_rows - w_rows / 2):\n",
" min_i = i - w_rows / 2\n",
" max_i = i + w_rows / 2 + 1\n",
" for j in range(w_cols / 2, im_cols - im_cols / 2):\n",
" min_j = j - w_cols / 2\n",
" max_j = j + w_cols / 2 + 1\n",
" \n",
" wIxx = Ixx[min_i:max_i, min_j:max_j]\n",
" wIxy = Ixy[min_i:max_i, min_j:max_j]\n",
" wIyx = Iyx[min_i:max_i, min_j:max_j]\n",
" wIyy = Iyy[min_i:max_i, min_j:max_j]\n",
" \n",
" Mxx = (weights * wIxx).sum()\n",
" Mxy = (weights * wIxy).sum()\n",
" Myx = (weights * wIyx).sum()\n",
" Myy = (weights * wIyy).sum()\n",
" \n",
" M = np.asarray([Mxx, Mxy, Myx, Myy]).reshape((2,2))\n",
" Rs[i, j] = np.linalg.det(M) - alpha * (M.trace() ** 2)\n",
" if norm:\n",
" Rs = cv2.normalize(Rs, Rs, alpha=0, beta=255,\n",
" norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)\n",
" return Rs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def generateGaussianWeights(window_size):\n",
" c = np.zeros((window_size,)*2, dtype=np.float32);\n",
" c[window_size / 2, window_size / 2] = 1.0\n",
" w = cv2.GaussianBlur(c, (window_size,)*2, 0)\n",
" return w"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gradx = calculateGradients(simA, 3, axis=1)\n",
"grady = calculateGradients(simA, 3, axis=0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"weights = generateGaussianWeights(3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"weights"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Rs = harrisScoreFunction(grad_x, grad_y, weights, 0.04)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.imshow(Rs)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def findCorner(scores, threshold, radius):\n",
" shape = scores.shape\n",
" scores[scores < threshold] = 0\n",
" scores = scores.reshape(shape)\n",
" \n",
" #Non-maximum suppression\n",
" for i in range(radius, shape[0] - radius):\n",
" min_i = i - radius\n",
" max_i = i + radius\n",
" for j in range(radius, shape[1] - radius):\n",
" min_j = j - radius\n",
" max_j = j + radius\n",
" \n",
" patch = scores[min_i:max_i, min_j:max_j]\n",
" max_val = np.amax(patch)\n",
" patch[patch < max_val] = 0\n",
" scores[min_i:max_i, min_j:max_j] = patch\n",
" \n",
" corners = []\n",
" for i in range(shape[0]):\n",
" for j in range(shape[1]):\n",
" if scores[i, j] > 0:\n",
" corners.append((i, j))\n",
" return corners"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"corners = findCorner(Rs, 100, 5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"imgA_color = cv2.imread('input/simA.jpg')\n",
"for r,c in corners:\n",
" cv2.circle(imgA_color, (c,r), 2, (0,0,255))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"imgA_color = cv2.cvtColor(imgA_color, cv2.COLOR_BGR2RGB)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.imshow(imgA_color)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment