table_line.py 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Thu Sep 9 23:11:51 2020
  5. table line detect
  6. @author: chineseocr
  7. """
  8. import copy
  9. import tensorflow as tf
  10. import tensorflow.keras as K
  11. from tensorflow.keras.models import Model
  12. from tensorflow.keras.layers import Input, concatenate, Conv2D, MaxPooling2D, BatchNormalization, UpSampling2D
  13. from tensorflow.keras.layers import LeakyReLU
  14. from config import tableModeLinePath
  15. from utils import letterbox_image, get_table_line, adjust_lines, line_to_line, draw_boxes
  16. import numpy as np
  17. import cv2
  18. import time
  19. from utils import draw_lines
  20. def dice_coef(y_true, y_pred, smooth=1e-5):
  21. y_true_f = K.flatten(y_true)
  22. y_pred_f = K.flatten(y_pred)
  23. intersection = K.sum(y_true_f * y_pred_f)
  24. return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
  25. def dice_coef_loss():
  26. def dice_coef_loss_fixed(y_true, y_pred):
  27. return -dice_coef(y_true, y_pred)
  28. return dice_coef_loss_fixed
  29. def focal_loss(gamma=3., alpha=.5):
  30. # 3 0.85 2000e acc-0.6 p-0.99 r-0.99 val_acc-0.56 val_p-0.86 val_r-0.95
  31. # 2 0.85 double_gpu acc-
  32. # 3 0.25 gpu 50e acc-0.5 p-0.99 r-0.99 val_acc-0.45 val_p-0.96 val_r-0.88
  33. # 2 0.25 gpu acc-
  34. # 3 0.5 double_gpu acc-0.6 p-0.99 r-0.99 val_acc-0.60 val_p-0.93 val_r-0.93
  35. def focal_loss_fixed(y_true, y_pred):
  36. pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))
  37. pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))
  38. return -K.backend.sum(alpha * K.backend.pow(1. - pt_1, gamma)
  39. * K.backend.log(K.backend.epsilon()+pt_1))-K.backend.sum((1-alpha) * K.backend.pow( pt_0, gamma) * K.backend.log(1. - pt_0 + K.backend.epsilon()))
  40. return focal_loss_fixed
  41. def table_net_large(input_shape=(1152, 896, 3), num_classes=1):
  42. inputs = Input(shape=input_shape)
  43. # 512
  44. use_bias = False
  45. down0a = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(inputs)
  46. down0a = BatchNormalization()(down0a)
  47. down0a = LeakyReLU(alpha=0.1)(down0a)
  48. down0a = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(down0a)
  49. down0a = BatchNormalization()(down0a)
  50. down0a = LeakyReLU(alpha=0.1)(down0a)
  51. down0a_pool = MaxPooling2D((2, 2), strides=(2, 2))(down0a)
  52. # 256
  53. down0 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(down0a_pool)
  54. down0 = BatchNormalization()(down0)
  55. down0 = LeakyReLU(alpha=0.1)(down0)
  56. down0 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(down0)
  57. down0 = BatchNormalization()(down0)
  58. down0 = LeakyReLU(alpha=0.1)(down0)
  59. down0_pool = MaxPooling2D((2, 2), strides=(2, 2))(down0)
  60. # 128
  61. down1 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(down0_pool)
  62. down1 = BatchNormalization()(down1)
  63. down1 = LeakyReLU(alpha=0.1)(down1)
  64. down1 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(down1)
  65. down1 = BatchNormalization()(down1)
  66. down1 = LeakyReLU(alpha=0.1)(down1)
  67. down1_pool = MaxPooling2D((2, 2), strides=(2, 2))(down1)
  68. # 64
  69. down2 = Conv2D(128, (3, 3), padding='same', use_bias=use_bias)(down1_pool)
  70. down2 = BatchNormalization()(down2)
  71. down2 = LeakyReLU(alpha=0.1)(down2)
  72. down2 = Conv2D(128, (3, 3), padding='same', use_bias=use_bias)(down2)
  73. down2 = BatchNormalization()(down2)
  74. down2 = LeakyReLU(alpha=0.1)(down2)
  75. down2_pool = MaxPooling2D((2, 2), strides=(2, 2))(down2)
  76. # 32
  77. down3 = Conv2D(256, (3, 3), padding='same', use_bias=use_bias)(down2_pool)
  78. down3 = BatchNormalization()(down3)
  79. down3 = LeakyReLU(alpha=0.1)(down3)
  80. down3 = Conv2D(256, (3, 3), padding='same', use_bias=use_bias)(down3)
  81. down3 = BatchNormalization()(down3)
  82. down3 = LeakyReLU(alpha=0.1)(down3)
  83. down3_pool = MaxPooling2D((2, 2), strides=(2, 2))(down3)
  84. # 16
  85. down4 = Conv2D(512, (3, 3), padding='same', use_bias=use_bias)(down3_pool)
  86. down4 = BatchNormalization()(down4)
  87. down4 = LeakyReLU(alpha=0.1)(down4)
  88. down4 = Conv2D(512, (3, 3), padding='same', use_bias=use_bias)(down4)
  89. down4 = BatchNormalization()(down4)
  90. down4 = LeakyReLU(alpha=0.1)(down4)
  91. down4_pool = MaxPooling2D((2, 2), strides=(2, 2))(down4)
  92. # 8
  93. center = Conv2D(1024, (3, 3), padding='same', use_bias=use_bias)(down4_pool)
  94. center = BatchNormalization()(center)
  95. center = LeakyReLU(alpha=0.1)(center)
  96. center = Conv2D(1024, (3, 3), padding='same', use_bias=use_bias)(center)
  97. center = BatchNormalization()(center)
  98. center = LeakyReLU(alpha=0.1)(center)
  99. # center
  100. up4 = UpSampling2D((2, 2))(center)
  101. up4 = concatenate([down4, up4], axis=3)
  102. up4 = Conv2D(512, (3, 3), padding='same', use_bias=use_bias)(up4)
  103. up4 = BatchNormalization()(up4)
  104. up4 = LeakyReLU(alpha=0.1)(up4)
  105. up4 = Conv2D(512, (3, 3), padding='same', use_bias=use_bias)(up4)
  106. up4 = BatchNormalization()(up4)
  107. up4 = LeakyReLU(alpha=0.1)(up4)
  108. up4 = Conv2D(512, (3, 3), padding='same', use_bias=use_bias)(up4)
  109. up4 = BatchNormalization()(up4)
  110. up4 = LeakyReLU(alpha=0.1)(up4)
  111. # 16
  112. up3 = UpSampling2D((2, 2))(up4)
  113. up3 = concatenate([down3, up3], axis=3)
  114. up3 = Conv2D(256, (3, 3), padding='same', use_bias=use_bias)(up3)
  115. up3 = BatchNormalization()(up3)
  116. up3 = LeakyReLU(alpha=0.1)(up3)
  117. up3 = Conv2D(256, (3, 3), padding='same', use_bias=use_bias)(up3)
  118. up3 = BatchNormalization()(up3)
  119. up3 = LeakyReLU(alpha=0.1)(up3)
  120. up3 = Conv2D(256, (3, 3), padding='same', use_bias=use_bias)(up3)
  121. up3 = BatchNormalization()(up3)
  122. up3 = LeakyReLU(alpha=0.1)(up3)
  123. # 32
  124. up2 = UpSampling2D((2, 2))(up3)
  125. up2 = concatenate([down2, up2], axis=3)
  126. up2 = Conv2D(128, (3, 3), padding='same', use_bias=use_bias)(up2)
  127. up2 = BatchNormalization()(up2)
  128. up2 = LeakyReLU(alpha=0.1)(up2)
  129. up2 = Conv2D(128, (3, 3), padding='same', use_bias=use_bias)(up2)
  130. up2 = BatchNormalization()(up2)
  131. up2 = LeakyReLU(alpha=0.1)(up2)
  132. up2 = Conv2D(128, (3, 3), padding='same', use_bias=use_bias)(up2)
  133. up2 = BatchNormalization()(up2)
  134. up2 = LeakyReLU(alpha=0.1)(up2)
  135. # 64
  136. up1 = UpSampling2D((2, 2))(up2)
  137. up1 = concatenate([down1, up1], axis=3)
  138. up1 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(up1)
  139. up1 = BatchNormalization()(up1)
  140. up1 = LeakyReLU(alpha=0.1)(up1)
  141. up1 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(up1)
  142. up1 = BatchNormalization()(up1)
  143. up1 = LeakyReLU(alpha=0.1)(up1)
  144. up1 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(up1)
  145. up1 = BatchNormalization()(up1)
  146. up1 = LeakyReLU(alpha=0.1)(up1)
  147. # 128
  148. up0 = UpSampling2D((2, 2))(up1)
  149. up0 = concatenate([down0, up0], axis=3)
  150. up0 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(up0)
  151. up0 = BatchNormalization()(up0)
  152. up0 = LeakyReLU(alpha=0.1)(up0)
  153. up0 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(up0)
  154. up0 = BatchNormalization()(up0)
  155. up0 = LeakyReLU(alpha=0.1)(up0)
  156. up0 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(up0)
  157. up0 = BatchNormalization()(up0)
  158. up0 = LeakyReLU(alpha=0.1)(up0)
  159. # 256
  160. up0a = UpSampling2D((2, 2))(up0)
  161. up0a = concatenate([down0a, up0a], axis=3)
  162. up0a = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up0a)
  163. up0a = BatchNormalization()(up0a)
  164. up0a = LeakyReLU(alpha=0.1)(up0a)
  165. up0a = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up0a)
  166. up0a = BatchNormalization()(up0a)
  167. up0a = LeakyReLU(alpha=0.1)(up0a)
  168. up0a = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up0a)
  169. up0a = BatchNormalization()(up0a)
  170. up0a = LeakyReLU(alpha=0.1)(up0a)
  171. # 512
  172. classify = Conv2D(num_classes, (1, 1), activation='sigmoid')(up0a)
  173. model = Model(inputs=inputs, outputs=classify)
  174. return model
  175. def table_net(input_shape=(1152, 896, 3), num_classes=1):
  176. inputs = Input(shape=input_shape)
  177. # 512
  178. use_bias = False
  179. down0a = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(inputs)
  180. down0a = BatchNormalization()(down0a)
  181. down0a = LeakyReLU(alpha=0.1)(down0a)
  182. down0a = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(down0a)
  183. down0a = BatchNormalization()(down0a)
  184. down0a = LeakyReLU(alpha=0.1)(down0a)
  185. down0a_pool = MaxPooling2D((2, 2), strides=(2, 2))(down0a)
  186. # 256
  187. down0 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(down0a_pool)
  188. down0 = BatchNormalization()(down0)
  189. down0 = LeakyReLU(alpha=0.1)(down0)
  190. down0 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(down0)
  191. down0 = BatchNormalization()(down0)
  192. down0 = LeakyReLU(alpha=0.1)(down0)
  193. down0_pool = MaxPooling2D((2, 2), strides=(2, 2))(down0)
  194. # 128
  195. down1 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(down0_pool)
  196. down1 = BatchNormalization()(down1)
  197. down1 = LeakyReLU(alpha=0.1)(down1)
  198. down1 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(down1)
  199. down1 = BatchNormalization()(down1)
  200. down1 = LeakyReLU(alpha=0.1)(down1)
  201. down1_pool = MaxPooling2D((2, 2), strides=(2, 2))(down1)
  202. # 64
  203. down2 = Conv2D(128, (3, 3), padding='same', use_bias=use_bias)(down1_pool)
  204. down2 = BatchNormalization()(down2)
  205. down2 = LeakyReLU(alpha=0.1)(down2)
  206. down2 = Conv2D(128, (3, 3), padding='same', use_bias=use_bias)(down2)
  207. down2 = BatchNormalization()(down2)
  208. down2 = LeakyReLU(alpha=0.1)(down2)
  209. down2_pool = MaxPooling2D((2, 2), strides=(2, 2))(down2)
  210. # 32
  211. up1 = UpSampling2D((2, 2))(down2)
  212. up1 = concatenate([down1, up1], axis=3)
  213. up1 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(up1)
  214. up1 = BatchNormalization()(up1)
  215. up1 = LeakyReLU(alpha=0.1)(up1)
  216. up1 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(up1)
  217. up1 = BatchNormalization()(up1)
  218. up1 = LeakyReLU(alpha=0.1)(up1)
  219. up1 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(up1)
  220. up1 = BatchNormalization()(up1)
  221. up1 = LeakyReLU(alpha=0.1)(up1)
  222. # 128
  223. up0 = UpSampling2D((2, 2))(up1)
  224. up0 = concatenate([down0, up0], axis=3)
  225. up0 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(up0)
  226. up0 = BatchNormalization()(up0)
  227. up0 = LeakyReLU(alpha=0.1)(up0)
  228. up0 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(up0)
  229. up0 = BatchNormalization()(up0)
  230. up0 = LeakyReLU(alpha=0.1)(up0)
  231. up0 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(up0)
  232. up0 = BatchNormalization()(up0)
  233. up0 = LeakyReLU(alpha=0.1)(up0)
  234. # 256
  235. up0a = UpSampling2D((2, 2))(up0)
  236. up0a = concatenate([down0a, up0a], axis=3)
  237. up0a = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up0a)
  238. up0a = BatchNormalization()(up0a)
  239. up0a = LeakyReLU(alpha=0.1)(up0a)
  240. up0a = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up0a)
  241. up0a = BatchNormalization()(up0a)
  242. up0a = LeakyReLU(alpha=0.1)(up0a)
  243. up0a = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up0a)
  244. up0a = BatchNormalization()(up0a)
  245. up0a = LeakyReLU(alpha=0.1)(up0a)
  246. # 512
  247. classify = Conv2D(num_classes, (1, 1), activation='sigmoid')(up0a)
  248. model = Model(inputs=inputs, outputs=classify)
  249. return model
  250. model = table_net((None, None, 3), 2)
  251. def table_line(img, model, size=(512, 1024), hprob=0.5, vprob=0.5, row=50, col=30, alph=15):
  252. sizew, sizeh = size
  253. # [..., ::-1] 最后一维内部反向输出
  254. # inputBlob, fx, fy = letterbox_image(img[..., ::-1], (sizew, sizeh))
  255. # pred = model.predict(np.array([np.array(inputBlob)]))
  256. # pred = model.predict(np.array([np.array(inputBlob)/255.0]))
  257. img_new = cv2.resize(img, (sizew, sizeh), interpolation=cv2.INTER_AREA)
  258. pred = model.predict(np.array([img_new]))
  259. pred = pred[0]
  260. vpred = pred[..., 1] > vprob # 横线
  261. hpred = pred[..., 0] > hprob # 竖线
  262. vpred = vpred.astype(int)
  263. hpred = hpred.astype(int)
  264. # print("vpred shape", vpred)
  265. # print("hpred shape", hpred)
  266. colboxes = get_table_line(vpred, axis=1, lineW=col)
  267. rowboxes = get_table_line(hpred, axis=0, lineW=row)
  268. # if len(rowboxes) > 0:
  269. # rowboxes = np.array(rowboxes)
  270. # rowboxes[:, [0, 2]] = rowboxes[:, [0, 2]]/fx
  271. # rowboxes[:, [1, 3]] = rowboxes[:, [1, 3]]/fy
  272. # rowboxes = rowboxes.tolist()
  273. # if len(colboxes) > 0:
  274. # colboxes = np.array(colboxes)
  275. # colboxes[:, [0, 2]] = colboxes[:, [0, 2]]/fx
  276. # colboxes[:, [1, 3]] = colboxes[:, [1, 3]]/fy
  277. # colboxes = colboxes.tolist()
  278. # nrow = len(rowboxes)
  279. # ncol = len(colboxes)
  280. # for i in range(nrow):
  281. # for j in range(ncol):
  282. # rowboxes[i] = line_to_line(rowboxes[i], colboxes[j], 10)
  283. # colboxes[j] = line_to_line(colboxes[j], rowboxes[i], 10)
  284. # 删掉贴着边框的line
  285. temp_list = []
  286. threshold = 5
  287. for line in rowboxes:
  288. if line[1]-0 <= threshold or size[1]-line[1] <= threshold:
  289. continue
  290. temp_list.append(line)
  291. rowboxes = temp_list
  292. temp_list = []
  293. for line in colboxes:
  294. if line[0]-0 <= threshold or size[0]-line[0] <= threshold:
  295. continue
  296. temp_list.append(line)
  297. colboxes = temp_list
  298. return rowboxes, colboxes, img_new
  299. def get_outline(points, image_np):
  300. # 取出x, y的最大值最小值
  301. x_min = points[0][0]
  302. x_max = points[-1][0]
  303. points.sort(key=lambda x: (x[1], x[0]))
  304. y_min = points[0][1]
  305. y_max = points[-1][1]
  306. # 创建空图
  307. # outline_img = np.zeros(image_size, np.uint8)
  308. outline_img = np.copy(image_np)
  309. cv2.rectangle(outline_img, (x_min-5, y_min-5), (x_max+5, y_max+5), (0, 0, 0), 2)
  310. # cv2.imshow("outline_img", outline_img)
  311. # cv2.waitKey(0)
  312. return outline_img
  313. def get_split_line(points, col_lines, image_np):
  314. # print("get_split_line", image_np.shape)
  315. points.sort(key=lambda x: (x[1], x[0]))
  316. # 遍历y坐标,并判断y坐标与上一个y坐标是否存在连接线
  317. i = 0
  318. split_line_y = []
  319. for point in points:
  320. # 从已分开的线下面开始判断
  321. if split_line_y:
  322. if point[1] <= split_line_y[-1] + 5:
  323. last_y = point[1]
  324. continue
  325. if last_y <= split_line_y[-1] + 5:
  326. last_y = point[1]
  327. continue
  328. if i == 0:
  329. last_y = point[1]
  330. i += 1
  331. continue
  332. current_line = (last_y, point[1])
  333. split_flag = 1
  334. for col in col_lines:
  335. # 只要找到一条col包含就不是分割线
  336. if current_line[0] >= col[1]-3 and current_line[1] <= col[3]+3:
  337. split_flag = 0
  338. # print("img", img.shape)
  339. # print("col", col)
  340. # print("current_line", current_line)
  341. break
  342. if split_flag:
  343. split_line_y.append(current_line[0]+5)
  344. split_line_y.append(current_line[1]-5)
  345. last_y = point[1]
  346. # 加上收尾分割线
  347. points.sort(key=lambda x: (x[1], x[0]))
  348. y_min = points[0][1]
  349. y_max = points[-1][1]
  350. # print("加上收尾分割线", y_min, y_max)
  351. if y_min-5 < 0:
  352. split_line_y.append(0)
  353. else:
  354. split_line_y.append(y_min-5)
  355. if y_max+5 > image_np.shape[0]:
  356. split_line_y.append(image_np.shape[0])
  357. else:
  358. split_line_y.append(y_max+5)
  359. split_line_y = list(set(split_line_y))
  360. # 剔除两条相隔太近分割线
  361. temp_split_line_y = []
  362. split_line_y.sort(key=lambda x: x)
  363. last_y = -20
  364. for y in split_line_y:
  365. # print(y)
  366. if y - last_y >= 20:
  367. # print(y, last_y)
  368. temp_split_line_y.append(y)
  369. last_y = y
  370. split_line_y = temp_split_line_y
  371. # print("split_line_y", split_line_y)
  372. # 生成分割线
  373. split_line = []
  374. last_y = 0
  375. for y in split_line_y:
  376. # if y - last_y <= 15:
  377. # continue
  378. split_line.append([(0, y), (image_np.shape[1], y)])
  379. last_y = y
  380. split_line.append([(0, 0), (image_np.shape[1], 0)])
  381. split_line.append([(0, image_np.shape[0]), (image_np.shape[1], image_np.shape[0])])
  382. split_line.sort(key=lambda x: x[0][1])
  383. # print("split_line", split_line)
  384. # 画图画线
  385. # split_line_img = np.copy(image_np)
  386. # for y in split_line_y:
  387. # cv2.line(split_line_img, (0, y), (image_np.shape[1], y), (0, 0, 0), 1)
  388. # cv2.imshow("split_line_img", split_line_img)
  389. # cv2.waitKey(0)
  390. return split_line, split_line_y
  391. def get_points(row_lines, col_lines, image_size):
  392. # 创建空图
  393. row_img = np.zeros(image_size, np.uint8)
  394. col_img = np.zeros(image_size, np.uint8)
  395. # 画线
  396. thresh = 3
  397. for row in row_lines:
  398. cv2.line(row_img, (int(row[0]-thresh), int(row[1])), (int(row[2]+thresh), int(row[3])), (255, 255, 255), 1)
  399. for col in col_lines:
  400. cv2.line(col_img, (int(col[0]), int(col[1]-thresh)), (int(col[2]), int(col[3]+thresh)), (255, 255, 255), 1)
  401. # 求出交点
  402. point_img = np.bitwise_and(row_img, col_img)
  403. # cv2.imshow("point_img", np.bitwise_not(point_img))
  404. # cv2.waitKey(0)
  405. # 识别黑白图中的白色交叉点,将横纵坐标取出
  406. ys, xs = np.where(point_img > 0)
  407. points = []
  408. for i in range(len(xs)):
  409. points.append((xs[i], ys[i]))
  410. points.sort(key=lambda x: (x[0], x[1]))
  411. return points
  412. def get_minAreaRect(image):
  413. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  414. gray = cv2.bitwise_not(gray)
  415. thresh = cv2.threshold(gray, 0, 255,
  416. cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
  417. coords = np.column_stack(np.where(thresh > 0))
  418. return cv2.minAreaRect(coords)
  419. def get_contours(image):
  420. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  421. ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
  422. contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  423. cv2.drawContours(image, contours, -1, (0, 0, 255), 3)
  424. cv2.imshow("get contours", image)
  425. cv2.waitKey(0)
  426. def fix_outline(image, row_lines, col_lines, points, split_y):
  427. # 分割线纵坐标
  428. # print("len(split_y)", split_y)
  429. if len(split_y) < 2:
  430. # split_y = [2000., 2000., 2000., 2000.]
  431. return [], [], []
  432. elif len(split_y) == 2:
  433. split_y = [2000., 2000., 2000., 2000.]
  434. split_y.sort(key=lambda x: x)
  435. split_y = split_y[1:-1]
  436. new_split_y = []
  437. for i in range(1, len(split_y), 2):
  438. new_split_y.append(int((split_y[i]+split_y[i-1])/2))
  439. # print("len(new_split_y)", len(new_split_y))
  440. # 预测线根据分割线纵坐标分为多个分割区域
  441. row_lines.sort(key=lambda x: (x[3], x[2], x[1], x[0]))
  442. col_lines.sort(key=lambda x: (x[3], x[2], x[1], x[0]))
  443. row_count = 0
  444. col_count = 0
  445. split_row_list = []
  446. split_col_list = []
  447. for y in new_split_y:
  448. row_lines = row_lines[row_count:]
  449. col_lines = col_lines[col_count:]
  450. row_count = 0
  451. col_count = 0
  452. for row in row_lines:
  453. if row[3] <= y:
  454. row_count += 1
  455. else:
  456. split_row_list.append(row_lines[:row_count])
  457. break
  458. if row_count == len(row_lines):
  459. split_row_list.append(row_lines[:row_count])
  460. break
  461. for col in col_lines:
  462. if col[3] <= y:
  463. col_count += 1
  464. else:
  465. split_col_list.append(col_lines[:col_count])
  466. break
  467. if col_count == len(col_lines):
  468. split_col_list.append(col_lines[:col_count])
  469. break
  470. if row_count < len(row_lines) - 1:
  471. row_lines = row_lines[row_count:]
  472. split_row_list.append(row_lines)
  473. if col_count < len(col_lines) - 1:
  474. col_lines = col_lines[col_count:]
  475. split_col_list.append(col_lines)
  476. # print("len(split_row_list)", len(split_row_list))
  477. # print("len(split_col_list)", len(split_col_list))
  478. # 预测线取上下左右4个边(会有超出表格部分) [(), ()]
  479. area_row_line = []
  480. area_col_line = []
  481. for area in split_row_list:
  482. if not area:
  483. continue
  484. area.sort(key=lambda x: (x[1], x[0]))
  485. # print(area)
  486. up_line = area[0]
  487. bottom_line = area[-1]
  488. area_row_line.append([up_line, bottom_line])
  489. for area in split_col_list:
  490. if not area:
  491. continue
  492. area.sort(key=lambda x: x[0])
  493. left_line = area[0]
  494. right_line = area[-1]
  495. area_col_line.append([left_line, right_line])
  496. # 线交点根据分割线纵坐标分为多个分割区域
  497. points.sort(key=lambda x: (x[1], x[0]))
  498. point_count = 0
  499. split_point_list = []
  500. for y in new_split_y:
  501. points = points[point_count:len(points)]
  502. point_count = 0
  503. for point in points:
  504. if point[1] <= y:
  505. point_count += 1
  506. else:
  507. split_point_list.append(points[:point_count])
  508. break
  509. if point_count == len(points):
  510. split_point_list.append(points[:point_count])
  511. break
  512. if point_count < len(points) - 1:
  513. points = points[point_count:len(points)]
  514. split_point_list.append(points)
  515. # print("len(split_point_list)", len(split_point_list))
  516. # 取每个分割区域的4条线(无超出表格部分)
  517. area_row_line2 = []
  518. area_col_line2 = []
  519. for area in split_point_list:
  520. if not area:
  521. continue
  522. area.sort(key=lambda x: (x[0], x[1]))
  523. left_up = area[0]
  524. right_bottom = area[-1]
  525. up_line = [left_up[0], left_up[1], right_bottom[0], left_up[1]]
  526. bottom_line = [left_up[0], right_bottom[1], right_bottom[0], right_bottom[1]]
  527. left_line = [left_up[0], left_up[1], left_up[0], right_bottom[1]]
  528. right_line = [right_bottom[0], left_up[1], right_bottom[0], right_bottom[1]]
  529. area_row_line2.append([up_line, bottom_line])
  530. area_col_line2.append([left_line, right_line])
  531. # 判断超出部分的长度,超出一定长度就补线
  532. new_row_lines = []
  533. new_col_lines = []
  534. longer_row_lines = []
  535. longer_col_lines = []
  536. all_longer_row_lines = []
  537. all_longer_col_lines = []
  538. # print(len(area_col_line))
  539. # print(len(area_col_line2))
  540. for i in range(len(area_row_line)):
  541. up_line = area_row_line[i][0]
  542. up_line2 = area_row_line2[i][0]
  543. bottom_line = area_row_line[i][1]
  544. bottom_line2 = area_row_line2[i][1]
  545. left_line = area_col_line[i][0]
  546. left_line2 = area_col_line2[i][0]
  547. right_line = area_col_line[i][1]
  548. right_line2 = area_col_line2[i][1]
  549. # 补左右两条竖线超出来的线的row
  550. # print("left_line2[1] - left_line[1]", left_line2[1] - left_line[1])
  551. if left_line2[1] - left_line[1] >= 10 and right_line2[1] - right_line[1] >= 10:
  552. if left_line2[1] - left_line[1] >= right_line2[1] - right_line[1]:
  553. new_row_lines.append([left_line[0], left_line[1], right_line[0], left_line[1]])
  554. new_col_y = left_line[1]
  555. # 补了row,要将其他短的col连到row上
  556. for col in split_col_list[i]:
  557. # 需判断该线在这个区域中
  558. if up_line2[1]-3 <= col[1] <= col[3] <= bottom_line2[1]+3:
  559. longer_col_lines.append([col[0], min([new_col_y, col[1]]), col[2], col[3]])
  560. else:
  561. new_row_lines.append([left_line[0], right_line[1], right_line[0], right_line[1]])
  562. new_col_y = right_line[1]
  563. # 补了row,要将其他短的col连到row上
  564. for col in split_col_list[i]:
  565. # 需判断该线在这个区域中
  566. if up_line2[1]-3 <= col[1] <= col[3] <= bottom_line2[1]+3:
  567. longer_col_lines.append([col[0], min([new_col_y, col[1]]), col[2], col[3]])
  568. # print("left_line[3] - left_line2[3]", left_line[3] - left_line2[3])
  569. if left_line[3] - left_line2[3] >= 10 and right_line[3] - right_line2[3] >= 10:
  570. if left_line[3] - left_line2[3] >= right_line[3] - right_line2[3]:
  571. new_row_lines.append([left_line[2], left_line[3], right_line[2], left_line[3]])
  572. new_col_y = left_line[3]
  573. # 补了row,要将其他短的col连到row上
  574. for col in split_col_list[i]:
  575. # 需判断该线在这个区域中
  576. if up_line2[1]-3 <= col[1] <= col[3] <= bottom_line2[1]+3:
  577. longer_col_lines.append([col[0], col[1], col[2], max([new_col_y, col[3]])])
  578. else:
  579. new_row_lines.append([left_line[2], right_line[3], right_line[2], right_line[3]])
  580. new_col_y = right_line[3]
  581. # 补了row,要将其他短的col连到row上
  582. for col in split_col_list[i]:
  583. # 需判断该线在这个区域中
  584. if up_line2[1]-3 <= col[1] <= col[3] <= bottom_line2[1]+3:
  585. longer_col_lines.append([col[0], col[1], col[2], max([new_col_y, col[3]])])
  586. # 补上下两条横线超出来的线的col
  587. if up_line2[0] - up_line[0] >= 10 and bottom_line2[0] - bottom_line[0] >= 10:
  588. if up_line2[0] - up_line[0] >= bottom_line2[0] - bottom_line[0]:
  589. new_col_lines.append([up_line[0], up_line[1], up_line[0], bottom_line[1]])
  590. new_row_x = up_line[0]
  591. # 补了col,要将其他短的row连到col上
  592. for row in split_row_list[i]:
  593. # 需判断该线在这个区域中
  594. if up_line2[1]-3 <= row[1] <= bottom_line2[1]+3:
  595. longer_row_lines.append([min([new_row_x, row[0]]), row[1], row[2], row[3]])
  596. # new_row_lines.append([bottom_line[0], up_line[1], bottom_line[2], bottom_line[3]])
  597. else:
  598. new_col_lines.append([bottom_line[0], up_line[1], bottom_line[0], bottom_line[1]])
  599. new_row_x = bottom_line[0]
  600. # 补了col,要将其他短的row连到col上
  601. for row in split_row_list[i]:
  602. # 需判断该线在这个区域中
  603. if up_line2[1]-3 <= row[1] <= bottom_line2[1]+3:
  604. longer_row_lines.append([min([new_row_x, row[0]]), row[1], row[2], row[3]])
  605. # new_row_lines.append([bottom_line[0], up_line[1], up_line[2], up_line[3]])
  606. if up_line[2] - up_line2[2] >= 10 and bottom_line[2] - bottom_line2[2] >= 10:
  607. if up_line[2] - up_line2[2] >= bottom_line[2] - bottom_line2[2]:
  608. new_col_lines.append([up_line[2], up_line[3], up_line[2], bottom_line[3]])
  609. new_row_x = up_line[2]
  610. # 补了col,要将其他短的row连到col上
  611. for row in split_row_list[i]:
  612. # 需判断该线在这个区域中
  613. if up_line2[1]-3 <= row[1] <= bottom_line2[1]+3:
  614. print([new_row_x, row[2]])
  615. longer_row_lines.append([row[0], row[1], max([new_row_x, row[2]]), row[3]])
  616. else:
  617. new_col_lines.append([bottom_line[2], up_line[3], bottom_line[2], bottom_line[3]])
  618. new_row_x = bottom_line[2]
  619. # 补了col,要将其他短的row连到col上
  620. for row in split_row_list[i]:
  621. # 需判断该线在这个区域中
  622. if up_line2[1]-3 <= row[1] <= bottom_line2[1]+3:
  623. print([new_row_x, row[2]])
  624. longer_row_lines.append([row[0], row[1], max([new_row_x, row[2]]), row[3]])
  625. if longer_row_lines:
  626. all_longer_row_lines += longer_row_lines
  627. else:
  628. all_longer_row_lines += split_row_list[i]
  629. if longer_col_lines:
  630. all_longer_col_lines += longer_col_lines
  631. else:
  632. all_longer_col_lines += split_col_list[i]
  633. # 删除表格内部的补线
  634. # temp_list = []
  635. # for row in new_row_lines:
  636. # if up_line[1]-5 <= row[1] <= bottom_line[1]+5:
  637. # continue
  638. # temp_list.append(row)
  639. # print("fix_outline", new_row_lines)
  640. # new_row_lines = temp_list
  641. # print("fix_outline", new_row_lines)
  642. # temp_list = []
  643. # for col in new_col_lines:
  644. # if left_line[0]-5 <= col[0] <= right_line[0]+5:
  645. # continue
  646. # temp_list.append(col)
  647. #
  648. # new_col_lines = temp_list
  649. # print("fix_outline", new_col_lines)
  650. # print("fix_outline", new_row_lines)
  651. # 删除重复包含的补线
  652. # temp_list = []
  653. # for row in new_row_lines:
  654. # if up_line[1]-5 <= row[1] <= bottom_line[1]+5:
  655. # continue
  656. # temp_list.append(row)
  657. # new_row_lines = temp_list
  658. # 展示上下左右边框线
  659. # for i in range(len(area_row_line)):
  660. # print("row1", area_row_line[i])
  661. # print("row2", area_row_line2[i])
  662. # print("col1", area_col_line[i])
  663. # print("col2", area_col_line2[i])
  664. # cv2.line(image, (int(area_row_line[i][0][0]), int(area_row_line[i][0][1])),
  665. # (int(area_row_line[i][0][2]), int(area_row_line[i][0][3])), (0, 255, 0), 2)
  666. # cv2.line(image, (int(area_row_line2[i][1][0]), int(area_row_line2[i][1][1])),
  667. # (int(area_row_line2[i][1][2]), int(area_row_line2[i][1][3])), (0, 0, 255), 2)
  668. # cv2.imshow("fix_outline", image)
  669. # cv2.waitKey(0)
  670. return new_row_lines, new_col_lines, all_longer_row_lines, all_longer_col_lines
  671. def fix_table(row_point_list, col_point_list, split_y, row_lines, col_lines):
  672. # 分割线纵坐标
  673. if len(split_y) < 2:
  674. return []
  675. # 获取bbox
  676. bbox = []
  677. # 每个点获取与其x最相近和y最相近的点
  678. for i in range(1, len(split_y)):
  679. # 循环每行
  680. for row in row_point_list:
  681. row.sort(key=lambda x: (x[0], x[1]))
  682. # 行不在该区域跳过
  683. if row[0][1] <= split_y[i-1] or row[0][1] >= split_y[i]:
  684. continue
  685. # print("len(row)", len(row))
  686. # print("row", row)
  687. # 循环行中的点
  688. for j in range(len(row)):
  689. if j == len(row) - 1:
  690. break
  691. current_point = row[j]
  692. next_point_in_row_list = row[j+1:]
  693. # 循环这一行的下一个点
  694. for next_point_in_row in next_point_in_row_list:
  695. # 是否在这一行点找到,找不到就这一行的下个点
  696. not_found = 1
  697. # 查询下个点所在列
  698. next_col = []
  699. for col in col_point_list:
  700. col.sort(key=lambda x: (x[1], x[0]))
  701. # 列不在该区域跳过
  702. if col[0][1] <= split_y[i-1] or col[-1][1] >= split_y[i]:
  703. continue
  704. if col[0][0]-3 <= next_point_in_row[0] <= col[0][0]+3:
  705. next_col = col
  706. break
  707. # 循环匹配当前点和下一列点
  708. next_col.sort(key=lambda x: (x[1], x[0]))
  709. for point1 in next_col:
  710. # 同一行的就跳过
  711. if current_point[1]-3 <= point1[1] <= current_point[1]+3:
  712. continue
  713. if point1[1] <= current_point[1]-3:
  714. continue
  715. # 候选bbox
  716. candidate_bbox = [current_point[0], current_point[1], point1[0], point1[1]]
  717. # print("candidate_bbox", candidate_bbox)
  718. # 判断该bbox是否存在,判断bbox的上下两条边是否有包含在row中
  719. contain_flag1 = 0
  720. contain_flag2 = 0
  721. for row1 in row_lines:
  722. # 行不在该区域跳过
  723. if row1[1] <= split_y[i-1] or row1[1] >= split_y[i]:
  724. continue
  725. # bbox上边框 y一样
  726. if not contain_flag1:
  727. if row1[1]-3 <= candidate_bbox[1] <= row1[1]+3:
  728. # 格子里的断开线段
  729. row1_break = (max([row1[0], candidate_bbox[0]]),
  730. row1[1],
  731. min([row1[2], candidate_bbox[2]]),
  732. row1[3])
  733. if row1_break[2] - row1_break[0] >= (candidate_bbox[2] - candidate_bbox[0])/3:
  734. contain_flag1 = 1
  735. # bbox下边框 y一样
  736. if not contain_flag2:
  737. if row1[1]-3 <= candidate_bbox[3] <= row1[1]+3:
  738. # 格子里的断开线段
  739. row1_break = (max([row1[0], candidate_bbox[0]]),
  740. row1[1],
  741. min([row1[2], candidate_bbox[2]]),
  742. row1[3])
  743. if row1_break[2] - row1_break[0] >= (candidate_bbox[2] - candidate_bbox[0])/3:
  744. contain_flag2 = 1
  745. # 判断该bbox是否存在,判断bbox的左右两条边是否有包含在col中
  746. contain_flag3 = 0
  747. contain_flag4 = 0
  748. for col1 in col_lines:
  749. # 列不在该区域跳过
  750. if col1[1] <= split_y[i-1] or col1[3] >= split_y[i]:
  751. continue
  752. # bbox左边线 x一样
  753. if not contain_flag3:
  754. if col1[0]-3 <= candidate_bbox[0] <= col1[0]+3:
  755. # 格子里的断开线段
  756. col1_break = (col1[0],
  757. max([col1[1], candidate_bbox[1]]),
  758. col1[2],
  759. min([col1[3], candidate_bbox[3]]))
  760. if col1_break[3] - col1_break[1] >= (candidate_bbox[3] - candidate_bbox[1])/3:
  761. contain_flag3 = 1
  762. # bbox右边框 x一样
  763. if not contain_flag4:
  764. if col1[0]-3 <= candidate_bbox[2] <= col1[0]+3:
  765. # 格子里的断开线段
  766. col1_break = (col1[0],
  767. max([col1[1], candidate_bbox[1]]),
  768. col1[2],
  769. min([col1[3], candidate_bbox[3]]))
  770. if col1_break[3] - col1_break[1] >= (candidate_bbox[3] - candidate_bbox[1])/3:
  771. contain_flag4 = 1
  772. # 找到了该bbox,并且是存在的
  773. if contain_flag1 and contain_flag2 and contain_flag3 and contain_flag4:
  774. bbox.append([(candidate_bbox[0], candidate_bbox[1]),
  775. (candidate_bbox[2], candidate_bbox[3])])
  776. not_found = 0
  777. break
  778. if not not_found:
  779. break
  780. return bbox
  781. def delete_close_points(point_list, row_point_list, col_point_list, threshold=5):
  782. new_point_list = []
  783. delete_point_list = []
  784. point_list.sort(key=lambda x: (x[1], x[0]))
  785. for i in range(len(point_list)):
  786. point1 = point_list[i]
  787. if point1 in delete_point_list:
  788. continue
  789. if i == len(point_list) - 1:
  790. new_point_list.append(point1)
  791. break
  792. point2 = point_list[i+1]
  793. # 判断坐标
  794. if abs(point1[0] - point2[0]) > threshold or abs(point1[1] - point2[1]) > threshold:
  795. new_point_list.append(point1)
  796. else:
  797. # 看两个点上的相同坐标点哪个多,就保留哪个
  798. count1 = 0
  799. count2 = 0
  800. for col in col_point_list:
  801. if point1[0] == col[0][0]:
  802. count1 += len(col)
  803. elif point2[0] == col[0][0]:
  804. count2 += len(col)
  805. if count1 >= count2:
  806. new_point_list.append(point1)
  807. delete_point_list.append(point2)
  808. else:
  809. new_point_list.append(point2)
  810. delete_point_list.append(point1)
  811. point_list = new_point_list
  812. new_point_list = []
  813. delete_point_list = []
  814. point_list.sort(key=lambda x: (x[0], x[1]))
  815. for i in range(len(point_list)):
  816. point1 = point_list[i]
  817. if point1 in delete_point_list:
  818. continue
  819. if i == len(point_list) - 1:
  820. new_point_list.append(point1)
  821. break
  822. point2 = point_list[i+1]
  823. # 判断坐标
  824. if abs(point1[0] - point2[0]) > threshold or abs(point1[1] - point2[1]) > threshold:
  825. new_point_list.append(point1)
  826. else:
  827. count1 = 0
  828. count2 = 0
  829. for row in row_point_list:
  830. if point1[0] == row[0][0]:
  831. count1 += len(row)
  832. elif point2[0] == row[0][0]:
  833. count2 += len(row)
  834. if count1 >= count2:
  835. new_point_list.append(point1)
  836. delete_point_list.append(point2)
  837. else:
  838. new_point_list.append(point2)
  839. delete_point_list.append(point1)
  840. return new_point_list
  841. def get_bbox2(image_np, points):
  842. # # 坐标点按行分
  843. # row_point_list = []
  844. # row_point = []
  845. # points.sort(key=lambda x: (x[0], x[1]))
  846. # for p in points:
  847. # if len(row_point) == 0:
  848. # x = p[0]
  849. # if x-5 <= p[0] <= x+5:
  850. # row_point.append(p)
  851. # else:
  852. # row_point_list.append(row_point)
  853. # row_point = []
  854. # # 坐标点按列分
  855. # col_point_list = []
  856. # col_point = []
  857. # points.sort(key=lambda x: (x[1], x[0]))
  858. # for p in points:
  859. # if len(col_point) == 0:
  860. # y = p[1]
  861. # if y-5 <= p[1] <= y+5:
  862. # col_point.append(p)
  863. # else:
  864. # col_point_list.append(col_point)
  865. # col_point = []
  866. row_point_list = get_points_row(points)
  867. col_point_list = get_points_col(points)
  868. print("len(points)", len(points))
  869. for point in points:
  870. cv2.circle(image_np, point, 1, (0, 255, 0), 1)
  871. cv2.imshow("points_deleted", image_np)
  872. points = delete_close_points(points, row_point_list, col_point_list)
  873. print("len(points)", len(points))
  874. for point in points:
  875. cv2.circle(image_np, point, 1, (255, 0, 0), 3)
  876. cv2.imshow("points_deleted", image_np)
  877. cv2.waitKey(0)
  878. row_point_list = get_points_row(points, 5)
  879. col_point_list = get_points_col(points, 5)
  880. print("len(row_point_list)", len(row_point_list))
  881. for row in row_point_list:
  882. print("row", len(row))
  883. print("col_point_list", len(col_point_list))
  884. for col in col_point_list:
  885. print("col", len(col))
  886. bbox = []
  887. for i in range(len(row_point_list)):
  888. if i == len(row_point_list) - 1:
  889. break
  890. # 遍历每个row的point,找到其所在列的下一个点和所在行的下一个点
  891. current_row = row_point_list[i]
  892. for j in range(len(current_row)):
  893. current_point = current_row[j]
  894. if j == len(current_row) - 1:
  895. break
  896. next_row_point = current_row[j+1]
  897. # 找出当前点所在的col,得到该列下一个point
  898. current_col = col_point_list[j]
  899. for k in range(len(current_col)):
  900. if current_col[k][1] > current_point[1] + 10:
  901. next_col_point = current_col[k]
  902. break
  903. next_row = row_point_list[k]
  904. for k in range(len(next_row)):
  905. if next_row[k][0] >= next_row_point[0] + 5:
  906. next_point = next_row[k]
  907. break
  908. # 得到bbox
  909. bbox.append([(current_point[0], current_point[1]), (next_point[0], next_point[1])])
  910. # bbox = []
  911. # for p in points:
  912. # # print("p", p)
  913. # p_row = []
  914. # p_col = []
  915. # for row in row_point_list:
  916. # if p[0] == row[0][0]:
  917. # for p1 in row:
  918. # if abs(p[1]-p1[1]) <= 5:
  919. # continue
  920. # p_row.append([p1, abs(p[1]-p1[1])])
  921. # p_row.sort(key=lambda x: x[1])
  922. # for col in col_point_list:
  923. # if p[1] == col[0][1]:
  924. # for p2 in col:
  925. # if abs(p[0]-p2[0]) <= 5:
  926. # continue
  927. # p_col.append([p2, abs(p[0]-p2[0])])
  928. # p_col.sort(key=lambda x: x[1])
  929. # if len(p_row) == 0 or len(p_col) == 0:
  930. # continue
  931. # break_flag = 0
  932. # for i in range(len(p_row)):
  933. # for j in range(len(p_col)):
  934. # # print(p_row[i][0])
  935. # # print(p_col[j][0])
  936. # another_point = (p_col[j][0][0], p_row[i][0][1])
  937. # # print("another_point", another_point)
  938. # if abs(p[0]-another_point[0]) <= 5 or abs(p[1]-another_point[1]) <= 5:
  939. # continue
  940. # if p[0] >= another_point[0] or p[1] >= another_point[1]:
  941. # continue
  942. # if another_point in points:
  943. # box = [p, another_point]
  944. # box.sort(key=lambda x: x[0])
  945. # if box not in bbox:
  946. # bbox.append(box)
  947. # break_flag = 1
  948. # break
  949. # if break_flag:
  950. # break
  951. #
  952. # # delete duplicate
  953. # delete_bbox = []
  954. # for i in range(len(bbox)):
  955. # for j in range(i+1, len(bbox)):
  956. # if bbox[i][0] == bbox[j][0]:
  957. # if bbox[i][1][0] - bbox[j][1][0] <= 3 \
  958. # and bbox[i][1][1] - bbox[j][1][1] <= 3:
  959. # delete_bbox.append(bbox[j])
  960. # if bbox[i][1] == bbox[j][1]:
  961. # if bbox[i][0][0] - bbox[j][0][0] <= 3 \
  962. # and bbox[i][0][1] - bbox[j][0][1] <= 3:
  963. # delete_bbox.append(bbox[j])
  964. # # delete too small area
  965. # # for box in bbox:
  966. # # if box[1][0] - box[0][0] <=
  967. # for d_box in delete_bbox:
  968. # if d_box in bbox:
  969. # bbox.remove(d_box)
  970. # print bbox
  971. bbox.sort(key=lambda x: (x[0][0], x[0][1], x[1][0], x[1][1]))
  972. # origin bbox
  973. # origin_bbox = []
  974. # for box in bbox:
  975. # origin_bbox.append([(box[0][0], box[0][1] - 40), (box[1][0], box[1][1] - 40)])
  976. # for box in origin_bbox:
  977. # cv2.rectangle(origin_image, box[0], box[1], (0, 0, 255), 2, 8)
  978. # cv2.imshow('AlanWang', origin_image)
  979. # cv2.waitKey(0)
  980. for box in bbox:
  981. cv2.rectangle(image_np, box[0], box[1], (0, 0, 255), 2, 8)
  982. cv2.imshow('bboxes', image_np)
  983. cv2.waitKey(0)
  984. # for point in points:
  985. # print(point)
  986. # cv2.circle(image_np, point, 1, (0, 0, 255), 3)
  987. # cv2.imshow('points', image_np)
  988. # cv2.waitKey(0)
  989. return bbox
  990. def get_bbox1(image_np, points, split_y):
  991. # 分割线纵坐标
  992. # print("split_y", split_y)
  993. if len(split_y) < 2:
  994. return []
  995. # 计算行列,剔除相近交点
  996. row_point_list = get_points_row(points)
  997. col_point_list = get_points_col(points)
  998. print("len(row_point_list)", row_point_list)
  999. print("len(col_point_list)", len(col_point_list))
  1000. # for point in points:
  1001. # cv2.circle(image_np, point, 1, (0, 255, 0), 1)
  1002. # cv2.imshow("points", image_np)
  1003. points = delete_close_points(points, row_point_list, col_point_list)
  1004. # print("len(points)", len(points))
  1005. # for point in points:
  1006. # cv2.circle(image_np, point, 1, (255, 0, 0), 3)
  1007. # cv2.imshow("points_deleted", image_np)
  1008. # cv2.waitKey(0)
  1009. # 获取bbox
  1010. bbox = []
  1011. # 每个点获取与其x最相近和y最相近的点
  1012. for i in range(1, len(split_y)):
  1013. for point1 in points:
  1014. if point1[1] <= split_y[i-1] or point1[1] >= split_y[i]:
  1015. continue
  1016. distance_x = 10000
  1017. distance_y = 10000
  1018. x = 0
  1019. y = 0
  1020. threshold = 10
  1021. for point2 in points:
  1022. if point2[1] <= split_y[i-1] or point2[1] >= split_y[i]:
  1023. continue
  1024. # 最近 x y
  1025. if 2 < point2[0] - point1[0] < distance_x and point2[1] - point1[1] <= threshold:
  1026. distance_x = point2[0] - point1[0]
  1027. x = point2[0]
  1028. if 2 < point2[1] - point1[1] < distance_y and point2[0] - point1[0] <= threshold:
  1029. distance_y = point2[1] - point1[1]
  1030. y = point2[1]
  1031. if not x or not y:
  1032. continue
  1033. bbox.append([(point1[0], point1[1]), (x, y)])
  1034. # 删除包含关系bbox
  1035. temp_list = []
  1036. for i in range(len(bbox)):
  1037. box1 = bbox[i]
  1038. for j in range(len(bbox)):
  1039. if i == j:
  1040. continue
  1041. box2 = bbox[j]
  1042. contain_flag = 0
  1043. if box2[0][0] <= box1[0][0] <= box1[1][0] <= box2[1][0] and \
  1044. box2[0][1] <= box1[0][1] <= box1[1][1] <= box2[1][1]:
  1045. contain_flag = 1
  1046. break
  1047. temp_list.append(box1)
  1048. bbox = temp_list
  1049. # 展示
  1050. for box in bbox:
  1051. # print(box[0], box[1])
  1052. # if abs(box[0][1] - box[1][1]) > abs(box[0][0] - box[1][0]):
  1053. # continue
  1054. cv2.rectangle(image_np, box[0], box[1], (0, 0, 255), 2, 8)
  1055. cv2.imshow('bboxes', image_np)
  1056. cv2.waitKey(0)
  1057. return bbox
  1058. def get_bbox0(image_np, row_point_list, col_point_list, split_y, row_lines, col_lines):
  1059. # 分割线纵坐标
  1060. if len(split_y) < 2:
  1061. return []
  1062. # 计算行列,剔除相近交点
  1063. # row_point_list = get_points_row(points)
  1064. # col_point_list = get_points_col(points)
  1065. # points = delete_close_points(points, row_point_list, col_point_list)
  1066. # row_point_list = get_points_row(points)
  1067. # col_point_list = get_points_col(points)
  1068. # 获取bbox
  1069. bbox = []
  1070. # print("get_bbox split_y", split_y)
  1071. # 每个点获取与其x最相近和y最相近的点
  1072. for i in range(1, len(split_y)):
  1073. # 循环每行
  1074. for row in row_point_list:
  1075. row.sort(key=lambda x: (x[0], x[1]))
  1076. # 行不在该区域跳过
  1077. if row[0][1] <= split_y[i-1] or row[0][1] >= split_y[i]:
  1078. continue
  1079. # 循环行中的点
  1080. for j in range(len(row)):
  1081. if j == len(row) - 1:
  1082. break
  1083. current_point = row[j]
  1084. next_point_in_row = row[j+1]
  1085. # 查询下个点所在列
  1086. next_col = []
  1087. for col in col_point_list:
  1088. col.sort(key=lambda x: (x[1], x[0]))
  1089. # 列不在该区域跳过
  1090. if col[0][1] <= split_y[i-1] or col[-1][1] >= split_y[i]:
  1091. continue
  1092. if col[0][0]-3 <= next_point_in_row[0] <= col[0][0]+3:
  1093. next_col = col
  1094. break
  1095. # 循环匹配当前点和下一列点
  1096. for point1 in next_col:
  1097. # 同一行的就跳过
  1098. if current_point[1]-3 <= point1[1] <= current_point[1]+3:
  1099. continue
  1100. if point1[1] <= current_point[1]-3:
  1101. continue
  1102. # 候选bbox
  1103. candidate_bbox = [current_point[0], current_point[1], point1[0], point1[1]]
  1104. # 判断该bbox是否存在,线条包含关系
  1105. contain_flag1 = 0
  1106. contain_flag2 = 0
  1107. for row1 in row_lines:
  1108. # 行不在该区域跳过
  1109. if row1[1] <= split_y[i-1] or row1[1] >= split_y[i]:
  1110. continue
  1111. # bbox上边框 y一样
  1112. if not contain_flag1:
  1113. if row1[1]-3 <= candidate_bbox[1] <= row1[1]+3:
  1114. # candidate的x1,x2需被包含在row线中
  1115. if row1[0]-3 <= candidate_bbox[0] <= candidate_bbox[2] <= row1[2]+3:
  1116. contain_flag1 = 1
  1117. # bbox下边框 y一样
  1118. if not contain_flag2:
  1119. if row1[1]-3 <= candidate_bbox[3] <= row1[1]+3:
  1120. # candidate的x1,x2需被包含在row线中
  1121. if row1[0]-3 <= candidate_bbox[0] <= candidate_bbox[2] <= row1[2]+3:
  1122. contain_flag2 = 1
  1123. # 找到了该bbox,并且是存在的
  1124. if contain_flag1 and contain_flag2:
  1125. bbox.append([(candidate_bbox[0], candidate_bbox[1]),
  1126. (candidate_bbox[2], candidate_bbox[3])])
  1127. break
  1128. return bbox
  1129. def get_bbox(image_np, row_point_list, col_point_list, split_y, row_lines, col_lines):
  1130. # 分割线纵坐标
  1131. if len(split_y) < 2:
  1132. return []
  1133. # 获取bbox
  1134. bbox = []
  1135. # 每个点获取与其x最相近和y最相近的点
  1136. for i in range(1, len(split_y)):
  1137. # 循环每行
  1138. for row in row_point_list:
  1139. row.sort(key=lambda x: (x[0], x[1]))
  1140. # 行不在该区域跳过
  1141. if row[0][1] <= split_y[i-1] or row[0][1] >= split_y[i]:
  1142. continue
  1143. # print("len(row)", len(row))
  1144. # print("row", row)
  1145. # 循环行中的点
  1146. for j in range(len(row)):
  1147. if j == len(row) - 1:
  1148. break
  1149. current_point = row[j]
  1150. next_point_in_row_list = row[j+1:]
  1151. # 循环这一行的下一个点
  1152. for next_point_in_row in next_point_in_row_list:
  1153. # 是否在这一行点找到,找不到就这一行的下个点
  1154. not_found = 1
  1155. # 查询下个点所在列
  1156. next_col = []
  1157. for col in col_point_list:
  1158. col.sort(key=lambda x: (x[1], x[0]))
  1159. # 列不在该区域跳过
  1160. if col[0][1] <= split_y[i-1] or col[-1][1] >= split_y[i]:
  1161. continue
  1162. if col[0][0]-3 <= next_point_in_row[0] <= col[0][0]+3:
  1163. next_col = col
  1164. break
  1165. # 循环匹配当前点和下一列点
  1166. next_col.sort(key=lambda x: (x[1], x[0]))
  1167. for point1 in next_col:
  1168. # 同一行的就跳过
  1169. if current_point[1]-3 <= point1[1] <= current_point[1]+3:
  1170. continue
  1171. if point1[1] <= current_point[1]-3:
  1172. continue
  1173. # 候选bbox
  1174. candidate_bbox = [current_point[0], current_point[1], point1[0], point1[1]]
  1175. # print("candidate_bbox", candidate_bbox)
  1176. # 判断该bbox是否存在,判断bbox的上下两条边是否有包含在row中
  1177. contain_flag1 = 0
  1178. contain_flag2 = 0
  1179. for row1 in row_lines:
  1180. # 行不在该区域跳过
  1181. if row1[1] <= split_y[i-1] or row1[1] >= split_y[i]:
  1182. continue
  1183. # bbox上边框 y一样
  1184. if not contain_flag1:
  1185. if row1[1]-3 <= candidate_bbox[1] <= row1[1]+3:
  1186. # 格子里的断开线段
  1187. row1_break = (max([row1[0], candidate_bbox[0]]),
  1188. row1[1],
  1189. min([row1[2], candidate_bbox[2]]),
  1190. row1[3])
  1191. if row1_break[2] - row1_break[0] >= (candidate_bbox[2] - candidate_bbox[0])/3:
  1192. contain_flag1 = 1
  1193. # # candidate的x1,x2需被包含在row线中
  1194. # if row1[0]-3 <= candidate_bbox[0] <= candidate_bbox[2] <= row1[2]+3:
  1195. # contain_flag1 = 1
  1196. #
  1197. # # 判断线条有无端点在格子中
  1198. # elif candidate_bbox[0] < row1[0] < candidate_bbox[2] \
  1199. # or candidate_bbox[0] < row1[2] < candidate_bbox[2]:
  1200. # # 线条会有缺一点情况,判断长度超过格子一半
  1201. # if row1_break[2] - row1_break[0] >= (candidate_bbox[2] - candidate_bbox[0])/3:
  1202. # contain_flag1 = 1
  1203. # bbox下边框 y一样
  1204. if not contain_flag2:
  1205. if row1[1]-3 <= candidate_bbox[3] <= row1[1]+3:
  1206. # 格子里的断开线段
  1207. row1_break = (max([row1[0], candidate_bbox[0]]),
  1208. row1[1],
  1209. min([row1[2], candidate_bbox[2]]),
  1210. row1[3])
  1211. if row1_break[2] - row1_break[0] >= (candidate_bbox[2] - candidate_bbox[0])/3:
  1212. contain_flag2 = 1
  1213. # # candidate的x1,x2需被包含在row线中
  1214. # if row1[0]-3 <= candidate_bbox[0] <= candidate_bbox[2] <= row1[2]+3:
  1215. # contain_flag2 = 1
  1216. #
  1217. # # 判断线条有无端点在格子中
  1218. # elif candidate_bbox[0] < row1[0] < candidate_bbox[2] \
  1219. # or candidate_bbox[0] < row1[2] < candidate_bbox[2]:
  1220. # # 线条会有缺一点情况,判断长度超过格子一半
  1221. # if row1_break[2] - row1_break[0] >= (candidate_bbox[2] - candidate_bbox[0])/3:
  1222. # contain_flag2 = 1
  1223. # 判断该bbox是否存在,判断bbox的左右两条边是否有包含在col中
  1224. contain_flag3 = 0
  1225. contain_flag4 = 0
  1226. for col1 in col_lines:
  1227. # 列不在该区域跳过
  1228. if col1[1] <= split_y[i-1] or col1[3] >= split_y[i]:
  1229. continue
  1230. # bbox左边线 x一样
  1231. if not contain_flag3:
  1232. if col1[0]-3 <= candidate_bbox[0] <= col1[0]+3:
  1233. # 格子里的断开线段
  1234. col1_break = (col1[0],
  1235. max([col1[1], candidate_bbox[1]]),
  1236. col1[2],
  1237. min([col1[3], candidate_bbox[3]]))
  1238. if col1_break[3] - col1_break[1] >= (candidate_bbox[3] - candidate_bbox[1])/3:
  1239. contain_flag3 = 1
  1240. # # candidate的y1,y2需被包含在col线中
  1241. # if col1[1]-3 <= candidate_bbox[1] <= candidate_bbox[3] <= col1[3]+3:
  1242. # contain_flag3 = 1
  1243. #
  1244. # # 判断线条有无端点在格子中
  1245. # elif candidate_bbox[1] < col1[1] < candidate_bbox[3] \
  1246. # or candidate_bbox[1] < col1[3] < candidate_bbox[3]:
  1247. # # 线条会有缺一点情况,判断长度超过格子一半
  1248. # if col1_break[3] - col1_break[1] >= (candidate_bbox[3] - candidate_bbox[1])/3:
  1249. # contain_flag3 = 1
  1250. # bbox右边框 x一样
  1251. if not contain_flag4:
  1252. if col1[0]-3 <= candidate_bbox[2] <= col1[0]+3:
  1253. # 格子里的断开线段
  1254. col1_break = (col1[0],
  1255. max([col1[1], candidate_bbox[1]]),
  1256. col1[2],
  1257. min([col1[3], candidate_bbox[3]]))
  1258. if col1_break[3] - col1_break[1] >= (candidate_bbox[3] - candidate_bbox[1])/3:
  1259. contain_flag4 = 1
  1260. # # candidate的y1,y2需被包含在col线中
  1261. # if col1[1]-3 <= candidate_bbox[1] <= candidate_bbox[3] <= col1[3]+3:
  1262. # contain_flag4 = 1
  1263. #
  1264. # # 判断线条有无端点在格子中
  1265. # elif candidate_bbox[1] < col1[1] < candidate_bbox[3] \
  1266. # or candidate_bbox[1] < col1[3] < candidate_bbox[3]:
  1267. # # 线条会有缺一点情况,判断长度超过格子一半
  1268. # if col1_break[3] - col1_break[1] >= (candidate_bbox[3] - candidate_bbox[1])/3:
  1269. # contain_flag4 = 1
  1270. # 找到了该bbox,并且是存在的
  1271. if contain_flag1 and contain_flag2 and contain_flag3 and contain_flag4:
  1272. bbox.append([(candidate_bbox[0], candidate_bbox[1]),
  1273. (candidate_bbox[2], candidate_bbox[3])])
  1274. not_found = 0
  1275. # print("candidate_bbox", candidate_bbox)
  1276. # print(contain_flag1, contain_flag2, contain_flag3, contain_flag4)
  1277. break
  1278. # else:
  1279. # print("candidate_bbox", candidate_bbox)
  1280. # print(contain_flag1, contain_flag2, contain_flag3, contain_flag4)
  1281. if not not_found:
  1282. break
  1283. return bbox
  1284. def get_bbox_by_contours(image_np):
  1285. img_gray = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)
  1286. ret, img_bin = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)
  1287. # 3.连通域分析
  1288. img_bin, contours, hierarchy = cv2.findContours(img_bin,
  1289. cv2.RETR_LIST,
  1290. cv2.CHAIN_APPROX_SIMPLE)
  1291. # 4.获取最小外接圆 圆心 半径
  1292. center, radius = cv2.minEnclosingTriangle(contours[0])
  1293. center = np.int0(center)
  1294. # 5.绘制最小外接圆
  1295. img_result = image_np.copy()
  1296. cv2.circle(img_result, tuple(center), int(radius), (255, 255, 255), 2)
  1297. # # 读入图片
  1298. # img = image_np
  1299. # cv2.imshow("get_bbox_by_contours ", image_np)
  1300. # # 中值滤波,去噪
  1301. # img = cv2.medianBlur(img, 3)
  1302. # gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  1303. # cv2.namedWindow('original', cv2.WINDOW_AUTOSIZE)
  1304. # cv2.imshow('original', gray)
  1305. #
  1306. # # 阈值分割得到二值化图片
  1307. # ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
  1308. #
  1309. # # 膨胀操作
  1310. # kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
  1311. # bin_clo = cv2.dilate(binary, kernel2, iterations=2)
  1312. #
  1313. # # 连通域分析
  1314. # num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(bin_clo, connectivity=8)
  1315. #
  1316. # # 查看各个返回值
  1317. # # 连通域数量
  1318. # print('num_labels = ',num_labels)
  1319. # # 连通域的信息:对应各个轮廓的x、y、width、height和面积
  1320. # print('stats = ',stats)
  1321. # # 连通域的中心点
  1322. # print('centroids = ',centroids)
  1323. # # 每一个像素的标签1、2、3.。。,同一个连通域的标签是一致的
  1324. # print('labels = ',labels)
  1325. #
  1326. # # 不同的连通域赋予不同的颜色
  1327. # output = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)
  1328. # for i in range(1, num_labels):
  1329. #
  1330. # mask = labels == i
  1331. # output[:, :, 0][mask] = np.random.randint(0, 255)
  1332. # output[:, :, 1][mask] = np.random.randint(0, 255)
  1333. # output[:, :, 2][mask] = np.random.randint(0, 255)
  1334. # cv2.imshow('oginal', output)
  1335. # cv2.waitKey()
  1336. # cv2.destroyAllWindows()
  1337. def get_points_col(points, split_y, threshold=5):
  1338. # 坐标点按行分
  1339. row_point_list = []
  1340. row_point = []
  1341. points.sort(key=lambda x: (x[0], x[1]))
  1342. x = points[0][0]
  1343. for i in range(1, len(split_y)):
  1344. for p in points:
  1345. if p[1] <= split_y[i-1] or p[1] >= split_y[i]:
  1346. continue
  1347. if x-threshold <= p[0] <= x+threshold:
  1348. row_point.append(p)
  1349. else:
  1350. row_point.sort(key=lambda x: (x[1], x[0]))
  1351. if row_point:
  1352. row_point_list.append(row_point)
  1353. row_point = []
  1354. x = p[0]
  1355. row_point.append(p)
  1356. if row_point:
  1357. row_point_list.append(row_point)
  1358. return row_point_list
  1359. def get_points_row(points, split_y, threshold=5):
  1360. # 坐标点按列分
  1361. col_point_list = []
  1362. col_point = []
  1363. points.sort(key=lambda x: (x[1], x[0]))
  1364. y = points[0][1]
  1365. for i in range(len(split_y)):
  1366. for p in points:
  1367. if p[1] <= split_y[i-1] or p[1] >= split_y[i]:
  1368. continue
  1369. if y-threshold <= p[1] <= y+threshold:
  1370. col_point.append(p)
  1371. else:
  1372. col_point.sort(key=lambda x: (x[0], x[1]))
  1373. if col_point:
  1374. col_point_list.append(col_point)
  1375. col_point = []
  1376. y = p[1]
  1377. col_point.append(p)
  1378. if col_point:
  1379. col_point_list.append(col_point)
  1380. return col_point_list
  1381. def get_outline_point(points, split_y):
  1382. # 分割线纵坐标
  1383. # print("get_outline_point split_y", split_y)
  1384. if len(split_y) < 2:
  1385. return []
  1386. outline_2point = []
  1387. points.sort(key=lambda x: (x[1], x[0]))
  1388. for i in range(1, len(split_y)):
  1389. area_points = []
  1390. for point in points:
  1391. if point[1] <= split_y[i-1] or point[1] >= split_y[i]:
  1392. continue
  1393. area_points.append(point)
  1394. if area_points:
  1395. area_points.sort(key=lambda x: (x[1], x[0]))
  1396. outline_2point.append([area_points[0], area_points[-1]])
  1397. return outline_2point
  1398. # def merge_row(row_lines):
  1399. # for row in row_lines:
  1400. # for row1 in row_lines:
  1401. def get_best_predict_size(image_np):
  1402. sizes = [1280, 1152, 1024, 896, 768, 640, 512, 384, 256, 128]
  1403. min_len = 10000
  1404. best_height = sizes[0]
  1405. for height in sizes:
  1406. if abs(image_np.shape[0] - height) < min_len:
  1407. min_len = abs(image_np.shape[0] - height)
  1408. best_height = height
  1409. min_len = 10000
  1410. best_width = sizes[0]
  1411. for width in sizes:
  1412. if abs(image_np.shape[1] - width) < min_len:
  1413. min_len = abs(image_np.shape[1] - width)
  1414. best_width = width
  1415. return best_height, best_width
  1416. def choose_longer_row(lines):
  1417. new_row = []
  1418. jump_row = []
  1419. for i in range(len(lines)):
  1420. row1 = lines[i]
  1421. jump_flag = 0
  1422. if row1 in jump_row:
  1423. continue
  1424. for j in range(i+1, len(lines)):
  1425. row2 = lines[j]
  1426. if row2 in jump_row:
  1427. continue
  1428. if row2[1]-5 <= row1[1] <= row2[1]+5:
  1429. if row1[0] <= row2[0] and row1[2] >= row2[2]:
  1430. new_row.append(row1)
  1431. jump_row.append(row1)
  1432. jump_row.append(row2)
  1433. jump_flag = 1
  1434. break
  1435. elif row2[0] <= row1[0] and row2[2] >= row1[2]:
  1436. new_row.append(row2)
  1437. jump_row.append(row1)
  1438. jump_row.append(row2)
  1439. jump_flag = 1
  1440. break
  1441. if not jump_flag:
  1442. new_row.append(row1)
  1443. jump_row.append(row1)
  1444. return new_row
  1445. def choose_longer_col(lines):
  1446. new_col = []
  1447. jump_col = []
  1448. for i in range(len(lines)):
  1449. col1 = lines[i]
  1450. jump_flag = 0
  1451. if col1 in jump_col:
  1452. continue
  1453. for j in range(i+1, len(lines)):
  1454. col2 = lines[j]
  1455. if col2 in jump_col:
  1456. continue
  1457. if col2[0]-5 <= col1[0] <= col2[0]+5:
  1458. if col1[1] <= col2[1] and col1[3] >= col2[3]:
  1459. new_col.append(col1)
  1460. jump_col.append(col1)
  1461. jump_col.append(col2)
  1462. jump_flag = 1
  1463. break
  1464. elif col2[1] <= col1[1] and col2[3] >= col1[3]:
  1465. new_col.append(col2)
  1466. jump_col.append(col1)
  1467. jump_col.append(col2)
  1468. jump_flag = 1
  1469. break
  1470. if not jump_flag:
  1471. new_col.append(col1)
  1472. jump_col.append(col1)
  1473. return new_col
  1474. def line_fix(image_np):
  1475. image_binary = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)
  1476. rows, cols = image_binary.shape
  1477. scale = 100
  1478. kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, rows//scale))
  1479. row_erode = cv2.erode(image_binary, kernel, iterations=1)
  1480. cv2.imshow("row_erode", row_erode)
  1481. scale = 100
  1482. kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (cols//scale, 1))
  1483. col_erode = cv2.erode(image_binary, kernel, iterations=1)
  1484. cv2.imshow("col_erode", col_erode)
  1485. image_erode = row_erode + col_erode
  1486. cv2.imshow("image_erode", image_erode)
  1487. return image_erode
  1488. if __name__ == '__main__':
  1489. net = table_net((1024, 768, 3), 2)
  1490. net.summary()