table_line.py 67 KB

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