Index:.../Applications/Measuring-2D/inspect_gasket_local_deformable.hdev
猛一看就是一個deformable的模板匹配,但裏面還有些不同。
首先就是這個變分?模型的建立
* Create variation model
sobel_amp (ModelImage, EdgeAmplitude, 'thin_max_abs', 5)
create_variation_model (425, 410, 'byte', 'direct', VariationModelID)
prepare_direct_variation_model (ModelImage, EdgeAmplitude, VariationModelID, 30, 1.5)
*
從幫助文檔看到,create_variation_model一般都是訓練一堆圖像的,但選擇 'direct'時,可以In this case, the variation model can only be trained by specifying the ideal image and the variation image directly with prepare_direct_variation_model.
並且文檔最後一句說,爲了減小變分模型的大小,先用sobel算子提取下邊緣。
prepare_direct_variation_model 這個函數就是讓模板和變分模型的句柄建立聯繫的函數。
然後往下是建立 deformable model。看來又是老一套
* Create locally deformable model
create_local_deformable_model (ModelImage, 'auto', rad(-10), rad(20), 'auto', 0.9, 1.1, 'auto', 0.9, 1.1, 'auto', 'none', 'use_polarity', 'auto', 'auto', [], [], ModelID)
get_deformable_model_contours (ModelContours, ModelID, 1)
area_center (ModelImage, Area, Row, Column)
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_translate (HomMat2DIdentity, Row, Column, HomMat2DTranslate)
affine_trans_contour_xld (ModelContours, ContoursAffinTrans, HomMat2DTranslate)
*
create_local_deformable_model 參數選擇都差不多
然後下面是畫出來deformable model的樣子,用仿射變換。
NumImages := 7
for Index := 1 to NumImages by 1
read_image (Image, 'gasket/gasket_' + Index$'02')
dev_set_window (WindowHandle2)
dev_display (Image)
disp_message (WindowHandle2, 'Search ...', 'window', 12, 12, 'black', 'true')
* Find the model in the search image.
* As result, the rectified image, the respective
* vector field, and the found contours are queried.
count_seconds (S1)
find_local_deformable_model (Image, ImageRectified, VectorField, DeformedContours, ModelID, rad(-10), rad(20), 1, 1, 1, 1, 0.93, 1, 0.7, 0, 0.4, ['image_rectified','vector_field','deformed_contours'], ['deformation_smoothness','expand_border','subpixel'],
[Smoothness,0,1], Score, Row, Column)
count_seconds (S2)
接下來就是檢測,讀取新的圖片,然後find_local_deformable_model
再往下又是老東西,gen_warped_mesh_region (VectorField, MeshRegion, Smoothness)畫那個全是格的函數
gen_region_contour_xld (DeformedContours, EdgeRegion, 'margin')
dilation_circle (EdgeRegion, RegionDilation, 2 * Smoothness)
intersection (RegionDilation, MeshRegion, RegionIntersection)
*
這就開始不同了,把匹配物品的XLD膨脹然後與生成的格做與運算。其實這些都是爲了美觀
然後 compare_variation_model (ImageRectified, Region, VariationModelID)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 30, 99999)
count_obj (SelectedRegions, Number)
compare_variation_model 這個是用來找之前用find找到的模板匹配出來的圖像與一開始創建的變分模型句柄來比較。
比較結果放在Region裏面。(草草草,我忽然發現變分模型比較和一個東西很像,那個東西叫差影法,用來提取儀表刻度的,用變分模型的話,是不是也可以提取刻度呢,比如先做deformable匹配,刻度匹配上了然後把不對的地方變分消去,然後就剩下刻度了可以用來與指針做運算。以後可以試試。)
比較結果做了一些形態學檢測,也就是不同面積多於多少的就判定爲不ok