文本检测预处理地址

#项目参考地址:https://blog.csdn.net/lipc_/article/details/80812258
#1、固定目标区域的提取,使用crop函数提取固定图像区域部分。
from PIL import Image
 # 第一部分
im = Image.open('C:/Users/Administrator/Desktop/1.jpg')
img_size = im.size
print("图片宽度和高度分别是{}".format(img_size))
left = 190
upper = 90
right = 510
lower = 130
region = im.crop((left,upper,right,lower))
region.save("C:/Users/Administrator/Desktop/test/Crop_test1.jpg")
# 第二部分
img_size = im.size
print("图片宽度和高度分别是{}".format(img_size))
left = 35
upper = 130
right = 550
lower = 175
region = im.crop((left,upper,right,lower))
region.save("C:/Users/Administrator/Desktop/test/Crop_test2.jpg")
# 第三部分
img_size = im.size
print("图片宽度和高度分别是{}".format(img_size))
left = 35
upper = 172
right = 600
lower = 255
region = im.crop((left,upper,right,lower))
region.save("C:/Users/Administrator/Desktop/test/Crop_test3.jpg")
# 第四部分
img_size = im.size
print("图片宽度和高度分别是{}".format(img_size))
left = 175
upper = 255
right = 451
lower = 305
region = im.crop((left,upper,right,lower))
region.save("C:/Users/Administrator/Desktop/test/Crop_test4.jpg")
# 第五部分
img_size = im.size
print("图片宽度和高度分别是{}".format(img_size))
left = 175
upper = 310
right = 451
lower = 355
region = im.crop((left,upper,right,lower))
region.save("C:/Users/Administrator/Desktop/test/Crop_test5.jpg")
# 第六部分
img_size = im.size
print("图片宽度和高度分别是{}".format(img_size))
left = 175
upper = 355
right = 451
lower = 410
region = im.crop((left,upper,right,lower))
region.save("C:/Users/Administrator/Desktop/test/Crop_test6.jpg")
# 第七部分
img_size = im.size
print("图片宽度和高度分别是{}".format(img_size))
left = 35
upper = 420
right = 455
lower = 455
region = im.crop((left,upper,right,lower))
region.save("C:/Users/Administrator/Desktop/test/Crop_test7.jpg")

#2、对提取的中文文本区域进行灰化和二值化处理得到文字使其变细并结构清晰

import cv2 as cv
import numpy as np

# 求出图像均值作为阈值来二值化
def custom_image(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    cv.imshow("原来", gray)
    h, w = gray.shape[:2]
    m = np.reshape(gray, [1, w * h])  # 化为一维数组
    mean = m.sum() / (w * h)
    print("mean: ", mean)
    ret, binary = cv.threshold(gray, mean, 170, cv.THRESH_BINARY)
    cv.imshow("二值", binary)

def local_threshold(image):
    gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)  #把输入图像灰度化
    #自适应阈值化能够根据图像不同区域亮度分布,改变阈值
    binary =  cv.adaptiveThreshold(gray, 170, cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY, 25, 10)
    cv.namedWindow("binary1", cv.WINDOW_NORMAL)
    cv.imshow("binary1", binary)

src = cv.imread("C:/Users/Administrator/Desktop/1.jpg")
custom_image(src)
cv.waitKey(0)
cv.destroyAllWindows()

#3、使用不同的二值化方法和过滤算法去除背景噪音达到降噪
import cv2 as cv
import numpy as np

#全局阈值
#def threshold_demo(image):
    #gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)  #把输入图像灰度化
    #直接阈值化是对输入的单通道矩阵逐像素进行阈值分割。
   # ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_TRIANGLE)
   #print("threshold value %s"%ret)
   #cv.imshow("binary0", binary)

#局部阈值
def local_threshold(image):
    gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)  #把输入图像灰度化
    #自适应阈值化能够根据图像不同区域亮度分布,改变阈值
    binary =  cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY, 25, 10)
    cv.namedWindow("binary1", cv.WINDOW_NORMAL)
    cv.imshow("binary1", binary)

#用户自己计算阈值
def custom_threshold(image):
    gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)  #把输入图像灰度化
    h, w =gray.shape[:2]
    m = np.reshape(gray, [1,w*h])
    mean = m.sum()/(w*h)
    print("mean:",mean)
    ret, binary =  cv.threshold(gray, mean, 255, cv.THRESH_BINARY)
    cv.namedWindow("binary2", cv.WINDOW_NORMAL)
    cv.imshow("binary2", binary)

src = cv.imread('C:/Users/Administrator/Desktop/1.jpg')
cv.namedWindow('input_image', cv.WINDOW_NORMAL) #设置为WINDOW_NORMAL可以任意缩放
cv.imshow('input_image', src)
threshold_demo(src)
local_threshold(src)
custom_threshold(src)
cv.waitKey(0)
cv.destroyAllWindows()

#4、使用不同的降噪方法除去干扰并对比去噪效果
#边缘保留滤波(EPF)  高斯双边、均值迁移
import cv2 as cv
import numpy as np

