结合模板匹配与测量的剃须刀缺陷检测

news/2024/11/15 2:13:24/

模板匹配与测量

  • 原图
    • 模板图
    • 检测图
  • 代码
  • 结果

原图

模板图

在这里插入图片描述

检测图

在这里插入图片描述

代码

* ------------------------------------------------------------------------------------------------
*此示例程序使用基于形状的匹配来对齐度量的roi
*工具,然后检查单个刀片。*该程序有两种运行模式:(1)全仿射变换
*2)采用平移测度
*修改下一行以在模式之间切换。*该参数控制是否使用translate_measure
USING_TRANSLATE_MEASURE := 1
* ------------------------------------------------------------------------------------------------dev_update_window ('off')
read_image (ModelImage, 'razors1')
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)
*显示设置
dev_set_color ('cyan')
dev_set_draw ('margin')
dev_set_line_width (2)
stop ()
* -------------------  start of the application  ----------------
* 模板对象确定
Row1 := 46
Column1 := 57
Row2 := 79
Column2 := 94
gen_rectangle1 (ROIPart1, Row1, Column1, Row2, Column2)
gen_rectangle1 (ROIPart2, Row1 + 364, Column1 + 13, Row2 + 364, Column2 + 13)
union2 (ROIPart1, ROIPart2, ModelROI)
area_center (ModelROI, Area, CenterROIRow, CenterROIColumn)
dev_display (ModelImage)
dev_display (ModelROI)
stop ()
* 建模
reduce_domain (ModelImage, ModelROI, ImageROI)
create_shape_model (ImageROI, 4, 0, 0, 'auto', 'none', 'use_polarity', 30, 10, ModelID)
inspect_shape_model (ImageROI, ShapeModelImage, ShapeModelRegion, 1, 30)
*通过get_shape_model_contours可知建立的模型中心在(0,0)处
get_shape_model_contours (ShapeModel, ModelID, 1)dev_clear_window ()
dev_set_color ('blue')
dev_display (ShapeModelRegion)
stop ()
* 测量ROI区域确定
Rect1Row := 244
Rect1Col := 73
DistColRect1Rect2 := 17
Rect2Row := Rect1Row
Rect2Col := Rect1Col + DistColRect1Rect2
RectPhi := rad(90)
RectLength1 := 122
RectLength2 := 2
gen_rectangle2 (MeasureROI1, Rect1Row, Rect1Col, RectPhi, RectLength1, RectLength2)
gen_rectangle2 (MeasureROI2, Rect2Row, Rect2Col, RectPhi, RectLength1, RectLength2)
dev_display (ModelImage)
dev_set_color ('yellow')
dev_display (MeasureROI1)
dev_display (MeasureROI2)
***************************************************
**方法一:建模默认原点仿射变换时,平移模型区域中心到原点
*通过变量OriginalClipRegion保留原始参数属性,'clip_region'是否裁剪
* get_system ('clip_region', OriginalClipRegion)
* set_system ('clip_region', 'false')
*把建模的区域的中心移到0,0点,此时角度为0。以便后面的仿射变换
* move_region (MeasureROI1, MeasureROI1Ref, -CenterROIRow, -CenterROIColumn)
* move_region (MeasureROI2, MeasureROI2Ref, -CenterROIRow, -CenterROIColumn)
* set_system ('clip_region', OriginalClipRegion)* DistRect1CenterRow := Rect1Row - CenterROIRow
* DistRect1CenterCol := Rect1Col - CenterROIColumn
* DistRect2CenterRow := Rect2Row - CenterROIRow
* DistRect2CenterCol := Rect2Col - CenterROIColumn
****************************************************
if (USING_TRANSLATE_MEASURE != 0)*测量对象生成一次,后续可以使用translate_measure继续使用gen_measure_rectangle2 (Rect1Row, Rect1Col, RectPhi, RectLength1, RectLength2, Width, Height, 'bilinear', MeasureHandle1)gen_measure_rectangle2 (Rect2Row, Rect2Col, RectPhi, RectLength1, RectLength2, Width, Height, 'bilinear', MeasureHandle2)
endif
stop ()
* find_shape_model模板匹配
read_image (SearchImage, 'razors2')
dev_display (SearchImage)
find_shape_model (SearchImage, ModelID, 0, 0, 0.8, 0, 0.5, 'least_squares', 0, 0.7, RowCheck, ColumnCheck, AngleCheck, Score)
if (|Score| > 0)for i := 0 to |Score| - 1 by 1* 方法一:建模默认原点仿射变换*vector_angle_to_rigid (0, 0, 0, RowCheck[i], ColumnCheck[i], AngleCheck[i], MovementOfObject)* affine_trans_contour_xld (ShapeModel, ModelAtNewPosition, MovementOfObject)*方法二:建模区域不回原点,使用监视模型  vector_angle_to_rigid (CenterROIRow, CenterROIColumn, 0, RowCheck[i], ColumnCheck[i], AngleCheck[i], MovementOfObject)       affine_trans_region (ShapeModelRegion, ModelAtNewPosition, MovementOfObject, 'nearest_neighbor')dev_display (ModelAtNewPosition)* 方法二:建模区域不回原点 affine_trans_region (MeasureROI1, MeasureROI1AtNewPosition, MovementOfObject, 'constant')affine_trans_region (MeasureROI2, MeasureROI2AtNewPosition, MovementOfObject, 'constant')*方法一:建模默认原点仿射变换* affine_trans_region (MeasureROI1Ref, MeasureROI1AtNewPosition, MovementOfObject, 'constant')* affine_trans_region (MeasureROI2Ref, MeasureROI2AtNewPosition, MovementOfObject, 'constant')dev_display (MeasureROI1AtNewPosition)dev_display (MeasureROI2AtNewPosition)*方法一:建模默认原点仿射变换
*         affine_trans_pixel (MovementOfObject, DistRect1CenterRow, DistRect1CenterCol, Rect1RowCheck, Rect1ColCheck)
*         affine_trans_pixel (MovementOfObject, DistRect2CenterRow, DistRect2CenterCol, Rect2RowCheck, Rect2ColCheck)* 方法二:建模区域不回原点 affine_trans_pixel (MovementOfObject, Rect1Row, Rect1Col, Rect1RowCheck, Rect1ColCheck)affine_trans_pixel (MovementOfObject, Rect2Row, Rect2Col, Rect2RowCheck, Rect2ColCheck)if (USING_TRANSLATE_MEASURE != 0)* -> translate the already created measure objects*前面已经生成了测量对象,直接转换测量translate_measure (MeasureHandle1, Rect1RowCheck, Rect1ColCheck)translate_measure (MeasureHandle2, Rect2RowCheck, Rect2ColCheck)measure_pairs (SearchImage, MeasureHandle1, 2, 25, 'negative', 'all', RowEdge11, ColEdge11, Amp11, RowEdge21, ColEdge21, Amp21, Width1, Distance1)measure_pairs (SearchImage, MeasureHandle2, 2, 25, 'negative', 'all', RowEdge12, ColEdge12, Amp12, RowEdge22, ColEdge22, Amp22, Width2, Distance2)else* -> create new measure objects and destroy them after the measurement*每次测量生成测量对象RectPhiCheck := RectPhi + AngleCheck[i]gen_measure_rectangle2 (Rect1RowCheck, Rect1ColCheck, RectPhiCheck, RectLength1, RectLength2, Width, Height, 'bilinear', MeasureHandle1)gen_measure_rectangle2 (Rect2RowCheck, Rect2ColCheck, RectPhiCheck, RectLength1, RectLength2, Width, Height, 'bilinear', MeasureHandle2)* step 5: perform the measurementmeasure_pairs (SearchImage, MeasureHandle1, 2, 25, 'negative', 'all', RowEdge11, ColEdge11, Amp11, RowEdge21, ColEdge21, Amp21, Width1, Distance1)measure_pairs (SearchImage, MeasureHandle2, 2, 25, 'negative', 'all', RowEdge12, ColEdge12, Amp12, RowEdge22, ColEdge22, Amp22, Width2, Distance2)close_measure (MeasureHandle1)close_measure (MeasureHandle2)endif* step 6: check for too short or missing teethNumberTeeth1 := |Width1|NumberTeeth2 := |Width2|dev_set_color ('red')*判断测量的边缘对数量是否不到37if (NumberTeeth1 < 37)for j := 0 to NumberTeeth1 - 2 by 1*如果边缘对宽度大于4,那么框选该部分并设置该部分区域在新窗口显示if (Distance1[j] > 4.0)RowFault := round(0.5 * (RowEdge11[j + 1] + RowEdge21[j]))ColFault := round(0.5 * (ColEdge11[j + 1] + ColEdge21[j]))disp_rectangle2 (WindowHandle, RowFault, ColFault, 0, 4, 4)dev_open_window (0, Width + 20, 80, 80, 'black', WindowHandleZoom)dev_set_part (RowFault - 10, ColFault - 10, RowFault + 10, ColFault + 10)dev_display (SearchImage)disp_rectangle2 (WindowHandleZoom, RowFault, ColFault, 0, 4, 4)stop ()dev_close_window ()dev_set_part (0, 0, Height - 1, Width - 1)endifendforendifif (NumberTeeth2 < 37)for j := 0 to NumberTeeth2 - 2 by 1if (Distance2[j] > 4.0)RowFault := round(0.5 * (RowEdge12[j + 1] + RowEdge22[j]))ColFault := round(0.5 * (ColEdge12[j + 1] + ColEdge22[j]))disp_rectangle2 (WindowHandle, RowFault, ColFault, 0, 4, 4)dev_open_window (0, Width + 20, 80, 80, 'black', WindowHandleZoom)dev_set_part (RowFault - 10, ColFault - 10, RowFault + 10, ColFault + 10)dev_display (SearchImage)disp_rectangle2 (WindowHandleZoom, RowFault, ColFault, 0, 4, 4)stop ()dev_close_window ()dev_set_part (0, 0, Height - 1, Width - 1)endifendforendifdev_set_color ('yellow')stop ()endfor
endif
* -------------------  end of the application  -----------------
* clean up
if (USING_TRANSLATE_MEASURE != 0)close_measure (MeasureHandle1)close_measure (MeasureHandle2)
endif
dev_update_window ('on')
clear_shape_model (ModelID)
dump_window (WindowHandle, 'png', '识别检测结果')

