In [1]:
%matplotlib inline
In [2]:
import numpy as np
import matplotlib.pyplot as plt
import boto3
import skimage.io as io
import skimage
from skimage import data
from skimage import feature
from skimage import color
from skimage import filters
from skimage import transform
from skimage import draw
from skimage import measure
from skimage import morphology
import skdemo
from scipy import ndimage as ndi
from toolbox.TAO.tao import TAO
from toolbox.TAO.taoDB import TAOdb
In [3]:
# get an image
tao = TAO()
taoDB = TAOdb()
url = taoDB.get_aperture_image_url('239849_7R', '1', '000000')
im = io.imread(url)
skdemo.imshow_with_histogram(im);
In [50]:
# crop the image to the interesting part, convert to HSV
xoff = 100
yoff = 300
imc = im[yoff:, xoff:1000, :]
#print (imc.dtype, imc.max())
imhsv = color.rgb2hsv(imc)
imgray = color.rgb2gray(imc)
print (imgray.dtype, imgray.max())
In [51]:
skdemo.imshow_with_histogram(imhsv[:,:,:]);
In [52]:
#convert to HSV space
imv = imhsv[:,:,0]
threshold = filters.threshold_otsu(imv )
print threshold
imb = imv < threshold
#skdemo.imshow_with_histogram(imb);
imb = skimage.img_as_int(imb)
# label it
imlabel = morphology.label(imb)
# remove small blobs
imnosmall = morphology.remove_small_objects(imlabel, min_size=4000, connectivity=1, in_place=False)
# fill small holes
imnosmall = morphology.remove_small_holes(imnosmall, min_size=200, connectivity=2, in_place=False)
# trim tendrils
se = morphology.disk(5)
imnosmall = morphology.opening(imnosmall, se)
# remove small blobs
imnosmall = morphology.remove_small_objects(imnosmall, min_size=4000, connectivity=1, in_place=False)
fig, (ax_1, ax_2, ax_3) = plt.subplots(ncols=3, figsize=(10, 5))
ax_1.imshow(imb, cmap='gray')
ax_1.set_title('segmented Hue')
ax_2.imshow(imlabel, cmap='jet')
ax_2.set_title('labeled')
ax_3.imshow(imnosmall, cmap='gray')
ax_3.set_title('after morphology')
plt.show()
In [53]:
print (imnosmall.dtype, imnosmall.max(), imnosmall.shape)
image = skimage.img_as_ubyte(imgray)
print (image.dtype, image.max(), image.shape)
image[:,:] = 0
image[imnosmall != 0] = 255
#skdemo.imshow_with_histogram(image);
In [54]:
# Line finding using the Probabilistic Hough Transform.
edges = feature.canny(image, sigma=4.0)
lines = transform.probabilistic_hough_line(edges)
fig, (ax0, ax1, ax2) = plt.subplots(1, 3, figsize=(10, 5), sharex=True,
sharey=True)
ax0.imshow(image, 'gray')
ax0.set_title('Input image')
ax1.imshow(edges, 'gray')
ax1.set_title('Canny edges')
ax2.imshow(imc)
for line in lines:
p0, p1 = line
ax2.plot((p0[0], p1[0]), (p0[1], p1[1]), linewidth=3.0)
row2, col2 = image.shape
ax2.axis((0, col2, row2, 0))
ax2.set_title('Probabilistic Hough')
Out[54]:
In [55]:
# Much better solution: find contours and then subsample the polygon
contours = measure.find_contours(image, 0.5)
tolerance1 = 2.5
tolerance2 = 5.0
fig, (ax0, ax1, ax2) = plt.subplots(1, 3, figsize=(10, 5), sharex=True,
sharey=True)
ax0.imshow(image, 'gray')
ax0.set_title('Input image')
ax1.imshow(edges, 'gray')
ax1.set_title('approximate_polygon: ' + str(tolerance1))
ax2.imshow(edges, 'gray')
ax2.set_title('approximate_polygon: ' + str(tolerance2))
for contour in measure.find_contours(image, 0):
coords = measure.approximate_polygon(contour, tolerance=tolerance1)
ax1.plot(coords[:, 1], coords[:, 0], '-r', linewidth=4)
coords2 = measure.approximate_polygon(contour, tolerance2)
ax2.plot(coords2[:, 1], coords2[:, 0], '-g', linewidth=4)
print("Number of coordinates:", len(contour), len(coords), len(coords2))
In [ ]: