***** dev_display_ncc_matching_results (ModelIds, ClassColors, Row, Column, Angle, Model)
*
NumMatches := |Row|
if (NumMatches > 0)
if (|Model| == 0)
tuple_gen_const (NumMatches, 0, Model)
elseif (|Model| == 1)
tuple_gen_const (NumMatches, Model, Model)
endif
for Index := 0 to |ModelID| - 1 by 1
get_ncc_model_region (ModelRegion, ModelID[Index])
gen_contour_region_xld (ModelRegion, ModelContours, 'border_holes')
dev_set_color (Color[Index % |Color|])
for Match := 0 to NumMatches - 1 by 1
if (Index == Model[Match])
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_rotate (HomMat2DIdentity, Angle[Match], 0, 0, HomMat2DRotate)
hom_mat2d_translate (HomMat2DRotate, Row[Match], Column[Match], HomMat2DTranslate)
affine_trans_contour_xld (ModelContours, ContoursAffinTrans, HomMat2DTranslate)
dev_display (ContoursAffinTrans)
affine_trans_pixel (HomMat2DTranslate, 0, 0, RowTrans, ColTrans)
gen_cross_contour_xld (Cross, RowTrans, ColTrans, 6, Angle[Match])
dev_display (Cross)
endif
endfor
endfor
endif
return ()
***analyze_result (ModelIds, Model, ClassLabels, Text, TextColors)
CableTypeFound := false
for I := 0 to |ModelIds| - 1 by 1
* 类型1、2确定
if (I < 2)
if (CableTypeFound)
continue
endif
if (find(Model,I) != -1)
Text := 'Cable is of ' + ClassLabels[I]
TextColors := 'black'
CableTypeFound := true
else
Text := '没找到此类型'
TextColors := 'red'
endif
else
*标签1、2、3、4确定
if (find(Model,I) == -1)
Text := [Text,ClassLabels[I] + ' 标签没有 ']
TextColors := [TextColors,'red']
else
Text := [Text,ClassLabels[I] + ' 标签存在']
TextColors := [TextColors,'black']
endif
endif
endfor
return ()
****show_result_example (ImageClass1, ImageClass2, ClassColors, WindowHandle)****
*********************************************************************************
get_image_size (ImageClass1, Width, Height)
*1、剪切一个矩形图像区域
crop_part (ImageClass1, ImagePart, Height/4, 0, Width, Height/2)
crop_part (ImageClass2, ImagePart1, Height/4, 0, Width, Height/2)
*2、关联两部分,做成数组
concat_obj (ImagePart, ImagePart1, ObjectsConcat)
****************************************
*tile_images_offset(Images : TiledImage : OffsetRow, OffsetCol, Row1, Col1, Row2, Col2, Width, Height : )
*其中OffsetRow, OffsetCol用来定义拼接后的图片在整张大图中的左上角位置;
*Row1,Col1,Row2,Col2用来定义被拼接的图片再次被裁剪的像素位置,分别是左上角和右下角坐标,如果不裁剪的话保持参数为-1;
*Row1,Col1,Row2,Col2是相对于offset后的位置为起点的相对值
*需要注意的是,上面六个参数需要和被拼接的图片数量保持一致。比如Images中有三张图,那么Row1的定义应该是如下形式:
*[-1,-1,-1] 或者 [-1, 200, 100]
*Width和Height用来定义拼接完成后的图片大小。
*****************************************
*3、图像数组内容拼接
tile_images_offset (ObjectsConcat, TiledImage, [0,Height/2], [0,0], [0,0], [0,0], [Height/2,Height/2], [Width,Width], Width, Height)
*4、显示
dev_display (TiledImage)
dev_set_color (ClassColors[0])
gen_rectangle1 (ROI_0, 314, 418, 370, 553)
dev_display (ROI_0)
dev_set_color (ClassColors[1])
gen_rectangle1 (ROI_1, 757, 267, 805, 427)
dev_display (ROI_1)
dev_set_color ('red')
gen_rectangle1 (ROI_2, 325, 187, 385, 364)
gen_rectangle1 (ROI_3, 308, 600, 368, 753)
gen_rectangle1 (ROI_4, 306, 810, 357, 946)
gen_rectangle1 (ROI_5, 299, 1003, 354, 1098)
dev_display (ROI_2)
dev_display (ROI_3)
dev_display (ROI_4)
dev_display (ROI_5)
dev_disp_text (['演示结果','如下:'], 'window', 'top', 'left', 'black', [], [])
return ()
dev_update_off ()
read_image (ImageClass1, 'cable_labels_01')
read_image (ImageClass2, 'cable_labels_02')
get_image_size (ImageClass1, Width, Height)
* Configure the graphics window.
dev_close_window ()
dev_open_window_fit_image (ImageClass1, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (2)
* 定义颜色与类型标签.
ClassLabels := ['type 1','type 2','STYLE','80°C','30V','VW-1']
ClassColors := ['green','blue','cyan','magenta','yellow','coral']
* 封装函数:展示一个解释要解决的任务的可视化效果
show_result_example (ImageClass1, ImageClass2, ClassColors, WindowHandle)
stop ()
*变量预定义
ClassIds := []
ModelIds := []
LabelIds := []
AngleStart := rad(-5)
AngleExtent := rad(10)
*类型一建模
dev_display (ImageClass1)
gen_rectangle1 (ROIClass1, 573.0, 417.6, 620.7, 543.9)
reduce_domain (ImageClass1, ROIClass1, ImageReduced)
create_ncc_model (ImageReduced, 'auto', AngleStart, AngleExtent, 'auto', 'use_polarity', ModelID)
ModelIds := [ModelIds,ModelID]
dev_set_color (ClassColors[0])
dev_display (ROIClass1)
dev_disp_text ('类型1:', 'window', 'top', 'left', 'black', [], [])
stop ()
*类型二建模
dev_display (ImageClass2)
gen_rectangle1 (ROIClass2, 498.7, 263.2, 544.8, 428.7)
reduce_domain (ImageClass2, ROIClass2, ImageReduced)
create_ncc_model (ImageReduced, 'auto', AngleStart, AngleExtent, 'auto', 'use_polarity', ModelID)
ModelIds := [ModelIds,ModelID]
dev_set_color (ClassColors[1])
dev_display (ROIClass2)
dev_disp_text ('类型2:', 'window', 'top', 'left', 'black', [], [])
stop ()
*类型二中的其他4个标签建模
dev_display (ImageClass2)
gen_rectangle1 (ROILabel1, 495.3, 49.8, 544.8, 215.4)
gen_rectangle1 (ROILabel2, 494.4, 489.3, 544.0, 615.6)
gen_rectangle1 (ROILabel3, 489.3, 680.5, 533.7, 777.7)
gen_rectangle1 (ROILabel4, 479.0, 842.6, 535.4, 999.6)
*连接两个对象(变成对象的数组)
concat_obj (ROILabel1, ROILabel2, ROILabels)
concat_obj (ROILabels, ROILabel3, ROILabels)
concat_obj (ROILabels, ROILabel4, ROILabels)
count_obj (ROILabels, NumberLabels)
for Index := 1 to NumberLabels by 1
select_obj (ROILabels, ObjectSelected, Index)
reduce_domain (ImageClass2, ObjectSelected, ImageReduced)
create_ncc_model (ImageReduced, 'auto', AngleStart, AngleExtent, 'auto', 'use_polarity', ModelID)
get_ncc_model_params (ModelID, NumLevels, AngleStart1, AngleExtent, AngleStep, Metric)
ModelIds := [ModelIds,ModelID]
dev_set_color (ClassColors[1 + Index])
dev_display (ObjectSelected)
endfor
dev_disp_text (['标签1:'+ClassLabels[2],'标签2:'+ClassLabels[3],'标签3:'+ClassColors[4],'标签4:'+ClassLabels[5]], 'window', 'top', 'left', 'black', [], [])
stop()
CableTypeFound := false
tuple_gen_const (|ModelIds|, 1, NumMatches)
NumImages := 5
Loops := 20
T1 := 0
T2 := 0
for Index := 1 to NumImages by 1
read_image (Image2, 'label/cable_labels_' + Index$'02')
* To speed up the search, reduce the domain in which the models are searched.
threshold (Image2, Region, 60, 150)
fill_up (Region, RegionFillUp)
reduce_domain (Image2, RegionFillUp, ImageReduced)
* find_ncc_model识别 及 耗费时间统计
count_seconds (Start)
for I := 1 to Loops by 1
for M := 0 to |ModelIds| - 1 by 1
find_ncc_model (ImageReduced, ModelIds[M], AngleStart, AngleExtent, 0.7, 1, 0.5, 'true', 0, Row1, Column1, Angle1, Score1)
endfor
endfor
count_seconds (End)
TimeSingle := (End - Start) / Loops * 1000
T1 := T1 + TimeSingle
count_seconds (Start)
* find_ncc_models识别 及 耗费时间统计
for I := 1 to Loops by 1
find_ncc_models (ImageReduced, ModelIds, AngleStart, AngleExtent, 0.7, NumMatches, 0.5, 'true', 0, Row, Column, Angle, Score, Model)
endfor
count_seconds (End)
TimeAll := (End - Start) / Loops * 1000
T2 := T2 + TimeAll
*确定图像中显示的电缆类型,并报告找到的标签(封装函数)
analyze_result (ModelIds, Model, ClassLabels, Text, TextColors)
dev_display (Image2)
* 显示查找结果
dev_disp_text ('Results for image ' + Index + ' :', 'window', 'top', 'left', 'black', [], [])
dev_disp_text (Text, 'window', 60, 'left', TextColors, [], [])
*用类别的颜色,显示寻找到的对象类别(封装函数)
dev_display_ncc_matching_results (ModelIds, ClassColors, Row, Column, Angle, Model)
dev_disp_text (['1x find_ncc_models:' + TimeAll$'5.2f' + ' ms','5x find_ncc_model:' + TimeSingle$'6.2f' + ' ms'], 'window', 'top', 'right', 'black', ['box_color','shadow'], ['#fef5ec','false'])
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
dump_window (WindowHandle, 'png', '结果:image'+Index)
stop ()
endfor
dump_window_image (Image, WindowHandle)
gauss_filter (Image, ImageGauss, 11)
dev_display (ImageGauss)
set_display_font (WindowHandle, 24, 'mono', 'true', 'false')
dev_disp_text (['A Total time find_ncc_models:' + T2$'6.2f' + ' ms','B Total time find_ncc_model:' + T1$'7.2f' + ' ms','Speedup: (B-A)/A = ' + ((T1 / T2 - 1) * 100)$'13.0f' + ' %'], 'window', 'center', 'center', 'black', ['box_color','shadow'], ['red','false'])
dump_window (WindowHandle, 'png', '效率比较结果:')