结果

在这里插入图片描述


http://www.ppmy.cn/news/361552.html

相关文章

Linux系统下安装Kubernetes(超详细。。。)

一、安装Kubernetes前的准备 1.1 准备Hosts文件 &#xff08;注意&#xff0c;请根据Linux虚拟机的IP地址&#xff0c;修改以下命令后再执行&#xff09; cat >>/etc/hosts<<EOF 192.168.100.146 deploy EOF 1.2 检查虚拟机的hostname cat /etc/hostname验证…

python写入excel,(二) 言简意赅傻瓜式写法

xlrd限制条数&#xff0c;openpyxl 上限较高&#xff0c;所以推荐这种写法 import openpyxl # openpyxl引入模块 def write_to_excel(path: str, sheetStr, info, data): # 实例化一个workbook对象 workbook openpyxl.Workbook() # 激活一个sheet …

Linux从大文件中截取指定范围的内容到文件中

在实际开发与维护中&#xff0c;可能会遇到需要从好几个G大的文本文件中获取包含指定文本的内容&#xff0c; 例如&#xff0c;需要从某天的系统日志文件里获取指定时间范围的日志&#xff0c;而这个日志文件有几个个G,在Windows中用文本编辑器打开会出现内存不足的情况&#…

领域建模之数据模型设计方法论 | 京东云技术团队

