• <tfoot id='fZwpT'></tfoot>

      • <bdo id='fZwpT'></bdo><ul id='fZwpT'></ul>
      <legend id='fZwpT'><style id='fZwpT'><dir id='fZwpT'><q id='fZwpT'></q></dir></style></legend>
    1. <small id='fZwpT'></small><noframes id='fZwpT'>

      <i id='fZwpT'><tr id='fZwpT'><dt id='fZwpT'><q id='fZwpT'><span id='fZwpT'><b id='fZwpT'><form id='fZwpT'><ins id='fZwpT'></ins><ul id='fZwpT'></ul><sub id='fZwpT'></sub></form><legend id='fZwpT'></legend><bdo id='fZwpT'><pre id='fZwpT'><center id='fZwpT'></center></pre></bdo></b><th id='fZwpT'></th></span></q></dt></tr></i><div id='fZwpT'><tfoot id='fZwpT'></tfoot><dl id='fZwpT'><fieldset id='fZwpT'></fieldset></dl></div>
      1. Python+opencv 实现图片文字的分割的方法示例

        时间:2023-12-16

      2. <tfoot id='doxLB'></tfoot>
        <i id='doxLB'><tr id='doxLB'><dt id='doxLB'><q id='doxLB'><span id='doxLB'><b id='doxLB'><form id='doxLB'><ins id='doxLB'></ins><ul id='doxLB'></ul><sub id='doxLB'></sub></form><legend id='doxLB'></legend><bdo id='doxLB'><pre id='doxLB'><center id='doxLB'></center></pre></bdo></b><th id='doxLB'></th></span></q></dt></tr></i><div id='doxLB'><tfoot id='doxLB'></tfoot><dl id='doxLB'><fieldset id='doxLB'></fieldset></dl></div>
        <legend id='doxLB'><style id='doxLB'><dir id='doxLB'><q id='doxLB'></q></dir></style></legend>

            <tbody id='doxLB'></tbody>
                • <bdo id='doxLB'></bdo><ul id='doxLB'></ul>

                  <small id='doxLB'></small><noframes id='doxLB'>

                  1. 导入必要的库

                  在使用Python+opencv实现图片文字的分割之前,首先要导入必要的库。通常需要使用的库包括cv2numpyPILmatplotlib,其中cv2为opencv对Python的接口。

                  import cv2
                  import numpy as np
                  from PIL import Image
                  import matplotlib.pyplot as plt
                  
                  1. 读取图片并进行灰度处理

                  使用opencv读取图片,将其转为灰度图像。

                  img = cv2.imread('test.png')
                  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
                  
                  1. 图像二值化

                  为了能够更好的从图像中分离文字,通常需要二值化图像。可以使用大津算法(Otsu)来自动找到最佳的阈值。

                  ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
                  
                  1. 进行形态学操作

                  使用形态学操作来进行图像处理,进一步分离文字。通常使用开运算和闭运算。

                  kernel = np.ones((3, 3), np.uint8)
                  opening = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=2)
                  closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel, iterations=2)
                  
                  1. 查找轮廓并剪切图像

                  查找轮廓,并根据轮廓信息,剪切分离出来的文字。

                  contours, hierarchy = cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
                  for i, contour in enumerate(contours):
                      x, y, w, h = cv2.boundingRect(contour)
                      if w < 10 or h < 10:
                          continue
                      cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
                      crop_img = img[y:y+h, x:x+w]
                      cv2.imwrite('crop' + str(i) + '.png', crop_img)
                  

                  通过以上的步骤,就可以实现将图片中的文字分割出来,并保存到单独的图像文件中。

                  示例1:

                  图像地址:https://cdn.pixabay.com/photo/2018/01/12/10/19/ford-3089868_960_720.jpg

                  img = cv2.imread('ford-3089868_960_720.jpg', cv2.IMREAD_COLOR)
                  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
                  ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
                  kernel = np.ones((3, 3), np.uint8)
                  opening = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=2)
                  closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel, iterations=2)
                  contours, hierarchy = cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
                  for i, contour in enumerate(contours):
                      x, y, w, h = cv2.boundingRect(contour)
                      if w < 10 or h < 10:
                          continue
                      cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
                      crop_img = img[y:y+h, x:x+w]
                      cv2.imwrite('crop' + str(i) + '.png', crop_img)
                  plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
                  plt.show()
                  

                  示例2:

                  图像地址:https://cdn.pixabay.com/photo/2015/02/18/12/38/wood-640828_960_720.jpg

                  img = cv2.imread('wood-640828_960_720.jpg', cv2.IMREAD_COLOR)
                  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
                  ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
                  kernel = np.ones((3, 3), np.uint8)
                  opening = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=2)
                  closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel, iterations=2)
                  contours, hierarchy = cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
                  for i, contour in enumerate(contours):
                      x, y, w, h = cv2.boundingRect(contour)
                      if w < 10 or h < 10:
                          continue
                      cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
                      crop_img = img[y:y+h, x:x+w]
                      cv2.imwrite('crop' + str(i) + '.png', crop_img)
                  plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
                  plt.show()
                  

                  通过以上两个示例,可以看到通过Python+opencv实现图片文字的分割,可以在图片中准确的分离出每个字,并保存为单独的图像文件。

                  上一篇:Python实现PDF文字识别提取并写入CSV文件 下一篇:python使用Thread的setDaemon启动后台线程教程

                  相关文章

                    <bdo id='lIP3U'></bdo><ul id='lIP3U'></ul>

                    1. <tfoot id='lIP3U'></tfoot>
                      <i id='lIP3U'><tr id='lIP3U'><dt id='lIP3U'><q id='lIP3U'><span id='lIP3U'><b id='lIP3U'><form id='lIP3U'><ins id='lIP3U'></ins><ul id='lIP3U'></ul><sub id='lIP3U'></sub></form><legend id='lIP3U'></legend><bdo id='lIP3U'><pre id='lIP3U'><center id='lIP3U'></center></pre></bdo></b><th id='lIP3U'></th></span></q></dt></tr></i><div id='lIP3U'><tfoot id='lIP3U'></tfoot><dl id='lIP3U'><fieldset id='lIP3U'></fieldset></dl></div>

                      <small id='lIP3U'></small><noframes id='lIP3U'>

                      <legend id='lIP3U'><style id='lIP3U'><dir id='lIP3U'><q id='lIP3U'></q></dir></style></legend>