二維位置定位--使用自適應灰度模板匹配
阿新 • • 發佈:2020-12-09
對應示例程式:
adapt_pattern.hdev
目標:使用自適應灰度模板對由影象採集裝置獲取的影象執行線上模式匹配
思路為:
1.視窗初始化
2.設定是線上視訊,還是本地圖片組成的一個視訊流。取第一幅影象為模板影象,線上視訊的話就手動畫個矩形,確定後續的匹配ROI,離線視訊的話就讀入已經設定好的引數。這一步主要是確定一個基於灰度值的匹配模板。
3.對後續的視訊影象進行匹配,然後從能匹配到的候選區域中找到最好的一個,如果最後一個最佳匹配的灰度差小於15,則為匹配建立一個新模板。其實就是從將最佳匹配作為新模板,一直迭代。
4.進行影象的顯示,以及最後的關閉控制代碼等。
影象:
*原圖:
*綠色矩形包圍的模板匹配ROI
*匹配結果
程式碼:
* *************************************************************** *這個例子展示瞭如何使用自適應灰度模板對由影象採集裝置獲取的影象執行線上模式匹配。 *如果變數“Online”設定為“true”,則影象將由open_framegrabber()中指定的影象採集裝置採集。 *否則,使用虛擬影象獲取裝置來讀取影象序列。 * *有關模板匹配的其他示例,請檢查程式pm*.hdev dev_update_off () dev_close_window () * * Open an image acquisition device * ----------------------------------------------------------------- * If Online is set to 'true' the specified image acquisition device * is opened. Otherwise a virtual image acquisition device is opened. Online := false if (Online) open_framegrabber ('GigEVision', 0, 0, 0, 0, 0, 0, 'progressive', -1, 'default', -1, 'false', 'default', 'default', 0, 0, AcqHandle) else open_framegrabber ('File', 1, 1, 0, 0, 0, 0, 'default', -1, 'default', -1, 'false', 'card/card', '', 1, 2, AcqHandle) endif * * Grab and display an image grab_image (Image, AcqHandle) get_image_size (Image, Width, Height) dev_open_window (0, 0, Width, Height, 'black', WindowHandle) dev_display (Image) * * Display settings dev_set_color ('green') dev_set_draw ('margin') dev_set_line_width (3) * * Set up if (Online) for Index := 0 to 100 by 1 grab_image (Image, AcqHandle) dev_display (Image) endfor endif * * Define a template for the pattern matching * -------------------------------------------------------- * Define a pattern for the matching which is then further * preprocessed for the template matching. * For Online = true, a pattern is generated by drawing a rectangle. * For Online = false, a default pattern is loaded. if (Online) * * Generate a pattern draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2) gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2) dev_display (Rectangle) reduce_domain (Image, Rectangle, ImageReduced) create_template (ImageReduced, 5, 4, 'sort', 'original', TemplateID) else * * Load a default pattern Row1 := 164.5 Row2 := 196.5 Column1 := 152.5 Column2 := 229.5 gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2) dev_display (Rectangle) reduce_domain (Image, Rectangle, ImageReduced) create_template (ImageReduced, 5, 4, 'sort', 'original', TemplateID) endif disp_message (WindowHandle, 'Initial template for pattern matching', 'window', 10, 10, 'black', 'true') disp_continue_message (WindowHandle, 'black', 'true') stop () * * Determine the half of template width and height TemplateHeight2 := (Row2 - Row1) / 2.0 TemplateWidth2 := (Column2 - Column1) / 2.0 * * Pattern matching * -------------------------------------------------------- * Search for best matches of the template in the image and * display the found template. If matches with small errors * were found, adapt the pattern for the next matching. for Index := 0 to 80 by 1 * * Grab and display an image for the matching if (Online) grab_image_async (Image, AcqHandle, -1) else grab_image (Image, AcqHandle) endif dev_display (Image) * * Adapt the template to the size of the image 使模板適應影象的大小 adapt_template (Image, TemplateID) * * Search all matches of the template in the image and * return all points showing an error smaller than 20 *搜尋影象中模板的所有匹配項,並返回所有顯示誤差小於20的點 fast_match_mg (Image, Matches, TemplateID, 20, 3) area_center (Matches, Area, Row, Column) if (Area > 0) dilation_circle (Matches, MergedMatches, 3.5) connection (MergedMatches, ConnMatches) add_channels (ConnMatches, Image, ImageMatches) * * Search the best match of the template and display * the found template *搜尋模板的最佳匹配並顯示找到的模板 best_match (ImageMatches, TemplateID, 20, 'true', Row, Column, Error) NumMatches := |Row| BestMatchIndex := -1 BestMatchError := 255 for I := 0 to NumMatches - 1 by 1 if (Error[I] < 255) disp_message (WindowHandle, 'Template found', 'window', 10, 10, 'black', 'true') disp_rectangle1 (WindowHandle, Row[I] - TemplateHeight2, Column[I] - TemplateWidth2, Row[I] + TemplateHeight2, Column[I] + TemplateWidth2) BestMatchIndex := I BestMatchError := Error[I] endif endfor * * Adapt the pattern * -------------------------------------------------------- * Create a new template for the matching if the gray value * difference of the last best match is smaller than 15 *調整模式 * 如果最後一個最佳匹配的灰度差小於15,則為匹配建立一個新模板 if (BestMatchIndex >= 0) if (BestMatchError < 15) clear_template (TemplateID) gen_rectangle1 (Rectangle, Row[BestMatchIndex] - TemplateHeight2, Column[BestMatchIndex] - TemplateWidth2, Row[BestMatchIndex] + TemplateHeight2, Column[BestMatchIndex] + TemplateWidth2) reduce_domain (Image, Rectangle, ImageReduced) create_template (ImageReduced, 5, 4, 'sort', 'original', TemplateID) endif endif endif endfor clear_template (TemplateID) close_framegrabber (AcqHandle)
用到的幾個運算元:
create_template --建立一個灰度值匹配的模板
adapt_template–使模板適應影象的大小
fast_match_mg–查詢匹配影象
best_match–搜尋模板的最佳匹配