欢迎光临
我们一直在努力

流水线工件自动计数:C# + 工业相机 + PLC 低成本改造方案

在这里插入图片描述
上个月帮一个五金冲压厂做了3条流水线的工件自动计数改造,总成本不到1200元,误检率低于0.1%,已经稳定运行2个月。

客户之前用的是光电开关计数,对于厚度小于1mm的薄铁片,经常出现重叠漏检和多检的情况,每天人工盘点误差超过5%。
咨询过专业的视觉计数系统,单条线报价就要8000元以上,而且后续维护还要额外收费。
最后我们用普通USB工业相机+现有PLC+C#自研程序的方案,实现了同样的功能,成本只有专业系统的1/7。

今天把完整的改造方案分享出来,不需要复杂的深度学习模型,纯传统图像处理就能搞定,有一点C#基础就能自己做。

一、项目痛点与方案选型

客户的3条流水线主要生产汽车用的冲压铁片,每分钟产出60-80个工件,要求计数准确率99.9%以上,数据直接对接现有PLC系统。

传统光电开关的致命问题:

  • 工件重叠时无法识别,漏检率高达3-5%
  • 薄铁片和透明工件容易穿透,导致误检
  • 不同规格工件需要重新调整传感器位置
  • 无法区分合格品和不良品,只能统计总数

最终技术选型:

  • 视觉处理:C# + OpenCvSharp4(传统图像处理,无需训练模型)
  • 工业相机:海康威视MV-U313-20GC(300元,USB3.0接口)
  • 光源:白色环形LED光源(50元,解决光照不均问题)
  • PLC通信:Modbus TCP协议(复用现有西门子S7-1200 PLC)
  • 部署环境:现有产线工控机(无需额外购买硬件)

二、整体系统架构与工作流程

整个系统完全基于现有硬件改造,不需要更换PLC和工控机,只需要加装相机和光源即可。

#mermaid-svg-GlzgNbwoofd7CubC{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-GlzgNbwoofd7CubC .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-GlzgNbwoofd7CubC .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-GlzgNbwoofd7CubC .error-icon{fill:#552222;}#mermaid-svg-GlzgNbwoofd7CubC .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-GlzgNbwoofd7CubC .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-GlzgNbwoofd7CubC .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-GlzgNbwoofd7CubC .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-GlzgNbwoofd7CubC .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-GlzgNbwoofd7CubC .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-GlzgNbwoofd7CubC .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-GlzgNbwoofd7CubC .marker{fill:#333333;stroke:#333333;}#mermaid-svg-GlzgNbwoofd7CubC .marker.cross{stroke:#333333;}#mermaid-svg-GlzgNbwoofd7CubC svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-GlzgNbwoofd7CubC p{margin:0;}#mermaid-svg-GlzgNbwoofd7CubC .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-GlzgNbwoofd7CubC .cluster-label text{fill:#333;}#mermaid-svg-GlzgNbwoofd7CubC .cluster-label span{color:#333;}#mermaid-svg-GlzgNbwoofd7CubC .cluster-label span p{background-color:transparent;}#mermaid-svg-GlzgNbwoofd7CubC .label text,#mermaid-svg-GlzgNbwoofd7CubC span{fill:#333;color:#333;}#mermaid-svg-GlzgNbwoofd7CubC .node rect,#mermaid-svg-GlzgNbwoofd7CubC .node circle,#mermaid-svg-GlzgNbwoofd7CubC .node ellipse,#mermaid-svg-GlzgNbwoofd7CubC .node polygon,#mermaid-svg-GlzgNbwoofd7CubC .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-GlzgNbwoofd7CubC .rough-node .label text,#mermaid-svg-GlzgNbwoofd7CubC .node .label text,#mermaid-svg-GlzgNbwoofd7CubC .image-shape .label,#mermaid-svg-GlzgNbwoofd7CubC .icon-shape .label{text-anchor:middle;}#mermaid-svg-GlzgNbwoofd7CubC .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-GlzgNbwoofd7CubC .rough-node .label,#mermaid-svg-GlzgNbwoofd7CubC .node .label,#mermaid-svg-GlzgNbwoofd7CubC .image-shape .label,#mermaid-svg-GlzgNbwoofd7CubC .icon-shape .label{text-align:center;}#mermaid-svg-GlzgNbwoofd7CubC .node.clickable{cursor:pointer;}#mermaid-svg-GlzgNbwoofd7CubC .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-GlzgNbwoofd7CubC .arrowheadPath{fill:#333333;}#mermaid-svg-GlzgNbwoofd7CubC .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-GlzgNbwoofd7CubC .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-GlzgNbwoofd7CubC .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-GlzgNbwoofd7CubC .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-GlzgNbwoofd7CubC .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-GlzgNbwoofd7CubC .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-GlzgNbwoofd7CubC .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-GlzgNbwoofd7CubC .cluster text{fill:#333;}#mermaid-svg-GlzgNbwoofd7CubC .cluster span{color:#333;}#mermaid-svg-GlzgNbwoofd7CubC div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-GlzgNbwoofd7CubC .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-GlzgNbwoofd7CubC rect.text{fill:none;stroke-width:0;}#mermaid-svg-GlzgNbwoofd7CubC .icon-shape,#mermaid-svg-GlzgNbwoofd7CubC .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-GlzgNbwoofd7CubC .icon-shape p,#mermaid-svg-GlzgNbwoofd7CubC .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-GlzgNbwoofd7CubC .icon-shape .label rect,#mermaid-svg-GlzgNbwoofd7CubC .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-GlzgNbwoofd7CubC .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-GlzgNbwoofd7CubC .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-GlzgNbwoofd7CubC :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