def bi_demo(image):   #双边滤波
    dst = cv.bilateralFilter(image, 0, 100, 15)
    cv.namedWindow("bi_demo", cv.WINDOW_NORMAL)
    cv.imshow("bi_demo", dst)

def shift_demo(image):   #均值迁移
    dst = cv.pyrMeanShiftFiltering(image, 10, 50)
    cv.namedWindow("shift_demo", cv.WINDOW_NORMAL)
    cv.imshow("shift_demo", dst)

src = cv.imread('C:/Users/Administrator/Desktop/1.jpg')
cv.namedWindow('input_image', cv.WINDOW_NORMAL)
cv.imshow('input_image', src)

bi_demo(src)
shift_demo(src)

cv.waitKey(0)
cv.destroyAllWindows()
#5、图像的开闭运算腐蚀和膨胀处理
# 腐蚀和膨胀
import cv2
import numpy as np

# 定义了一个5×5的十字形结构元素,
# 用结构元素与其覆盖的二值图像做“与”操作
# 如果都为1,结果图像的该像素为1。否则为0
# 腐蚀处理的结果是使原来的二值图像减小一圈。
# 00100
# 00100
# 11111
# 00100
# 00100
kernel = np.uint8(np.zeros((5, 5)))
for x in range(5):
    kernel[x, 2] = 1;
    kernel[2, x] = 1;
# 读入图片
img = cv2.imread('C:/Users/Administrator/Desktop/test/5.png', 0)
# 腐蚀图像
eroded = cv2.erode(img, kernel);
# 膨胀图像
dilated = cv2.dilate(img, kernel)

# 将两幅图像相减获得边,第一个参数是膨胀后的图像,第二个参数是腐蚀后的图像
result = cv2.absdiff(dilated, eroded);
# 取反
x = 0;
y = 0;
width = result.shape[0]
height = result.shape[1]
while x < width:
    y = 0
    while y < height:
        result[x][y] = 255 - result[x][y]
        y = y + 1;
    x = x + 1
cv2.imwrite("./eroded.jpg", eroded)
cv2.imwrite("./dilated.jpg", dilated)
cv2.imwrite("./result.jpg", result)

cv2.waitKey(0)
cv2.destroyAllWindows()
#6、图像的锐化的处理使其结构清晰文本线完整
import cv2
import numpy as np

image = cv2.imread('C:/Users/Administrator/Desktop/test/5.png')

kernel_sharpen_1 = np.array([
		[-1,-1,-1],
		[-1,9,-1],
		[-1,-1,-1]])
kernel_sharpen_2 = np.array([
		[1,1,1],
		[1,-7,1],
		[1,1,1]])
kernel_sharpen_3 = np.array([
		[-1,-1,-1,-1,-1],
		[-1,2,2,2,-1],
		[-1,2,8,2,-1],
		[-1,2,2,2,-1],
		[-1,-1,-1,-1,-1]])/8.0
#第一步加载图像,第二步自定义卷积核,第三步卷积,第四步显示锐化结果
output_1 = cv2.filter2D(image,-1,kernel_sharpen_1)
output_2 = cv2.filter2D(image,-1,kernel_sharpen_2)
output_3 = cv2.filter2D(image,-1,kernel_sharpen_3)

cv2.imshow('Original Image',iamge)
cv2.imshow('sharpen_1 Image',output_1)
cv2.imshow('sharpen_2 Image',output_2)
cv2.imshow('sharpen_3 Image',output_3)

if cv2.waitKey(0)& 0xFF  == 27:
	cv2.destroyAllWindows()
	
#7、用keras建立简单的汉子识别模型
项目地址:https://blog.csdn.net/codebay118/article/details/72630091
			https://blog.csdn.net/u010379996/article/details/80797942

#8、文本素材的切割并保存图片
import cv2
import cv numpy as np
import matplotlib.pyplot as plt

def median_split_ranges(peek_ranges):
	new_peek_ranges = []
	width = []
	for peek_range in peek_ranges:
	w = peek_range[1] - peek_range[0]+1
	widths.append(w)
	width = np.asarray(widthes)
	median_w = np.median(widthes)
	for i,peek_range in enumerate(peek_ranges):
		num_char = int(round(widthes[i]/median_w,0))
		if num_char > 1:
			char_w = float(widthes[i]/num_char)
			for i in range(num_char):
				start_point = peek_range[0]+int(i*char_w)
				end_point = peek_range[0]+int(i+1)*char_w)
				new_peek_ranges.append((start_point,end_point))
			else:
				new_peek_ranges.append(peek_range)
	return new_peek_ranges
	
