57-OpenCVSharp 创建实现Halcon的Illuminate算子(用于图像的光照校正,通过增强局部对比度来平衡光照不均)


OpenCVSharp实现Halcon Illuminate算子的函数及性能优化

Halcon的Illuminate算子常用于图像的光照校正,通过增强局部对比度来平衡光照不均。以下是基于OpenCVSharp的等效实现:


功能实现步骤
  1. 高斯模糊估计背景:对输入图像进行高斯模糊,模拟全局光照。
  2. 背景光照调整:根据背景进行减法(暗区域增强)或除法(全局光照归一化)操作。
  3. 对比度拉伸与剪辑:将结果映射到有效像素范围(如0-255),增强可视对比度。
  4. 掩码应用:可选掩码限制处理区域,提升效率。

代码实现与优化
using OpenCvSharp
``` dev_update_off() * 参数定义 GaussSize := 5 ScratchLengthMin := 100 StainAreaMin := 100 * 获取图像文件列表 ImageFolder := 'E:/毕业论文/瓶盖图/' list_files(ImageFolder, ['files','follow_links'], ImageFiles) tuple_regexp_select(ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles) * 创建日志文件 open_file('defect_log.txt', 'output', FileHandle) * 主循环 for ImgIndex := 0 to |ImageFiles| - 1 by 1 * 窗口管理 dev_close_window() dev_open_window(0, 0, 600, 400, 'black', WindowHandle) * 文件读取(带异常捕获) try read_image(Image, ImageFiles[ImgIndex]) catch (Exception) write_string(FileHandle, 'Failed:'+ImageFiles[ImgIndex]+'\n') continue endtry get_image_size (Image, Width, Height) dev_open_window (0, 0, 600, 400, 'black', WindowHandle) set_display_font (WindowHandle, 16, 'mono', 'true', 'false') dev_set_draw ('margin') dev_set_line_width (2) * 1. 图像预处理(保留V通道信息) try decompose3 (Image, R, G, B) trans_from_rgb (R, G, B, H, S, V, 'hsv') gauss_filter (V, VFiltered, GaussSize) catch (Exception) disp_message (WindowHandle, '预处理错误', 'window', 12, 12, 'red', 'true') stop() endtry * 2. 瓶盖区域定位 binary_threshold (VFiltered, Regions, 'max_separability', 'dark', UsedThreshold) connection (Regions, ConnectedRegions) select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 5000, 9999999) fill_up (SelectedRegions, RegionFillUp) shape_trans (RegionFillUp, CapRegion, 'convex') * 3. 缺陷检测流程 * 3.1 划痕检测 reduce_domain (VFiltered, CapRegion, VReduced) equ_histo_image (VReduced, VEquHist) //增强对比度 edges_sub_pix (VEquHist, Edges, 'canny', 0.8, 15, 25) select_shape_xld (Edges, Scratches, ['contlength','width'], 'and', [100,1], [9999,3]) gen_region_contour_xld (Scratches, ScratchesRegion, 'filled') * 3.2 污渍检测 texture_laws (VReduced, TextureImage, 'el', 5, 5) var_threshold (TextureImage, Stains, 25, 25, 0.2, 1.5, 'dark') connection (Stains, ConnectedStains) select_shape (ConnectedStains, SelectedStains, ['area','compactness'], 'and', [100,0.1], [9999,0.7]) * 4. 合并缺陷区域 union2 (ScratchesRegion, SelectedStains, AllDefects) * 5. 后处理 connection (AllDefects, ConnectedDefects) opening_circle (ConnectedDefects, FinalDefects, 1.5) * 6. 结果显示 count_obj (FinalDefects, NumDefects) // 此时NumDefects会被正确初始化 dev_display (Image) dev_set_color ('green') dev_display (CapRegion) if (NumDefects > 0) * 划痕显示(蓝色) dev_set_color ('blue') dev_display (ScratchesRegion) * 污渍显示(紫色) dev_set_color ('magenta') dev_display (SelectedStains) * 添加图例说明 disp_message (WindowHandle, '检测结果:', 'window', 12, 12, 'white', 'true') disp_message (WindowHandle, '蓝色:划痕缺陷', 'window', 40, 12, 'blue', 'true') disp_message (WindowHandle, '紫色:污渍缺陷', 'window', 60, 12, 'magenta', 'true') else disp_message (WindowHandle, '检测通过:无缺陷', 'window', 12, 12, 'green', 'true') endif endfor *主循环结束```该代码检测出来的都是无缺陷的,为什么检测不出来瓶盖商标的划痕与污渍
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

X-Vision

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值