觸發式影像擷取#
若未啟用觸發功能,相機將自由運轉,導致畫面可能錯過關鍵時刻;而啟用觸發功能後,外部事件會觸發相機,使相機在恰到好處的瞬間捕捉到拍攝對象。
這可確保以下事項:
- 影像與現實世界同步。
- 測量結果具有可重複性。
- 系統的行為具有決定性。
可作為觸發條件的外部事件範例:
- 移動物體抵達特定位置
- 感測器偵測零件
- 自動化系統中的 PLC 訊號
- 閃光脈衝或閃光事件
此視覺化圖表概述了一個典型的作業流程:
快門按鈕會觸發相機開始曝光。影像會從感光元件讀取出來,經過預處理後,再傳輸至主機。最後,影像會傳遞給應用程式。
典型應用案例#
請在以下情況下使用觸發式擷取:
- 時機至關重要:影像必須與實際事件完全吻合,例如在高速分揀的應用中。
- 動作必須精確地凍結:您希望將快速移動的物體定格在同一位置,例如用於輸送帶的檢測。
- 外部系統負責控制此流程:可程式邏輯控制器(PLC)、感測器、編碼器或其他硬體會決定何時進行擷取,例如在機器人應用中。
- 必須具備確定性行為:每個觸發都會產生一個具有明確時序的幀,例如用於測量系統。
軟體觸發器#
軟體觸發功能可讓您透過軟體呼叫來啟動曝光,而非仰賴外部硬體事件。若觸發來源並非硬體事件,而是運算系統的狀態變化,此功能便相當實用。其精準度通常與硬體觸發相當,但在許多情況下已足夠使用。其主要優勢在於,這比將運算系統的輸出連接至相機(或多台相機)來得更為簡便。
camera.TriggerSelector.Value = "FrameStart"
camera.TriggerMode.Value = "On"
camera.TriggerSource .Value = "Software"
camera.ExecuteSoftwareTrigger()
範例:週期性軟體觸發(1 Hz)#
以下範例使用軟體觸發器,每秒觸發一次影像曝光。
import time
from pypylon import pylon
def process(image):
h, w = image.shape[:2]
center_pixel = image[h // 2, w // 2]
print("Center pixel:", center_pixel)
with pylon.InstantCamera(pylon.FirstFound) as camera:
# Open is necessary before accessing the parameters.
camera.Open()
# Enable trigger mode.
camera.TriggerSelector.Value = "FrameStart"
camera.TriggerMode.Value = "On"
camera.TriggerSource.Value = "Software"
camera.StartGrabbing(pylon.GrabStrategy_OneByOne)
while camera.IsGrabbing():
camera.ExecuteSoftwareTrigger()
with camera.RetrieveResult(2000) as grab_result:
if grab_result.GrabSucceeded():
image = grab_result.Array
process(image)
else:
print("Grab failed:", grab_result.ErrorDescription)
time.sleep(1.0)
硬體觸發#
如果軟體觸發器對於您的使用情境而言不夠精確,或者已經有硬體訊號可用,那麼使用硬體觸發器是最佳選擇。
在此情況下,視相機型號而定,通常會提供一個或多個可用輸入端子,這些端子可作為觸發輸入使用。透過上升沿或下降沿,即可觸發特定事件,例如啟動相機的曝光。
camera.TriggerSource.Value = "Line1"
camera.TriggerSelector.Value = "FrameStart"
camera.TriggerActivation.Value = "RisingEdge"
範例:硬體觸發資料擷取#
在以下範例中,會使用第一個找到的攝影機的輸入通道 1 來啟動曝光,並輸出中心像素。
from pypylon import pylon
def process(image):
height, width = image.shape[:2]
center_pixel = image[height // 2, width // 2]
print("Center pixel:", center_pixel)
with pylon.InstantCamera(pylon.FirstFound) as camera:
# Open is necessary before accessing the parameters.
camera.Open()
# Configure hardware trigger
camera.TriggerSelector.Value = "FrameStart"
camera.TriggerMode.Value = "On"
camera.TriggerActivation.Value = "RisingEdge"
camera.TriggerSource.Value = "Line1"
camera.StartGrabbing(pylon.GrabStrategy_OneByOne)
while camera.IsGrabbing():
with camera.RetrieveResult(5000) as grab_result:
if grab_result.GrabSucceeded():
image = grab_result.Array
process(image)
else:
print("Grab failed:", grab_result.ErrorDescription)