以数组形式形成轮廓
为数组求平均值
*读取照片
dev_update_window ('off')
read_image(Origin,'FPC/Pic_2017_09_29_104747_blockId#12909.bmp')
dev_close_window()
dev_open_window_fit_image(Origin,0,0,-1,-1,Handle1)
dev_display(Origin)
*手动选取ROI(1个)
*左边的mark点区域:
gen_rectangle1 (ROI_0, 999.607, 511.753, 1136.14, 651.769)
*右边的mark点区域:
*gen_rectangle1 (ROI_0, 1006.43, 2144.13, 1149.79, 2284.15)
reduce_domain(Origin,ROI_0,ImageReduced)
area_center(ROI_0, Area1, Row2, Column2)
emphasize(ImageReduced, ImageEmphasize, 7, 7, 1)
crop_domain(ImageEmphasize,ImagePart)
area_center (ImagePart, Area, Row, Column)
get_image_size(ImagePart, Width, Height)
*ImagePart 坐标变换
vector_angle_to_rigid (0, 0, 0, Row, Column, 0, HomMat2D)
vector_angle_to_rigid (0, 0, 0, Row2, Column2, 0, HomMat2D2)
*提取模板
create_shape_model(ImagePart, 10, rad(-10), rad(10), 'auto', 'auto', 'ignore_global_polarity',40,10, ModelID2)
get_shape_model_contours(ModelContours, ModelID2, 1)
select_contours_xld(ModelContours, SelectedContours, 'contour_length', 20, 20000, -0.5, 0.5)
close_contours_xld(SelectedContours,ClosedContours)
select_contours_xld(ClosedContours, SelectedContours1, 'contour_length', 200, 20000, -0.5, 0.5)
*移到中心
affine_trans_contour_xld(SelectedContours1, ContoursAffinTrans1, HomMat2D2)
dev_set_line_width (2)
dev_set_color ('green')
dev_display(Origin)
dev_display(ContoursAffinTrans1)
disp_message (Handle1, 'Model image and contours', 'window', 12, 12, 'black', 'true')
disp_message(Handle1,'Location1: '+Column2+', '+Row2, 'Origin', 200, 200, 'black', 'true')
disp_continue_message (Handle1, 'black', 'true')
dev_close_window ()
dev_open_window(0,0,Width,Height,'white',WindowHandle)
gen_image_const(Image, 'byte', Width, Height)
*第二次提取模板
affine_trans_contour_xld(SelectedContours1, ContoursAffinTran, HomMat2D)
paint_xld(ContoursAffinTran,Image, ImageResult, 255)
create_scaled_shape_model (ImageResult, 5, rad(-10), rad(10), 'auto', 0.8, 1.0, 'auto', 'none', 'ignore_global_polarity', 40, 10, ModelID)
write_shape_model(ModelID,'FPC/Model12909.shm')
dev_clear_window ()
dev_set_color ('blue')
dev_display(ImageResult)
get_shape_model_contours (ShapeModel, ModelID, 1)
disp_continue_message (Handle1, 'black', 'true')
*模板匹配
read_image(Image,'FPC/Pic_2017_09_29_105241_blockId#18797.bmp')
dev_resize_window_fit_image (Image, 0, 0, -1, -1)
dev_display (Image)
disp_message (WindowHandle, 'Search ...', 'window', 12, 12, 'black', 'true')
count_seconds (S1)
find_shape_model(Image, ModelID, rad(-5), rad(5), 0.8, 1, 0.5, 'least_squares', 0, 0.9, Row1, Column1, Angle, Score)
count_seconds(S2)
Time:=(S2-S1)*1000
disp_message (Handle1, |Score| + ' match(es) found in ' + Time$'3.1f' + ' ms', 'window', 12, 12, 'black', 'true')
********************************
vector_angle_to_rigid(0,0,0,Row1, Column1, Angle, HomMat2D1)
affine_trans_contour_xld(ShapeModel, ContoursAffinTrans, HomMat2D1)
dev_set_line_width (2)
dev_set_color ('yellow')
dev_display ( ContoursAffinTrans)
*****************************************
disp_message(WindowHandle,'Location2: '+Column1+', '+Row1, 'Origin', 200, 200, 'black', 'true')
clear_shape_model(ModelID2)
clear_shape_model(ModelID)
* ------------------------------------------------------------------------------------------------
* This example program searches for multiple instances of a security ring.
* ------------------------------------------------------------------------------------------------
* general configuration of HDevelop
dev_update_window ('off')
* image acquisition and window size
open_framegrabber ('File', 1, 1, 0, 0, 0, 0, 'default', -1, 'default', -1, 'default', 'rings/rings.seq', 'default', -1, 1, FGHandle)
grab_image (ModelImage, FGHandle)
get_image_pointer1 (ModelImage, Pointer, Type, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'white', WindowHandle)
dev_set_part (0, 0, Height - 1, Width - 1)
dev_display (ModelImage)
* colors and other settings for the visualization
dev_set_color ('blue')
dev_set_draw ('margin')
dev_set_line_width (2)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* ------------------- start of the application ----------------
* step 1: select the model object
Row := 251
Column := 196
Radius := 103
gen_circle (ModelROI, Row, Column, Radius)
dev_display (ModelROI)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* step 2: inspect the model region
reduce_domain (ModelImage, ModelROI, ImageROI)
inspect_shape_model (ImageROI, ShapeModelImage, ShapeModelRegion, 1, 30)
dev_clear_window ()
dev_display (ShapeModelRegion)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* step 3: create the model
reduce_domain (ModelImage, ModelROI, ImageROI)
create_shape_model (ImageROI, 'auto', 0, rad(360), 'auto', 'none', 'use_polarity', 30, 10, ModelID)
get_shape_model_contours (ShapeModel, ModelID, 1)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* step 4: find the object in other images
for i := 1 to 7 by 1
grab_image (SearchImage, FGHandle)
dev_display (SearchImage)
find_shape_model (SearchImage, ModelID, 0, rad(360), 0.6, 0, 0.55, 'least_squares', 0, 0.8, RowCheck, ColumnCheck, AngleCheck, Score)
for j := 0 to |Score| - 1 by 1
vector_angle_to_rigid (0, 0, 0, RowCheck[j], ColumnCheck[j], AngleCheck[j], MovementOfObject)
affine_trans_contour_xld (ShapeModel, ModelAtNewPosition, MovementOfObject)
dev_set_color ('cyan')
dev_display (ModelAtNewPosition)
dev_set_color ('blue')
affine_trans_pixel (MovementOfObject, -120, 0, RowArrowHead, ColumnArrowHead)
disp_arrow (WindowHandle, RowCheck[j], ColumnCheck[j], RowArrowHead, ColumnArrowHead, 2)
endfor
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endfor
* ------------------- end of the application -----------------
* clean up
dev_update_window ('on')
clear_shape_model (ModelID)
close_framegrabber (FGHandle)