ProfFan
2016-06-05 18:31:15 +08:00
POC
```
import numpy as np
import matplotlib.pyplot as plt
import skimage.color
from skimage import data
from scipy import misc
from skimage.feature import match_template
def rotate(image, angle, center = None, scale = 1.0):
(h, w) = image.shape[:2]
if center is None:
center = (w / 2, h / 2)
# Perform the rotation
M = cv2.getRotationMatrix2D(center, angle, scale)
rotated = cv2.warpAffine(image, M, (w, h))
return rotated
captcha_q = skimage.color.rgb2gray(misc.imread("captcha_q.jpg"))
captcha_a = skimage.color.rgb2gray(misc.imread("captcha_a.jpg"))
image = (captcha_a<0.37)
#coin_raw = (captcha_q<0.37)[15:,55:79]
#coin_raw = (captcha_q<0.37)[15:,12:35]
#coin_raw = (captcha_q<0.37)[15:,79:101]
coin_raw = (captcha_q<0.37)[15:,36:55]
coin_rot = rotate(np.asarray((coin_raw)*255, dtype=np.uint8),-20)
coin = cv2.resize(coin_rot,
tuple(int(1.5*x) for x in coin_rot.T.shape),
interpolation = cv2.INTER_AREA)
result = match_template(image, coin)
ij = np.unravel_index(np.argmax(result), result.shape)
x, y = ij[::-1]
fig, (ax1, ax2, ax3) = plt.subplots(1, 3,gridspec_kw = {'width_ratios':[1, 4, 3]},figsize=(12, 6))
ax1.imshow(coin)
ax1.set_axis_off()
ax1.set_title('char')
ax2.imshow(image)
ax2.set_axis_off()
ax2.set_title('image')
# highlight matched region
hcoin, wcoin = coin.shape
rect = plt.Rectangle((x, y), wcoin, hcoin, edgecolor='r', facecolor='none')
ax2.add_patch(rect)
ax3.imshow(result)
ax3.set_axis_off()
ax3.set_title('matched')
# highlight matched region
ax3.autoscale(False)
ax3.plot(x, y, 'o', markeredgecolor='r', markerfacecolor='none', markersize=10)
plt.show()
```