halcom-极限坐标转换
* 检测瓶口缺陷 * * * tuning parameters SmoothX := 501 ThresholdOffset := 25 MinDefectSize := 50 * * initialization PolarResolution := 640 RingSize := 70 *获取存放空区域的状态 get_system ('store_empty_region', StoreEmptyRegion) *设置存放空区域的状态 set_system ('store_empty_region', 'false') *读取图像 read_image (Image, 'bottles/bottle_mouth_01') *关闭程序计数器,变量更新,图形窗口更新 dev_update_off () *关闭窗口 dev_close_window () dev_close_window () *按图像大小创建一个新窗口 dev_open_window_fit_image (Image, 0, 0, 640, 512, WindowHandle1) *设置字体信息 set_display_font (WindowHandle1, 16, 'mono', 'true', 'false') *显示图像 dev_display (Image) *设置区域填充方式 dev_set_draw ('margin') *设置输出对象线宽 dev_set_line_width (3) *按图像大小创建一个新窗口 dev_open_window_fit_size (0, 648, RingSize, PolarResolution, 150, 512, WindowHandle) *设置区域填充方式 dev_set_draw ('margin') *设置输出对象线宽 dev_set_line_width (3) *设置对象显示颜色 dev_set_color ('red') *激活WindowHandle1窗口 dev_set_window(WindowHandle1) * Main loop * * Detect defects in bottle necks for Index := 2 to 16 by 1 *读取一张图像 read_image (Image, 'bottles/bottle_mouth_'+Index$'.02') *显示图像 dev_display (Image) *自动阈值 auto_threshold (Image, Regions, 2) *获取区域一 select_obj (Regions, DarkRegion, 1) *对区域一进行开运算 opening_circle (DarkRegion, RegionOpening, 3.5) *对开运算区域进行闭运算 closing_circle (RegionOpening, RegionClosing, 25.5) *填充闭运算后区域 fill_up (RegionClosing, RegionFillUp) *获取区域外边界 boundary (RegionFillUp, RegionBorder, 'outer') *对区域边界进行膨胀运算 dilation_circle (RegionBorder, RegionDilation, 3.5) *剪切区域里的图像 reduce_domain (Image, RegionDilation, ImageReduced) * * 运用CANNY算法进行边缘探测 edges_sub_pix (ImageReduced, Edges, 'canny', 0.5, 20, 40) *对边缘轮廓分割成直线和圆 segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 2) *将共圆上的轮廓连接起来 union_cocircular_contours_xld (ContoursSplit, UnionContours, 0.9, 0.5, 0.5, 200, 50, 50, 'true', 1) *获取轮廓的长度 length_xld (UnionContours, Length) *对长度数值进行降序排列,并获取数值最大的长度值 select_obj (UnionContours, LongestContour, sort_index(Length)[|Length|-1]+1) *对最大的轮廓进行拟合圆操作 fit_circle_contour_xld (LongestContour, 'ahuber', -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder) * * Part 2: Transform the ring-shaped bottle neck region to a rectangle *生成拟合圆 gen_circle (Circle, Row, Column, Radius) *对拟合圆进行膨胀运算 dilation_circle (Circle, RegionDilation, 5) *对拟合圆进行腐蚀运算 erosion_circle (Circle, RegionErosion, RingSize-5) *求两区域补集 difference (RegionDilation, RegionErosion, RegionDifference) *获取补集区域里的图像 reduce_domain (Image, RegionDifference, ImageReduced) *将图像从笛卡尔直角坐标系转换到极坐标系 polar_trans_image (ImageReduced, ImagePolar, Row, Column, PolarResolution, Radius+5) * * Part 3: Find defects with a dynamic threshold * Note the strong smoothing in x-direction in the transformed image. *剪切矩形区域里的图像 crop_part (ImagePolar, ImagePart, Radius-RingSize, 0, PolarResolution, RingSize) *将最大灰度值在0-255范围拉伸 scale_image_max (ImagePart, ImageScaleMax) *对灰度拉伸的图像进行均值滤波 mean_image (ImageScaleMax, ImageMean, SmoothX, 3) *局部阈值处理 dyn_threshold (ImageScaleMax, ImageMean, Regions1, 50, 'not_equal') *连通处理 connection (Regions1, Connection) *根据高度过滤区域 select_shape (Connection, SelectedRegions, 'height', 'and', 9, 99999) * 用矩形结构元素进行闭运算 closing_rectangle1 (SelectedRegions, RegionClosing1, 10, 20) *将区域连接起来 union1 (RegionClosing1, RegionUnion) * 将区域从极坐标转换到直角坐标系中 polar_trans_region_inv (RegionUnion, XYTransRegion, Row, Column, 6.28319, 0, Radius-RingSize, Radius, 640, RingSize, 1280, 1024, 'nearest_neighbor') * * Part 4: Display results * 激活窗口WindowHandle1 dev_set_window (WindowHandle1) dev_display (Image) dev_set_color ('blue') dev_display (RegionDifference) dev_set_color ('red') dev_display (XYTransRegion) * display polar transformed inspected region with results * The image and resulting region are rotated by 90 degrees * only for visualization purposes! (I.e. to fit better on the screen) * The rotation is NOT necessary for the detection algorithm. *激活窗口WindowHandle dev_set_window (WindowHandle) *旋转图像 rotate_image (ImagePart, ImageRotate, 90, 'constant') dev_display (ImageRotate) count_obj (RegionUnion, Number) if (Number>0) *缺陷区域沿对角线镜像 mirror_region (RegionUnion, RegionMirror, 'diagonal', PolarResolution) *对镜像区域再次沿列方向镜像 mirror_region (RegionMirror, RegionMirror, 'row', PolarResolution) *显示镜像以后的区域 dev_display (RegionMirror) disp_message (WindowHandle1, 'Not OK', 'window', -1, -1, 'red', 'false') else disp_message (WindowHandle1, 'OK', 'window', -1, -1, 'forest green', 'false') endif if (Index<16) disp_continue_message (WindowHandle1, 'black', 'true') stop () endif dev_set_window (WindowHandle1) endfor * 恢复存储空区域的状态 set_system ('store_empty_region', StoreEmptyRegion)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)