import cv2
import matplotlib.pyplot as plt
from IPython.html import widgets
%matplotlib inline
trex = cv2.imread("data/images/trex.png")[:,:,::-1]
plt.imshow(trex)
<matplotlib.image.AxesImage at 0x7fd877facf50>
fig, axes = plt.subplots(2, 2, figsize = (8, 8))
fig.tight_layout()
axes = axes.ravel()
axes[0].set_title("original")
axes[0].imshow(trex)
for i, k in enumerate([3, 5, 7], 1):
axes[i].imshow(cv2.blur(trex, (k, k)))
axes[i].set_title("blur with window %i" % k)
fig, axes = plt.subplots(2, 2, figsize = (8, 8))
fig.tight_layout()
axes = axes.ravel()
axes[0].set_title("original")
axes[0].imshow(trex)
for i, k in enumerate([3, 5, 7], 1):
axes[i].imshow(cv2.GaussianBlur(trex, (k, k), 0))
axes[i].set_title("blur with window %i" % k)
fig, axes = plt.subplots(2, 2, figsize = (8, 8))
fig.tight_layout()
axes = axes.ravel()
axes[0].set_title("original")
axes[0].imshow(trex)
for i, k in enumerate([3, 5, 7], 1):
axes[i].imshow(cv2.medianBlur(trex, k))
axes[i].set_title("blur with window %i" % k)
fig, axes = plt.subplots(2, 2, figsize = (8, 8))
fig.tight_layout()
axes = axes.ravel()
axes[0].set_title("original")
axes[0].imshow(trex)
for i, (k,spatial_sigma, color_sigma) in enumerate([(5, 21, 21), (7, 31, 31), (9, 41, 42)], 1):
axes[i].imshow(cv2.bilateralFilter(trex, k, spatial_sigma, color_sigma, ))
axes[i].set_title("blur with window %i" % k)
coins = cv2.imread("data/images/coins.png")[:,:,::-1]
plt.imshow(coins)
coins_gray = cv2.cvtColor(coins, cv2.COLOR_RGB2GRAY)
plt.figure()
plt.imshow(coins_gray, cmap = plt.cm.gray)
gaussian_coins = cv2.GaussianBlur(coins_gray, (5, 5), 0)
plt.figure()
plt.imshow(gaussian_coins, cmap = plt.cm.gray)
<matplotlib.image.AxesImage at 0x7fd86e53a790>
coin_hist = cv2.calcHist([coins_gray], [0], None, [256], [0, 256])
plt.plot(coin_hist)
plt.axvline(185, color = "red")
<matplotlib.lines.Line2D at 0x7fd86e7003d0>
thr, binary_coins = cv2.threshold(coins_gray, 155, 255, cv2.THRESH_BINARY)
plt.imshow(binary_coins, cmap = plt.cm.gray)
thr, mask = cv2.threshold(coins_gray, 155, 255, cv2.THRESH_BINARY_INV)
masked_coins = cv2.bitwise_and(coins_gray, coins_gray, mask = mask)
plt.figure()
plt.imshow(masked_coins, cmap = plt.cm.gray)
<matplotlib.image.AxesImage at 0x7fd86e435e90>
## TRY reduce the noise first
## simple filtering has IMPROVED THE RESULT
thr, binary_coins = cv2.threshold(gaussian_coins, 155, 255, cv2.THRESH_BINARY)
plt.imshow(binary_coins, cmap = plt.cm.gray)
thr, mask = cv2.threshold(gaussian_coins, 155, 255, cv2.THRESH_BINARY_INV)
masked_coins = cv2.bitwise_and(coins_gray, coins_gray, mask = mask)
plt.figure()
plt.imshow(masked_coins, cmap = plt.cm.gray)
<matplotlib.image.AxesImage at 0x7fd86e36d210>
mask = cv2.adaptiveThreshold(gaussian_coins, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 11, 5)
plt.imshow(mask, cmap = plt.cm.gray)
plt.figure()
plt.imshow(cv2.bitwise_and(coins_gray, coins_gray, mask = mask), cmap = plt.cm.gray)
<matplotlib.image.AxesImage at 0x7fd86d01a150>
def plot_gaussian_adaptive_thr(K, C):
mask = cv2.adaptiveThreshold(gaussian_coins, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, K, C, )
plt.imshow(mask, cmap = plt.cm.gray)
plt.figure()
plt.imshow(cv2.bitwise_and(coins_gray, coins_gray, mask = mask), cmap = plt.cm.gray)
widgets.interact(plot_gaussian_adaptive_thr, K = (1, 251, 2), C = (1, 251, 2))
import mahotas as mh
T = mh.otsu(coins_gray, )
print T
135
thr, binary_coins = cv2.threshold(gaussian_coins, T, 255, cv2.THRESH_BINARY)
plt.imshow(binary_coins, cmap = plt.cm.gray)
thr, mask = cv2.threshold(gaussian_coins, T, 255, cv2.THRESH_BINARY_INV)
masked_coins = cv2.bitwise_and(coins_gray, coins_gray, mask = mask)
plt.figure()
plt.imshow(masked_coins, cmap = plt.cm.gray)
<matplotlib.image.AxesImage at 0x7fd85b9cc650>
thr, mask = cv2.threshold(gaussian_coins, 155, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
masked_coins = cv2.bitwise_and(coins_gray, coins_gray, mask = mask)
plt.figure()
plt.imshow(masked_coins, cmap = plt.cm.gray)
print thr
137.0