diff --git a/document-scanner.ipynb b/document-scanner.ipynb index 856685a..c8638b4 100644 --- a/document-scanner.ipynb +++ b/document-scanner.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1031, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -17,7 +17,7 @@ }, { "cell_type": "code", - "execution_count": 1032, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -30,7 +30,7 @@ }, { "cell_type": "code", - "execution_count": 1033, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -40,7 +40,7 @@ }, { "cell_type": "code", - "execution_count": 1034, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -57,7 +57,7 @@ }, { "cell_type": "code", - "execution_count": 1035, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -86,14 +86,14 @@ }, { "cell_type": "code", - "execution_count": 1036, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def sort_contours(elem):\n", " return cv2.arcLength(elem, closed=True)\n", " \n", - "def get_quadrilateral(grayscale: cv2.Mat, output: cv2.Mat):\n", + "def get_corners(grayscale: cv2.Mat, output: cv2.Mat):\n", " convex_hull_mask = np.zeros((grayscale.shape[0], grayscale.shape[1], 3), dtype=np.uint8)\n", "\n", "\n", @@ -157,7 +157,7 @@ " print(\"corners: \", corners)\n", "\n", "\n", - " cv2.imshow('Lines', output)\n", + " cv2.imshow('Corners', output)\n", " cv2.waitKey(10)\n", "\n", " \n", @@ -166,7 +166,7 @@ }, { "cell_type": "code", - "execution_count": 1037, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -188,78 +188,7 @@ "cv2.imshow('Canny', edges)\n", "cv2.waitKey(10)\n", "\n", - "corners = get_quadrilateral(edges, input_img)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "cnts,_ = cv2.findContours(edged_img.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) # find all the contours\n", - "print(cnts)\n", - "\n", - "# cv2.drawContours(img, cnts, -1, (0, 255, 0), 3)\n", - " \n", - "# cv2.imshow('Contours', img)\n", - "# cv2.waitKey(5000)\n", - "\n", - "# Sorting the contours in descending order based on their contour area and just taking the first 5\n", - "cnts = sorted(cnts,key=cv2.contourArea,reverse=True)\n", - "\n", - "c = cnts[0]\n", - "peri = cv2.arcLength(c,True)\n", - "approx = cv2.approxPolyDP(c,0.03*peri,True)\n", - "x, y, w, h = cv2.boundingRect(c)\n", - "print(x,y,w,h)\n", - "to_show = img.copy()\n", - "# draw a green rectangle to visualize the bounding rect\n", - "cv2.rectangle(to_show, (x, y), (x+w, y+h), (0, 255, 0), 2)\n", - "plt.imshow(to_show)\n", - "plt.title(\"contour\")\n", - "plt.show()\n", - "\n", - "# Traversing in contours and finding the contour with 4 sides using cv2.approxPolyDP().\n", - "# for c in cnts:\n", - "# peri = cv2.arcLength(c,True)\n", - "# approx = cv2.approxPolyDP(c,0.096*peri,True)\n", - "# x, y, w, h = cv2.boundingRect(c)\n", - "# # draw a green rectangle to visualize the bounding rect\n", - "# cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)\n", - "# plt.imshow(img)\n", - "# plt.title(\"soemthing\")\n", - "# plt.show()\n", - "# print(len(approx), cv2.contourArea(c))\n", - "# if len(approx)==4:\n", - "# doc = approx\n", - "# break" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "len(cnts)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# draw points/circles around the corners of the document. \n", - "p=[]\n", - "for d in doc:\n", - " tuple_point = tuple(d[0])\n", - " cv2.circle(img,tuple_point,3,(0,0,255),4)\n", - " p.append(tuple_point)\n", - " \n", - "cv2.imshow('Corner points detected',img)\n", - "cv2.waitKey(0)" + "corners = get_corners(edges, input_img)\n" ] }, {