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

发布于:2024-07-11 ⋅ 阅读:(14) ⋅ 点赞:(0)

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 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
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 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)
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中的代码,并且附上范例代码