# coding:utf8
import cv2
import cv2 as cv
import numpy as np
from PIL import Image
from numpy import fft
from pylab import *
from aip import AipOcr
save_path = "change.png"
# 图片写入文字
def Drawworld(img, text, p_x, p_y, font_type, font_size, bold, color):
pos = (p_x, p_y)
cv.putText(img, text, pos, font_type, font_size, color, bold)
cv.imwrite(save_path, img)
# 空间转换
def color_space(img, c_type):
if c_type == 1:
out = cv.cvtColor(img, cv.COLOR_BGR2HSV)
elif c_type == 2:
out = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
elif c_type == 3:
out = cv.cvtColor(img, cv.COLOR_BGR2BGRA)
elif c_type == 4:
out = cv.cvtColor(img, cv.COLOR_BGR2HLS)
elif c_type == 5:
out = cv.cvtColor(img, cv.COLOR_BGR2YUV)
cv.imwrite(save_path, out)
return out
# 图像旋转
def rotate(image, angle):
(h, w) = image.shape[:2]
center = (w / 2, h / 2)
Roa = cv.getRotationMatrix2D(center, angle, 1.0)
out = cv.warpAffine(image, Roa, (w, h))
cv.imwrite(save_path, out)
return out
# 图像缩放
def changescale(image, size):
b = image[:, :, 0]
g = image[:, :, 1]
r = image[:, :, 2]
b2 = cv.resize(b, (0, 0), fx=size, fy=size, interpolation=cv.INTER_NEAREST)
g2 = cv.resize(g, (0, 0), fx=size, fy=size, interpolation=cv.INTER_NEAREST)
r2 = cv.resize(r, (0, 0), fx=size, fy=size, interpolation=cv.INTER_NEAREST)
out = cv.merge([b2, g2, r2])
cv.imwrite(save_path, out)
return out
# 图像翻转
# def myflip(image , direction):
# out = cv.flip(image, direction)
# cv.imwrite(save_path, out)
# return out
# 图像投影矫正
def correct(image):
pts1 = np.float32([[158, 25], [267, 136], [58, 66], [144, 212]])
# 变换后分别在左上、右上、左下、右下四个点
pts2 = np.float32([[0, 0], [320, 0], [0, 200], [320, 200]])
# 生成透视变换矩阵
M = cv.getPerspectiveTransform(pts1, pts2)
# 进行透视变换
dst = cv.warpPerspective(image, M, (320, 200))
out = dst
cv.imwrite(save_path, out)
return out
def horizontal_flip(I):
I=cv2.flip(I,1)
return I
def vertical_flip(I):
I=cv2.flip(I,0)
return I
def rotate_clockwise(I):
dst=I
for _ in range(3):
dst = cv2.flip(dst, 1) # 原型:cv2.flip(src, flipCode[, dst]) → dst flipCode表示对称轴 0:x轴 1:y轴. -1:both
dst = cv2.transpose(dst)
return dst
def rotate_anticlockwise(I):
dst = cv2.flip(I, 1) # 原型:cv2.flip(src, flipCode[, dst]) → dst flipCode表示对称轴 0:x轴 1:y轴. -1:both
dst = cv2.transpose(dst)
return dst
#二值化
def erzhihua(I):
import cv2
import numpy as np
from PIL import Image
from PIL import ImageEnhance
from scipy.ndimage import gaussian_filter
def getImageVar(img):
img2gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imageVar = cv2.Laplacian(img2gray, cv2.CV_64F).var()
return imageVar
def gauss_division(image):
src1 = image.astype(np.float32)
gauss = gaussian_filter(image, sigma=101)
gauss1 = gauss.astype(np.float32)
dst1 = (src1 / gauss1)
return dst1
def image_enhancement(image):
'''图像增强,增强对比度和亮度
image PIL'''
# 对比度增强
im = Image.fromarray(np.uint8(image))
enh_con = ImageEnhance.Contrast(im)
# contrast = 5
image_contrasted = enh_con.enhance(10)
# 亮度增强
enh_bri = ImageEnhance.Brightness(image_contrasted)
image_contrasted1 = cv2.cvtColor(np.asarray(image_contrasted), cv2.COLOR_RGB2BGR) # PIL转cv2
clear = getImageVar(image_contrasted1)
# print(clear)
brightness = max(round(clear / 2000, 1), 1)
# print(brightness)
image_brightened = enh_bri.enhance(brightness)
return image_brightened
def ezh(I):
hest = np.zeros([256], dtype=np.float)
for row in range(h):
for col in range(w):
pv = (int)(I[row, col] + 0.5)
hest[pv] += 1
hest = hest / (h * w)
T0 = 0
for i in range(256):
T0 = T0 + hest[i] * i
T1den = 0
T1 = 0
T1num = 0
for i in range((int)(T0 + 0.5)):
T1den = T1den + hest[i]
T1num = T1num + hest[i] * i
T1 = T1num / T1den
T1num = 0
T1den = 0
for i in range((int)(T0 + 0.5) + 1, 256):
T1den = T1den + hest[i]
T1num = T1num + hest[i] * i
T1 = T1 + T1num / T1den
T1 = T1 / 2
while (abs(T1 - T0) > 0.0001):
T0 = T1
T1den = 0
T1 = 0
T1num = 0
for i in range((int)(T0 + 0.5)):
T1den = T1den + hest[i]
T1num = T1num + hest[i] * i
T1 = T1num / T1den
T1num = 0
T1den = 0
for i in range((int)(T0 + 0.5) + 1, 256):
T1den = T1den + hest[i]
T1num = T1num + hest[i] * i
T1 = T1 + T1num / T1den
T1 = T1 / 2
r, I = cv2.threshold(I, (int)(T1 + 0.5), 255, type=cv2.THRESH_BINARY)
#cv2.imshow('out', I)
#cv2.waitKey(0)
return I
# I = cv2.imread("testimg.jpg")
I = cv2.cvtColor(I, cv2.COLOR_BGR2GRAY)
h, w = np.shape(I)
#I=ezh(I)
I = gauss_division(I)
I = image_enhancement(I)
I = np.array(I)
for row in range(h):
for col in range(w):
if I[row, col] > 1:
I[row, col] = 1
I = I * 255
'''
cv2.imshow('out',I)
cv2.waitKey(0)
'''
return I
#校正
def jiaozheng(I):
try:
# 输入原图,return矫正后的新图像
# I=cv2.imread("binimg2.jpg",cv2.IMREAD_GRAYSCALE)
I = cv2.threshold(I, 127, 255, cv2.THRESH_BINARY)[1]
Icpy = I
I = cv2.erode(I, np.ones((3, 3), np.uint8), iterations=1)
N, M = I.shape[0], I.shape[1]
p = np.zeros(N * M)
for i in range(N * M):
p[i] = i
def find(x):
x = int(x)
if (x != p[x]):
p[x] = find(p[x])
return int(p[x])
idx2area = {}
area2idx = {}
def merge():
idx = 0
for j in range(M):
for i in range(N):
if I[i, j] == 0:
pos = i * M + j
if p[pos] not in area2idx:
temp = []
temp.append(pos)
idx2area[idx] = temp
area2idx[int(p[pos])] = idx
idx = idx + 1
else:
cur = area2idx[int(p[pos])]
temp = idx2area[cur]
temp.append(pos)
idx2area[cur] = temp
return idx
for j in range(1, M):
for i in range(1, N):
if I[i, j] == 0:
pos = i * M + j
if I[i - 1, j] == 0:
posl = (int)(i - 1) * M + j
ppos = find(pos)
pposl = find(posl)
if ppos != pposl:
p[ppos] = pposl
if I[i, j - 1] == 0:
posu = i * M + j - 1
ppos = find(pos)
pposu = find(int(posu))
if ppos != pposu:
p[int(ppos)] = int(pposu)
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
python.rar (4个子文件)
python
photoscaningmaster.py 25KB
cutter.py 3KB
function.py 40KB
cutterLabel.py 4KB
共 4 条
- 1
资源评论
GM5
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功