findContours对于二值化图片的物体找轮廓真的挺好用的。
项目所需,效果如下:
每个模块有用的部分都在注释里了,希望小白的自己以后有用的话,能找得到。
import cv2
import numpy as np
import os
import shutil
##获得一张图片的文字框信息,左上角和右下角 并写入文件中
def get_condition(img,path,title):
#img 图片 path :txt文件的路径+名字 title:这一行的标注名
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
binary = 255-binary
contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# x0,y0 左上角 x1,y1右下角 chushihua
x0,y0,x1,y1 = 255,255,0,0
for c in contour