mirror of
https://github.com/wassname/scikit-image.git
synced 2026-08-11 11:25:30 +08:00
Solving white space + improving code +PEP8
Solving white space + Correcting code Solving white spaces Solving white spaces Solving white spaces Answering comments Correcting silly mistakes Solving white spaces Trying again... now with Travis enabled
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
r"""
|
||||
"""
|
||||
=============================
|
||||
Straight line Hough transform
|
||||
=============================
|
||||
@@ -6,7 +6,7 @@ Straight line Hough transform
|
||||
The Hough transform in its simplest form is a `method to detect straight lines
|
||||
<http://en.wikipedia.org/wiki/Hough_transform>`__.
|
||||
|
||||
In the following example, we construct an image with a line intersection. We
|
||||
In the following example, we construct an image with a line intersection. We
|
||||
then use the Hough transform to explore a parameter space for straight lines
|
||||
that may run through the image.
|
||||
|
||||
@@ -53,9 +53,9 @@ References
|
||||
.. [2] Duda, R. O. and P. E. Hart, "Use of the Hough Transformation to
|
||||
Detect Lines and Curves in Pictures," Comm. ACM, Vol. 15,
|
||||
pp. 11-15 (January, 1972)
|
||||
|
||||
"""
|
||||
|
||||
from matplotlib import cm
|
||||
from skimage.transform import (hough_line, hough_line_peaks,
|
||||
probabilistic_hough_line)
|
||||
from skimage.feature import canny
|
||||
@@ -64,70 +64,71 @@ from skimage import data
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# Construct test image
|
||||
|
||||
# Constructing test image.
|
||||
image = np.zeros((100, 100))
|
||||
|
||||
|
||||
# Classic straight-line Hough transform
|
||||
|
||||
idx = np.arange(25, 75)
|
||||
image[idx[::-1], idx] = 255
|
||||
image[idx, idx] = 255
|
||||
|
||||
# Classic straight-line Hough transform.
|
||||
h, theta, d = hough_line(image)
|
||||
|
||||
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(8,4))
|
||||
# Generating figure 1.
|
||||
fig, (ax0, ax1, ax2) = plt.subplots(1, 3, figsize=(12, 6))
|
||||
plt.tight_layout()
|
||||
|
||||
ax1.imshow(image, cmap=plt.cm.gray)
|
||||
ax1.set_title('Input image')
|
||||
ax1.set_axis_off()
|
||||
ax0.imshow(image, cmap=cm.gray)
|
||||
ax0.set_title('Input image')
|
||||
ax0.set_axis_off()
|
||||
|
||||
ax2.imshow(np.log(1 + h),
|
||||
extent=[np.rad2deg(theta[-1]), np.rad2deg(theta[0]),
|
||||
d[-1], d[0]],
|
||||
cmap=plt.cm.gray, aspect=1/1.5)
|
||||
ax2.set_title('Hough transform')
|
||||
ax2.set_xlabel('Angles (degrees)')
|
||||
ax2.set_ylabel('Distance (pixels)')
|
||||
ax2.axis('image')
|
||||
ax1.imshow(np.log(1 + h), extent=[np.rad2deg(theta[-1]), np.rad2deg(theta[0]),
|
||||
d[-1], d[0]], cmap=cm.gray, aspect=1/1.5)
|
||||
ax1.set_title('Hough transform')
|
||||
ax1.set_xlabel('Angles (degrees)')
|
||||
ax1.set_ylabel('Distance (pixels)')
|
||||
ax1.axis('image')
|
||||
|
||||
ax3.imshow(image, cmap=plt.cm.gray)
|
||||
rows, cols = image.shape
|
||||
ax2.imshow(image, cmap=cm.gray)
|
||||
row1, col1 = image.shape
|
||||
for _, angle, dist in zip(*hough_line_peaks(h, theta, d)):
|
||||
y0 = (dist - 0 * np.cos(angle)) / np.sin(angle)
|
||||
y1 = (dist - cols * np.cos(angle)) / np.sin(angle)
|
||||
ax3.plot((0, cols), (y0, y1), '-r')
|
||||
ax3.axis((0, cols, rows, 0))
|
||||
ax3.set_title('Detected lines')
|
||||
ax3.set_axis_off()
|
||||
|
||||
# Line finding, using the Probabilistic Hough Transform
|
||||
y1 = (dist - col1 * np.cos(angle)) / np.sin(angle)
|
||||
ax2.plot((0, col1), (y0, y1), '-r')
|
||||
ax2.axis((0, col1, row1, 0))
|
||||
ax2.set_title('Detected lines')
|
||||
ax2.set_axis_off()
|
||||
|
||||
# Line finding using the Probabilistic Hough Transform.
|
||||
image = data.camera()
|
||||
edges = canny(image, 2, 1, 25)
|
||||
lines = probabilistic_hough_line(edges, threshold=10, line_length=5,
|
||||
line_gap=3)
|
||||
|
||||
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(8,4), sharex=True, sharey=True)
|
||||
# Generating figure 2.
|
||||
fig, (ax0, ax1, ax2) = plt.subplots(1, 3, figsize=(16, 6), sharex=True,
|
||||
sharey=True)
|
||||
plt.tight_layout()
|
||||
|
||||
ax1.imshow(image, cmap=plt.cm.gray)
|
||||
ax1.set_title('Input image')
|
||||
ax0.imshow(image, cmap=cm.gray)
|
||||
ax0.set_title('Input image')
|
||||
ax0.set_axis_off()
|
||||
ax0.set_adjustable('box-forced')
|
||||
|
||||
ax1.imshow(edges, cmap=cm.gray)
|
||||
ax1.set_title('Canny edges')
|
||||
ax1.set_axis_off()
|
||||
ax1.set_adjustable('box-forced')
|
||||
|
||||
ax2.imshow(edges, cmap=plt.cm.gray)
|
||||
ax2.set_title('Canny edges')
|
||||
ax2.imshow(edges * 0)
|
||||
for line in lines:
|
||||
p0, p1 = line
|
||||
ax2.plot((p0[0], p1[0]), (p0[1], p1[1]))
|
||||
|
||||
row2, col2 = image.shape
|
||||
ax2.axis((0, col2, row2, 0))
|
||||
|
||||
ax2.set_title('Probabilistic Hough')
|
||||
ax2.set_axis_off()
|
||||
ax2.set_adjustable('box-forced')
|
||||
|
||||
ax3.imshow(edges * 0)
|
||||
|
||||
for line in lines:
|
||||
p0, p1 = line
|
||||
ax3.plot((p0[0], p1[0]), (p0[1], p1[1]))
|
||||
|
||||
ax3.set_title('Probabilistic Hough')
|
||||
ax3.set_axis_off()
|
||||
ax3.set_adjustable('box-forced')
|
||||
plt.show()
|
||||
|
||||
@@ -34,19 +34,20 @@ independent of the size of blobs as internally the implementation uses
|
||||
box filters instead of convolutions. Bright on dark as well as dark on
|
||||
bright blobs are detected. The downside is that small blobs (<3px) are not
|
||||
detected accurately. See :py:meth:`skimage.feature.blob_doh` for usage.
|
||||
|
||||
"""
|
||||
|
||||
from matplotlib import pyplot as plt
|
||||
from skimage import data
|
||||
from skimage.feature import blob_dog, blob_log, blob_doh
|
||||
from math import sqrt
|
||||
from skimage.color import rgb2gray
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
image = data.hubble_deep_field()[0:500, 0:500]
|
||||
image_gray = rgb2gray(image)
|
||||
|
||||
blobs_log = blob_log(image_gray, max_sigma=30, num_sigma=10, threshold=.1)
|
||||
|
||||
# Compute radii in the 3rd column.
|
||||
blobs_log[:, 2] = blobs_log[:, 2] * sqrt(2)
|
||||
|
||||
@@ -61,14 +62,17 @@ titles = ['Laplacian of Gaussian', 'Difference of Gaussian',
|
||||
'Determinant of Hessian']
|
||||
sequence = zip(blobs_list, colors, titles)
|
||||
|
||||
fig, axes = plt.subplots(1, 3, figsize=(14, 4), sharex=True, sharey=True,
|
||||
subplot_kw={'adjustable': 'box-forced'})
|
||||
plt.tight_layout()
|
||||
|
||||
fig,axes = plt.subplots(1, 3, sharex=True, sharey=True, subplot_kw={'adjustable':'box-forced'})
|
||||
axes = axes.ravel()
|
||||
for blobs, color, title in sequence:
|
||||
ax = axes[0]
|
||||
axes = axes[1:]
|
||||
ax.set_title(title)
|
||||
ax.imshow(image, interpolation='nearest')
|
||||
ax.set_axis_off()
|
||||
for blob in blobs:
|
||||
y, x, r = blob
|
||||
c = plt.Circle((x, y), r, color=color, linewidth=2, fill=False)
|
||||
|
||||
@@ -10,23 +10,21 @@ structuring element.
|
||||
|
||||
The example compares the local threshold with the global threshold.
|
||||
|
||||
.. note: local is much slower than global thresholding
|
||||
.. Note: local is much slower than global thresholding
|
||||
|
||||
.. [1] http://en.wikipedia.org/wiki/Otsu's_method
|
||||
|
||||
"""
|
||||
import matplotlib
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
from skimage import data
|
||||
from skimage.morphology import disk
|
||||
from skimage.filters import threshold_otsu, rank
|
||||
from skimage.util import img_as_ubyte
|
||||
|
||||
import matplotlib
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
matplotlib.rcParams['font.size'] = 9
|
||||
|
||||
|
||||
img = img_as_ubyte(data.page())
|
||||
|
||||
radius = 15
|
||||
@@ -36,26 +34,26 @@ local_otsu = rank.otsu(img, selem)
|
||||
threshold_global_otsu = threshold_otsu(img)
|
||||
global_otsu = img >= threshold_global_otsu
|
||||
|
||||
fig, ax = plt.subplots(2, 2, figsize=(8, 5), sharex=True, sharey=True,
|
||||
subplot_kw={'adjustable': 'box-forced'})
|
||||
ax0, ax1, ax2, ax3 = ax.ravel()
|
||||
|
||||
fig, ax = plt.subplots(2, 2, figsize=(8, 5), sharex=True, sharey=True, subplot_kw={'adjustable':'box-forced'})
|
||||
ax1, ax2, ax3, ax4 = ax.ravel()
|
||||
fig.colorbar(ax0.imshow(img, cmap=plt.cm.gray),
|
||||
ax=ax0, orientation='horizontal')
|
||||
ax0.set_title('Original')
|
||||
ax0.axis('off')
|
||||
|
||||
fig.colorbar(ax1.imshow(img, cmap=plt.cm.gray),
|
||||
fig.colorbar(ax1.imshow(local_otsu, cmap=plt.cm.gray),
|
||||
ax=ax1, orientation='horizontal')
|
||||
ax1.set_title('Original')
|
||||
ax1.set_title('Local Otsu (radius=%d)' % radius)
|
||||
ax1.axis('off')
|
||||
|
||||
fig.colorbar(ax2.imshow(local_otsu, cmap=plt.cm.gray),
|
||||
ax=ax2, orientation='horizontal')
|
||||
ax2.set_title('Local Otsu (radius=%d)' % radius)
|
||||
ax2.imshow(img >= local_otsu, cmap=plt.cm.gray)
|
||||
ax2.set_title('Original >= Local Otsu' % threshold_global_otsu)
|
||||
ax2.axis('off')
|
||||
|
||||
ax3.imshow(img >= local_otsu, cmap=plt.cm.gray)
|
||||
ax3.set_title('Original >= Local Otsu' % threshold_global_otsu)
|
||||
ax3.imshow(global_otsu, cmap=plt.cm.gray)
|
||||
ax3.set_title('Global Otsu (threshold = %d)' % threshold_global_otsu)
|
||||
ax3.axis('off')
|
||||
|
||||
ax4.imshow(global_otsu, cmap=plt.cm.gray)
|
||||
ax4.set_title('Global Otsu (threshold = %d)' % threshold_global_otsu)
|
||||
ax4.axis('off')
|
||||
|
||||
plt.show()
|
||||
|
||||
@@ -19,19 +19,14 @@ but with very different mean structural similarity indices.
|
||||
assessment: From error visibility to structural similarity," IEEE
|
||||
Transactions on Image Processing, vol. 13, no. 4, pp. 600-612,
|
||||
Apr. 2004.
|
||||
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
import matplotlib
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
from skimage import data, img_as_float
|
||||
from skimage.measure import structural_similarity as ssim
|
||||
|
||||
|
||||
matplotlib.rcParams['font.size'] = 9
|
||||
|
||||
|
||||
img = img_as_float(data.camera())
|
||||
rows, cols = img.shape
|
||||
|
||||
@@ -45,7 +40,10 @@ def mse(x, y):
|
||||
img_noise = img + noise
|
||||
img_const = img + abs(noise)
|
||||
|
||||
fig, (ax0, ax1, ax2) = plt.subplots(nrows=1, ncols=3, figsize=(8, 4), sharex=True, sharey=True, subplot_kw={'adjustable':'box-forced'})
|
||||
fig, (ax0, ax1, ax2) = plt.subplots(nrows=1, ncols=3, figsize=(16, 6),
|
||||
sharex=True, sharey=True,
|
||||
subplot_kw={'adjustable': 'box-forced'})
|
||||
plt.tight_layout()
|
||||
|
||||
mse_none = mse(img, img)
|
||||
ssim_none = ssim(img, img, dynamic_range=img.max() - img.min())
|
||||
@@ -63,13 +61,16 @@ label = 'MSE: %2.f, SSIM: %.2f'
|
||||
ax0.imshow(img, cmap=plt.cm.gray, vmin=0, vmax=1)
|
||||
ax0.set_xlabel(label % (mse_none, ssim_none))
|
||||
ax0.set_title('Original image')
|
||||
ax0.axes.get_yaxis().set_visible(False)
|
||||
|
||||
ax1.imshow(img_noise, cmap=plt.cm.gray, vmin=0, vmax=1)
|
||||
ax1.set_xlabel(label % (mse_noise, ssim_noise))
|
||||
ax1.set_title('Image with noise')
|
||||
ax1.axes.get_yaxis().set_visible(False)
|
||||
|
||||
ax2.imshow(img_const, cmap=plt.cm.gray, vmin=0, vmax=1)
|
||||
ax2.set_xlabel(label % (mse_const, ssim_const))
|
||||
ax2.set_title('Image plus constant')
|
||||
ax2.axes.get_yaxis().set_visible(False)
|
||||
|
||||
plt.show()
|
||||
|
||||
Reference in New Issue
Block a user