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

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,用于罗列文件夹中所有图片文件

csharp 复制代码
/// <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 变量创建
csharp 复制代码
/// <summary>
/// ImageFile_HANDLE
/// </summary>
private struct ImageFile_HANDLE
{
    public int n_index;// 当前img file index
    public List<string> list_imgs;// img  file name list
    public 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
csharp 复制代码
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)
csharp 复制代码
/// <summary>
/// 打开相机
/// </summary>
/// <returns></returns>
public int Open(int CamNO)
{
    int nRet = 0;
    if (CamNO < 0 || CamNO > camCount - 1) return -1;
    // To Do
    string 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)
csharp 复制代码
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()关闭相机
csharp 复制代码
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的实例

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

csharp 复制代码
//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中的代码,并且附上范例代码
相关推荐
桦0几秒前
[C++复习]:STL
开发语言·c++
主宰者7 分钟前
C# CommunityToolkit.Mvvm全局事件
java·前端·c#
前端小咸鱼一条36 分钟前
16.迭代器 和 生成器
开发语言·前端·javascript
小陈工43 分钟前
2026年3月31日技术资讯洞察:AI智能体安全、异步编程突破与Python运行时演进
开发语言·jvm·数据库·人工智能·python·安全·oracle
ok_hahaha1 小时前
java从头开始-黑马点评-Redission
java·开发语言
无巧不成书02181 小时前
Java面向对象零基础实战:从Employee类吃透自定义类核心,掌握封装精髓
java·开发语言·java入门·面向对象·自定义类·employee类·java核心技术
跃上青空1 小时前
Java如何优雅的使用fastjson2进行枚举序列化/反序列化,欢迎探讨
java·开发语言
ZoeJoy81 小时前
C# + 机器视觉 + AI:从工业相机取图到 YOLO 目标检测的完整工控解决方案
人工智能·数码相机·c#
Leo655352 小时前
动态透视报表 + 查询接口 + Excel导出
开发语言·windows·python
BioRunYiXue2 小时前
Nature Methods:CellVoyager 自主 AI 智能体开启生物数据分析新时代
大数据·开发语言·前端·javascript·人工智能·数据挖掘·数据分析