第三十七章 image圖像對比實驗
在上一章節中,介紹了image模塊中圖像色塊追蹤方法給的使用,本章將繼續介紹image模塊中圖像對比方法的使用。通過本章的學習,讀者將學習到image模塊中圖像對比的使用。
本章分為如下幾個小節:
37.1 image模塊圖像對比方法介紹
37.2 硬件設計
37.3 程序設計
37.4 運行驗證
37.1 image模塊圖像對比方法介紹
image模塊為Image對象提供了difference()方法,用于計算兩個圖像的差值絕對值,difference()方法如下所示:
image.difference(image, mask)
difference()方法計算兩個圖像的差值的絕對值,并返回一個image對象,返回的圖像中較暗的部分,即兩個對比圖像差別不大的部分,返回圖像中較亮的部分,即兩個對比圖像中相差較大的部分。
image指的是與image對象比較的另一個image對象。
mask指的是另一個用作繪圖操作的像素級掩碼的圖像,掩碼應該是一個只有黑色和白色像素的圖像,并且因該與所處理的Image對象具有相同的大小,僅有掩碼中設置的像素會被修改。
difference()方法會返回經過處理的Image對象。
difference()方法的使用示例如下所示:
import image
img1 = image.Image(size=(320, 240))
img2 = img1.copy()
img1.difference(img2)
image模塊為Image對象提供了get_similarity()方法,用于計算兩個圖像之間的相似程度,get_similarity()方法如下所示:
image.get_similaraity(image)
get_similarity()方法用于使用SSIM算法計算兩個圖像之間的8*8像素色塊的相似度,并會返回一個similarity對象。
image指的是與image對象進行計算的另一個image對象。
get_similarity()方法會返回一個similarity對象。
get_similarity()方法的使用示例如下所示:
import image
img1 = image.Image(size=(320, 240))
img2 = img1.copy()
sim = img1.get_similarity(img2)
37.2 硬件設計
37.2.1 例程功能
1. 不斷地獲取攝像頭輸出的圖像,并保存相鄰的兩幀圖像,對這兩幀圖像使用差幀算法或SSIM算法進行圖像對比,并將對比結果和當前獲取到的攝像頭圖像一起在LCD上進行顯示。
2. KEY0按鍵可以切換圖像對比時使用的算法。
37.2.2 硬件資源
本章實驗內容,主要講解image模塊的使用,無需關注硬件資源。
37.2.3 原理圖
本章實驗內容,主要講解image模塊的使用,無需關注原理圖。
37.3 程序設計
37.3.1 image模塊圖像對比方法介紹
有關image模塊圖像對比方法的介紹,請見第37.1小節《image模塊圖像對比方法介紹》。
37.3.2 程序流程圖
37.3.3 main.py代碼
main.py中的腳本代碼如下所示:
from board import board_info
from fpioa_manager import fm
from maix import GPIO
import time
import lcd
import sensor
import image
import gc
lcd.init()
sensor.reset()
sensor.set_framesize(sensor.QVGA)
sensor.set_pixformat(sensor.RGB565)
sensor.set_hmirror(False)
type = 0
type_dict = {
? ? 0: "Normal",
? ? 1: "Frame",
? ? 2: "SSIM"
}
fm.register(board_info.KEY0, fm.fpioa.GPIOHS0)
key0 = GPIO(GPIO.GPIOHS0, GPIO.IN, GPIO.PULL_UP)
def key_irq_handler(key):
? ? global key0
? ? global type
? ?time.sleep_ms(20)
? ? if key is key0 and key.value() == 0:
? ?? ? type = type + 1
? ?? ? if type == len(type_dict):
? ?? ?? ???type = 0
key0.irq(key_irq_handler, GPIO.IRQ_FALLING, GPIO.WAKEUP_NOT_SUPPORT, 7)
while True:
? ? img= sensor.snapshot()
? ? if type == 0:
? ?? ? # 原圖
? ?? ? pass
? ? elif type == 1:
? ?? ? # 差幀算法
? ?? ? threshold = 5
? ?? ? if "prev" not in dir():
? ?? ?? ???prev = img.copy()
? ?? ? prev.difference(img)
? ?? ? hist = prev.histogram()
? ?? ? diff = hist.get_percentile(0.99).l_value() - hist.get_percentile(0.90).l_value()
? ?? ? img.draw_string(10, 30, "Different" if diff > threshold else "Same", color=(255, 0, 0), scale=1.6)
? ?? ? prev = img.copy()
? ? elif type == 2:
? ?? ? # SSIM算法
? ?? ? threshold = -0.4
? ?? ? if "prev" not in dir():
? ?? ?? ???prev = img.copy()
? ?? ? sim = prev.get_similarity(img)
? ?? ? img.draw_string(10, 30, "Different" if sim.min() < threshold else "Same", color=(255, 0, 0), scale=1.6)
? ? else:
? ?? ? type = 0
? ? img.draw_string(10, 10, type_dict[type], color=(255, 0, 0), scale=1.6)
? ? lcd.display(img)
? ? gc.collect()
可以看到一開始是先初始化了LCD、攝像頭和中斷按鍵,并且按下中斷按鍵可以切換圖像對比時使用的對比算法。
接著在一個循環中不斷地獲取攝像頭輸出的圖像,因為獲取到的圖像就是Image對象,因此可以直接調用image模塊為Image對象提供的各種方法,然后就是對當前圖像與上一幀圖像進行圖像對比,最后在LCD顯示圖像以及圖像對比結果。
37.4 運行驗證
將DNK210開發板連接CanMV IDE,點擊CanMV IDE上的“開始(運行腳本)”按鈕后,按下KEY0按鍵能夠切換圖像對比時使用的對比算法,圖像對比完成后會在LCD上顯示對比結果,如果前后兩幀圖像相似度較高,則LCD上顯示“Same”,如果前后兩幀圖像有點差異,則LCD上顯示“Different”。