2.5 C#视觉程序开发实例1----CamManager实现模拟相机采集图片

embedded/2024/10/18 14:24:05/

2.5 C#视觉程序开发实例1----CamManager实现模拟相机采集图片

1 目标效果视频

CamManager

2 CamManager读取本地文件时序

在这里插入图片描述

3 BD_Vision_Utility添加代码

3.0 导入链接库
  1. BD_OperatorSets.dll
  2. System.Windows.Forms.dll
  3. OpencvSharp
3.1 导入VisionParam中创建的文件Util_FileOP

并且添加代码GetFileNamewithextension,用于罗列文件夹中所有图片文件

/// <summary>
/// GetFileNamewithextension/// </summary>/// <param name="FilePath"></param>/// <param name="FileName"></param>public void GetFileNamewithextension(string FilePath, ref List<string> FileName){//获取指定文件夹下的文件夹名try{FileName.Clear();if (FolderExist(FilePath)){string[] StrFileName = Directory.GetFiles(FilePath);for (int i = 0; i < StrFileName.Length; i++){string extension = System.IO.Path.GetExtension(StrFileName[i]);//扩展名 “.aspx”if (extension == ".png" || extension == ".bmp" || extension == ".jpg" || extension == ".jpeg")FileName.Add(StrFileName[i]);}}}catch (Exception ex){throw new Exception("GetFileNamewithextension :" + ex.Message);}}
3.2 创建CamManager.cs
3.2.1 变量创建
/// <summary>
/// ImageFile_HANDLE
/// </summary>
private struct ImageFile_HANDLE
{public int n_index;// 当前img file indexpublic List<string> list_imgs;// img  file name listpublic void  Release(){n_index = -1;list_imgs.Clear(); }public ImageFile_HANDLE(int a=0){n_index = -1; list_imgs = new List<string>();}
}
private int camCount;/// <summary>
/// imgfiles_cams
/// 管理各个相机的文件资源
/// </summary>
private ImageFile_HANDLE[] imgfiles_cams;
3.2.2 Constructor
public Cam_Manager(int _camCount) 
{if (_camCount < 1) _camCount = 1;camCount = _camCount;imgfiles_cams = new ImageFile_HANDLE[camCount];for(int i=0;i< camCount;i++){imgfiles_cams[i] = new ImageFile_HANDLE();}
}
3.2.3 open(int CamNO)
/// <summary>
/// 打开相机
/// </summary>
/// <returns></returns>
public int Open(int CamNO)
{int nRet = 0;if (CamNO < 0 || CamNO > camCount - 1) return -1;// To Dostring camFiles_path = Application.StartupPath + "\\camSimulator\\Cam" + CamNO.ToString();ContextManager.get_fileopCtx().GetFileNamewithextension(camFiles_path, ref imgfiles_cams[CamNO].list_imgs);if (imgfiles_cams[CamNO].list_imgs.Count > 0) imgfiles_cams[CamNO].n_index = 0;else{nRet = -1; }return nRet;
}
3.2.4 grab(ref Mat ,ing CamNo)
public int Grab(ref Mat himg_tmp,int CamNO)
{
int nRet = 0;
if (CamNO < 0 || CamNO > camCount - 1) return -1;
if (himg_tmp == null) return -1; try
{if (imgfiles_cams[CamNO].list_imgs.Count > 0)// 文件夹中有图片{// 读取图片索引imgfiles_cams[CamNO].n_index = imgfiles_cams[CamNO].n_index % imgfiles_cams[CamNO].list_imgs.Count;// NewImage = Cv2.ImRead( ContextManager.getCamCtx().AcqHandle[CamNO].list_imgs[ ContextManager.getCamCtx().AcqHandle[CamNO].n_index], ImreadModes.Color);BD_OperateSet.Assign_Temp(ref himg_tmp, Cv2.ImRead(imgfiles_cams[CamNO].list_imgs[imgfiles_cams[CamNO].n_index], ImreadModes.AnyColor));imgfiles_cams[CamNO].n_index++;}else MessageBox.Show("Cam" + CamNO.ToString() + "没有图像来源");
}
catch (Exception ex)
{nRet = -2;
}
return nRet;
}
3.2.5 close()关闭相机
public int Close()
{int nRet = 0;// To Do for(int i=0;i<camCount;i++)imgfiles_cams[i].Release();Array.Clear(imgfiles_cams,0, imgfiles_cams.Length);return nRet;
}
3.3 ContextManager中增加一个CamManager的实例|FileOP的实例

