用python做一個簡單軟體
前言
這是一個課設,用python做一個掃描完軟體
我主要做的GUI部分,記錄分享一下,也是第一次用python做小軟體,python的方便果然是名不虛傳
遇到問題
1.python版本
下載了python3.7的編譯器
由于最終軟體要在win7上運行,即32位的,因此下載了python3.7的32位
打包后遇到問題:w10打包的不能在w7上運行----->下載python32位的解釋器
在w10執行python代碼,參考博客: https://blog.csdn.net/qq_27280237/article/details/84644900
2.opencv降級
參考博客: https://www.cnblogs.com/guobin-/p/10842486.html
3.安裝打包軟體pyinstaller
參考博客: https://blog.csdn.net/Nire_Yeyu/article/details/104683888/
https://blog.csdn.net/Nire_Yeyu/article/details/104683888/
https://www.cnblogs.com/xbblogs/p/9682708.html
##最終打包代碼
pyinstaller -F -w -i 圖片名.ico 檔案名.py

4.輸出的圖片沒法保存
有中文路徑
軟體效果

python代碼
1.GUI部分
import PySimpleGUI as sg
import PIL.Image
import scanner_doee2
import cv2
import os
import numpy as np
import other
from other import convert_to_bytes
from tkinter import *
# 全域變數
mp_path = ['讀取影像','原圖處理','濾鏡']
mp_key = ['原圖處理', '影像翻轉', '尋找輪廓','讀取影像','img0']
choices = ('素描濾鏡','復古濾鏡','反色濾鏡','邊界濾鏡', '模糊濾鏡','不加濾鏡','浮雕濾鏡')
#快取圖片標號
# 原圖/翻轉后的圖片 0
# 圈出輪廓的圖 10
# 透視變換后 11
# 調整亮度和對比度后的圖 2
# 添加濾鏡后的圖 3
sg.theme('Light Blue 2')
layout1 = [
[sg.Frame(layout=[
[sg.Text('影像地址'), sg.Input(key='path_in'), sg.FileBrowse()],
[sg.Button('讀取影像')]
], title='讀圖',title_color='blue')],
[sg.Button('翻轉調整'),sg.Button('矯正處理')],
[sg.Frame(layout=[
[sg.Button('手動調節'), sg.Button('自適應均衡化'), sg.Button('清空效果')],
], title='亮度和對比度調節',title_color='blue')],
[sg.Frame(layout = [
[sg.Listbox(choices, size=(15, len(choices)), key='filter')],
[sg.Button('濾鏡處理')]
], title='濾鏡', title_color='blue') ],
[sg.Frame(layout=[
[sg.Button('閾值調節')]],
title='閾值', title_color='blue', relief=sg.RELIEF_SUNKEN, tooltip='Use these to set flags')],
[sg.Frame(layout=[
[sg.Radio('普通', "RADIO1",key='普通', default=True, size=(10, 1)), sg.Radio('拍書', "RADIO1",key='拍書'),sg.Radio('證書', "RADIO1", key='證件',default=False, size=(10, 1))]],
title='應用場景', title_color='blue', relief=sg.RELIEF_SUNKEN, tooltip='Use these to set flags')],
[sg.Frame(layout=[
[sg.Text('保存地址'), sg.Input(key='path_out'),sg.FolderBrowse(target='path_out')],
[sg.Button('輸出影像')]
], title='輸出',title_color='blue')],
]
layout2 = [[sg.Text('原圖:')],
[sg.Image(key='img0',size=(300,300))],
[sg.Text('尋找到輪廓后的圖:')],
[sg.Image(key='img10', size=(300, 300))],
[sg.Text('位置矯正+裁剪后的圖:')],
[sg.Image(key='img11',size=(300, 300))]
]
layout3=[ [sg.Text('調整后的圖')],
[sg.Image(key='img2',size=(500,500))]
]
layout = [[sg.Column(layout1, element_justification='c'), sg.VSeperator(),sg.Column(layout2, element_justification='c'),sg.Column(layout3, element_justification='c')]]
window = sg.Window('掃描王', layout)
while (True):
event, values = window.read()
if event !=None:
print(event,values)
if event =='讀取影像':
path_in = values['path_in']
path_save=os.path.dirname(path_in)
img0=cv2.imread(path_in)
print(path_save)
scanner_doee2.varible(path_save)
# orig = img0 #備份原圖
# 重新設定圖片的大小,以便對其進行處理:選擇最佳維度,以便重要內容不會丟失
# img0 = cv2.resize(img0, (1500, 880))
cv2.imwrite(path_save+'/img0.jpg',img0)
window['img0'].update(data=convert_to_bytes(path_in, (300,300)))
if event =='翻轉調整':
img0=np.rot90(img0)
cv2.imwrite(path_save + '/img0.jpg', img0)
window['img0'].update(data=convert_to_bytes(path_save+'/img0.jpg', (300, 300)))
if event=='矯正處理':
img1=scanner_doee2.solve(img0)
img2=img1
img3=img1
window['img10'].update(data=convert_to_bytes(path_save+'/img10.jpg', resize=(300,300)))
window['img11'].update(data=convert_to_bytes(path_save+'/img11.jpg', resize=(300,300)))
if event=='清空效果':
img2=img1
cv2.imwrite(path_save + '/img2.jpg', img2)
window['img2'].update(data=convert_to_bytes(path_save+'/img2.jpg', resize=(500,500)))
if event=='手動調節':
img2=scanner_doee2.light(img2)
cv2.imwrite(path_save + '/img2.jpg', img2)
window['img2'].update(data=convert_to_bytes(path_save+'/img2.jpg', resize=(500,500)))
if event=='自適應均衡化':
img2=scanner_doee2.autoEqualHistColor(img2)
cv2.imwrite(path_save + '/img2.jpg', img2)
window['img2'].update(data=convert_to_bytes(path_save+'/img2.jpg', resize=(500,500)))
if event=='濾鏡處理':
img3=img2
ss=values['filter']
print(ss,ss[0])
if ss[0]=='復古濾鏡':
img3 = scanner_doee2.mirror2(img2)
elif ss[0]=='素描濾鏡':
print(ss)
img3 = scanner_doee2.mirror1(img2)
elif ss[0] == '反色濾鏡':
print(ss)
img3 = scanner_doee2.mirror3(img2)
elif ss[0] == '邊界濾鏡':
img3 = scanner_doee2.mirror4(img2)
elif ss[0] == '浮雕濾鏡':
img3 = scanner_doee2.mirror5(img2,1)
elif ss[0] == '模糊濾鏡':
img3 = scanner_doee2.mirror5(img2,2)
elif ss[0]=='不加濾鏡':
img3=img2
cv2.imwrite(path_save + '/img2.jpg', img3)
window['img2'].update(data=convert_to_bytes(path_save + '/img2.jpg', resize=(500, 500)))
if event=='閾值調節':
img4=img2
img4=scanner_doee2.yuzhi(img2)
cv2.imwrite(path_save + '/img2.jpg', img4)
window['img2'].update(data=convert_to_bytes(path_save + '/img2.jpg', resize=(500, 500)))
if event=='輸出影像':
img5=cv2.imread(path_save + '/img2.jpg')
h,w,c=img5.shape
# A4 297*210mm
# B5 250*176
# 身份證 54*85.6
if values['拍書']==True:
scale = min(h/250, w/176)
img5=cv2.resize(img5,(int(176* scale), int(250* scale)))
elif values['證件']==True:
scale = min(h/54, w/85.6)
img5=cv2.resize(img5,(int(85.6* scale), int(54* scale)))
elif values['普通']==True:
img5 = cv2.imread(path_save + '/img2.jpg')
cv2.imshow('output',img5)
path_out=values['path_out']
cv2.imwrite( path_out+ '/out.jpg', img5)
# cv2.imwrite(path_save + '/img2.jpg', img5)
if event == sg.WIN_CLOSED or event == 'Exit':
break
2.演算法部分
import cv2
import numpy as np
from math import sqrt
import cmath
from PIL import Image, ImageFilter
path_save='yes'
def varible(ss):
global path_save
path_save=ss
print(path_save)
def rectify(h):
h = h.reshape((4,2)) #改變陣列的形狀,變成4*2形狀的陣列
hnew = np.zeros((4,2), dtype = np.float32) #創建一個4*2的零矩陣
#確定檢測檔案的四個頂點
add = h.sum(1)
hnew[0] = h[np.argmin(add)] #argmin()函式是回傳最大數的索引
hnew[2] = h[np.argmax(add)]
diff = np.diff(h, axis = 1) #沿著制定軸計算第N維的離散差值
hnew[1] = h[np.argmin(diff)]
hnew[3] = h[np.argmax(diff)]
return hnew
# 擬合曲線頂點的去中心化
def approxCenter(approx):
sum_x,sum_y = 0,0
approx_center = approx;
for a in approx:
sum_x = sum_x + a[0][0];
sum_y = sum_y + a[0][1];
avr_x = sum_x/len(approx);
avr_y = sum_y/len(approx);
for a in approx_center:
a[0][0] = a[0][0] - avr_x
a[0][1] = a[0][1] - avr_y
return approx_center,avr_x,avr_y
#將頂點極坐標化,回傳極角
def approxTheta(approx):
cn = complex(approx[0][0],approx[0][1]) #得到每個點相對中心的直角坐標
r,theta = cmath.polar(cn) #將直角坐標轉為極坐標,得到極角
return theta
# 合并擬合多邊形頂點中的相近點
# approx:擬合多邊形(n維陣列)
# M:距離閾值
def approxCombine(approx,M):
del_indexs = []
for i in range(len(approx)):
if i not in del_indexs: #判斷是否是已刪點,如果是則跳過計算
for j in range(i+1,len(approx)):
if j not in del_indexs: #判斷是否是已刪點,如果是則跳過計算
#計算兩點距離
dis = sqrt((approx[i][0][0] - approx[j][0][0])**2 + (approx[i][0][1] - approx[j][0][1])**2)
if dis < M :
#將兩個相近點,近似為中值點
approx[i][0][0] = (approx[i][0][0] + approx[j][0][0])/2
approx[i][0][1] = (approx[i][0][1] + approx[j][0][1])/2
del_indexs.append(j)
approx = np.delete(approx, del_indexs,0) #洗掉多余的近似點
approx,avr_x,avr_y = approxCenter(approx); #將頂點去中心化,用于計算極坐標
approx = sorted(approx, key = approxTheta, reverse = True) #按照極角進行降序排序
approx = np.array(approx) #sorted回傳list型,轉換為ndarray
# 恢復去中心的頂點
for a in approx:
a[0][0] = a[0][0] + avr_x
a[0][1] = a[0][1] + avr_y
return approx
#伽馬變換
#gamma > 1時,影像對比度增強
def gamma_trans(input_image, gamma):
img_norm = input_image/255.0
img_gamma = np.power(img_norm,gamma)*255.0
img_gamma = img_gamma.astype(np.uint8)
return img_gamma
# 彩色直方圖均衡(對比度增強)(效果一般)
def equalHistColor(img_in):
b, g, r = cv2.split(img_in)
b1 = cv2.equalizeHist(b)
g1 = cv2.equalizeHist(g)
r1 = cv2.equalizeHist(r)
img_out = cv2.merge([b1,g1,r1])
return img_out
# 彩色伽馬變換(對比度增強)(效果較好)
def gammaColor(img_in,gamma):
b, g, r = cv2.split(img_in)
b1 = gamma_trans(b,gamma)
g1 = gamma_trans(g,gamma)
r1 = gamma_trans(r,gamma)
img_out = cv2.merge([b1,g1,r1])
return img_out
# 亮度調節,原理:將原圖與一張全黑影像融合,調節融合的比例,即為亮度調節
# c為原圖所占比例,c > 1時,亮度增強
def light_img(img1, c):
rows, cols, channels = img1.shape
# 新建全零(黑色)圖片陣列:np.zeros(img1.shape, dtype=uint8)
blank = np.zeros([rows, cols, channels], img1.dtype)
dst = cv2.addWeighted(img1, c, blank, 1-c, 0) #兩幅影像融合,當1-c小于0時,亮度增強
return dst
def solve(image):
# print(path_save)
# path_save='C:/Users/53055/Desktop/pythonProject3'
#創建原始影像的副本
orig = image.copy()
orig_w, orig_h, ch = orig.shape # 讀取大小
#重新設定圖片的大小,以便對其進行處理:選擇最佳維度,以便重要內容不會丟失
image = cv2.resize(image, (1500,880))
orig_h_ratio = orig_h / 1500.0 # 保存縮放比例
orig_w_ratio = orig_w / 880.0 # 保存縮放比例
#對影像進行灰度處理,并進而進行行高斯模糊處理
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5,5), 0)
#使用canny演算法進行邊緣檢測
edged = cv2.Canny(blurred,0,50)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
edged = cv2.dilate(edged, kernel) # 膨脹
#創建canny演算法處理后的副本
orig_edged = edged.copy()
#找到邊緣影像中的輪廓,只保留最大的,并初始化螢屏輪廓
#findContours()函式用于從二值影像中查找輪廓
# RETR_LIST:尋找所有輪廓
# CHAIN_APPROX_NONE:輸出輪廓上所有的連續點
contours, hierarchy = cv2.findContours(edged, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
approxs = []
for c in contours:
p = cv2.arcLength(c, True) #計算封閉輪廓的周長或者曲線的長度
approx = cv2.approxPolyDP(c, 0.02*p, True) #指定0.02*p精度逼近多邊形曲線,這種近似曲線為閉合曲線,因此引數closed為True
approx_cmb = approxCombine(approx,60) # 合并輪廓中相近的坐標點
if len(approx_cmb) == 4: #如果是四邊
approxs.append(approx_cmb) #該輪廓為可能的目標輪廓
# 將輪廓的擬合多邊型按面積大小降序排序
approxs = sorted(approxs, key = cv2.contourArea, reverse = True)
# 選取面積最大的四邊形輪廓
target = approxs[0]
# 將輪廓映射到原圖上
for t in target:
t[0][0] = t[0][0] * orig_h_ratio
t[0][1] = t[0][1] * orig_w_ratio
# 在原灰度圖上繪制尋找到的目標四邊形輪廓
orig_marked = orig
# all_approxs = cv2.cvtColor(temp, cv2.COLOR_GRAY2RGB)
cv2.drawContours(orig_marked,[target],-1,(0,255,0),8)
# cv2.imshow('orig_marked',orig_marked)
# 保存圈出輪廓的圖
cv2.imwrite(path_save + '/img10.jpg', orig_marked)
# for i in range(len(approxs)):
# cv2.drawContours(all_approxs,[approxs[i]],-1,(0,255,0),2)
#將目標輪廓映射到800*800四邊形(用于透視變換)
approx = rectify(target)
pts2 = np.float32([[0,0],[800,0],[800,800],[0,800]])
# 透視變換
# 使用gtePerspectiveTransform函式獲得透視變換矩陣:approx是源影像中四邊形的4個定點集合位置;pts2是目標影像的4個定點集合位置
M = cv2.getPerspectiveTransform(approx, pts2)
# 使用warpPerspective函式對源影像進行透視變換,輸出影像dst大小為800*800
dst = cv2.warpPerspective(orig, M, (800,800))
# 進行位置校正、裁剪(透視變換)后的影像
# cv2.imshow("trans",dst)
cv2.imwrite(path_save + '/img11.jpg', dst)
return dst
# 彩色限制對比度自適應直方圖均衡化(影像亮度均衡)
def autoEqualHistColor(img_in):
b, g, r = cv2.split(img_in)
clahe = cv2.createCLAHE(1,tileGridSize = (8,8))
b1 = clahe.apply(b)
g1 = clahe.apply(g)
r1 = clahe.apply(r)
img_out = cv2.merge([b1,g1,r1])
return img_out
# 手動調節亮度和對比度
def light(dst):
data=[110,220]
def l_c_regulate(x):
l = cv2.getTrackbarPos('light', 'light & contrast regulate')
gamma = cv2.getTrackbarPos('contrast', 'light & contrast regulate')
lighted = light_img(img_lc_regulate, l / 100.0) # 亮度調節
gammaed = gammaColor(lighted, gamma / 100.0) # gamma變換
cv2.imshow("light & contrast regulate", gammaed)
data=[l,gamma]
return gammaed
img_lc_regulate = dst # 復制原圖
cv2.namedWindow('light & contrast regulate') #創建window
cv2.createTrackbar('light', 'light & contrast regulate', 110, 500, l_c_regulate) #亮度滑動條
cv2.createTrackbar('contrast', 'light & contrast regulate', 210, 500, l_c_regulate) #對比度滑動條
l_c_regulate(0) #先運行一次回呼函式
while(1):
k=cv2.waitKey(1)&0xFF
if k==27: #ECS鍵
cv2.destroyWindow('light & contrast regulate')
lighted = light_img(img_lc_regulate, data[0] / 100.0) # 亮度調節
gammaed = gammaColor(lighted, data[1] / 100.0) # gamma變換
break
return gammaed
# 素描濾鏡
def mirror1(img_in):
img_in = cv2.cvtColor(img_in, cv2.COLOR_BGR2GRAY) # 轉為灰度圖
img_in = cv2.equalizeHist(img_in) # 直方圖均衡化
inv = 255- img_in # 影像取反
blur = cv2.GaussianBlur(inv, ksize=(5, 5), sigmaX=50, sigmaY=50) # 高斯濾波
res = cv2.divide(img_in, 255- blur, scale= 255) #顏色減淡混合
res = gamma_trans(res,2) #伽馬變換,增強對比度
return res
#復古濾鏡(運行超級慢)
def mirror2(img_in):
img_in = cv2.cvtColor(img_in, cv2.COLOR_BGR2GRAY) # 轉為灰度圖
im_color = cv2.applyColorMap(img_in, cv2.COLORMAP_PINK)
return im_color
# 反色濾鏡
def mirror3(img_in):
inv = 255- img_in # 影像取反
return inv
# 邊界濾鏡(利用canny算子實作)
def mirror4(img_in):
img_in = cv2.cvtColor(img_in, cv2.COLOR_BGR2GRAY)
img_f = cv2.Canny(img_in,100,200)
return img_f
# cv2.imshow('img_f',img_f)
def mirror5(dst,type):
img_f = Image.fromarray(cv2.cvtColor(dst,cv2.COLOR_BGR2RGB))
if type ==1:
img_f = img_f.filter(ImageFilter.EMBOSS) #浮雕濾鏡
elif type==2:
img_f = img_f.filter(ImageFilter.BLUR) #模糊濾鏡
img_f = cv2.cvtColor(np.asarray(img_f),cv2.COLOR_RGB2BGR)
return img_f
# # 以下為PIL庫的部分濾鏡效果
#
# # OpenCV的圖片格式轉換成PIL.Image格式
# img_f = Image.fromarray(cv2.cvtColor(dst,cv2.COLOR_BGR2RGB))
#
# # 濾鏡處理
# # ImageFilter.BLUR 模糊濾鏡
# # ImageFilter.SHARPEN 銳化濾鏡
# # ImageFilter.SMOOTH 平滑濾鏡
# # ImageFilter.SMOOTH_MORE 平滑濾鏡(閥值更大)
# # ImageFilter.EMBOSS 浮雕濾鏡
# # ImageFilter.FIND_EDGES 邊界濾鏡
# # ImageFilter.EDGE_ENHANCE 邊界加強
# # ImageFilter.EDGE_ENHANCE_MORE 邊界加強(閥值更大)
# # ImageFilter.CONTOUR 輪廓濾鏡
# img_f = img_f.filter(ImageFilter.EMBOSS) #浮雕濾鏡
# # img_f = img_f.filter(ImageFilter.CONTOUR) #素描濾鏡
# # img_f = img_f.filter(ImageFilter.FIND_EDGES) #邊界濾鏡
#
# # PIL.Image轉換成OpenCV格式
# img_f = cv2.cvtColor(np.asarray(img_f),cv2.COLOR_RGB2BGR)
def yuzhi(img_in):
# 二值化閾值調節示例
# 關于二值化,用身份證照片測驗時,全域閾值進行二值化效果還可以,但如果存在灰度不均勻,會出現部分資訊缺失
# OTSU自動閾值法的效果也不錯(效果不錯的前提是影像灰度均勻,本質是一種全域最佳閾值的方法,依舊存在全域閾值的缺點)
# 使用區域自適應閾值時,對不同灰度的區域有很好的效果,但如果視窗過小,會導致噪點被放大,可以通過調節偏移閾值去除噪點
# 視窗調大到一定值時,效果等同于使用全域閾值,因此最終使用區域自適應閾值方法進行二值化
# demo中使用滑塊調節自適應閾值視窗的size,
# 關于消除噪點,嘗試過高斯濾波、膨脹,效果不好
data=[57,30]
def bin_regulate(x):
data[0] = cv2.getTrackbarPos('auto size', 'bin regulate') # 自適應閾值視窗大小
if data[0] == 0:
data[0] = 1 # 視窗最小大小為3
data[1] = cv2.getTrackbarPos('threshold', 'bin regulate') # 自適應閾值偏移量
# img_bin = cv2.GaussianBlur(img_bin, ksize=(3, 3), sigmaX=100, sigmaY=100) #高斯濾波
# 固定全域閾值二值化
# ret,img_bin = cv2.threshold(img_bin, t, 255, cv2.THRESH_BINARY)
# OTSU自動閾值
# ret,img_bin = cv2.threshold(img_bin, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# 以下兩種區域自適應閾值方法類似
# 自適應閾值二值化(均值):第二個引數為領域內均值,第五個引數為規定正方形領域大小(11*11),第六個引數是常數C:閾值等于均值減去這個常數
# img_bin = cv2.adaptiveThreshold(img_bin, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 21, 2)
# 自適應閾值二值化(高斯視窗)第二個引數為領域內像素點加權和,權重為一個高斯視窗,第五個引數為規定正方形領域大小(11*11),第六個引數是常數C:閾值等于加權值減去這個常數
img_bin = cv2.adaptiveThreshold(img_bin_i, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 2 * data[0] + 1, data[1])
# 膨脹
# kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
# img_bin = cv2.dilate(img_bin,kernel) #膨脹
# 中值濾波
# img_bin = cv2.medianBlur(img_bin, 2*blur_size+1)
cv2.imshow("bin regulate", img_bin)
pass
img_gray = cv2.cvtColor(img_in, cv2.COLOR_BGR2GRAY) # 轉為灰度圖
img_bin_i = img_gray # 復制灰度圖
cv2.namedWindow('bin regulate') # 創建window
cv2.createTrackbar('auto size', 'bin regulate', 57, 400, bin_regulate) # 自適應閾值的視窗size值
cv2.createTrackbar('threshold', 'bin regulate', 30, 100, bin_regulate) # 自適應閾值偏移量
bin_regulate(0) # 先運行一次回呼函式
while (1):
k = cv2.waitKey(1) & 0xFF
if k == 27: # ECS鍵
img_bin = cv2.adaptiveThreshold(img_bin_i, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,
2 * data[0] + 1, data[1])
cv2.destroyWindow('bin regulate')
break
return img_bin
# while (1):
# k = cv2.waitKey(1) & 0xFF
#
# if k == 27: # ECS鍵
# cv2.destroyWindow('light & contrast regulate')
# img_bin = cv2.adaptiveThreshold(img_bin_i, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,
# 2 * data[0] + 1, data[1])
# break
return img_bin
def other():
# 二值化
# 對透視變換后的影像進行灰度處理
img_gray = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY)
img_gray = gamma_trans(img_gray,1.2) #伽馬變換,增強對比度
# 二值化閾值調節示例
# 關于這個二值化,用身份證照片測驗時,全域閾值進行二值化效果還可以,但如果存在灰度不均勻,會出現部分資訊缺失
# 使用區域自適應閾值時,對不同灰度的區域有很好的效果,但如果視窗過小,會有很多噪點被放大
# 視窗調大到一定值時,效果等同于使用全域閾值,因此最終使用區域自適應閾值方法進行二值化
# demo中使用滑塊調節自適應閾值視窗的size
# 為了消除噪點,嘗試過高斯濾波、膨脹,效果不好
# OTSU自動閾值法的效果也不錯(效果不錯的前提是影像灰度均勻,本質是一種全域最佳閾值的方法,依舊存在全域閾值的缺點)
def bin_regulate(x):
t = cv2.getTrackbarPos('auto size', 'bin regulate')
if t == 0:
t = 1 # 視窗最小大小為3
# blur_size = cv2.getTrackbarPos('blursize', 'bin regulate')
# img_bin = cv2.GaussianBlur(img_bin_regulate, ksize=(3, 3), sigmaX=100, sigmaY=100) #高斯濾波
# ret,img_bin = cv2.threshold(img_bin_regulate, t, 255, cv2.THRESH_BINARY) #進行固定閾值處理,得到二值影像
img_bin = img_bin_regulate
# 以下兩種自適應閾值方法類似
# 自適應閾值二值化(均值):第二個引數為領域內均值,第五個引數為規定正方形領域大小(11*11),第六個引數是常數C:閾值等于均值減去這個常數
# img_bin = cv2.adaptiveThreshold(img_bin, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 21, 2)
# 自適應閾值二值化(高斯視窗)第二個引數為領域內像素點加權和,權重為一個高斯視窗,第五個引數為規定正方形領域大小(11*11),第六個引數是常數C:閾值等于加權值減去這個常數
img_bin = cv2.adaptiveThreshold(img_bin,255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 2*t+1, 2)
# OTSU自動閾值(效果還可以)
# ret,img_bin = cv2.threshold(img_bin, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# 膨脹
# kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
# img_bin = cv2.dilate(img_bin,kernel) #膨脹
cv2.imshow("bin regulate",img_bin)
pass
img_bin_regulate = img_gray #復制灰度圖
cv2.namedWindow('bin regulate') #創建window
cv2.createTrackbar('auto size', 'bin regulate', 1, 400, bin_regulate) #自適應閾值的視窗size值
# cv2.createTrackbar('blursize', 'bin regulate', 1, 100, bin_regulate) #高斯濾波size滾動條
bin_regulate(0) #先運行一次回呼函式
# #對透視變換后的影像使用閾值進行約束獲得掃描結果
# # 使用固定閾值操作:threshold()函式:有四個引數:第一個是原影像,第二個是進行分類的閾值,第三個是高于(低于)閾值時賦予的新值,
# # 第四個是一個方法選擇引數:cv2.THRESH_BINARY(黑白二值)
# # 該函式回傳值有兩個引數,第一個是retVal(得到的閾值值(在OTSU會用到)),第二個是閾值化后的影像
# ret, th1 = cv2.threshold(dst, 132, 255, cv2.THRESH_BINARY) #進行固定閾值處理,得到二值影像
# # 使用Otsu's二值化,在最后一個引數加上cv2.THRESH_OTSU
# ret2, th2 = cv2.threshold(dst, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# # 使用自適應閾值操作:adaptiveThreshold()函式
# # 第二個引數為領域內均值,第五個引數為規定正方形領域大小(11*11),第六個引數是常數C:閾值等于均值減去這個常數
# th3 = cv2.adaptiveThreshold(dst, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
# # 第二個引數為領域內像素點加權和,權重為一個高斯視窗,第五個引數為規定正方形領域大小(11*11),第六個引數是常數C:閾值等于加權值減去這個常數
# th4 = cv2.adaptiveThreshold(dst,255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
#輸出處理后的影像
cv2.imshow("orig", orig)
cv2.imshow("gray", gray)
cv2.imshow("blurred", blurred)
cv2.imshow("canny_edge", orig_edged)
cv2.imshow("marked", image)
# cv2.imshow("thre_constant", th1)
# cv2.imshow("thre_ostu", th2)
# cv2.imshow("thre_auto1", th3)
# cv2.imshow("thre_auto2", th4)
cv2.imshow("orig_mark", dst)
# cv2.imwrite("orig.jpg",dst)
# cv2.imshow('all-approxs',all_approxs)
cv2.waitKey(0)
cv2.destroyAllWindows()
3.輔助代碼
import PIL.Image
import io
import base64
global filename
def convert_to_bytes(file_or_bytes, resize=None):
'''
Will convert into bytes and optionally resize an image that is a file or a base64 bytes object.
Turns into PNG format in the process so that can be displayed by tkinter
:param file_or_bytes: either a string filename or a bytes base64 image object
:type file_or_bytes: (Union[str, bytes])
:param resize: optional new size
:type resize: (Tuple[int, int] or None)
:return: (bytes) a byte-string object
:rtype: (bytes)
'''
if isinstance(file_or_bytes, str):
img = PIL.Image.open(file_or_bytes)
else:
try:
img = PIL.Image.open(io.BytesIO(base64.b64decode(file_or_bytes)))
except Exception as e:
dataBytesIO = io.BytesIO(file_or_bytes)
img = PIL.Image.open(dataBytesIO)
cur_width, cur_height = img.size
if resize:
new_width, new_height = resize
scale = min(new_height/cur_height, new_width/cur_width)
img = img.resize((int(cur_width*scale), int(cur_height*scale)), PIL.Image.ANTIALIAS)
bio = io.BytesIO()
img.save(bio, format="PNG")
del img
return bio.getvalue()
def save_pic(filename,type,id):
mp_type = {'0': '原圖翻轉', '白元芳': 78, '狄仁杰': 82}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/290279.html
標籤:python
