Last active
September 26, 2015 04:54
-
-
Save agmarrugo/0523b7c1333364d4f461 to your computer and use it in GitHub Desktop.
A script for processing whitelinesLink images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/local/bin/python | |
# -*- coding: utf-8 -*- | |
""" | |
A script for processing whitelinesLink images | |
Created on Wed Sep 23 08:31:23 2015 | |
@author: Andres Marrugo - andresmarrugo.net | |
""" | |
import numpy as np | |
from PIL import Image | |
import matplotlib.pylab as plt | |
from skimage.filters import threshold_otsu | |
from scipy import ndimage | |
import argparse | |
# construct the argument parse and parse the arguments | |
ap = argparse.ArgumentParser() | |
ap.add_argument("input", | |
help="path to the input image file") | |
ap.add_argument("output", | |
help="path to the input image file") | |
# ap.add_argument("-o", "--output", | |
# help="path to the output image file") | |
args = vars(ap.parse_args()) | |
# Read image | |
im = plt.array(Image.open(args['input'])) | |
height, width, channels = im.shape | |
# Read grayscale image | |
im_gray = plt.array(Image.open(args['input']).convert('L')) | |
# The masking works better if we first blur the gray scale image | |
im_gray = ndimage.gaussian_filter(im_gray, 3) | |
# Compute threshold using Otsu's method | |
thresh = threshold_otsu(im_gray) | |
# Compute mask - a fixed threshold of about 158 should also work. | |
mask = im_gray < thresh | |
# Create a white rgb image | |
rgb_image = np.ones((height,width,3), np.uint8)*255 | |
# index with mask to assign only the pixels of interest | |
rgb_image[mask,0] = im[mask,0] | |
rgb_image[mask,1] = im[mask,1] | |
rgb_image[mask,2] = im[mask,2] | |
resultImage = Image.fromarray(rgb_image) | |
resultImage.save(args['output']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment