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]:
In [166]:
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
In [167]:
plt.imshow(image_rgb)
Out[167]:
In [129]:
image_gray = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2GRAY)
In [130]:
plt.imshow(image_gray, cmap='gray')
Out[130]:
In [131]:
thresh_image = cv2.threshold(image_gray, 100, 255, cv2.THRESH_BINARY)[1]
plt.imshow(thresh_image, cmap='gray')
Out[131]:
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]:
In [134]:
draw_image = cv2.drawContours(find_image, contours, -1, (0, 0, 255), 3)
In [135]:
plt.imshow(draw_image, cmap='gray')
Out[135]:
In [145]:
draw_image = cv2.drawContours(image_rgb, contours, -1, (0, 255, 0), 3)
In [146]:
plt.imshow(draw_image)
Out[146]:
In [162]:
print(contours[4])
In [147]:
len(contours)
Out[147]:
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]:
In [171]:
plt.imshow(outputs[0])
Out[171]:
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]:
In [154]:
rects[0]
Out[154]:
In [155]:
len(outputs)
Out[155]:
In [157]:
plt.imshow(outputs[1])
Out[157]:
In [105]:
draw_image = cv2.drawContours(image_rgb, approxes, -1, (255, 0, 0), 3)
plt.imshow(draw_image)
Out[105]:
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]:
In [177]:
thresh_image = cv2.threshold(image_2_gray, 50, 255, cv2.THRESH_BINARY)[1]
plt.imshow(thresh_image, cmap='gray')
Out[177]:
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]:
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]: