VisionPro二次开发学习笔记2-使用C#从图像数据库文件获取图像

发布于:2024-08-09 ⋅ 阅读:(147) ⋅ 点赞:(0)

使用C#从图像数据库文件获取图像

图像文件对象使您可以从图像文件获取图像,以及将获取的图像保存到图像文件中以备后用。VisionPro为图像文件对象提供了几种类,具体取决于您要使用的图像格式:

  • 对CDB / IDB文件使用CogImageFileCDB对象
  • 将CogImageFileBMP对象用于位图文件
  • 对TIFF文件使用CogImageFileTIFF对象

读取带有图像文件对象的图像文件需要执行以下步骤:

  1. 创建一个图像文件对象。
  2. 打开图像文件进行读取。
  3. 获取图像以供使用和显示。

1. 添加引用

  • Cognex.VisionPro
  • Cognex.VisionPro.ImageFile

2. 定义变量和枚举

public enum ImageFileType {
        OTHER,
        IDB,
        CDB,
        BMP,
        TIFF,
        
    }

        private CogImageFileCDB myImageFileOperator;
        private CogImageFileBMP myImageFileBMP;
        private CogImageFileTIFF myImageFileTIFF;

        private int counter;
        private int maxImage;
        private ImageFileType CurrentFileType;

3. 创建一个图像文件对象

private ImageFileType ConvertStringToImageFileType(string text) {
            if (text.Contains(".cdb"))
                return ImageFileType.CDB;
            if (text.Contains(".idb"))
                return ImageFileType.IDB;
            if (text.Contains(".bmp"))
                return ImageFileType.BMP;
            if (text.Contains(".tiff"))
                return ImageFileType.TIFF;
            else
                return ImageFileType.OTHER;
        }

private void InitializeFifo() {

            counter = 0;
            CurrentFileType = ConvertStringToImageFileType(Path.GetExtension(textBoxFilePath.Text));
            switch (CurrentFileType)
            {
                case ImageFileType.IDB:
                case ImageFileType.CDB:
                    myImageFileOperator = new CogImageFileCDB();
                    myImageFileOperator.Open(textBoxFilePath.Text, CogImageFileModeConstants.Read);
                    maxImage = myImageFileOperator.Count;
                    break;
                case ImageFileType.BMP:
                    myImageFileBMP = new CogImageFileBMP();
                    myImageFileBMP.Open(textBoxFilePath.Text,CogImageFileModeConstants.Read);
                    maxImage = myImageFileBMP.Count;
                    break;
                case ImageFileType.TIFF:
                    myImageFileTIFF = new CogImageFileTIFF();
                    myImageFileTIFF.Open(textBoxFilePath.Text,CogImageFileModeConstants.Read);
                    maxImage = myImageFileTIFF.Count;
                    break;
                case ImageFileType.OTHER:
                    maxImage = 0;
                    break;
                default:
                    break;
            }
            ;
            
            
        }

4. 打开图像文件进行读取

打开Form2.cs[设计]界面,新增两个按钮,一个TextBox用于显示路径,一个label显示当前图片位置和总数,一个CogDisplay控件

界面如下:

在这里插入图片描述

处理按钮点击事件

private void button2_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                // 设置文件过滤器
                openFileDialog.Filter = "Image Files (*.cdb;*.idb;*.bmp;*.tiff)|*.cdb;*.idb;*.bmp;*.tiff";
                openFileDialog.Title = "选择图片文件";

                // 显示对话框并检查用户是否选择了文件
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    // 将选择的文件路径赋值给文本框
                    textBoxFilePath.Text = openFileDialog.FileName;
                }
            }
            InitializeFifo();
        }

5. 获取图像以供使用和显示

/// <summary>
        /// 浏览
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (counter==maxImage)
            {
                counter = 0;
                
            }
            switch (CurrentFileType)
            {
                case ImageFileType.IDB:
                case ImageFileType.CDB:
                    cogDisplay1.Image = myImageFileOperator[counter];
                    break;
                case ImageFileType.BMP:
                    cogDisplay1.Image = myImageFileBMP[counter];
                    break;
                case ImageFileType.TIFF:
                    cogDisplay1.Image = myImageFileTIFF[counter];
                    break;
                case ImageFileType.OTHER:
                    MessageBox.Show("图片格式错误!");
                    break;
                default:
                    break;
            }
            
            counter = counter + 1;
            this.label1.Text = $"第{counter}张/共{maxImage}张";
        }

6. 处理窗体关闭事件

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.DoEvents();
            cogDisplay1.Dispose();
            
        }

总结

在这里插入图片描述
在这里插入图片描述

一个简易的图片数据库文件浏览器到这里就实现了.

扩展

如果您的应用程序要求您读取多种文件类型,可以使用通用的CogImageFile对象。
后台代码改动如下:

        private int counter;
        private int maxImages;
        private CogImageFile ImageFile;
        
        private void InitializeFifo() {
            ImageFile = new CogImageFile();
            ImageFile.Open(textBoxFilePath.Text,CogImageFileModeConstants.Read);
            counter = 0;
            maxImages = ImageFile.Count;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (ImageFile==null)
            {
                MessageBox.Show("没有输入图像!");
            }
            if (counter == maxImages)
            {
                counter = 0;
            }
            cogDisplay1.Image = ImageFile[counter];
            counter = counter + 1;
            label1.Text = $"第{counter}/{maxImages}张";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog fileDialog=new OpenFileDialog())
            {
                if (fileDialog.ShowDialog()==DialogResult.OK)
                {
                    textBoxFilePath.Text = fileDialog.FileName;
                }
            }
            InitializeFifo();
        }
    }
}


网站公告

今日签到

点亮在社区的每一天
去签到