import cv2
import numpy as np
def gettubao(imgray):
img = np.zeros(shape=imgray.shape)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[1]
epsilon = 0.1 * cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, epsilon, True)
cv2.polylines(img, [approx], True, (255), 2)
return img
imgray = cv2.imread('/home/lhq/tests/test1591.jpg',0)
img = gettubao(imgray)
cv2.imshow('show', img)
cv2.waitKey()