Skip to content

Add flag to preserve color. Small changes to README.md #16

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

Open
wants to merge 4 commits into
base: master
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ On my test dataset of 280 images, the program correctly detected the corners of
This project makes use of the transform and imutils modules from pyimagesearch (which can be accessed [here](http://www.pyimagesearch.com/2014/09/01/build-kick-ass-mobile-document-scanner-just-5-minutes/)). The UI code for the interactive mode is adapted from `poly_editor.py` from [here](https://matplotlib.org/examples/event_handling/poly_editor.html).

* You can manually click and drag the corners of the document to be perspective transformed:
![Example of interactive GUI](https://github.com/andrewdcampbell/doc_scanner/blob/master/ui.gif)
![Example of interactive GUI](readme_media/ui.gif)

* The scanner can also process an entire directory of images automatically and save the output in an output directory:
![Image Directory of images to be processed](https://github.com/andrewdcampbell/doc_scanner/blob/master/before_after.gif)
![Image Directory of images to be processed](readme_media/before_after.gif)

#### Here are some examples of images before and after scan:
<img src="https://github.com/andrewdcampbell/doc_scanner/blob/master/sample_images/cell_pic.jpg" height="450"> <img src="https://github.com/andrewdcampbell/doc_scanner/blob/master/output/cell_pic.jpg" height="450">
<img src="sample_images/cell_pic.jpg" height="450"> <img src="sample_output/cell_pic.jpg" height="450">

<img src="https://github.com/andrewdcampbell/doc_scanner/blob/master/sample_images/receipt.jpg" height="450"> <img src="https://github.com/andrewdcampbell/doc_scanner/blob/master/output/receipt.jpg" height="450">
<img src="sample_images/receipt.jpg" height="450"> <img src="sample_output/receipt.jpg" height="450">

<img src="https://github.com/andrewdcampbell/doc_scanner/blob/master/sample_images/math_cheat_sheet.JPG" height="450"> <img src="https://github.com/andrewdcampbell/doc_scanner/blob/master/output/math_cheat_sheet.JPG" height="450">
<img src="sample_images/math_cheat_sheet.JPG" height="450"> <img src="sample_output/math_cheat_sheet.JPG" height="450">

<img src="https://github.com/andrewdcampbell/doc_scanner/blob/master/sample_images/dollar_bill.JPG" width="350"> <img src="https://github.com/andrewdcampbell/doc_scanner/blob/master/output/dollar_bill.JPG" width="350">
<img src="sample_images/dollar_bill.JPG" width="350"> <img src="sample_output/dollar_bill.JPG" width="350">


### Usage
Expand Down
Binary file removed output/cell_pic.jpg
Binary file not shown.
Binary file removed output/chart.JPG
Binary file not shown.
Binary file removed output/desk.JPG
Binary file not shown.
Binary file removed output/dollar_bill.JPG
Binary file not shown.
Binary file removed output/math_cheat_sheet.JPG
Binary file not shown.
Binary file removed output/notepad.JPG
Binary file not shown.
Binary file removed output/receipt.jpg
Binary file not shown.
Binary file removed output/tax.jpeg
Binary file not shown.
File renamed without changes
File renamed without changes
Binary file added sample_output/cell_pic.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 sample_output/chart.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 sample_output/desk.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 sample_output/dollar_bill.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 sample_output/math_cheat_sheet.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 sample_output/notepad.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 sample_output/receipt.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 sample_output/tax.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 19 additions & 11 deletions scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class DocScanner(object):
"""An image scanner"""

def __init__(self, interactive=False, MIN_QUAD_AREA_RATIO=0.25, MAX_QUAD_ANGLE_RANGE=40):
def __init__(self, interactive=False, color_preserve=False, MIN_QUAD_AREA_RATIO=0.25, MAX_QUAD_ANGLE_RANGE=40):
"""
Args:
interactive (boolean): If True, user can adjust screen contour before
Expand All @@ -37,6 +37,7 @@ def __init__(self, interactive=False, MIN_QUAD_AREA_RATIO=0.25, MAX_QUAD_ANGLE_R
of its interior angles exceeds MAX_QUAD_ANGLE_RANGE. Defaults to 40.
"""
self.interactive = interactive
self.color_preserve = color_preserve
self.MIN_QUAD_AREA_RATIO = MIN_QUAD_AREA_RATIO
self.MAX_QUAD_ANGLE_RANGE = MAX_QUAD_ANGLE_RANGE

Expand Down Expand Up @@ -285,21 +286,24 @@ def scan(self, image_path):
screenCnt = self.interactive_get_contour(screenCnt, rescaled_image)

# apply the perspective transformation
warped = transform.four_point_transform(orig, screenCnt * ratio)
output = transform.four_point_transform(orig, screenCnt * ratio) #warped

# convert the warped image to grayscale
gray = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
if self.color_preserve == False:

# sharpen image
sharpen = cv2.GaussianBlur(gray, (0,0), 3)
sharpen = cv2.addWeighted(gray, 1.5, sharpen, -0.5, 0)
# convert the warped image to grayscale
gray = cv2.cvtColor(output, cv2.COLOR_BGR2GRAY)

# sharpen image
sharpen = cv2.GaussianBlur(gray, (0,0), 3)
sharpen = cv2.addWeighted(gray, 1.5, sharpen, -0.5, 0)

# apply adaptive threshold to get black and white effect
output = cv2.adaptiveThreshold(sharpen, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 21, 15) #thresh

# apply adaptive threshold to get black and white effect
thresh = cv2.adaptiveThreshold(sharpen, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 21, 15)

# save the transformed image
basename = os.path.basename(image_path)
cv2.imwrite(OUTPUT_DIR + '/' + basename, thresh)
cv2.imwrite(OUTPUT_DIR + '/' + basename, output)
print("Proccessed " + basename)


Expand All @@ -310,13 +314,17 @@ def scan(self, image_path):
group.add_argument("--image", help="Path to single image to be scanned")
ap.add_argument("-i", action='store_true',
help = "Flag for manually verifying and/or setting document corners")
ap.add_argument("-c", action='store_true',
help = "Flag to preserve color on the cropped images")

args = vars(ap.parse_args())
im_dir = args["images"]
im_file_path = args["image"]
interactive_mode = args["i"]
color_preserve_mode = args["c"]

scanner = DocScanner(interactive_mode)
scanner = DocScanner(interactive=interactive_mode,
color_preserve=color_preserve_mode)

valid_formats = [".jpg", ".jpeg", ".jp2", ".png", ".bmp", ".tiff", ".tif"]

Expand Down