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

issue 1 solution i.e. align image of paper #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,371 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Step by step DocScanner Algorithm Execution"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# By Abhinav Chauhan\n",
"# Refrence : Murtaza's Workshop (Youtube Channel)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Importing Libraries"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"# Resizing image ( This step is optional)\n",
"widthImg = 500\n",
"heightImg = 540"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-1"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"path = \"1.jpg\"\n",
"img = cv2.imread(path)\n",
"img = cv2.resize(img, (widthImg, heightImg))\n",
"cv2.imshow('Real Image', img)\n",
"cv2.waitKey(0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Image Preprocessing"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-1"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Converting Image to Gray\n",
"\n",
"imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n",
"cv2.imwrite('Gray.jpg', imgGray)\n",
"cv2.imshow('Gray Image', imgGray)\n",
"cv2.waitKey(0)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-1"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Blurring Image\n",
"\n",
"imgBlur = cv2.GaussianBlur(imgGray,(5,5),1)\n",
"cv2.imwrite('Blur.jpg', imgBlur)\n",
"cv2.imshow('Blur Image', imgBlur)\n",
"cv2.waitKey(0)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-1"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Detecting edges using canny edge detector\n",
"\n",
"imgCanny = cv2.Canny(imgBlur,200,200)\n",
"cv2.imwrite('Canny.jpg', imgCanny)\n",
"cv2.imshow('Canny Image', imgCanny)\n",
"cv2.waitKey(0)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-1"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Dialation image\n",
"\n",
"kernel = np.ones((5,5))\n",
"imgDial = cv2.dilate(imgCanny,kernel, iterations = 2)\n",
"cv2.imwrite('Dilated.jpg', imgDial)\n",
"cv2.imshow('Dialated Image', imgDial)\n",
"cv2.waitKey(0)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-1"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Eroded Image (Final image after Perprocessing)\n",
"\n",
"kernel = np.ones((5,5))\n",
"imgThres = cv2.erode(imgDial, kernel, iterations = 1)\n",
"cv2.imwrite('Threshold.jpg', imgThres)\n",
"cv2.imshow('Threshold Image', imgThres)\n",
"cv2.waitKey(0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Function to get the desired countour and draw corner points around it"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[350 38]]\n",
"\n",
" [[105 50]]\n",
"\n",
" [[135 473]]\n",
"\n",
" [[410 433]]]\n"
]
}
],
"source": [
"imgContour = img.copy()\n",
"def getContours(img):\n",
" biggest = np.array([])\n",
" maxArea = 0\n",
" #_, contours, _= cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\n",
" contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\n",
" for cnt in contours:\n",
" area = cv2.contourArea(cnt)\n",
" if area> 5000:\n",
" #cv2.drawContours(imgContour, cnt, -1, (255,0,0), 3)\n",
" peri = cv2.arcLength(cnt, True)\n",
" approx = cv2.approxPolyDP(cnt, 0.03*peri, True)\n",
" if area > maxArea and len(approx) == 4:\n",
" biggest = approx\n",
" maxArea = area\n",
"\n",
" cv2.drawContours(imgContour, biggest, -1, (255,0,0), 20)\n",
" cv2.imwrite('Conttour.jpg', imgContour)\n",
" cv2.imshow('Corner', imgContour)\n",
" cv2.waitKey(0)\n",
" return biggest\n",
"\n",
"biggest = getContours(imgThres)\n",
"print(biggest)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# Now, the above printed biggest variable has the coordinates of the biggest contour detected\n",
"# But they are not in the correct order taken by the Wraping function.\n",
"# So we create another function to reorder them correctly according to the order required by Wraping function \n",
"# i.e (0,0) (width, 0) (0, height) and (width, height)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Function to get correct order of the corner points of the biggest contour detected."
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"def reorder(myPoints):\n",
" myPoints = myPoints.reshape((4,2))\n",
" myPointsNew = np.zeros((4,1,2), np.int32)\n",
" add = myPoints.sum(1)\n",
" myPointsNew[0] = myPoints[np.argmin(add)]\n",
" myPointsNew[3] = myPoints[np.argmax(add)]\n",
" diff = np.diff(myPoints, axis=1)\n",
" myPointsNew[1] = myPoints[np.argmin(diff)]\n",
" myPointsNew[2] = myPoints[np.argmax(diff)]\n",
" return myPointsNew\n",
"\n",
"# We call this fucntion inside the Wraping fucntion which is in the very nect cell"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Function to wrap the image with the ordered points i.e. (0,0) (width, 0) (0, height) and (width, height)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-1"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def getWarp(img, biggest):\n",
" biggest = reorder(biggest)\n",
" pts1 = np.float32(biggest)\n",
" pts2 = np.float32([[0,0],[widthImg,0],[0,heightImg],[widthImg,heightImg]])\n",
" matrix = cv2.getPerspectiveTransform(pts1,pts2)\n",
" imgOutput = cv2.warpPerspective(img,matrix,(widthImg,heightImg))\n",
"\n",
" return imgOutput\n",
"\n",
"imgWarped = getWarp(img,biggest)\n",
"cv2.imwrite('Scanned.jpg', imgWarped)\n",
"cv2.imshow('Scanned Image',imgWarped)\n",
"cv2.waitKey(0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# All the output images at every step are saved and placed in the folder.\n",
"# Also the image will be saved at runtime!"
]
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
6 changes: 6 additions & 0 deletions phase1solution/.ipynb_checkpoints/Untitled-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}
Binary file added phase1solution/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added phase1solution/Blur.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added phase1solution/Canny.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added phase1solution/Conttour.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added phase1solution/Dilated.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading