# coding:utf8
 
import sys
import matplotlib.pyplot as plt # plt 用于显示图片
import cv2
import numpy as np
 
def preprocess(gray):
    filter = np.array([[ -1,-1,-1,-1,-1]])
        # 5. 腐蚀一次，去掉细节，如表格线等。注意这里去掉的是竖直的线
    a = cv2.erode(gray, filter)
    element2 = cv2.getStructuringElement(cv2.MORPH_RECT, (18, 4))

    # 4. 膨胀一次，让轮廓突出
    b = cv2.dilate(a, element2, iterations = 1)


    # 6. 再次膨胀，让轮廓明显一些
    c = cv2.dilate(b, element2, iterations = 3)

    # 7. 存储中间图片 
    cv2.imwrite("data/gray.png", gray)
    cv2.imwrite("data/a.png", a)
    cv2.imwrite("data/b.png", b)
    cv2.imwrite("data/c.png", c)

 
    return c
 
 
def findTextRegion(img):
    region = []
  
    # 1. 查找轮廓
    img1, contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
 
    # 2. 筛选那些面积小的
    for i in range(len(contours)):
        cnt = contours[i]
        # 计算该轮廓的面积
        area = cv2.contourArea(cnt) 
 
        # 面积小的都筛选掉
        if(area < 1000):
            continue
 
        # 轮廓近似，作用很小
        epsilon = 0.001 * cv2.arcLength(cnt, True)
        approx = cv2.approxPolyDP(cnt, epsilon, True)
 
        # 找到最小的矩形，该矩形可能有方向
        rect = cv2.minAreaRect(cnt)
        print ("rect is: ")
        print(rect)
 
        # box是四个点的坐标
        
        box = cv2.boxPoints(rect)
        box = np.int0(box)
 
        # 计算高和宽
        height = abs(box[0][1] - box[2][1])
        width = abs(box[0][0] - box[2][0])
 
        # 筛选那些太细的矩形，留下扁的
        if(height > width * 1.2):
            continue
 
        region.append(box)
 
    return region
 
 
def detect(img):
    # 1.  转化成灰度图
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
    # 2. 形态学变换的预处理，得到可以查找矩形的图片
    dilation = preprocess(gray)
 
    # 3. 查找和筛选文字区域
    region = findTextRegion(dilation)
 
    # 4. 用绿线画出这些找到的轮廓
    for box in region:
        cv2.drawContours(img, [box], 0, (0, 255, 0), 2)
 
    showImage(img)
    # 带轮廓的图片
    cv2.imwrite("contours.png", img)
 
    cv2.waitKey(0)
    cv2.destroyAllWindows()
 
def showImage(img):
    plt.imshow(img) # 显示图片
    plt.axis('off') # 不显示坐标轴
    plt.show()
 
if __name__ == '__main__':
    # 读取文件
    imagePath = "D:\\1.jpg"
    img = cv2.imread(imagePath)
    detect(img)

# coding:utf8
 
import sys
import matplotlib.pyplot as plt # plt 用于显示图片
import cv2
import numpy as np
 
def preprocess(gray):
    filter = np.array([[ -1,-1,-1,-1,-1]])
        # 5. 腐蚀一次，去掉细节，如表格线等。注意这里去掉的是竖直的线
    a = cv2.erode(gray, filter)
    element2 = cv2.getStructuringElement(cv2.MORPH_RECT, (18, 4))

    # 4. 膨胀一次，让轮廓突出
    b = cv2.dilate(a, element2, iterations = 1)


    # 6. 再次膨胀，让轮廓明显一些
    c = cv2.dilate(b, element2, iterations = 3)

    # 7. 存储中间图片 
    cv2.imwrite("data/gray.png", gray)
    cv2.imwrite("data/a.png", a)
    cv2.imwrite("data/b.png", b)
    cv2.imwrite("data/c.png", c)

 
    return c
 
 
def findTextRegion(img):
    region = []
  
    # 1. 查找轮廓
    img1, contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
 
    # 2. 筛选那些面积小的
    for i in range(len(contours)):
        cnt = contours[i]
        # 计算该轮廓的面积
        area = cv2.contourArea(cnt) 
 
        # 面积小的都筛选掉
        if(area < 1000):
            continue
 
        # 轮廓近似，作用很小
        epsilon = 0.001 * cv2.arcLength(cnt, True)
        approx = cv2.approxPolyDP(cnt, epsilon, True)
 
        # 找到最小的矩形，该矩形可能有方向
        rect = cv2.minAreaRect(cnt)
        print ("rect is: ")
        print(rect)
 
        # box是四个点的坐标
        
        box = cv2.boxPoints(rect)
        box = np.int0(box)
 
        # 计算高和宽
        height = abs(box[0][1] - box[2][1])
        width = abs(box[0][0] - box[2][0])
 
        # 筛选那些太细的矩形，留下扁的
        if(height > width * 1.2):
            continue
 
        region.append(box)
 
    return region
 
 
def detect(img):
    # 1.  转化成灰度图
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
    # 2. 形态学变换的预处理，得到可以查找矩形的图片
    dilation = preprocess(gray)
 
    # 3. 查找和筛选文字区域
    region = findTextRegion(dilation)
 
    # 4. 用绿线画出这些找到的轮廓
    for box in region:
        cv2.drawContours(img, [box], 0, (0, 255, 0), 2)
 
    showImage(img)
    # 带轮廓的图片
    cv2.imwrite("contours.png", img)
 
    cv2.waitKey(0)
    cv2.destroyAllWindows()
 
def showImage(img):
    plt.imshow(img) # 显示图片
    plt.axis('off') # 不显示坐标轴
    plt.show()
 
if __name__ == '__main__':
    # 读取文件
    imagePath = "D:\\1.jpg"
    img = cv2.imread(imagePath)
    detect(img)

# coding:utf8 # plt 用于显示图片 # 5. 腐蚀一次，去掉细节，如表格线等。注意这里去掉的是竖直的线 # 4. 膨胀一次，让轮廓突出 # 6. 再次膨胀，让轮廓明显一些 # 7. 存储中间图片 # 1. 查找轮廓 # 2. 筛选那些面积小的 # 计算该轮廓的面积 # 面积小的都筛选掉 # 轮廓近似，作用很小 # 找到最小的矩形，该矩形可能有方向 # box是四个点的坐标 # 计算高和宽 # 筛选那些太细的矩形，留下扁的 # 1.  转化成灰度图 # 2. 形态学变换的预处理，得到可以查找矩形的图片 # 3. 查找和筛选文字区域 # 4. 用绿线画出这些找到的轮廓 # 带轮廓的图片 # 显示图片 # 不显示坐标轴 # 读取文件