Skip to content

Commit

Permalink
created modules'
Browse files Browse the repository at this point in the history
  • Loading branch information
karan316 committed Jul 6, 2022
1 parent fdfbf55 commit 193946a
Showing 1 changed file with 75 additions and 106 deletions.
181 changes: 75 additions & 106 deletions document-scanner.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 191,
"execution_count": 338,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -18,20 +18,20 @@
},
{
"cell_type": "code",
"execution_count": 192,
"execution_count": 339,
"metadata": {},
"outputs": [],
"source": [
"rng.seed(12345)\n",
"\n",
"# read and show the input image\n",
"img_path = 'document.jpeg'\n",
"img_path = 'document-2.png'\n",
"img = cv2.imread(img_path)\n"
]
},
{
"cell_type": "code",
"execution_count": 193,
"execution_count": 340,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -41,7 +41,7 @@
},
{
"cell_type": "code",
"execution_count": 194,
"execution_count": 341,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -58,16 +58,14 @@
},
{
"cell_type": "code",
"execution_count": 195,
"execution_count": 342,
"metadata": {},
"outputs": [],
"source": [
"def get_intersection_points(coeff_1: tuple, coeff_2: tuple):\n",
" a1, b1, c1 = coeff_1\n",
" a2, b2, c2 = coeff_2\n",
"\n",
" print(coeff_1, coeff_2)\n",
"\n",
" x = 0 \n",
" y = 0\n",
"\n",
Expand All @@ -85,39 +83,87 @@
" if det != 0:\n",
" x = x_num / det\n",
" y = y_num / det\n",
" print(\"lines not parallel: \", coeff_1, coeff_2)\n",
" print(\"det: \",det)\n",
" print(\"corner: \", (x,y))\n",
" return (x, y)\n",
" \n",
" return None\n"
]
},
{
"cell_type": "code",
"execution_count": 196,
"execution_count": 343,
"metadata": {},
"outputs": [],
"source": [
"def distance_between_points(p1: tuple, p2: tuple):\n",
" x1, y1 = p1\n",
" x2, y2, = p2\n",
" return sqrt((x2 - x1)**2 + (y2 - y1)**2)"
" distance = sqrt((x2 - x1)**2 + (y2 - y1)**2)\n",
" return distance"
]
},
{
"cell_type": "code",
"execution_count": 197,
"execution_count": 344,
"metadata": {},
"outputs": [],
"source": [
"def are_similar_corners(c1: tuple, c2: tuple): \n",
" return True if distance_between_points(c1, c2) < 5 else False\n"
" return True if distance_between_points(c1, c2) < 3 else False\n"
]
},
{
"cell_type": "code",
"execution_count": 345,
"metadata": {},
"outputs": [],
"source": [
"def remove_similar_corners(all_corners):\n",
" corners = []\n",
" # get one corner\n",
" for i in range(len(all_corners)): \n",
" # check with all other corners\n",
" similar_corner = False\n",
" for j in range(len(all_corners)): \n",
" # if we have checked all corners upto this one then break\n",
" if i == j:\n",
" break\n",
" # if corner is similar to any of the previous corners then we do not add this corner\n",
" if are_similar_corners(all_corners[i], all_corners[j]): \n",
" similar_corner = True\n",
" break\n",
" if not similar_corner:\n",
" corners.append(all_corners[i])\n",
" \n",
" return corners"
]
},
{
"cell_type": "code",
"execution_count": 200,
"execution_count": 346,
"metadata": {},
"outputs": [],
"source": [
"def get_all_possible_corners(coefficients, row_size, col_size): \n",
" all_corners = []\n",
"\n",
" # for every combination of coeffs get the intersection points\n",
" for i in range(len(coefficients)):\n",
" for j in range(i, len(coefficients)):\n",
" if(i != j): \n",
" \n",
" int_point = get_intersection_points(coefficients[i], coefficients[j])\n",
" if int_point != None: \n",
" x, y = int_point\n",
" # coords should be within the image boundaries\n",
" if x > 0 and y > 0 and x < col_size and y < row_size:\n",
" all_corners.append(int_point)\n",
" \n",
" return all_corners"
]
},
{
"cell_type": "code",
"execution_count": 347,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -152,68 +198,34 @@
"\n",
" lines = cv2.HoughLinesP(image = convex_hull_mask_grayscale, rho = 2, theta = np.pi / 200, minLineLength=200, maxLineGap=0, threshold=40)\n",
"\n",
"\n",
"\n",
" # draw all houghlines\n",
" if lines is not None:\n",
" for line in lines:\n",
" l = line[0]\n",
" cv2.line(output, (l[0], l[1]), (l[2], l[3]), (0,255,0), 2, cv2.LINE_AA )\n",
" \n",
" print(\"lines\", len(lines))\n",
"\n",
" # if at any point there are exactly four lines forming a quad\n",
" # TODO: make it work even if there are a minimum of 4 lines\n",
" # if at any point there are at least four lines then find the corners\n",
" if len(lines) >= 4:\n",
" coefficients = []\n",
" for line in lines:\n",
" l = line[0]\n",
" coefficients.append(get_line_coefficients((l[0], l[1]), (l[2], l[3])))\n",
" \n",
" all_corners = []\n",
"\n",
" rows, cols = grayscale.shape\n",
"\n",
" # for every combination of coeffs get the intersection points\n",
" for i in range(len(coefficients)):\n",
" for j in range(i, len(coefficients)):\n",
" if(i != j): \n",
" \n",
" int_point = get_intersection_points(coefficients[i], coefficients[j])\n",
"\n",
" print(\"------------------------------------------\")\n",
"\n",
" if int_point != None: \n",
" x, y = int_point\n",
" # coords should be within the image boundaries\n",
" if x > 0 and y > 0 and x < cols and y < rows:\n",
" all_corners.append(int_point)\n",
" \n",
" # get all the corners from lines\n",
" all_corners = get_all_possible_corners(coefficients, rows, cols)\n",
"\n",
" corners = []\n",
"\n",
" # check for similar corners by computing the distance between the corners\n",
"\n",
" # get one corner\n",
" for i in range(len(all_corners)): \n",
" # check with all other corners\n",
" similar_corner = False\n",
" for j in range(len(all_corners)): \n",
" # if we have checked all corners upto this one then break\n",
" if i == j:\n",
" break\n",
" # if corner is similar to any of the previous corners then we do not add this corner\n",
" if are_similar_corners(all_corners[i], all_corners[j]): \n",
" similar_corner = True\n",
" break\n",
" if not similar_corner:\n",
" corners.append(all_corners[i])\n",
" # remove corners that are similar to one corner\n",
" corners = remove_similar_corners(all_corners)\n",
" \n",
" print(\"FINAL CORNERS: \", len(corners), corners)\n",
"\n",
" if len(corners) != 4:\n",
" print(\"corner length not equal to 4\")\n",
" return\n",
" \n",
" print(\"FINAL CORNERS: \", corners)\n",
"\n",
"\n",
" # draw corner points on the image\n",
" for x, y in corners:\n",
Expand All @@ -229,45 +241,14 @@
},
{
"cell_type": "code",
"execution_count": 199,
"execution_count": 348,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"lines 4\n",
"(-3, 228, -23034) (-269, 47, 76525)\n",
"lines not parallel: (-3, 228, -23034) (-269, 47, 76525)\n",
"det: 61191\n",
"corner: (302.82718046771583, 105.01088395352258)\n",
"------------------------------------------\n",
"(-3, 228, -23034) (347, 38, -28318)\n",
"lines not parallel: (-3, 228, -23034) (347, 38, -28318)\n",
"det: -79230\n",
"corner: (70.4431654676259, 101.95319954562666)\n",
"------------------------------------------\n",
"(-3, 228, -23034) (10, 329, -151341)\n",
"lines not parallel: (-3, 228, -23034) (10, 329, -151341)\n",
"det: -3267\n",
"corner: (8242.290174471993, 209.4775022956841)\n",
"------------------------------------------\n",
"(-269, 47, 76525) (347, 38, -28318)\n",
"lines not parallel: (-269, 47, 76525) (347, 38, -28318)\n",
"det: -26531\n",
"corner: (159.77143718668728, -713.7549658889601)\n",
"------------------------------------------\n",
"(-269, 47, 76525) (10, 329, -151341)\n",
"lines not parallel: (-269, 47, 76525) (10, 329, -151341)\n",
"det: -88971\n",
"corner: (362.9244585314316, 448.9719009564914)\n",
"------------------------------------------\n",
"(347, 38, -28318) (10, 329, -151341)\n",
"lines not parallel: (347, 38, -28318) (10, 329, -151341)\n",
"det: 113783\n",
"corner: (31.33740541205628, 459.0505347899071)\n",
"------------------------------------------\n",
"FINAL CORNERS: 4 [(302.82718046771583, 105.01088395352258), (70.4431654676259, 101.95319954562666), (362.9244585314316, 448.9719009564914), (31.33740541205628, 459.0505347899071)]\n"
"FINAL CORNERS: [(11.0, 33.077922077922075), (11.0, 497.0), (357.1346747999683, 28.58266656103937), (342.8536585365854, 497.0)]\n"
]
}
],
Expand All @@ -290,7 +271,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -358,21 +339,9 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": null,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'org' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m/Users/karanhejmadi/Desktop/python-document-scanner/document-scanner.ipynb Cell 9'\u001b[0m in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/karanhejmadi/Desktop/python-document-scanner/document-scanner.ipynb#ch0000009?line=0'>1</a>\u001b[0m \u001b[39m# apply four point transform to extract the document rectangle\u001b[39;00m\n\u001b[0;32m----> <a href='vscode-notebook-cell:/Users/karanhejmadi/Desktop/python-document-scanner/document-scanner.ipynb#ch0000009?line=1'>2</a>\u001b[0m warped \u001b[39m=\u001b[39m four_point_transform(org, doc\u001b[39m.\u001b[39mreshape(\u001b[39m4\u001b[39m, \u001b[39m2\u001b[39m) \u001b[39m*\u001b[39m ratio)\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/karanhejmadi/Desktop/python-document-scanner/document-scanner.ipynb#ch0000009?line=2'>3</a>\u001b[0m warped \u001b[39m=\u001b[39m cv2\u001b[39m.\u001b[39mcvtColor(warped, cv2\u001b[39m.\u001b[39mCOLOR_BGR2GRAY)\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/karanhejmadi/Desktop/python-document-scanner/document-scanner.ipynb#ch0000009?line=3'>4</a>\u001b[0m cv2\u001b[39m.\u001b[39mimshow(\u001b[39m\"\u001b[39m\u001b[39mWarped\u001b[39m\u001b[39m\"\u001b[39m, imutils\u001b[39m.\u001b[39mresize(warped, height \u001b[39m=\u001b[39m \u001b[39m650\u001b[39m))\n",
"\u001b[0;31mNameError\u001b[0m: name 'org' is not defined"
]
}
],
"outputs": [],
"source": [
"# apply four point transform to extract the document rectangle\n",
"warped = four_point_transform(org, doc.reshape(4, 2) * ratio)\n",
Expand Down

0 comments on commit 193946a

Please sign in to comment.