主頁 > 後端開發 > 用python做一個簡單GUI小軟體

用python做一個簡單GUI小軟體

2021-07-27 07:49:24 後端開發

用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

mark

4.輸出的圖片沒法保存

有中文路徑

軟體效果

mark

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

上一篇:【PTA|Python】浙大版《Python 程式設計》題目集:第六章

下一篇:千萬別再瞎學Python了,過來人的一些學習經驗,能讓你少走很多彎路!

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more