<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 使用 OpenCV,Python 和模板匹配來播放“哪里是 Waldo?” > 原文: [https://machinelearningmastery.com/using-opencv-python-and-template-matching-to-play-wheres-waldo/](https://machinelearningmastery.com/using-opencv-python-and-template-matching-to-play-wheres-waldo/) **_ 這是來自 [PyImageSearch](http://www.pyimagesearch.com) 的 Adrian Rosebrock 的客座文章,這是一個關于計算機視覺,圖像處理和建筑圖像搜索引擎的博客。_** [![Where's Waldo](https://img.kancloud.cn/50/bc/50bc1da97e535eda0a4b541f9f655e47_1000x707.jpg)](https://3qeqpr26caki16dnhd19sv6by6v-wpengine.netdna-ssl.com/wp-content/uploads/2014/05/puzzle_small.jpg) 圖 1:在這個難題中找到 Waldo 需要多長時間? 看看上面的 Where's Waldo 拼圖。找到 Waldo 需要多長時間? 10 秒? 30 秒?一分鐘? Waldo 是人類隱藏和尋求的終極游戲。他實際上是“隱藏”在平常的視線中 - 但由于所有的噪音和分心,我們不能立即接他! Waldo 的核心只是一種視覺模式。他戴眼鏡。一頂帽子。而他經典的白色和紅色水平條紋襯衫。我們可能需要花一點時間在頁面上上下左右掃描,但我們的大腦能夠挑選出這種模式,即使是在所有的分心中也是如此。 問題是,計算機可以做得更好嗎?我們可以創建一個程序可以 **_ 自動 _** 找到 Waldo 嗎? 事實上,我們可以。 使用計算機視覺技術,我們可以在一秒鐘內找到 Waldo,比我們任何人都快得多! 在這篇博文中,我將向您展示如何使用 OpenCV 和模板匹配功能來找到那個總是隱藏在視線中的討厭的 Waldo。 以下是我們將要做的快速概述: * **我們要做什么:**使用 OpenCV 構建一個 Python 腳本,可以在“Where's Waldo?”拼圖中找到 Waldo。 * **您將學到什么:**如何使用`cv2.matchTemplate`和`cv2.minMaxLoc`來利用 Python,OpenCV 和模板匹配。使用這些功能,我們將能夠在拼圖中找到 Waldo。 * **你需要什么:** Python,NumPy 和 OpenCV。對基本圖像處理概念有一點了解會有所幫助,但 _ 絕對不是一個要求 _。本操作指南旨在介紹如何使用 OpenCV 應用模板匹配。沒有安裝這些庫?沒問題。我創建了一個預配置的虛擬機,其中預裝了所有必要的計算機視覺,圖像處理和機器學習包。 [點擊此處了解更多](https://www.pyimagesearch.com/practical-python-opencv/)。 * **假設:**我假設您在 python2.6 或 python2.7 環境中安裝了 [NumPy](http://www.numpy.org/) 和 [OpenCV](http://opencv.org/) 。同樣,您可以在此處下載已安裝所有必需軟件包[的預配置虛擬機。](https://www.pyimagesearch.com/practical-python-opencv/) ## 目標: 那么我們要創建的 Python 腳本的總體目標是什么? **給出 Waldo 和拼圖圖像的查詢圖像的目的是在拼圖中找到 Waldo 并突出顯示他的位置。** 正如您將在本文后面看到的,我們將能夠在 _ 中完成這兩行 Python 代碼 _。其余的代碼只是處理邏輯,如參數解析和顯示解決的拼圖到我們的屏幕。 ## 我們的拼圖和查詢圖像 我們需要兩個圖像來構建我們的 Python 腳本來執行模板匹配。 第一張圖片是我們要解決的 Where's Waldo 拼圖。您可以在本文頂部的**圖 1** 中看到我們的拼圖圖像。 第二張圖片是我們對 Waldo 的查詢圖片: [![Our Waldo query image](https://img.kancloud.cn/d0/da/d0dad12e1ef4132fc1c5ab4898697949_214x137.jpg)](https://3qeqpr26caki16dnhd19sv6by6v-wpengine.netdna-ssl.com/wp-content/uploads/2014/05/waldo_query.jpg) 圖 2:我們的 Waldo 查詢圖像 使用我們的 Waldo 查詢圖像,我們將在原始拼圖中找到他。 不幸的是,這是我們的方法的實用性破壞的地方。 為了在我們的拼圖中找到 Waldo,我們首先需要 Waldo 自己的形象。你可能會問,如果我已經有了 Waldo 的形象,為什么我在玩這個拼圖呢? 好點子。 使用計算機視覺和圖像處理技術在圖像 _ 中找到 Waldo 當然是可能的 _。 但是,它需要一些稍微更先進的技術,例如: 1. 過濾掉不是紅色的顏色。 2. 計算條紋圖案的相關性,以匹配 Waldo 襯衫的紅色和白色過渡。 3. 圖像區域的二值化與條紋圖案具有高度相關性。 這篇文章的目的是 _ 介紹 _ 到基本的計算機視覺技術,如模板匹配。稍后我們可以深入研究更先進的技術。哪個是 Waldo 只是一個很酷而簡單的方法來執行模板匹配,我 _ 只是 _ 與你分享! ## 弄臟我們的手 準備看一些代碼?好吧,我們這樣做: A How-To Guide to Template Matching with OpenCV and Python Python ``` # import the necessary packages import numpy as np import argparse import imutils import cv2 # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-p", "--puzzle", required = True, help = "Path to the puzzle image") ap.add_argument("-w", "--waldo", required = True, help = "Path to the waldo image") args = vars(ap.parse_args()) # load the puzzle and waldo images puzzle = cv2.imread(args["puzzle"]) waldo = cv2.imread(args["waldo"]) (waldoHeight, waldoWidth) = waldo.shape[:2] ``` **第 1-13 行**只是導入我們將要使用的包并配置我們的參數解析器。我們將使用 NumPy 進行數組操作,使用`argparse`解析命令行參數,使用`cv2`進行 OpenCV 綁定。包`imutils`實際上是一組便利功能,用于處理基本圖像處理,例如旋轉,調整大小和平移。您可以在這里閱讀更多關于這些類型的基本圖像操作 [](http://www.pyimagesearch.com/2014/01/20/basic-image-manipulations-in-python-and-opencv-resizing-scaling-rotating-and-cropping/) 。 從那里,我們需要設置兩個命令行參數。第一個,`--puzzle`是我們 Where's Waldo 拼圖圖像的路徑,`--waldo`是 Waldo 查詢圖像的路徑。 同樣,我們的目標是使用模板匹配在拼圖圖像中查找查詢圖像。 現在我們已經有了圖像的路徑,我們使用`cv2.imread`功能將它們從**第 16 行和第 17 行**上的磁盤上加載 - 這種方法只是從磁盤上讀取圖像,然后將其存儲為多個 - 維數 NumPy 數組。 由于圖像在 OpenCV 中表示為 NumPy 數組,因此我們可以輕松訪問圖像的尺寸。在**第 18 行**上,我們分別獲取 Waldo 查詢圖像的高度和寬度。 我們現在準備執行模板匹配: A How-To Guide to Template Matching with OpenCV and Python Python ``` # import the necessary packages import numpy as np import argparse import imutils import cv2 # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-p", "--puzzle", required = True, help = "Path to the puzzle image") ap.add_argument("-w", "--waldo", required = True, help = "Path to the waldo image") args = vars(ap.parse_args()) # load the puzzle and waldo images puzzle = cv2.imread(args["puzzle"]) waldo = cv2.imread(args["waldo"]) (waldoHeight, waldoWidth) = waldo.shape[:2] # find the waldo in the puzzle result = cv2.matchTemplate(puzzle, waldo, cv2.TM_CCOEFF) (_, _, minLoc, maxLoc) = cv2.minMaxLoc(result) ``` 我們使用`cv2.matchTemplate function`在 **Line 21** 上完成模板匹配。該方法需要三個參數。第一個是我們的`puzzle`圖像,圖像包含我們正在搜索的內容。第二個是我們的查詢圖像,`waldo`。此圖像包含在拼圖圖像中,我們希望確定其位置。最后,第三個參數是我們的模板匹秘籍法。有多種方法可以執行模板匹配,但在這種情況下,我們使用由標志`cv2.TM_CCOEFF`指定的相關系數。 那么`cv2.matchTemplate`函數究竟在做什么呢? 本質上,這個函數采用我們的`waldo`查詢圖像的“滑動窗口”,并在我們的拼圖圖像中從左到右,從上到下,一次一個像素。然后,對于這些位置中的每一個,我們計算相關系數以確定匹配的“好”或“壞”。對于我們的 waldo 模板,具有足夠高相關性的區域可以被認為是“匹配”。 從那里,我們所需要的是調用 **22 行**上的`cv2.minMaxLoc`來找到我們的“好”匹配的位置。 這就是模板匹配的全部! 實際上,它只花了我們兩行代碼。 我們的其余源代碼涉及提取包含 Waldo 的區域,然后在原始拼圖圖像中突出顯示: A How-To Guide to Template Matching with OpenCV and Python Python ``` # import the necessary packages import numpy as np import argparse import imutils import cv2 # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-p", "--puzzle", required = True, help = "Path to the puzzle image") ap.add_argument("-w", "--waldo", required = True, help = "Path to the waldo image") args = vars(ap.parse_args()) # load the puzzle and waldo images puzzle = cv2.imread(args["puzzle"]) waldo = cv2.imread(args["waldo"]) (waldoHeight, waldoWidth) = waldo.shape[:2] # find the waldo in the puzzle result = cv2.matchTemplate(puzzle, waldo, cv2.TM_CCOEFF) (_, _, minLoc, maxLoc) = cv2.minMaxLoc(result) # grab the bounding box of waldo and extract him from # the puzzle image topLeft = maxLoc botRight = (topLeft[0] + waldoWidth, topLeft[1] + waldoHeight) roi = puzzle[topLeft[1]:botRight[1], topLeft[0]:botRight[0]] # construct a darkened transparent 'layer' to darken everything # in the puzzle except for waldo mask = np.zeros(puzzle.shape, dtype = "uint8") puzzle = cv2.addWeighted(puzzle, 0.25, mask, 0.75, 0) ``` **第 26 行**根據我們的滑動窗口抓取包含最佳匹配的圖像的左上角(x,y)坐標。然后,我們根據 **Line 27** 上`waldo`圖像的寬度和高度計算右下角(x,y)坐標。最后,我們在**第 28 行**上提取`roi`(感興趣區域)。 下一步是構建一個透明層,使圖像 _ 中的所有內容變暗,但 _ Waldo。我們首先在 **Line 32** 上初始化`mask`,其形狀與填充零的拼圖相同。通過用零填充圖像,我們創建了一個充滿黑色的圖像。 為了創建透明效果,我們使用 **Line 33** 上的`cv2.addWeighted`功能。第一個參數是我們的`puzzle`圖像,第二個參數表示我們希望它占我們輸出圖像的 25%。然后我們提供`mask`作為第三個參數,使其占我們輸出圖像的 75%。通過利用`cv2.addWeighted`功能,我們已經能夠創建透明效果。 但是,我們仍需要突出 Waldo 地區!這很簡單: A How-To Guide to Template Matching with OpenCV and Python Python ``` # import the necessary packages import numpy as np import argparse import imutils import cv2 # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-p", "--puzzle", required = True, help = "Path to the puzzle image") ap.add_argument("-w", "--waldo", required = True, help = "Path to the waldo image") args = vars(ap.parse_args()) # load the puzzle and waldo images puzzle = cv2.imread(args["puzzle"]) waldo = cv2.imread(args["waldo"]) (waldoHeight, waldoWidth) = waldo.shape[:2] # find the waldo in the puzzle result = cv2.matchTemplate(puzzle, waldo, cv2.TM_CCOEFF) (_, _, minLoc, maxLoc) = cv2.minMaxLoc(result) # grab the bounding box of waldo and extract him from # the puzzle image topLeft = maxLoc botRight = (topLeft[0] + waldoWidth, topLeft[1] + waldoHeight) roi = puzzle[topLeft[1]:botRight[1], topLeft[0]:botRight[0]] # construct a darkened transparent 'layer' to darken everything # in the puzzle except for waldo mask = np.zeros(puzzle.shape, dtype = "uint8") puzzle = cv2.addWeighted(puzzle, 0.25, mask, 0.75, 0) # put the original waldo back in the image so that he is # 'brighter' than the rest of the image puzzle[topLeft[1]:botRight[1], topLeft[0]:botRight[0]] = roi # display the images cv2.imshow("Puzzle", imutils.resize(puzzle, height = 650)) cv2.imshow("Waldo", waldo) cv2.waitKey(0) ``` 在這里,我們只是使用 **Line 37** 上的一些 NumPy 陣列切片技術將 Waldo ROI 重新放回到原始圖像中。什么都沒有。 最后,**第 40-42 行**通過在屏幕上顯示我們的 Waldo 查詢和拼圖圖像并等待按鍵來顯示我們的工作結果。 要運行我們的腳本,請啟動 shell 并執行以下命令: A How-To Guide to Template Matching with OpenCV and Python Shell ``` $ python find_waldo.py --puzzle puzzle.png --waldo waldo.png ``` 當您的腳本執行完畢后,您應該在屏幕上看到類似的內容: [![We have successfully found Waldo](https://img.kancloud.cn/50/2c/502c576b750204cbaae63911a806357b_937x693.jpg)](https://3qeqpr26caki16dnhd19sv6by6v-wpengine.netdna-ssl.com/wp-content/uploads/2014/05/puzzle_found_waldo1.jpg) 圖 3:我們成功找到了 Waldo! 我們在圖像的左下角發現了 Waldo! 所以你有它! 使用 Python 和 OpenCV 進行模板匹配實際上非常簡單。首先,您只需要兩個圖像 - 要匹配的對象的圖像和包含該對象的圖像。從那里,你只需要調用 cv2.matchTemplate 和 cv2.minMaxLaoc。其余的只是將這些函數的輸出粘合在一起的包裝代碼。 ## 在單一周末學習計算機視覺 當然,我們只是在研究計算機視覺和圖像處理的表面。模板匹配只是一個開始。 幸運的是,_ 我可以在一個周末教你計算機視覺的基礎知識。_ 我知道,這聽起來很瘋狂。 但我的方法確實有效。 看,我剛剛寫完了我的新書, _Practical Python 和 OpenCV_ 。我希望這本書盡可能親自動手。我想要一些你可以輕松學習的東西,沒有與大學水平的計算機視覺和圖像處理課程相關的所有嚴謹和細節。 最重要的是,Practical Python 和 OpenCV 是**最好的,保證快速入門指南**學習計算機視覺和圖像處理的基礎知識。 另外,我已經創建了一個可下載的 Ubuntu VirtualBox 虛擬機,其中包含 OpenCV,PIL,mahotas,scikit-image,scikit-learn 以及許多其他預先配置和預安裝的計算機視覺和圖像處理庫。 所以,繼續,開始你的計算機視覺教育。 **不要浪費時間安裝包裝......投入時間學習!** 要了解有關我的新書和可下載虛擬機??的更多信息,請 **[點擊此處](https://www.pyimagesearch.com/practical-python-opencv/)** 。 **UPDATE** :繼續關于 Reddit 的[討論。](http://www.reddit.com/r/computervision/comments/25p85i/play_wheres_waldo_with_opencv_and_python/)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看