Skip to content

Commit 2d57d11

Browse files
CamScanner Script (HarshCasper#918)
* Camscanner Script Added * Sample Image and Readme Added * 8 ways to Scan * README updated * README updated * Updated requirements.txt in README * Create requirements.txt Co-authored-by: vybhav72954 <[email protected]>
1 parent 4a7989f commit 2d57d11

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

Python/Camscanner/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Generate Scanned Image
2+
3+
## This script is used to generate a scanned copy of the image provided.
4+
5+
+ On running this script, a scanned file of the orignal image is generated after applying Adaptive Thresholding.
6+
7+
+ It is effective when an image has different lighting conditions in different areas. Here, the algorithm determines the threshold for a pixel based on a small region around it. So we get different thresholds for different regions of the same image which gives better results for images with varying illumination.
8+
9+
+ And this is what done in the script, multiple threshold values are set up in order to give image a scanned and enhanced look.
10+
11+
12+
## To run the script:
13+
14+
1. Setup a virtual environment.
15+
2. Install the dependencies by running ```pip3 install -r requirements.txt```
16+
3. Run the script.
17+
18+
```sh
19+
python camscanner.py \<filename\>
20+
```
21+
22+
## References:
23+
24+
For Thresholding, consider going through [OpenCV Doecumantation](https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html) and this [Article](https://medium.com/@anupriyam/basic-image-thresholding-in-opencv-5af9020f2472).

Python/Camscanner/camscanner.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import sys,cv2
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
5+
if __name__ == '__main__':
6+
7+
# the image that needs to be scanned
8+
filename= sys.argv[1]
9+
10+
img= cv2.imread(filename)
11+
gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
12+
13+
thresh1 = cv2.adaptiveThreshold(gray_img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
14+
cv2.imwrite('try1.jpg',thresh1)
15+
16+
thresh2 = cv2.adaptiveThreshold(gray_img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 31, 3)
17+
cv2.imwrite('try2.jpg',thresh2)
18+
19+
thresh3 = cv2.adaptiveThreshold(gray_img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 13, 5)
20+
cv2.imwrite('try3.jpg',thresh3)
21+
22+
thresh4 = cv2.adaptiveThreshold(gray_img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 4)
23+
cv2.imwrite('try4.jpg',thresh4)
24+
25+
thresh5 = cv2.adaptiveThreshold(gray_img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
26+
cv2.imwrite('try5.jpg',thresh5)
27+
28+
thresh6 = cv2.adaptiveThreshold(gray_img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 31, 5)
29+
cv2.imwrite('try6.jpg',thresh6)
30+
31+
thresh7 = cv2.adaptiveThreshold(gray_img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 21,5 )
32+
cv2.imwrite('try7.jpg',thresh7)
33+
34+
thresh8 = cv2.adaptiveThreshold(gray_img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 5)
35+
cv2.imwrite('try8.jpg',thresh8)
36+
37+
38+
cv2.waitKey(0)
39+
cv2.destroyAllWindows()

Python/Camscanner/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
opencv-python==4.5.1.48
2+
numpy>=1.20
3+
matplotlib==3.1.2

0 commit comments

Comments
 (0)