流水线运行

PLC触发相机拍照

工业相机采集图像

C#图像处理与计数

结果校验与去重

发送计数信号给PLC

PLC累加计数并控制流水线

数据存储与报表生成

核心工作原理:
PLC在流水线每前进一个固定距离时,给上位机发送一个触发信号。
上位机控制相机拍照,通过图像处理算法识别工件数量,然后将结果返回给PLC。
PLC根据计数结果控制流水线启停,当达到设定数量时自动报警提示换箱。

三、分步实现:3天完成整条线改造

3.1 硬件安装与调试(1天)

硬件安装是最容易被忽视的环节,直接决定了后续图像处理的难度。

相机安装要点:

  • 垂直安装在流水线正上方,距离传送带高度30-40cm
  • 镜头对准流水线中心,确保视野覆盖整个传送带宽度
  • 使用可调节支架,方便后续调整焦距和角度

光源安装要点:

  • 环形光源与相机同轴安装,避免产生阴影
  • 光源亮度调至中等,不要过亮导致工件过曝
  • 加装遮光罩,避免环境光变化影响检测效果

调试时先拍一张空传送带的照片作为背景,然后放几个工件测试,确保工件和背景有明显的对比度。

3.2 C#核心计数算法实现

我们没有使用YOLO等深度学习模型,纯传统图像处理就能满足需求,速度更快,稳定性更高。

核心算法流程:背景差分 → 二值化 → 形态学开运算 → 轮廓检测 → 计数统计。

using OpenCvSharp;

public int CountWorkpieces(Mat frame, Mat background)
{
// 背景差分,提取前景工件
Cv2.Absdiff(frame, background, frame);
// 转灰度图并二值化
Cv2.CvtColor(frame, frame, ColorConversionCodes.BGR2GRAY);
Cv2.Threshold(frame, frame, 30, 255, ThresholdTypes.Binary);
// 形态学开运算,去除噪点
Mat kernel=Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
Cv2.MorphologyEx(frame, frame, MorphTypes.Open, kernel);
// 检测轮廓并计数
Cv2.FindContours(frame, out var contours, out _,
RetrievalModes.External, ContourApproximationModes.ApproxSimple);

return contours.Count(c=>c.Area>100); // 过滤面积过小的噪点
}

