Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pull #16

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

pull #16

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions Assignments/Aditya/ Sudoku_LINE_DETECTION.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "b71feeb8",
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"\n",
"img = cv2.imread('Sudoku.jpg')\n",
"\n",
"\n",
"#grayscale and canny edges\n",
"gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n",
"\n",
" #blur = cv2.GaussianBlur(img,(1,1),0)\n",
"\n",
"#kernel = np.ones((1,1),np.float32)/0.8\n",
"#blur = cv2.filter2D(img,-1,kernel)\n",
"blur = cv2.blur(img,(1,1))\n",
"edges = cv2.Canny(blur, 50, 400, apertureSize=3)\n",
"cv2.imshow('edges', edges)\n",
"\n",
"lines = cv2.HoughLines(edges, 1, np.pi/180, 222)\n",
"\n",
"\n",
"if lines is not None:\n",
" for i in range(0, len(lines)):\n",
" \n",
" rho = lines[i][0][0]\n",
" theta = lines[i][0][1]\n",
" a = np.cos(theta)\n",
" b = np.sin(theta)\n",
" x0 = a * rho\n",
" y0 = b * rho\n",
" pt1 = (int(x0 + 1000*(-b)), int(y0 + 1000*(a)))\n",
" pt2 = (int(x0 - 1000*(-b)), int(y0 - 1000*(a)))\n",
" cv2.line(img, pt1, pt2, (0,0,255), 3)\n",
" pass\n",
"\n",
"cv2.imshow('hough lines', img)\n",
"cv2.waitKey(0)\n",
"cv2.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "7bf8045e",
"metadata": {},
"outputs": [],
"source": [
"for rho, theta in lines[0]:\n",
" a=np.cos(theta)\n",
" b=np.sin(theta)\n",
" x0=a*rho\n",
" y0=b*rho\n",
" x1=int(x0+1000*(-b))\n",
" y1=int(y0+1000*(a))\n",
" x2=int(x0-1000*(-b))\n",
" y2=int(x0-1000*(a))\n",
" cv2.line(image, (x1,y1), (x2,y2), (255,0,0),2) "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "8cecb189",
"metadata": {},
"outputs": [],
"source": [
"# Python program to illustrate HoughLine\n",
"# method for line detection\n",
"import cv2\n",
"import numpy as np\n",
" \n",
"# Reading the required image in \n",
"# which operations are to be done. \n",
"# Make sure that the image is in the same \n",
"# directory in which this python program is\n",
"img = cv2.imread('Sudoku.jpg')\n",
"cv2.imshow('img', img)\n",
" \n",
"# Convert the img to grayscale\n",
"gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n",
" \n",
"# Apply edge detection method on the image\n",
"edges = cv2.Canny(gray,50,150,apertureSize = 3)\n",
" \n",
"# This returns an array of r and theta values\n",
"lines = cv2.HoughLines(edges,1,np.pi/180, 200)\n",
" \n",
"# The below for loop runs till r and theta values \n",
"# are in the range of the 2d array\n",
"for r,theta in lines[0]:\n",
" \n",
" # Stores the value of cos(theta) in a\n",
" a = np.cos(theta)\n",
" \n",
" # Stores the value of sin(theta) in b\n",
" b = np.sin(theta)\n",
" \n",
" # x0 stores the value rcos(theta)\n",
" x0 = a*r\n",
" \n",
" # y0 stores the value rsin(theta)\n",
" y0 = b*r\n",
" \n",
" # x1 stores the rounded off value of (rcos(theta)-1000sin(theta))\n",
" x1 = int(x0 + 1000*(-b))\n",
" \n",
" # y1 stores the rounded off value of (rsin(theta)+1000cos(theta))\n",
" y1 = int(y0 + 1000*(a))\n",
" \n",
" # x2 stores the rounded off value of (rcos(theta)+1000sin(theta))\n",
" x2 = int(x0 - 1000*(-b))\n",
" \n",
" # y2 stores the rounded off value of (rsin(theta)-1000cos(theta))\n",
" y2 = int(y0 - 1000*(a))\n",
" \n",
" # cv2.line draws a line in img from the point(x1,y1) to (x2,y2).\n",
" # (0,0,255) denotes the colour of the line to be \n",
" #drawn. In this case, it is red. \n",
" cv2.line(img,(x1,y1), (x2,y2), (0,0,255),2)\n",
" \n",
"# All the changes made in the input image are finally\n",
"# written on a new image houghlines.jpg\n",
"cv2.imshow('linesDetected.jpg', img)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f65d4371",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
176 changes: 176 additions & 0 deletions Assignments/Aditya/Detect_shapes1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "e440673a",
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"\n",
"image = cv2.imread('Untitled.jpg')\n",
"cv2.imshow('shapes', image)\n",
"\n",
"gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
" \n",
"\n",
"ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)\n",
"\n",
"contours, heirarchy = cv2.findContours(thresh,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)\n",
"len(contours)\n",
"\n",
"cnt_list = []\n",
"len(cnt_list)\n",
"i = 0\n",
"\n",
"for cnt in contours:\n",
" if i == 0:\n",
" i = 1\n",
" continue\n",
" approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True)\n",
" i = len(approx)\n",
" cnt_list.append(i)\n",
" if len(approx) == 3:\n",
" shape_name = \"Triangle 3\"\n",
" cv2.drawContours(image, [cnt], 0, (0, 255, 0), 5)\n",
"\n",
" #placing text at centre\n",
" M = cv2.moments(cnt)\n",
" cx = int(M['m10'] / M['m00'])\n",
" cy = int(M['m01'] / M['m00'])\n",
" cv2.putText(image, shape_name, (cx-50, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 1)\n",
" \n",
" elif len(approx) == 5:\n",
" shape_name = \"Pentagon 5\"\n",
" cv2.drawContours(image, [cnt], -1, (0,255,0), 5)\n",
" #placing text at centre\n",
" M = cv2.moments(cnt)\n",
" cx = int(M['m10'] / M['m00'])\n",
" cy = int(M['m01'] / M['m00'])\n",
" cv2.putText(image, shape_name, (cx-50, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 1)\n",
" \n",
" elif len(approx) == 6:\n",
" shape_name = \"Hexagon 6\"\n",
" cv2.drawContours(image, [cnt], -1, (0,255,0), 5)\n",
" #placing text at centre\n",
" M = cv2.moments(cnt)\n",
" cx = int(M['m10'] / M['m00'])\n",
" cy = int(M['m01'] / M['m00'])\n",
" cv2.putText(image, shape_name, (cx-50, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 1)\n",
" \n",
" elif len(approx) == 8:\n",
" shape_name = \"Octagon 8\"\n",
" cv2.drawContours(image, [cnt], -1, (0,255,0), 5)\n",
" #placing text at centre\n",
" M = cv2.moments(cnt)\n",
" cx = int(M['m10'] / M['m00'])\n",
" cy = int(M['m01'] / M['m00'])\n",
" cv2.putText(image, shape_name, (cx-50, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 1) \n",
" \n",
" elif len(approx) == 12:\n",
" shape_name = \"Star 12\"\n",
" cv2.drawContours(image, [cnt], -1, (0,255,0), 5)\n",
" #placing text at centre\n",
" M = cv2.moments(cnt)\n",
" cx = int(M['m10'] / M['m00'])\n",
" cy = int(M['m01'] / M['m00'])\n",
" cv2.putText(image, shape_name, (cx-50, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 1)\n",
" \n",
" elif len(approx) == 4:\n",
" x, y, w, h = cv2.boundingRect(approx)\n",
" aspectRatio = float(w)/h\n",
" if aspectRatio >= 0.5 and aspectRatio <= 1.5:\n",
" shape_name = \"Square 4\"\n",
" cv2.drawContours(image, [cnt], -1, (0,255,0), 5)\n",
" #placing text at centre\n",
" M = cv2.moments(cnt)\n",
" cx = int(M['m10'] / M['m00'])\n",
" cy = int(M['m01'] / M['m00'])\n",
" cv2.putText(image, shape_name, (cx-50, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 1) \n",
" else:\n",
" shape_name = \"Rectangle 4\"\n",
" cv2.drawContours(image, [cnt], -1, (0,255,0), 5)\n",
" #placing text at centre\n",
" M = cv2.moments(cnt)\n",
" cx = int(M['m10'] / M['m00'])\n",
" cy = int(M['m01'] / M['m00'])\n",
" cv2.putText(image, shape_name, (cx-50, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 1)\n",
" \n",
" elif len(approx) > 15:\n",
" shape_name = \"Circle\"\n",
" cv2.drawContours(image, [cnt], -1, (0,255,0), 5)\n",
" #placing text at centre\n",
" M = cv2.moments(cnt)\n",
" cx = int(M['m10'] / M['m00'])\n",
" cy = int(M['m01'] / M['m00'])\n",
" cv2.putText(image, shape_name, (cx-50, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 1)\n",
" \n",
" \n",
"print(len(cnt_list))\n",
"\n",
"cnt_list.sort()\n",
"print(len(cnt_list))\n",
"\n",
"for i in cnt_list:\n",
" if i == 3:\n",
" print(\"triangle\")\n",
" elif i == 4:\n",
" print(\"quadrilateral\")\n",
" elif i == 5:\n",
" print(\"Pentagon\")\n",
" elif i == 6:\n",
" print(\"Hexagon\")\n",
" elif i == 8:\n",
" print(\"Octagon\")\n",
" elif i == 12:\n",
" print(\"Star\")\n",
" elif i > 15:\n",
" print(\"Circle\")\n",
" \n",
"cv2.imshow(\"image1\", image)\n",
"cv2.waitKey(0)\n",
"cv2.clearAllWindows() \n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5448b713",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "3276c7b9",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading