1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| import cv2 import numpy as np import time import random
def detCut(imgPath,savePath="./result",show = False):
# 将透明背景图片转换为白色背景 start_time = time.time() image = cv2.imread(imgPath,-1) if(len(image[0,0]) >= 4): image[image[:,:,3]==0] = [255,255,255,255] end_time = time.time() elapsed_time = end_time - start_time print(f"透明背景转白色耗时:{elapsed_time:.3f}秒")
if(show): cv2.imshow("白底黑字", image) cv2.waitKey(0) # cv2.imwrite("白底黑字.png",image)
# 灰度 start_time = time.time() gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #灰度 #thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 21, 25) end_time = time.time() elapsed_time = end_time - start_time print(f"灰度耗时为:{elapsed_time:.3f}秒")
# 二值化 start_time = time.time() ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV) end_time = time.time() elapsed_time = end_time - start_time print(f"二值化耗时为:{elapsed_time:.3f}秒")
if(show): cv2.imshow("二值化", binary) cv2.waitKey(0) # cv2.imwrite("二值化.png",binary)
# 腐蚀核 去除边缘毛刺 # erode_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2)) # # 腐蚀 去除噪点 # erosion = cv2.erode(binary, erode_kernel, iterations=1)
# 开运算,先腐蚀后膨胀 # re_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (4, 4)) # opening = cv2.morphologyEx(binary, cv2.MORPH_OPEN, re_kernel)
# cv2.imshow("erosion", erosion) # cv2.waitKey(0) # cv2.imwrite("erosion.png",erosion)
start_time = time.time() # 膨胀核 dilation_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1)) # 膨胀 dilation = cv2.dilate(binary, dilation_kernel, iterations = 1) end_time = time.time() elapsed_time = end_time - start_time print(f"膨胀耗时为:{elapsed_time:.3f}秒")
if(show): cv2.imshow("膨胀.png",binary) cv2.waitKey(0)
# 文本检测 start_time = time.time() contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) end_time = time.time() elapsed_time = end_time - start_time print(f"轮廓耗时为:{elapsed_time:.2f}秒")
# 绘图 im2 = image.copy() color = [(0, 0, 255), (0, 255, 0), (255, 0, 0),(0,0,0)] # 红 绿 蓝 黑 # 右下坐标系 # rect = cv2.rectangle(im2, (155, 277), (155 + 339, 277 + 10),(255, 0, 0), 2) a =0 fileName = imgPath.split("\\")[-1].split(".")[0] for cnt in contours: x, y, w, h = cv2.boundingRect(cnt) # print("x={},y={},w={},h={}".format(x,y,w,h)) rect = cv2.rectangle(im2, (x, y), (x + w, y + h), color[a], 2) a= a+1 #截取 crop_img = dilation[y:y+h, x:x+w] originW = crop_img.shape[1] originH = crop_img.shape[0] # print("originW={},originH={}".format(originW,originH)) #如果高度大于28*2,就再次膨胀 # if originH > 28*2: # diameter = int(originH/28) # print("再次膨胀 {}".format(diameter)) # dilation_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (diameter, diameter)) # # 膨胀 # crop_img = cv2.dilate(crop_img, dilation_kernel, iterations = 1) #变为白底黑字 crop_img = cv2.bitwise_not(crop_img) #保存 #cv2.imwrite(str(a)+".png",crop_img) #改变大小 newH = 24 newW = int(originW * newH / originH) crop_img = cv2.resize(crop_img, (newW, newH)) #保存 #cv2.imwrite(str(a)+"_resize.png",crop_img) #补充白边 crop_img =cv2.copyMakeBorder(crop_img, 2, 2, 4, 4, cv2.BORDER_CONSTANT, value=[255,255,255]) #保存 #cv2.imwrite(str(a)+"_white.png",crop_img)
# 查看文件夹是否存在 if not os.path.exists(savePath): os.makedirs(savePath) #outName = imgPath.split("\\")[-1].split(".")[0] + "_result.jpg" outName = fileName + "_" +str(a) +".jpg" print("输出: {}".format(os.path.join(savePath,outName))) cv2.imwrite(os.path.join(savePath,outName),crop_img) if(show): cv2.imshow("结果.png",rect) cv2.waitKey(0) import os # 将指定文件夹中图片全部转化 def changeImgFolder(ImgFolderPath,outPath): for root, dirs, files in os.walk(ImgFolderPath): for file in files: if file.endswith(".png"): #print(os.path.join(root, file)) detCut(os.path.join(root, file),outPath)
|