In [ ]:
!wget http://www7.pcmag.com/media/images/407653-333.jpg?thumb=y

In [ ]:
!mv 407653-333.jpg?thumb=y test2.jpg

In [ ]:
from IPython.display import Image as IpyImage 
IpyImage(filename='test2.jpg')

In [ ]:
from PIL import Image
img1 = Image.open('test2.jpg')
img1.size

In [ ]:
img_a = img1.crop((67,73,91,97))
img_a.save('test_a.jpg')
IpyImage(filename='test_a.jpg')

In [ ]:
img_b = img1.crop((104,73,126,97))
img_b.save('test_b.jpg')
IpyImage(filename='test_b.jpg')

In [ ]:
img_c = img1.crop((137,73,160,97))
img_c.save('test_c.jpg')
IpyImage(filename='test_c.jpg')

In [ ]:
from PIL import Image
img1 = Image.open('Screenshot_2016-02-23-12-47-43.png')
img1.size

In [ ]:
from PIL import Image
img1 = Image.open('Screenshot_2016-02-23-12-47-43.png')
img2 = img1.resize((400,800))
img2.size
img2.save('ldh1.jpg')
from IPython.display import Image as IpyImage 
IpyImage(filename='ldh1.jpg')

In [ ]:
import cv2
import numpy as np
img_rgb = cv2.imread('ldh1.jpg')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread('test_c.jpg',0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
    cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
cv2.imwrite('res.jpg',img_rgb)
IpyImage(filename = 'res.jpg')

In [ ]:
%matplotlib inline
from matplotlib import pyplot as plt
from matplotlib import cm
plt.figure(figsize=(10,15))
test2_gray = cv2.imread('ldh1.jpg', cv2.IMREAD_GRAYSCALE)
plt.imshow(test2_gray, cmap = cm.gray)
plt.show()

In [ ]:
edges2 = cv2.Canny(test2_gray,40,150,L2gradient=True)
plt.figure(figsize=(10,15))
plt.imshow(edges2, cmap = cm.gray)
plt.show()

In [ ]:
import skimage
from skimage.measure import label
plt.figure(figsize=(10,15))
label_bc = label(edges2)
plt.imshow(label_bc)
plt.show()

In [ ]:
from skimage.measure import regionprops
www = regionprops(label_bc)
len(www)

In [ ]:
plt.imshow(www[110].image)
plt.show()

In [ ]:
import matplotlib.patches as mpatches

fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(10, 15))
ax.imshow(test2_gray, cmap = cm.gray)

minr, minc, maxr, maxc = www[235].bbox
rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr, fill=False, edgecolor='blue', linewidth=2)
ax.add_patch(rect)

minr, minc, maxr, maxc = www[110].bbox
rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr, fill=False, edgecolor='red', linewidth=2)
ax.add_patch(rect)

plt.show()

In [ ]:
xxx = www[110].bbox
test1 = test2_gray[xxx[0]+2:xxx[2]-2,xxx[1]+2:xxx[3]-2]
plt.imshow(test1, cmap=cm.gray)
plt.show()

In [ ]:
w, h = test1.shape[::-1]

In [ ]:
import numpy as np
test2_gray = cv2.imread('ldh1.jpg', cv2.IMREAD_GRAYSCALE)
res = cv2.matchTemplate(test2_gray, test1,cv2.TM_CCOEFF_NORMED)
threshold = 0.9
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
    cv2.rectangle(test2_gray, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
plt.figure(figsize=(10,15))
plt.imshow(test2_gray, cmap = cm.gray)
plt.show()