这段代码就是整个计数系统的核心,不到15行,在普通工控机上运行时间不到1ms。

3.3 PLC通信与信号交互

上位机和PLC通过Modbus TCP协议通信,只需要两个寄存器就能完成所有交互。

  • PLC寄存器DB1.DBW0:触发信号(PLC写1,上位机收到后置0)
  • PLC寄存器DB1.DBW2:计数结果(上位机写入,PLC读取)

核心通信代码复用了我们之前Modbus TCP项目的实现,只需要添加简单的信号处理逻辑:

while (isRunning)
{
// 读取PLC触发信号
short trigger=await plcClient.ReadInt16Async("DB1.DBW0");
if (trigger==1)
{
// 拍照并计数
Mat frame=camera.CaptureFrame();
int count=CountWorkpieces(frame, background);
// 写入计数结果并复位触发信号
await plcClient.WriteInt16Async("DB1.DBW2", (short)count);
await plcClient.WriteInt16Async("DB1.DBW0", 0);
}
await Task.Delay(10);
}

四、工业级稳定性优化

工业现场环境复杂,光照变化、灰尘、振动都会影响检测结果,我们做了以下优化:

1. 自动背景更新机制

每隔1小时自动更新一次背景图,解决灰尘积累和光照缓慢变化的问题。
当流水线空闲时自动触发背景更新,不需要人工干预。

2. 防重复计数算法

设置一个时间窗口,在100ms内只处理一次触发信号,避免PLC信号抖动导致的重复计数。
同时记录上一次的计数结果,如果两次结果相差过大则标记为异常,重新拍照检测。

3. 异常处理机制

  • 相机断开连接后自动重连,重连成功后继续工作
  • 连续3次检测异常时自动报警,暂停流水线
  • 所有检测结果和异常信息都记录到本地数据库,方便追溯

五、成本核算与效果对比

单条流水线改造成本明细:

硬件名称型号单价数量小计
工业相机 海康威视MV-U313-20GC 300元 1 300元
环形光源 白色LED环形灯 50元 1 50元
可调节支架 铝合金万向支架 30元 1 30元
电源适配器 12V 2A 20元 1 20元
合计 400元

改造前后效果对比:

指标传统光电开关本方案
计数准确率 95-97% 99.9%以上
误检率 3-5% <0.1%
支持工件类型 有限 所有不透明工件
调整时间 每次换型30分钟 5分钟
单条线成本 200元 400元

六、现场踩坑实录

1. 工件重叠导致漏检

现象:当两个工件重叠在一起时,算法会识别为一个,导致漏检。
解决方法:在流水线前端加装一个分料器,让工件均匀排列通过检测区域。
对于轻微重叠的情况,可以通过调整轮廓检测的面积阈值来解决。

2. 光照变化导致误检

现象:早上和下午的光照不同,导致二值化阈值不合适,出现误检。
解决方法:不要使用固定阈值,改用自适应阈值算法。
同时加装遮光罩,最大限度减少环境光的影响。

3. PLC触发信号延迟

现象:偶尔会出现拍照时机不对,工件没有完全进入视野。
解决方法:在PLC中添加一个延迟定时器,触发信号发出后延迟50ms再拍照。
这个延迟时间需要根据流水线速度进行调整。

七、总结与扩展

这个低成本视觉计数方案已经在3条流水线稳定运行2个月,客户非常满意。
相比专业的视觉系统,我们的方案成本极低,而且完全自主可控,后续可以根据需求进行扩展。

可扩展功能:

  • 增加工件尺寸检测,识别变形和破损的不良品
  • 接入MES系统,实现生产数据实时上报
  • 添加产量统计和报表生成功能
  • 支持多条流水线集中管理

对于中小工厂来说,这种低成本的改造方案性价比极高,不需要投入大量资金就能显著提高生产效率。
工业自动化不一定非要用最先进的技术,适合自己的才是最好的。

赞(0)
未经允许不得转载:171主机测评 » 流水线工件自动计数:C# + 工业相机 + PLC 低成本改造方案
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址