Skip to content

Commit

Permalink
Angular Calibration
Browse files Browse the repository at this point in the history
  • Loading branch information
ttthomaschan committed Apr 22, 2021
1 parent c0a3836 commit dc79de3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 63 deletions.
20 changes: 14 additions & 6 deletions tools/angleCalibration.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import cv2
import numpy as np

'''1. 检测表格外角点'''
class ExtractAngularPoint():
# 自适应二值化 --> 边缘检测 --> 边界筛选
def __init__(self,srcImg,logPath):
self.src = srcImg
self.thres_img = cv2.adaptiveThreshold(self.src, 255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2)
cv2.imwrite(logPath+"/thresImg.jpg", thres_img)
self.canny_img = cv2.Canny(thres_img,100,200)
v2.imwrite(logPath+"/cannyImg.jpg", canny_img)
contours, hierarchy = cv2.findContours(canny_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
self.grey = cv2.cvtColor(self.src, cv2.COLOR_BGR2GRAY)
self.thres_img = cv2.adaptiveThreshold(self.grey, 255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2)
cv2.imwrite(logPath+"/thresImg.jpg", self.thres_img)
self.canny_img = cv2.Canny(self.thres_img,100,200)
cv2.imwrite(logPath+"/cannyImg.jpg", self.canny_img)
_, contours, hierarchy = cv2.findContours(self.canny_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
self.contours = contours
area=[]
for k in range(len(contours)):
area.append(cv2.contourArea(contours[k]))
max_idx = np.argmax(np.array(area))
cv2.drawContours(self.src,contours,max_idx,(0,255,0),5)
cv2.imwrite(logPath+"/contours.jpg", self.src)

# 计算外角点,返回4个角点坐标
def __call__(self):
Expand Down Expand Up @@ -47,5 +55,5 @@ def __call__(self):
if __name__ == "__main__":
imgPath = "/home/elimen/Data/dbnet_pytorch/test_images/Red_thres.jpg"
logPath = "/home/elimen/Data/dbnet_pytorch/test_results"
srcImg = cv2.imread(imgPath,CV_8UC1)
srcImg = cv2.imread(imgPath)
extract_angular = ExtractAngularPoint(srcImg,logPath)
11 changes: 6 additions & 5 deletions tools/demowithUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,12 @@ def recognition_clicked(self):
txt_f.write(str(bbox_cornerlist[i]))
txt_f.write(out[0][0]+ '\n')
txt_f.close()

tab_rec = TabRecognition(img_bak)
crop_list,height_list, width_list= tab_rec.detnrec()
self.resultname = res_name + '.xlsx'
generateExcelFile(self.resPath,self.resultname,bbox_cornerlist,self.rec_cont,crop_list,height_list,width_list)

'''Recognition and Generation of table'''
# tab_rec = TabRecognition(img_bak)
# crop_list,height_list, width_list= tab_rec.detnrec()
# self.resultname = res_name + '.xlsx'
# generateExcelFile(self.resPath,self.resultname,bbox_cornerlist,self.rec_cont,crop_list,height_list,width_list)
return box_list, self.rec_cont

def detResult_clicked(self):
Expand Down
123 changes: 71 additions & 52 deletions tools/table_detectionv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import numpy as np
import math
import xlwt
src='/home/elimen/Data/dbnet_pytorch/test_images/ins01.png'
src='/home/elimen/Data/dbnet_pytorch/test_images/Red_thres.jpg'
respath='/home/elimen/Data/dbnet_pytorch/test_results_cell/'
img_name='ins01'
img_name='Red_thres'

'''
tmp operation:
Expand Down Expand Up @@ -65,40 +65,59 @@
cv2.imwrite(respath+"4_横竖交点阈值化.jpg", binary)

'''
关键点:1. findcontours()的应用, 定位每个cell。输出为 ys,xs
keypoint: detect contours
'''
# contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# area=[]
# for k in range(len(contours)):
# area.append(cv2.contourArea(contours[k]))
# max_idx = np.argmax(np.array(area))
# m_d_r=[]
# m_u_l=[]
# max_p=0
# min_p=1e6
# for l1 in contours[max_idx]:
# for l2 in l1:
# if sum(l2)>max_p:
# max_p=sum(l2)
# d_r=l2
# if sum(l2)<min_p:
# min_p=sum(l2)
# u_l=l2
# m_d_r=d_r
# m_u_l=u_l

# ## 截取图片中表格部分,被裁剪图片包括 1)bitwise_and, 2)merge, 3)raw
# padding=5
# x0=max(m_u_l[0]-padding, 0)
# x1=min(m_d_r[0]+padding, raw.shape[1])
# y0=max(m_u_l[1]-padding, 0)
# y1=min(m_d_r[1]+padding, raw.shape[0])
# image,contours,hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# print(len(contours))
# img = raw
# cv2.drawContours(img,contours,0,(0,255,0),5)
# cv2.imwrite(respath+"5_contours_0.jpg", img)

'''
关键点:1. findcontours()的应用, 定位每个cell。输出为 ys,xs
'''
image, contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
area=[]
for k in range(len(contours)):
area.append(cv2.contourArea(contours[k]))
max_idx = np.argmax(np.array(area))
m_d_r=[]
m_u_l=[]
max_p=0
min_p=1e6
img = raw
for l1 in contours[max_idx]:
for l2 in l1:
cv2.circle(img,(l2[0],l2[1]),2,(0,0,255))
if sum(l2)>max_p:
max_p=sum(l2)
d_r=l2
if sum(l2)<min_p:
min_p=sum(l2)
u_l=l2
m_d_r=d_r
m_u_l=u_l

## 截取图片中表格部分
padding=0 # =5
x0=max(m_u_l[0]-padding, 0)
x1=min(m_d_r[0]+padding, raw.shape[1])
y0=max(m_u_l[1]-padding, 0)
y1=min(m_d_r[1]+padding, raw.shape[0])


print(len(contours[0]))
# cv2.drawContours(img,contours,-1,(0,255,0),5)
# cv2.circle(img,(x0,y0),2,(0,0,255))
# cv2.circle(img,(x1,y1),2,(0,0,255))
cv2.imwrite(respath+"5_contourspt.jpg", img)

## 被裁剪图片包括 1)bitwise_and, 2)merge, 3)raw
# bitwise_and_crop = bitwise_and.copy()
#bitwise_and_crop = bitwise_and[y0:y1,x0:x1]
#raw = raw[y0:y1,x0:x1]
#merge = merge[y0:y1,x0:x1]
#cv2.imwrite(respath+"5_裁剪融合图.jpg", merge)
# bitwise_and_crop = bitwise_and[y0:y1,x0:x1]
# raw = raw[y0:y1,x0:x1]
# merge = merge[y0:y1,x0:x1]
# cv2.imwrite(respath+"5_裁剪融合图.jpg", merge)


# # 两张图片进行减法运算,去掉表格框线
Expand All @@ -125,17 +144,17 @@
i = 0
sort_x_point = np.sort(xs)
for i in range(len(sort_x_point) - 1):
if sort_x_point[i + 1] - sort_x_point[i] > 3:
x_point_arr.append(sort_x_point[i])
i = i + 1
if sort_x_point[i + 1] - sort_x_point[i] > 3:
x_point_arr.append(sort_x_point[i])
i = i + 1
x_point_arr.append(sort_x_point[i]) # 要将最后一个点加入

i = 0
sort_y_point = np.sort(ys)
for i in range(len(sort_y_point) - 1):
if sort_y_point[i + 1] - sort_y_point[i] > 3:
y_point_arr.append(sort_y_point[i])
i = i + 1
if sort_y_point[i + 1] - sort_y_point[i] > 3:
y_point_arr.append(sort_y_point[i])
i = i + 1
y_point_arr.append(sort_y_point[i])
print("sorted coor:")
print(len(sort_x_point),len(sort_y_point))
Expand Down Expand Up @@ -207,11 +226,11 @@ def __init__(self,lt,rd,belong):

for i in range(len(lt_list_x)):
for j in range(len(lt_list_y)):
## p点格式为(y,x)。假设 左上角 lt(y1,x1), 右下角 rd(y2,x2) ==> 左下角 p1(y2,x1), 右上角 p3(y1,x2)
## p点格式为(y,x)。假设 左上角 lt(y1,x1), 右下角 rd(y2,x2) ==> 左下角 p1(y2,x1), 右上角 p3(y1,x2)
p1 = [d['cell_{}_{}'.format(i,j)].rd[1], d['cell_{}_{}'.format(i,j)].lt[0]] #左下点
p2 = [d['cell_{}_{}'.format(i,j)].rd[1], d['cell_{}_{}'.format(i,j)].rd[0]] #右下点
p3 = [d['cell_{}_{}'.format(i,j)].lt[1], d['cell_{}_{}'.format(i,j)].rd[0]] #右上点
## 查看两点之间是否连接,确定单元格归属
## 查看两点之间是否连接,确定单元格归属
if not islianjie(p1,p2,merge):
d['cell_{}_{}'.format(i,j+1)].belong = d['cell_{}_{}'.format(i,j)].belong
if not islianjie(p2,p3,merge):
Expand Down Expand Up @@ -245,19 +264,19 @@ def __init__(self,lt,rd,belong):


merge_format = workbook.add_format({
'bold': True,
'border': 1,
'align': 'left',#水平居中
'valign': 'vcenter',#垂直居中
#'fg_color': '#D7E4BC',#颜色填充
'bold': True,
'border': 1,
'align': 'left',#水平居中
'valign': 'vcenter',#垂直居中
#'fg_color': '#D7E4BC',#颜色填充
})

header_format = workbook.add_format({
'bold': True,
'border': 1,
'align': 'center',#水平居中
'valign': 'vcenter',#垂直居中
#'fg_color': 'blue',#颜色填充
'bold': True,
'border': 1,
'align': 'center',#水平居中
'valign': 'vcenter',#垂直居中
#'fg_color': 'blue',#颜色填充
})

def is_inside(cell, box):
Expand Down Expand Up @@ -327,7 +346,7 @@ def is_inside(cell, box):
#print(rd_col)
## 竖直方向
for i in range(len(h_list)+1):
# 左上角
# 左上角
if lt_dist2ori[1]==sum(h_list[:i]):
lt_row=i+2
#print(lt_row)
Expand Down

0 comments on commit dc79de3

Please sign in to comment.