本文通过实际业务需求场景建模案例&#xff0c;为读者提供一种业务模型向数据模型设计的方法论&#xff0c;用于指导实际开发中如何进行业务模型向数据模型转化抽象&#xff0c;并对设计的数据模型可用性、扩展性提供了建议性思考。通过文章&#xff0c;读者可以收获到业务模型…

IT安全解决方案保护企业网络安全

全球每年报告数以万计的网络安全事件&#xff0c;云解决方案和远程工作的大规模采用意味着大多数组织的攻击面呈指数级增长。采用正确的网络安全解决方案是保护企业免受这些攻击的唯一方法。使用正确IT安全解决方案企业网络安全。 IT安全解决方案 ManageEngine IT安全解决方案…

JVM 调优分析 如何进行JVM调优

文章目录 1.为什么需要进行JVM调优&#xff1f;2.什么情况下可能需要JVM调优3.JVM调优参数4.JVM调优参数设置参考5.JVM内部结构1. 类加载器&#xff08;Class Loader&#xff09;2. 运行时数据区&#xff08;Runtime Data Area&#xff09;3. 垃圾收集器&#xff08;Garbage Co…

java常用api篇【更新中】

13-常用api Math abs&#xff0c;绝对值ceil&#xff0c;向上取整floor&#xff0c;向下取整round&#xff0c;四舍五入max&#xff0c;获取两个int中的较大值min&#xff0c;获取两个int中的较小值random&#xff0c;返回值为double的随机数sqrt&#xff0c;开平方根cqrt&am…

windows下创建指定版本的python虚拟环境

本机已安装3.9和3.7版本的python&#xff0c;想创建python3.6版本的虚拟环境。原本使用指令virtualenv -p D:\Python36\python.exe venv36&#xff0c;出现下面的错误&#xff1a; 使用该指令创建3.7和3.9版本的python虚拟环境都是可以的&#xff0c;请教同事后改用下面的指令创…