实现自动的资源管理和初始化

//Cam_Manager 
private static Cam_Manager cam_Ctx;
public static Cam_Manager get_camCtx()
{if (cam_Ctx == null) cam_Ctx = new Cam_Manager(4);return cam_Ctx;
}
//  FileOP Manager
private static Util_FileOP fileop_Ctx;
public static Util_FileOP get_fileopCtx()
{if (fileop_Ctx == null) fileop_Ctx = new Util_FileOP();return fileop_Ctx;
}
4 下一节 我们会实现Form_Vision中的代码,并且附上范例代码

http://www.ppmy.cn/embedded/58556.html

相关文章

09.AOP-尚硅谷Spring零基础入门到进阶,一套搞定spring6全套视频教程(源码级讲解)

现有代码缺陷 针对带日志功能的实现类&#xff0c;我们发现有如下缺陷&#xff1a; 对核心业务功能有干扰&#xff0c;导致程序员在开发核心业务功能时分散了精力附加功能分散在各个业务功能方法中&#xff0c;不利于统一维护 解决思路 解决核心&#xff1a;解耦。把附加功能从…

科研绘图系列:R语言小提琴图(Violin Plot)

介绍 小提琴图(Violin Plot)是一种结合了箱线图和密度图的图表,它能够展示数据的分布密度和分布形状。以下是对小提琴图的详细解释: 小提琴图能表达: 数据分布:小提琴图通过在箱线图的两侧绘制曲线来展示数据的分布密度,曲线的宽度表示数据点的密度。集中趋势:箱线图部…

主流电商平台营销中大数据的应用◆

随着经济的不断发展&#xff0c;网络信息技术不断加强&#xff0c;电子商务和大数据的蓬勃发展极大地方便了人们的生活。本文章主要阐述大数据分析与电商营销的含义、大数据分析在电子商务营销中的应用&#xff0c;以及该应用的作用和存在哪些不足及解决方法。探究大数据分析在…

odoo 去掉debug开启开发者模式

路径 addons\web\models\ir_http.py 搜索注释掉 方法_handle_debug()

Redis实践经验

优雅的Key结构 Key实践约定&#xff1a; 遵循基本格式&#xff1a;[业务名称]:[数据名]:id例&#xff1a;login:user:10长度步超过44字节&#xff08;版本不同&#xff0c;上限不同&#xff09;不包含特殊字符 优点&#xff1a; 可读性强避免key冲突方便管理节省内存&#x…

【易捷海购-注册安全分析报告】

前言 由于网站注册入口容易被黑客攻击&#xff0c;存在如下安全问题&#xff1a; 暴力破解密码&#xff0c;造成用户信息泄露短信盗刷的安全问题&#xff0c;影响业务及导致用户投诉带来经济损失&#xff0c;尤其是后付费客户&#xff0c;风险巨大&#xff0c;造成亏损无底洞…

CSS3实现彩色变形爱心动画【附源码】

随着前端技术的发展&#xff0c;CSS3 为我们提供了丰富的动画效果&#xff0c;使得网页设计更加生动和有趣。今天&#xff0c;我们将探讨如何使用 CSS3 实现一个彩色变形爱心加载动画特效。这种动画不仅美观&#xff0c;而且可以应用于各种网页元素&#xff0c;比如加载指示器或…

css看见彩虹,吃定彩虹

css彩虹 .f111 {width: 200px;height: 200px;border-radius: 50%;box-shadow: 0 0 0 5px inset red, 0 0 0 10px inset orange, 0 0 0 15px inset yellow, 0 0 0 20px inset lime, 0 0 0 25px inset aqua, 0 0 0 30px inset blue, 0 0 0 35px inset magenta;clip-path: polygo…