文字檢測預處理地址

JimmyChoo發表於2018-10-10
#專案參考地址: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()




相關文章