python人工智能opencv工具作業(yè):使用tkinter展示邊緣檢測(cè)

opencv工具作業(yè):使用tkinter展示邊緣檢測(cè)

圖片.png
圖片.png
圖片.png
圖片.png

參考資料

代碼

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# 技術(shù)支持:http://www.itdecent.cn/u/69f40328d4f0 
# 技術(shù)支持 https://china-testing.github.io/
# https://github.com/china-testing/python-api-tesing/blob/master/practices/cv/cv1.py
# 項(xiàng)目實(shí)戰(zhàn)討論QQ群630011153 144081101
# CreateDate: 2018-12-04

# import the necessary packages
from tkinter import *
from PIL import Image
from PIL import ImageTk
from tkinter import filedialog
import cv2

def select_image():
    # grab a reference to the image panels
    global panelA, panelB

    # open a file chooser dialog and allow the user to select an input
    # image
    path = filedialog.askopenfilename()

    # ensure a file path was selected
    if len(path) > 0:
        # load the image from disk, convert it to grayscale, and detect
        # edges in it
        image = cv2.imread(path)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        edged = cv2.Canny(gray, 50, 100)

        # OpenCV represents images in BGR order; however PIL represents
        # images in RGB order, so we need to swap the channels
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        # convert the images to PIL format...
        image = Image.fromarray(image)
        edged = Image.fromarray(edged)

        # ...and then to ImageTk format
        image = ImageTk.PhotoImage(image)
        edged = ImageTk.PhotoImage(edged)

        # if the panels are None, initialize them
        if panelA is None or panelB is None:
            # the first panel will store our original image
            panelA = Label(image=image)
            panelA.image = image
            panelA.pack(side="left", padx=10, pady=10)

            # while the second panel will store the edge map
            panelB = Label(image=edged)
            panelB.image = edged
            panelB.pack(side="right", padx=10, pady=10)

        # otherwise, update the image panels
        else:
            # update the pannels
            panelA.configure(image=image)
            panelB.configure(image=edged)
            panelA.image = image
            panelB.image = edged

# initialize the window toolkit along with the two image panels
root = Tk()
panelA = None
panelB = None
root.title("opencv 邊緣檢測(cè)演示")  

# create a button, then when pressed, will trigger a file chooser
# dialog and allow the user to select an input image; then add the
# button the GUI
btn = Button(root, text="選擇圖片", command=select_image)
btn.pack(side="bottom", fill="both", expand="yes", padx="100", pady="100")

# kick off the GUI
root.mainloop()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,281評(píng)論 25 708
  • 用兩張圖告訴你,為什么你的 App 會(huì)卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 14,076評(píng)論 2 59
  • 申明: 本文翻譯自PyImageSearch社區(qū)2016年的一篇關(guān)于OpenCV的安裝教程“Ubuntu 16.0...
    曉笑閱讀 19,953評(píng)論 0 17
  • Badusb簡(jiǎn)介 BadUSB是USB的一款嚴(yán)重漏洞。攻擊者可利用該缺陷,在有效且不被檢測(cè)到的情況下,悄悄地植入惡...
    RedTeamWing閱讀 15,435評(píng)論 0 12
  • 前言 在iOS中我們經(jīng)常需要使用到時(shí)間戳,但在iOS下獲取“時(shí)間”的方法有很多。不過(guò)總的來(lái)說(shuō),我們可以在iOS中獲...
    Tidus閱讀 4,493評(píng)論 3 52

友情鏈接更多精彩內(nèi)容