def extract_peek_ranges_from_array(array_vals,minimun_val=10,minimun_range=2):
	start_i = None
	end_i = None
	peek_ranges = []
	for i,val in enumerate(array_vals):
	if val > minimun_val and start_i is None:
		start_i = i
		elif val >minimun_val and start_i is not None:
			pass
		elif val<minimun_val and start_i is not None:
			end_i = i
			if end_i - start_i >= minimun_range:
				peek_ranges.append((start_i,end_i))
			start_i = None
			end_i = None
		elif val< minimun_val and start_i is None:
			pass
		else:
			raise ValueError("cannot parse this case ...")
		return peek_ranges
	
	def get_font_face_peek_ranges(path_test_image):
		imag _color = cv2.imread(path_test_image)
		new_shape = ()
		
	
			
#@
from PIL import Image
# 1
im = Image.open('C:/Users/Administrator/Desktop/1.jpg')
img_size = im.size
print("图片宽度和高度分别是{}".format(img_size))
left = 190
upper = 90
right = 510
lower = 130
region = im.crop((left,upper,right,lower))
region.save("C:/Users/Administrator/Desktop/test/Crop_test1.jpg")
# 2
img_size = im.size
print("图片宽度和高度分别是{}".format(img_size))
left = 35
upper = 130
right = 550
lower = 175
region = im.crop((left,upper,right,lower))
region.save("C:/Users/Administrator/Desktop/test/Crop_test2.jpg")
# 3
img_size = im.size
print("图片宽度和高度分别是{}".format(img_size))
left = 35
upper = 172
right = 600
lower = 255
region = im.crop((left,upper,right,lower))
region.save("C:/Users/Administrator/Desktop/test/Crop_test3.jpg")
# 4
img_size = im.size
print("图片宽度和高度分别是{}".format(img_size))
left = 175
upper = 255
right = 451
lower = 305
region = im.crop((left,upper,right,lower))
region.save("C:/Users/Administrator/Desktop/test/Crop_test4.jpg")
# 5
img_size = im.size
print("图片宽度和高度分别是{}".format(img_size))
left = 175
upper = 310
right = 451
lower = 355
region = im.crop((left,upper,right,lower))
region.save("C:/Users/Administrator/Desktop/test/Crop_test5.jpg")
# 6
img_size = im.size
print("图片宽度和高度分别是{}".format(img_size))
left = 175
upper = 355
right = 451
lower = 410
region = im.crop((left,upper,right,lower))
region.save("C:/Users/Administrator/Desktop/test/Crop_test6.jpg")
# 7
img_size = im.size
print("图片宽度和高度分别是{}".format(img_size))
left = 35
upper = 420
right = 455
lower = 455
region = im.crop((left,upper,right,lower))
region.save("C:/Users/Administrator/Desktop/test/Crop_test7.jpg")
#@
import cv2 as cv
import numpy as np


# 求出图像均值作为阈值来二值化
def custom_image(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    cv.imshow("原来", gray)
    h, w = gray.shape[:2]
    m = np.reshape(gray, [1, w * h])  # 化为一维数组
    mean = m.sum() / (w * h)
    print("mean: ", mean)
    ret, binary = cv.threshold(gray, mean, 170, cv.THRESH_BINARY)
    cv.imshow("二值", binary)

def local_threshold(image):
    gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)  #把输入图像灰度化
    #自适应阈值化能够根据图像不同区域亮度分布,改变阈值
    binary =  cv.adaptiveThreshold(gray, 170, cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY, 25, 10)
    cv.namedWindow("binary1", cv.WINDOW_NORMAL)
    cv.imshow("binary1", binary)

src = cv.imread("C:/Users/Administrator/Desktop/1.jpg")
custom_image(src)
cv.waitKey(0)
cv.destroyAllWindows()

import cv2 as cv
import numpy as np

#全局阈值
#def threshold_demo(image):
    #gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)  #把输入图像灰度化
    #直接阈值化是对输入的单通道矩阵逐像素进行阈值分割。
   # ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_TRIANGLE)
   #print("threshold value %s"%ret)
   #cv.imshow("binary0", binary)

#局部阈值
def local_threshold(image):
    gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)  #把输入图像灰度化
    #自适应阈值化能够根据图像不同区域亮度分布,改变阈值
    binary =  cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY, 25, 10)
    cv.namedWindow("binary1", cv.WINDOW_NORMAL)
    cv.imshow("binary1", binary)

#用户自己计算阈值
def custom_threshold(image):
    gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)  #把输入图像灰度化
    h, w =gray.shape[:2]
    m = np.reshape(gray, [1,w*h])
    mean = m.sum()/(w*h)
    print("mean:",mean)
    ret, binary =  cv.threshold(gray, mean, 255, cv.THRESH_BINARY)
    cv.namedWindow("binary2", cv.WINDOW_NORMAL)
    cv.imshow("binary2", binary)

src = cv.imread('C:/Users/Administrator/Desktop/1.jpg')
cv.namedWindow('input_image', cv.WINDOW_NORMAL) #设置为WINDOW_NORMAL可以任意缩放
cv.imshow('input_image', src)
threshold_demo(src)
#local_threshold(src)
custom_threshold(src)
cv.waitKey(0)
cv.destroyAllWindows()




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值