﻿{"id":1757,"date":"2018-09-05T15:46:05","date_gmt":"2018-09-05T07:46:05","guid":{"rendered":"http:\/\/nick.txtcc.com\/?p=1757"},"modified":"2018-09-05T15:46:06","modified_gmt":"2018-09-05T07:46:06","slug":"python%e5%9b%be%e7%89%87%e9%aa%8c%e8%af%81%e7%a0%81%e8%af%86%e5%88%ab","status":"publish","type":"post","link":"http:\/\/nick.txtcc.com\/index.php\/linux\/1757","title":{"rendered":"Python\u56fe\u7247\u9a8c\u8bc1\u7801\u8bc6\u522b"},"content":{"rendered":"<h2>\u5927\u81f4\u4ecb\u7ecd<\/h2>\n<p>\u5728python\u722c\u866b\u722c\u53d6\u67d0\u4e9b\u7f51\u7ad9\u7684\u9a8c\u8bc1\u7801\u7684\u65f6\u5019\u53ef\u80fd\u4f1a\u9047\u5230\u9a8c\u8bc1\u7801\u8bc6\u522b\u7684\u95ee\u9898\uff0c\u73b0\u5728\u7684\u9a8c\u8bc1\u7801\u5927\u591a\u5206\u4e3a\u56db\u7c7b\uff1a<\/p>\n<p>1\u3001\u8ba1\u7b97\u9a8c\u8bc1\u7801<\/p>\n<p>2\u3001\u6ed1\u5757\u9a8c\u8bc1\u7801<\/p>\n<p>3\u3001\u8bc6\u56fe\u9a8c\u8bc1\u7801<\/p>\n<p>4\u3001\u8bed\u97f3\u9a8c\u8bc1\u7801<\/p>\n<p>\u8fd9\u7bc7\u535a\u5ba2\u4e3b\u8981\u5199\u7684\u5c31\u662f\u8bc6\u56fe\u9a8c\u8bc1\u7801\uff0c\u8bc6\u522b\u7684\u662f\u7b80\u5355\u7684\u9a8c\u8bc1\u7801\uff0c\u8981\u60f3\u8ba9\u8bc6\u522b\u7387\u66f4\u9ad8\uff0c\u8bc6\u522b\u7684\u66f4\u52a0\u51c6\u786e\u5c31\u9700\u8981\u82b1\u5f88\u591a\u7684\u7cbe\u529b\u53bb\u8bad\u7ec3\u81ea\u5df1\u7684\u5b57\u4f53\u5e93\u3002<\/p>\n<p>\u8bc6\u522b\u9a8c\u8bc1\u7801\u901a\u5e38\u662f\u8fd9\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<p>1\u3001\u7070\u5ea6\u5904\u7406<\/p>\n<p>2\u3001\u4e8c\u503c\u5316<\/p>\n<p>3\u3001\u53bb\u9664\u8fb9\u6846\uff08\u5982\u679c\u6709\u7684\u8bdd\uff09<\/p>\n<p>4\u3001\u964d\u566a<\/p>\n<p>5\u3001\u5207\u5272\u5b57\u7b26\u6216\u8005\u503e\u659c\u5ea6\u77eb\u6b63<\/p>\n<p>6\u3001\u8bad\u7ec3\u5b57\u4f53\u5e93<\/p>\n<p>7\u3001\u8bc6\u522b<\/p>\n<p>\u8fd96\u4e2a\u6b65\u9aa4\u4e2d\u524d\u4e09\u4e2a\u6b65\u9aa4\u662f\u57fa\u672c\u7684\uff0c4\u6216\u80055\u53ef\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u9009\u62e9\u662f\u5426\u9700\u8981\uff0c\u5e76\u4e0d\u4e00\u5b9a\u5207\u5272\u9a8c\u8bc1\u7801\uff0c\u8bc6\u522b\u7387\u5c31\u4f1a\u4e0a\u5347\u5f88\u591a\u6709\u65f6\u5019\u8fd8\u4f1a\u4e0b\u964d<\/p>\n<p>\u8fd9\u7bc7\u535a\u5ba2\u4e0d\u6d89\u53ca\u8bad\u7ec3\u5b57\u4f53\u5e93\u7684\u5185\u5bb9\uff0c\u8bf7\u81ea\u884c\u641c\u7d22\u3002\u540c\u6837\u4e5f\u4e0d\u8bb2\u89e3\u57fa\u7840\u7684\u8bed\u6cd5\u3002<\/p>\n<p>\u7528\u5230\u7684\u51e0\u4e2a\u4e3b\u8981\u7684python\u5e93\uff1a Pillow(python\u56fe\u50cf\u5904\u7406\u5e93)\u3001OpenCV(\u9ad8\u7ea7\u56fe\u50cf\u5904\u7406\u5e93)\u3001pytesseract(\u8bc6\u522b\u5e93)<\/p>\n<p>&nbsp;<\/p>\n<h3>\u7070\u5ea6\u5904\u7406&#038;\u4e8c\u503c\u5316<\/h3>\n<p>\u7070\u5ea6\u5904\u7406\uff0c\u5c31\u662f\u628a\u5f69\u8272\u7684\u9a8c\u8bc1\u7801\u56fe\u7247\u8f6c\u4e3a\u7070\u8272\u7684\u56fe\u7247\u3002<\/p>\n<p>\u4e8c\u503c\u5316\uff0c\u662f\u5c06\u56fe\u7247\u5904\u7406\u4e3a\u53ea\u6709\u9ed1\u767d\u4e24\u8272\u7684\u56fe\u7247\uff0c\u5229\u4e8e\u540e\u9762\u7684\u56fe\u50cf\u5904\u7406\u548c\u8bc6\u522b<\/p>\n<p>\u5728OpenCV\u4e2d\u6709\u73b0\u6210\u7684\u65b9\u6cd5\u53ef\u4ee5\u8fdb\u884c\u7070\u5ea6\u5904\u7406\u548c\u4e8c\u503c\u5316\uff0c\u5904\u7406\u540e\u7684\u6548\u679c\uff1a<\/p>\n<img decoding=\"async\" src=\"https:\/\/images2017.cnblogs.com\/blog\/951708\/201711\/951708-20171120151348665-939380157.png\" alt=\"\" \/>\n<p>&nbsp;<\/p>\n<p>\u4ee3\u7801\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\"><\/a><\/span><\/div>\n<pre> 1 # \u81ea\u9002\u5e94\u9600\u503c\u4e8c\u503c\u5316\n 2 def _get_dynamic_binary_image(filedir, img_name):\n 3   filename =   '.\/out_img\/' + img_name.split('.')[0] + '-binary.jpg'\n 4   img_name = filedir + '\/' + img_name\n 5   print('.....' + img_name)\n 6   im = cv2.imread(img_name)\n 7   im = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) #\u7070\u503c\u5316\n 8   # \u4e8c\u503c\u5316\n 9   th1 = cv2.adaptiveThreshold(im, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 21, 1)\n10   cv2.imwrite(filename,th1)\n11   return th1<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\"><\/a><\/span><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<h3>\u53bb\u9664\u8fb9\u6846<\/h3>\n<p>\u5982\u679c\u9a8c\u8bc1\u7801\u6709\u8fb9\u6846\uff0c\u90a3\u6211\u4eec\u5c31\u9700\u8981\u53bb\u9664\u8fb9\u6846\uff0c\u53bb\u9664\u8fb9\u6846\u5c31\u662f\u904d\u5386\u50cf\u7d20\u70b9\uff0c\u627e\u5230\u56db\u4e2a\u8fb9\u6846\u4e0a\u7684\u6240\u6709\u70b9\uff0c\u628a\u4ed6\u4eec\u90fd\u6539\u4e3a\u767d\u8272\uff0c\u6211\u8fd9\u91cc\u8fb9\u6846\u662f\u4e24\u4e2a\u50cf\u7d20\u5bbd<\/p>\n<p>\u6ce8\u610f\uff1a\u5728\u7528OpenCV\u65f6\uff0c\u56fe\u7247\u7684\u77e9\u9635\u70b9\u662f\u53cd\u7684\uff0c\u5c31\u662f\u957f\u548c\u5bbd\u662f\u98a0\u5012\u7684<\/p>\n<p>\u4ee3\u7801\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\"><\/a><\/span><\/div>\n<pre># \u53bb\u9664\u8fb9\u6846\ndef clear_border(img,img_name):\n  filename = '.\/out_img\/' + img_name.split('.')[0] + '-clearBorder.jpg'\n  h, w = img.shape[:2]\n  for y in range(0, w):\n    for x in range(0, h):\n      if y < 2 or y > w - 2:\n        img[x, y] = 255\n      if x < 2 or x > h -2:\n        img[x, y] = 255\n\n  cv2.imwrite(filename,img)\n  return img<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\"><\/a><\/span><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>\u6548\u679c\uff1a<\/p>\n<img decoding=\"async\" src=\"https:\/\/images2017.cnblogs.com\/blog\/951708\/201711\/951708-20171120152624243-444859808.png\" alt=\"\" \/>\n<h3>\u964d\u566a<\/h3>\n<p>\u964d\u566a\u662f\u9a8c\u8bc1\u7801\u5904\u7406\u4e2d\u6bd4\u8f83\u91cd\u8981\u7684\u4e00\u4e2a\u6b65\u9aa4\uff0c\u6211\u8fd9\u91cc\u4f7f\u7528\u4e86\u70b9\u964d\u566a\u548c\u7ebf\u964d\u566a<\/p>\n<img decoding=\"async\" src=\"https:\/\/images2017.cnblogs.com\/blog\/951708\/201711\/951708-20171120153159446-1886387754.png\" alt=\"\" \/>\n<p>\u7ebf\u964d\u566a\u7684\u601d\u8def\u5c31\u662f\u68c0\u6d4b\u8fd9\u4e2a\u70b9\u76f8\u90bb\u7684\u56db\u4e2a\u70b9\uff08\u56fe\u4e2d\u6807\u51fa\u7684\u7eff\u8272\u70b9\uff09\uff0c\u5224\u65ad\u8fd9\u56db\u4e2a\u70b9\u4e2d\u662f\u767d\u70b9\u7684\u4e2a\u6570\uff0c\u5982\u679c\u6709\u4e24\u4e2a\u4ee5\u4e0a\u7684\u767d\u8272\u50cf\u7d20\u70b9\uff0c\u90a3\u4e48\u5c31\u8ba4\u4e3a\u8fd9\u4e2a\u70b9\u662f\u767d\u8272\u7684\uff0c\u4ece\u800c\u53bb\u9664\u6574\u4e2a\u5e72\u6270\u7ebf\uff0c\u4f46\u662f\u8fd9\u79cd\u65b9\u6cd5\u662f\u6709\u9650\u5ea6\u7684\uff0c\u5982\u679c\u5e72\u6270\u7ebf\u7279\u522b\u7c97\u5c31\u6ca1\u6709\u529e\u6cd5\u53bb\u9664\uff0c\u53ea\u80fd\u53bb\u9664\u7ec6\u7684\u5e72\u6270\u7ebf<\/p>\n<p>\u4ee3\u7801\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\"><\/a><\/span><\/div>\n<pre> 1 # \u5e72\u6270\u7ebf\u964d\u566a\n 2 def interference_line(img, img_name):\n 3   filename =  '.\/out_img\/' + img_name.split('.')[0] + '-interferenceline.jpg'\n 4   h, w = img.shape[:2]\n 5   # \uff01\uff01\uff01opencv\u77e9\u9635\u70b9\u662f\u53cd\u7684\n 6   # img[1,2] 1:\u56fe\u7247\u7684\u9ad8\u5ea6\uff0c2\uff1a\u56fe\u7247\u7684\u5bbd\u5ea6\n 7   for y in range(1, w - 1):\n 8     for x in range(1, h - 1):\n 9       count = 0\n10       if img[x, y - 1] > 245:\n11         count = count + 1\n12       if img[x, y + 1] > 245:\n13         count = count + 1\n14       if img[x - 1, y] > 245:\n15         count = count + 1\n16       if img[x + 1, y] > 245:\n17         count = count + 1\n18       if count > 2:\n19         img[x, y] = 255\n20   cv2.imwrite(filename,img)\n21   return img<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\"><\/a><\/span><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>\u70b9\u964d\u566a\u7684\u601d\u8def\u548c\u7ebf\u964d\u566a\u7684\u5dee\u4e0d\u591a\uff0c\u53ea\u662f\u4f1a\u9488\u5bf9\u4e0d\u540c\u7684\u4f4d\u7f6e\u68c0\u6d4b\u7684\u70b9\u4e0d\u4e00\u6837,\u6ce8\u91ca\u5199\u7684\u5f88\u6e05\u695a\u4e86<\/p>\n<p>\u4ee3\u7801\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\"><\/a><\/span><\/div>\n<pre># \u70b9\u964d\u566a\ndef interference_point(img,img_name, x = 0, y = 0):\n    \"\"\"\n    9\u90bb\u57df\u6846,\u4ee5\u5f53\u524d\u70b9\u4e3a\u4e2d\u5fc3\u7684\u7530\u5b57\u6846,\u9ed1\u70b9\u4e2a\u6570\n    :param x:\n    :param y:\n    :return:\n    \"\"\"\n    filename =  '.\/out_img\/' + img_name.split('.')[0] + '-interferencePoint.jpg'\n    # todo \u5224\u65ad\u56fe\u7247\u7684\u957f\u5bbd\u5ea6\u4e0b\u9650\n    cur_pixel = img[x,y]# \u5f53\u524d\u50cf\u7d20\u70b9\u7684\u503c\n    height,width = img.shape[:2]\n\n    for y in range(0, width - 1):\n      for x in range(0, height - 1):\n        if y == 0:  # \u7b2c\u4e00\u884c\n            if x == 0:  # \u5de6\u4e0a\u9876\u70b9,4\u90bb\u57df\n                # \u4e2d\u5fc3\u70b9\u65c1\u8fb93\u4e2a\u70b9\n                sum = int(cur_pixel) \\\n                      + int(img[x, y + 1]) \\\n                      + int(img[x + 1, y]) \\\n                      + int(img[x + 1, y + 1])\n                if sum <= 2 * 245:\n                  img[x, y] = 0\n            elif x == height - 1:  # \u53f3\u4e0a\u9876\u70b9\n                sum = int(cur_pixel) \\\n                      + int(img[x, y + 1]) \\\n                      + int(img[x - 1, y]) \\\n                      + int(img[x - 1, y + 1])\n                if sum <= 2 * 245:\n                  img[x, y] = 0\n            else:  # \u6700\u4e0a\u975e\u9876\u70b9,6\u90bb\u57df\n                sum = int(img[x - 1, y]) \\\n                      + int(img[x - 1, y + 1]) \\\n                      + int(cur_pixel) \\\n                      + int(img[x, y + 1]) \\\n                      + int(img[x + 1, y]) \\\n                      + int(img[x + 1, y + 1])\n                if sum <= 3 * 245:\n                  img[x, y] = 0\n        elif y == width - 1:  # \u6700\u4e0b\u9762\u4e00\u884c\n            if x == 0:  # \u5de6\u4e0b\u9876\u70b9\n                # \u4e2d\u5fc3\u70b9\u65c1\u8fb93\u4e2a\u70b9\n                sum = int(cur_pixel) \\\n                      + int(img[x + 1, y]) \\\n                      + int(img[x + 1, y - 1]) \\\n                      + int(img[x, y - 1])\n                if sum <= 2 * 245:\n                  img[x, y] = 0\n            elif x == height - 1:  # \u53f3\u4e0b\u9876\u70b9\n                sum = int(cur_pixel) \\\n                      + int(img[x, y - 1]) \\\n                      + int(img[x - 1, y]) \\\n                      + int(img[x - 1, y - 1])\n\n                if sum <= 2 * 245:\n                  img[x, y] = 0\n            else:  # \u6700\u4e0b\u975e\u9876\u70b9,6\u90bb\u57df\n                sum = int(cur_pixel) \\\n                      + int(img[x - 1, y]) \\\n                      + int(img[x + 1, y]) \\\n                      + int(img[x, y - 1]) \\\n                      + int(img[x - 1, y - 1]) \\\n                      + int(img[x + 1, y - 1])\n                if sum <= 3 * 245:\n                  img[x, y] = 0\n        else:  # y\u4e0d\u5728\u8fb9\u754c\n            if x == 0:  # \u5de6\u8fb9\u975e\u9876\u70b9\n                sum = int(img[x, y - 1]) \\\n                      + int(cur_pixel) \\\n                      + int(img[x, y + 1]) \\\n                      + int(img[x + 1, y - 1]) \\\n                      + int(img[x + 1, y]) \\\n                      + int(img[x + 1, y + 1])\n\n                if sum <= 3 * 245:\n                  img[x, y] = 0\n            elif x == height - 1:  # \u53f3\u8fb9\u975e\u9876\u70b9\n                sum = int(img[x, y - 1]) \\\n                      + int(cur_pixel) \\\n                      + int(img[x, y + 1]) \\\n                      + int(img[x - 1, y - 1]) \\\n                      + int(img[x - 1, y]) \\\n                      + int(img[x - 1, y + 1])\n\n                if sum <= 3 * 245:\n                  img[x, y] = 0\n            else:  # \u5177\u59079\u9886\u57df\u6761\u4ef6\u7684\n                sum = int(img[x - 1, y - 1]) \\\n                      + int(img[x - 1, y]) \\\n                      + int(img[x - 1, y + 1]) \\\n                      + int(img[x, y - 1]) \\\n                      + int(cur_pixel) \\\n                      + int(img[x, y + 1]) \\\n                      + int(img[x + 1, y - 1]) \\\n                      + int(img[x + 1, y]) \\\n                      + int(img[x + 1, y + 1])\n                if sum <= 4 * 245:\n                  img[x, y] = 0\n    cv2.imwrite(filename,img)\n    return img<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\"><\/a><\/span><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>\u6548\u679c\uff1a<\/p>\n<img decoding=\"async\" src=\"https:\/\/images2017.cnblogs.com\/blog\/951708\/201711\/951708-20171120154450540-1142507545.png\" alt=\"\" \/>\n<p>&nbsp;<\/p>\n<p>\u5176\u5b9e\u5230\u4e86\u8fd9\u4e00\u6b65\uff0c\u8fd9\u4e9b\u5b57\u7b26\u5c31\u53ef\u4ee5\u8bc6\u522b\u4e86\uff0c\u6ca1\u5fc5\u8981\u8fdb\u884c\u5b57\u7b26\u5207\u5272\u4e86\uff0c\u73b0\u5728\u8fd9\u4e09\u79cd\u7c7b\u578b\u7684\u9a8c\u8bc1\u7801\u8bc6\u522b\u7387\u5df2\u7ecf\u8fbe\u523050%\u4ee5\u4e0a\u4e86<\/p>\n<p>&nbsp;<\/p>\n<h3>\u5b57\u7b26\u5207\u5272<\/h3>\n<p>\u5b57\u7b26\u5207\u5272\u901a\u5e38\u7528\u4e8e\u9a8c\u8bc1\u7801\u4e2d\u6709\u7c98\u8fde\u7684\u5b57\u7b26\uff0c\u7c98\u8fde\u7684\u5b57\u7b26\u4e0d\u597d\u8bc6\u522b\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u5c06\u7c98\u8fde\u7684\u5b57\u7b26\u5207\u5272\u4e3a\u5355\u4e2a\u7684\u5b57\u7b26\uff0c\u5728\u8fdb\u884c\u8bc6\u522b<\/p>\n<p>\u5b57\u7b26\u5207\u5272\u7684\u601d\u8def\u5c31\u662f\u627e\u5230\u4e00\u4e2a\u9ed1\u8272\u7684\u70b9\uff0c\u7136\u540e\u5728\u904d\u5386\u4e0e\u4ed6\u76f8\u90bb\u7684\u9ed1\u8272\u7684\u70b9\uff0c\u76f4\u5230\u904d\u5386\u5b8c\u6240\u6709\u7684\u8fde\u63a5\u8d77\u6765\u7684\u9ed1\u8272\u7684\u70b9\uff0c\u627e\u51fa\u8fd9\u4e9b\u70b9\u4e2d\u7684\u6700\u9ad8\u7684\u70b9\u3001\u6700\u4f4e\u7684\u70b9\u3001\u6700\u53f3\u8fb9\u7684\u70b9\u3001\u6700\u5de6\u8fb9\u7684\u70b9\uff0c\u8bb0\u5f55\u4e0b\u8fd9\u56db\u4e2a\u70b9\uff0c\u8ba4\u4e3a\u8fd9\u662f\u4e00\u4e2a\u5b57\u7b26\uff0c\u7136\u540e\u5728\u5411\u540e\u904d\u5386\u70b9\uff0c\u76f4\u81f3\u627e\u5230\u9ed1\u8272\u7684\u70b9\uff0c\u7ee7\u7eed\u4ee5\u4e0a\u7684\u6b65\u9aa4\u3002\u6700\u540e\u901a\u8fc7\u6bcf\u4e2a\u5b57\u7b26\u7684\u56db\u4e2a\u70b9\u8fdb\u884c\u5207\u5272<\/p>\n<img decoding=\"async\" src=\"https:\/\/images2017.cnblogs.com\/blog\/951708\/201711\/951708-20171120160210040-1561904136.png\" alt=\"\" \/>\n<p>\u56fe\u4e2d\u7ea2\u8272\u7684\u70b9\u5c31\u662f\u4ee3\u7801\u6267\u884c\u5b8c\u540e\uff0c\u6807\u8bc6\u51fa\u7684\u6bcf\u4e2a\u5b57\u7b26\u7684\u56db\u4e2a\u70b9\uff0c\u7136\u540e\u5c31\u4f1a\u6839\u636e\u8fd9\u56db\u4e2a\u70b9\u8fdb\u884c\u5207\u5272\uff08\u56fe\u4e2d\u753b\u7684\u6709\u4e9b\u8bef\u5dee\uff0c\u61c2\u5c31\u597d\uff09<\/p>\n<p>\u4f46\u662f\u4e5f\u53ef\u4ee5\u770b\u5230\uff0cm2\u662f\u7c98\u8fde\u7684\uff0c\u4ee3\u7801\u8ba4\u4e3a\u4ed6\u662f\u4e00\u4e2a\u5b57\u7b26\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u5bf9\u6bcf\u4e2a\u5b57\u7b26\u7684\u5bbd\u5ea6\u8fdb\u884c\u68c0\u6d4b\uff0c\u5982\u679c\u4ed6\u7684\u5bbd\u5ea6\u8fc7\u5bbd\uff0c\u6211\u4eec\u5c31\u8ba4\u4e3a\u4ed6\u662f\u4e24\u4e2a\u7c98\u8fde\u5728\u4e00\u8d77\u7684\u5b57\u7b26\uff0c\u5e76\u5c06\u5b83\u5728\u4ece\u4e2d\u95f4\u5207\u5272<\/p>\n<p>\u786e\u5b9a\u6bcf\u4e2a\u5b57\u7b26\u7684\u56db\u4e2a\u70b9\u4ee3\u7801\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\"><\/a><\/span><\/div>\n<pre>def cfs(im,x_fd,y_fd):\n  '''\u7528\u961f\u5217\u548c\u96c6\u5408\u8bb0\u5f55\u904d\u5386\u8fc7\u7684\u50cf\u7d20\u5750\u6807\u4ee3\u66ff\u5355\u7eaf\u9012\u5f52\u4ee5\u89e3\u51b3cfs\u8bbf\u95ee\u8fc7\u6df1\u95ee\u9898\n  '''\n\n  # print('**********')\n\n  xaxis=[]\n  yaxis=[]\n  visited =set()\n  q = Queue()\n  q.put((x_fd, y_fd))\n  visited.add((x_fd, y_fd))\n  offsets=[(1, 0), (0, 1), (-1, 0), (0, -1)]#\u56db\u90bb\u57df\n\n  while not q.empty():\n      x,y=q.get()\n\n      for xoffset,yoffset in offsets:\n          x_neighbor,y_neighbor = x+xoffset,y+yoffset\n\n          if (x_neighbor,y_neighbor) in (visited):\n              continue  # \u5df2\u7ecf\u8bbf\u95ee\u8fc7\u4e86\n\n          visited.add((x_neighbor, y_neighbor))\n\n          try:\n              if im[x_neighbor, y_neighbor] == 0:\n                  xaxis.append(x_neighbor)\n                  yaxis.append(y_neighbor)\n                  q.put((x_neighbor,y_neighbor))\n\n          except IndexError:\n              pass\n  # print(xaxis)\n  if (len(xaxis) == 0 | len(yaxis) == 0):\n    xmax = x_fd + 1\n    xmin = x_fd\n    ymax = y_fd + 1\n    ymin = y_fd\n\n  else:\n    xmax = max(xaxis)\n    xmin = min(xaxis)\n    ymax = max(yaxis)\n    ymin = min(yaxis)\n    #ymin,ymax=sort(yaxis)\n\n  return ymax,ymin,xmax,xmin\n\ndef detectFgPix(im,xmax):\n  '''\u641c\u7d22\u533a\u5757\u8d77\u70b9\n  '''\n\n  h,w = im.shape[:2]\n  for y_fd in range(xmax+1,w):\n      for x_fd in range(h):\n          if im[x_fd,y_fd] == 0:\n              return x_fd,y_fd\n\ndef CFS(im):\n  '''\u5207\u5272\u5b57\u7b26\u4f4d\u7f6e\n  '''\n\n  zoneL=[]#\u5404\u533a\u5757\u957f\u5ea6L\u5217\u8868\n  zoneWB=[]#\u5404\u533a\u5757\u7684X\u8f74[\u8d77\u59cb\uff0c\u7ec8\u70b9]\u5217\u8868\n  zoneHB=[]#\u5404\u533a\u5757\u7684Y\u8f74[\u8d77\u59cb\uff0c\u7ec8\u70b9]\u5217\u8868\n\n  xmax=0#\u4e0a\u4e00\u533a\u5757\u7ed3\u675f\u9ed1\u70b9\u6a2a\u5750\u6807,\u8fd9\u91cc\u662f\u521d\u59cb\u5316\n  for i in range(10):\n\n      try:\n          x_fd,y_fd = detectFgPix(im,xmax)\n          # print(y_fd,x_fd)\n          xmax,xmin,ymax,ymin=cfs(im,x_fd,y_fd)\n          L = xmax - xmin\n          H = ymax - ymin\n          zoneL.append(L)\n          zoneWB.append([xmin,xmax])\n          zoneHB.append([ymin,ymax])\n\n      except TypeError:\n          return zoneL,zoneWB,zoneHB\n\n  return zoneL,zoneWB,zoneHB<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\"><\/a><\/span><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>\u5206\u5272\u7c98\u8fde\u5b57\u7b26\u4ee3\u7801\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\"><\/a><\/span><\/div>\n<pre>      # \u5207\u5272\u7684\u4f4d\u7f6e\n      im_position = CFS(im)\n\n      maxL = max(im_position[0])\n      minL = min(im_position[0])\n\n      # \u5982\u679c\u6709\u7c98\u8fde\u5b57\u7b26\uff0c\u5982\u679c\u4e00\u4e2a\u5b57\u7b26\u7684\u957f\u5ea6\u8fc7\u957f\u5c31\u8ba4\u4e3a\u662f\u7c98\u8fde\u5b57\u7b26\uff0c\u5e76\u4ece\u4e2d\u95f4\u8fdb\u884c\u5207\u5272\n      if(maxL > minL + minL * 0.7):\n        maxL_index = im_position[0].index(maxL)\n        minL_index = im_position[0].index(minL)\n        # \u8bbe\u7f6e\u5b57\u7b26\u7684\u5bbd\u5ea6\n        im_position[0][maxL_index] = maxL \/\/ 2\n        im_position[0].insert(maxL_index + 1, maxL \/\/ 2)\n        # \u8bbe\u7f6e\u5b57\u7b26X\u8f74[\u8d77\u59cb\uff0c\u7ec8\u70b9]\u4f4d\u7f6e\n        im_position[1][maxL_index][1] = im_position[1][maxL_index][0] + maxL \/\/ 2\n        im_position[1].insert(maxL_index + 1, [im_position[1][maxL_index][1] + 1, im_position[1][maxL_index][1] + 1 + maxL \/\/ 2])\n        # \u8bbe\u7f6e\u5b57\u7b26\u7684Y\u8f74[\u8d77\u59cb\uff0c\u7ec8\u70b9]\u4f4d\u7f6e\n        im_position[2].insert(maxL_index + 1, im_position[2][maxL_index])\n\n      # \u5207\u5272\u5b57\u7b26\uff0c\u8981\u60f3\u5207\u5f97\u597d\u5c31\u5f97\u914d\u7f6e\u53c2\u6570\uff0c\u901a\u5e38 1 or 2 \u5c31\u53ef\u4ee5\n      cutting_img(im,im_position,img_name,1,1)<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\"><\/a><\/span><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>\u5207\u5272\u7c98\u8fde\u5b57\u7b26\u4ee3\u7801\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\"><\/a><\/span><\/div>\n<pre>def cutting_img(im,im_position,img,xoffset = 1,yoffset = 1):\n  filename =  '.\/out_img\/' + img.split('.')[0]\n  # \u8bc6\u522b\u51fa\u7684\u5b57\u7b26\u4e2a\u6570\n  im_number = len(im_position[1])\n  # \u5207\u5272\u5b57\u7b26\n  for i in range(im_number):\n    im_start_X = im_position[1][i][0] - xoffset\n    im_end_X = im_position[1][i][1] + xoffset\n    im_start_Y = im_position[2][i][0] - yoffset\n    im_end_Y = im_position[2][i][1] + yoffset\n    cropped = im[im_start_Y:im_end_Y, im_start_X:im_end_X]\n    cv2.imwrite(filename + '-cutting-' + str(i) + '.jpg',cropped)<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\"><\/a><\/span><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>\u6548\u679c\uff1a<\/p>\n<img decoding=\"async\" src=\"https:\/\/images2017.cnblogs.com\/blog\/951708\/201711\/951708-20171120161744321-1099534644.png\" alt=\"\" \/>\n<p>&nbsp;<\/p>\n<h3>\u3000\u3000\u8bc6\u522b<\/h3>\n<p>\u8bc6\u522b\u7528\u7684\u662ftypesseract\u5e93\uff0c\u4e3b\u8981\u8bc6\u522b\u4e00\u884c\u5b57\u7b26\u548c\u5355\u4e2a\u5b57\u7b26\u65f6\u7684\u53c2\u6570\u8bbe\u7f6e\uff0c\u8bc6\u522b\u4e2d\u82f1\u6587\u7684\u53c2\u6570\u8bbe\u7f6e\uff0c\u4ee3\u7801\u5f88\u7b80\u5355\u5c31\u4e00\u884c\uff0c\u6211\u8fd9\u91cc\u5927\u591a\u662ffilter\u6587\u4ef6\u7684\u64cd\u4f5c<\/p>\n<p>\u4ee3\u7801\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\"><\/a><\/span><\/div>\n<pre>      # \u8bc6\u522b\u9a8c\u8bc1\u7801\n      cutting_img_num = 0\n      for file in os.listdir('.\/out_img'):\n        str_img = ''\n        if fnmatch(file, '%s-cutting-*.jpg' % img_name.split('.')[0]):\n          cutting_img_num += 1\n      for i in range(cutting_img_num):\n        try:\n          file = '.\/out_img\/%s-cutting-%s.jpg' % (img_name.split('.')[0], i)\n          # \u8bc6\u522b\u5b57\u7b26\n          str_img = str_img + image_to_string(Image.open(file),lang = 'eng', config='-psm 10') #\u5355\u4e2a\u5b57\u7b26\u662f10\uff0c\u4e00\u884c\u6587\u672c\u662f7\n        except Exception as err:\n          pass\n      print('\u5207\u56fe\uff1a%s' % cutting_img_num)\n      print('\u8bc6\u522b\u4e3a\uff1a%s' % str_img)<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\"><\/a><\/span><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>\u6700\u540e\u8fd9\u79cd\u7c98\u8fde\u5b57\u7b26\u7684\u8bc6\u522b\u7387\u662f\u572830%\u5de6\u53f3\uff0c\u800c\u4e14\u8fd9\u79cd\u53ea\u662f\u5904\u7406\u4e24\u4e2a\u5b57\u7b26\u7c98\u8fde\uff0c\u5982\u679c\u6709\u4e24\u4e2a\u4ee5\u4e0a\u7684\u5b57\u7b26\u7c98\u8fde\u8fd8\u4e0d\u80fd\u8bc6\u522b\uff0c\u4f46\u662f\u6839\u636e\u5b57\u7b26\u5bbd\u5ea6\u5224\u522b\u7684\u8bdd\u4e5f\u4e0d\u96be\uff0c\u6709\u5174\u8da3\u7684\u53ef\u4ee5\u8bd5\u4e00\u4e0b<\/p>\n<p>\u65e0\u9700\u5207\u5272\u5b57\u7b26\u8bc6\u522b\u7684\u6548\u679c\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2017.cnblogs.com\/blog\/951708\/201711\/951708-20171120162844493-1942982171.png\" alt=\"\" \/><img decoding=\"async\" src=\"https:\/\/images2017.cnblogs.com\/blog\/951708\/201711\/951708-20171120162856133-1401872893.png\" alt=\"\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>\u9700\u8981\u5207\u5272\u5b57\u7b26\u7684\u8bc6\u522b\u6548\u679c\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/images2017.cnblogs.com\/blog\/951708\/201711\/951708-20171120162922102-1512482614.png\" alt=\"\" \/><img decoding=\"async\" src=\"https:\/\/images2017.cnblogs.com\/blog\/951708\/201711\/951708-20171120162928680-1009339990.png\" alt=\"\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u8fd9\u79cd\u53ea\u662f\u80fd\u591f\u8bc6\u522b\u7b80\u5355\u9a8c\u8bc1\u7801\uff0c\u590d\u6742\u7684\u9a8c\u8bc1\u7801\u8fd8\u8981\u9760\u5927\u5bb6\u4e86<\/p>\n<p>\u53c2\u8003\u8d44\u6599\uff1a<\/p>\n<p>1\u3001<a href=\"http:\/\/www.jianshu.com\/p\/41127bf90ca9\">http:\/\/www.jianshu.com\/p\/41127bf90ca9<\/a><\/p>\n<p>\u672c\u6765\u53c2\u8003\u4e86\u633a\u591a\u7684\u8d44\u6599\uff0c\u4f46\u662f\u65f6\u95f4\u957f\u4e86\u5c31\u627e\u4e0d\u5230\u4e86\uff0c\u5982\u679c\u6709\u4eba\u53d1\u73b0\u4e86\uff0c\u53ef\u4ee5\u544a\u8bc9\u6211\uff0c\u6211\u518d\u6dfb\u52a0<\/p>\n<p>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<p>&nbsp;<\/p>\n<p>1\u3001\u5c06\u8981\u8bc6\u522b\u7684\u9a8c\u8bc1\u7801\u56fe\u7247\u653e\u5165\u4e0e\u811a\u672c\u540c\u7ea7\u7684img\u6587\u4ef6\u5939\u4e2d\uff0c\u521b\u5efaout_img\u6587\u4ef6\u5939\n2\u3001python3 filename\n3\u3001\u4e8c\u503c\u5316\u3001\u964d\u566a\u7b49\u5404\u4e2a\u9636\u6bb5\u7684\u56fe\u7247\u5c06\u5b58\u50a8\u5728out_img\u6587\u4ef6\u5939\u4e2d\uff0c\u6700\u7ec8\u8bc6\u522b\u7ed3\u679c\u4f1a\u6253\u5370\u5230\u5c4f\u5e55\u4e0a<\/p>\n<p>&nbsp;<\/p>\n<p>\u6700\u540e\u9644\u4e0a\u6e90\u7801\uff08\u5e26\u5207\u5272\uff0c\u4e0d\u60f3\u8981\u5207\u5272\u7684\u5c31\u81ea\u5df1\u4fee\u6539\u5427\uff09\uff1a<\/p>\n<div class=\"cnblogs_code\"><img decoding=\"async\" id=\"code_img_closed_739b0c9f-48c5-44e3-8af6-edbcdad81cff\" class=\"code_img_closed\" src=\"https:\/\/images.cnblogs.com\/OutliningIndicators\/ContractedBlock.gif\" alt=\"\">\u00a0<span class=\"cnblogs_code_collapse\">View Code<\/span><\/div>\n<p>&nbsp;<\/p>","protected":false},"excerpt":{"rendered":"<p>\u5927\u81f4\u4ecb\u7ecd \u5728python\u722c\u866b\u722c\u53d6\u67d0\u4e9b\u7f51\u7ad9\u7684\u9a8c\u8bc1\u7801\u7684\u65f6\u5019\u53ef\u80fd\u4f1a\u9047\u5230\u9a8c\u8bc1\u7801\u8bc6\u522b\u7684\u95ee\u9898\uff0c\u73b0\u5728\u7684\u9a8c\u8bc1\u7801\u5927\u591a\u5206\u4e3a\u56db\u7c7b\uff1a 1\u3001\u8ba1\u7b97\u9a8c\u8bc1\u7801 2\u3001\u6ed1\u5757\u9a8c\u8bc1\u7801 3\u3001\u8bc6\u56fe\u9a8c\u8bc1\u7801 4\u3001\u8bed\u97f3\u9a8c\u8bc1\u7801 \u8fd9\u7bc7\u535a\u5ba2\u4e3b\u8981\u5199\u7684\u5c31\u662f\u8bc6\u56fe\u9a8c&#46;&#46;&#46;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[495],"class_list":["post-1757","post","type-post","status-publish","format-standard","hentry","category-linux","tag-495"],"_links":{"self":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/1757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/comments?post=1757"}],"version-history":[{"count":1,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/1757\/revisions"}],"predecessor-version":[{"id":1758,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/1757\/revisions\/1758"}],"wp:attachment":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/media?parent=1757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/categories?post=1757"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/tags?post=1757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}