In [3]:
import cv2
import numpy as np
import matplotlib.pyplot as plt
import os
%matplotlib inline

In [139]:
image = cv2.imread('./1.jpg')

In [140]:
plt.imshow(image)


Out[140]:
<matplotlib.image.AxesImage at 0x114b7d470>
/Users/kazumatamaki/.pyenv/versions/anaconda3-2.3.0/lib/python3.4/site-packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family ['IPAexGothic'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))

In [166]:
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

In [167]:
plt.imshow(image_rgb)


Out[167]:
<matplotlib.image.AxesImage at 0x11582eac8>
/Users/kazumatamaki/.pyenv/versions/anaconda3-2.3.0/lib/python3.4/site-packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family ['IPAexGothic'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))

In [129]:
image_gray = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2GRAY)

In [130]:
plt.imshow(image_gray, cmap='gray')


Out[130]:
<matplotlib.image.AxesImage at 0x1144d72e8>
/Users/kazumatamaki/.pyenv/versions/anaconda3-2.3.0/lib/python3.4/site-packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family ['IPAexGothic'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))

In [131]:
thresh_image = cv2.threshold(image_gray, 100, 255, cv2.THRESH_BINARY)[1]
plt.imshow(thresh_image, cmap='gray')


Out[131]:
<matplotlib.image.AxesImage at 0x1145c64e0>
/Users/kazumatamaki/.pyenv/versions/anaconda3-2.3.0/lib/python3.4/site-packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family ['IPAexGothic'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))

In [132]:
find_image, contours, hierarchy = cv2.findContours(thresh_image, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

In [133]:
plt.imshow(find_image, cmap='gray')


Out[133]:
<matplotlib.image.AxesImage at 0x114793320>
/Users/kazumatamaki/.pyenv/versions/anaconda3-2.3.0/lib/python3.4/site-packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family ['IPAexGothic'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))

In [134]:
draw_image = cv2.drawContours(find_image, contours, -1, (0, 0, 255), 3)

In [135]:
plt.imshow(draw_image, cmap='gray')


Out[135]:
<matplotlib.image.AxesImage at 0x114847390>
/Users/kazumatamaki/.pyenv/versions/anaconda3-2.3.0/lib/python3.4/site-packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family ['IPAexGothic'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))

In [145]:
draw_image = cv2.drawContours(image_rgb, contours, -1, (0, 255, 0), 3)

In [146]:
plt.imshow(draw_image)


Out[146]:
<matplotlib.image.AxesImage at 0x112f21908>
/Users/kazumatamaki/.pyenv/versions/anaconda3-2.3.0/lib/python3.4/site-packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family ['IPAexGothic'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))

In [162]:
print(contours[4])


[[[471 434]]]

In [147]:
len(contours)


Out[147]:
1443

In [148]:
th_area = image_rgb.shape[0] * image_rgb.shape[1] / 100
contours_large = list(filter(lambda c:cv2.contourArea(c) > th_area, contours))

In [151]:
def getRectByPoints(points):
    # prepare simple array 
    points = list(map(lambda x: x[0], points))

    points = sorted(points, key=lambda x:x[1])
    top_points = sorted(points[:2], key=lambda x:x[0])
    bottom_points = sorted(points[2:4], key=lambda x:x[0])
    points = top_points + bottom_points

    left = min(points[0][0], points[2][0])
    right = max(points[1][0], points[3][0])
    top = min(points[0][1], points[1][1])
    bottom = max(points[2][1], points[3][1])
    return (top, bottom, left, right)

def getPartImageByRect(rect):
    img = cv2.imread('./1.jpg', 1)
    return img[rect[0]:rect[1], rect[2]:rect[3]]

In [168]:
outputs = []
rects = []
approxes = []

for (i,cnt) in enumerate(contours_large):
    arclen = cv2.arcLength(cnt, True)  # 面積の計算
    approx = cv2.approxPolyDP(cnt, 0.02*arclen, True)  # 周囲長を計算(要は多角形の辺の総和)
    # 小さいやつは除外
    if len(approx) < 4:
        continue
    approxes.append(approx)
    x, y, w, h = cv2.boundingRect(cnt)
    outputs.append(cv2.rectangle(image_rgb,(x,y),(x+w,y+h), (0,255,0), 2))

In [173]:
len(outputs)


Out[173]:
2

In [171]:
plt.imshow(outputs[0])


Out[171]:
<matplotlib.image.AxesImage at 0x115f31240>
/Users/kazumatamaki/.pyenv/versions/anaconda3-2.3.0/lib/python3.4/site-packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family ['IPAexGothic'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))

In [152]:
outputs = []
rects = []
approxes = []

for (i,cnt) in enumerate(contours_large):
    arclen = cv2.arcLength(cnt, True)  # 面積の計算
    approx = cv2.approxPolyDP(cnt, 0.02*arclen, True)  # 周囲長を計算(要は多角形の辺の総和)
    # 小さいやつは除外
    if len(approx) < 4:
        continue
    approxes.append(approx)
    rect = getRectByPoints(approx)
    rects.append(rect)
    outputs.append(getPartImageByRect(rect))

In [153]:
len(rects)


Out[153]:
2

In [154]:
rects[0]


Out[154]:
(115, 341, 547, 598)

In [155]:
len(outputs)


Out[155]:
2

In [157]:
plt.imshow(outputs[1])


Out[157]:
<matplotlib.image.AxesImage at 0x112bdb828>
/Users/kazumatamaki/.pyenv/versions/anaconda3-2.3.0/lib/python3.4/site-packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family ['IPAexGothic'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))

In [105]:
draw_image = cv2.drawContours(image_rgb, approxes, -1, (255, 0, 0), 3)
plt.imshow(draw_image)


Out[105]:
<matplotlib.image.AxesImage at 0x113d4e588>
/Users/kazumatamaki/.pyenv/versions/anaconda3-2.3.0/lib/python3.4/site-packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family ['IPAexGothic'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))

In [192]:
image_2 = cv2.imread('./2.jpg')

In [193]:
image_2_gray = cv2.cvtColor(image_2, cv2.COLOR_BGR2GRAY)

In [194]:
plt.imshow(image_2_gray, cmap='gray')


Out[194]:
<matplotlib.image.AxesImage at 0x1171af2e8>
/Users/kazumatamaki/.pyenv/versions/anaconda3-2.3.0/lib/python3.4/site-packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family ['IPAexGothic'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))

In [177]:
thresh_image = cv2.threshold(image_2_gray, 50, 255, cv2.THRESH_BINARY)[1]
plt.imshow(thresh_image, cmap='gray')


Out[177]:
<matplotlib.image.AxesImage at 0x1160e4e10>
/Users/kazumatamaki/.pyenv/versions/anaconda3-2.3.0/lib/python3.4/site-packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family ['IPAexGothic'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))

In [178]:
im2, contours, hierarchy = cv2.findContours(thresh_image, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

In [179]:
plt.imshow(im2, cmap='gray')


Out[179]:
<matplotlib.image.AxesImage at 0x1163f5c18>
/Users/kazumatamaki/.pyenv/versions/anaconda3-2.3.0/lib/python3.4/site-packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family ['IPAexGothic'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))

In [195]:
_image_2 = image_2.copy()
draw_image = cv2.drawContours(_image_2, contours, -1, (0, 255, 0), 3)

In [196]:
plt.imshow(draw_image, cmap='gray')


Out[196]:
<matplotlib.image.AxesImage at 0x11729d358>
/Users/kazumatamaki/.pyenv/versions/anaconda3-2.3.0/lib/python3.4/site-packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family ['IPAexGothic'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))