Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Harris corner detection demo"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Ref: https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_features_harris/py_features_harris.html"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'3.4.2'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# tested on 3.4.2\n",
"import cv2\n",
"cv2.__version__"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import platform\n",
"\n",
"cap=cv2.VideoCapture(0)\n",
"\n",
"\n",
"# Detector parameters\n",
"blockSize = 2\n",
"apertureSize = 3\n",
"k = 0.04\n",
"\n",
"while (True):\n",
" \n",
" ret,frame=cap.read()\n",
" \n",
" cv2.namedWindow(\"camera\",cv2.WND_PROP_FULLSCREEN)\n",
" cv2.setWindowProperty(\"camera\",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)\n",
" \n",
" gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY);\n",
" \n",
" dst = cv2.cornerHarris(gray, blockSize, apertureSize, k)\n",
" \n",
" #result is dilated for marking the corners, not important\n",
" dst = cv2.dilate(dst,None)\n",
"\n",
" # Threshold for an optimal value, it may vary depending on the image.\n",
" frame[dst>0.01*dst.max()]=[0,0,255]\n",
" \n",
" cv2.imshow('camera',frame)\n",
" \n",
" if cv2.waitKey(1) &0xFF == ord('q'): # press q or ESC to quit. You probably need to hit the screen first\n",
" break\n",
"\n",
" \n",
"cap.release()\n",
"cv2.destroyAllWindows()"
]
}
],
"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.15rc1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.