VisionPro读取图片、取相机视频

1.从文化中读取图片

注意:需要在界面创建一个CogDisplay1控件

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "请选择图片";
openFileDialog.Multiselect = false;
openFileDialog.Filter = "位图文件(*.bmp)|*.bmp|JPEG 文件(*.jpg)|*.jpg|PNG 文件(*.png)|*.png|TIFF 文件(*.tif)|*.tif|所有文件(*.bmp,*.jpg,*.png,*.tif)|*.bmp;*.jpg;*.png;*.tif";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
    //创建一个图像打开工具
    CogImageFileTool cogImageFileTool = new CogImageFileTool();
    cogImageFileTool.Operator.Open(openFileDialog.FileName, CogImageFileModeConstants.Read);
    cogImageFileTool.Run();
    
    cogDisplay1.DrawingEnabled = false;
    cogDisplay1.Image = cogImageFileTool.OutputImage;
    cogDisplay1.Fit();
    //显示绘图  绘图后显示是为了防止看到图片由大变小或者由小变大自动适应的过程 实践才是真理  你可以把上面的cogDisplay1.DrawingEnabled = false;注释掉试试看
    cogDisplay1.DrawingEnabled = true;
}

2.使用VisionPro自带的库进行取图或者视频

1.先创建四个程序集变量CogFrameGrabbers、ICogFrameGrabber、ICogAcqFifo和xjIndex
private CogFrameGrabbers cogFrameGrabbers;
private ICogFrameGrabber myCogFrameGrabber;
private ICogAcqFifo cogAcqFifo;
private int xjIndex = -1;
2.获取相机的列表

注意:USB相机和自带的相机不行,需要工业相机

cogFrameGrabbers = new CogFrameGrabbers();
if (cogFrameGrabbers.Count == 0)
{
    MessageBox.Show("没有找到相机,请检查!","错误",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
else
{
    comboBox1.Items.Clear();
    foreach (ICogFrameGrabber item in cogFrameGrabbers)
    {
        comboBox1.Items.Add(item.Name);
    }
}
3.连接相机
if (xjIndex <= 0)
{
    MessageBox.Show("请选择相机!!!","错误",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
else
{
    try
    {
        myCogFrameGrabber = cogFrameGrabbers[xjIndex];
        const string VIDEO_FORMAT = "Sony XC75 640x480";
        cogAcqFifo = myCogFrameGrabber.CreateAcqFifo(VIDEO_FORMAT, Cognex.VisionPro.CogAcqFifoPixelFormatConstants.Format8Grey, 0, false);

        button1.Enabled = button2.Enabled = comboBox1.Enabled = false;
        button3.Enabled = button4.Enabled =  button6.Enabled = true;
    }
    catch (Exception)
    {
        throw;
    }
}
4.实时显示与关闭

1.实时显示

cogDisplay1.StartLiveDisplay(cogAcqFifo, false);

2.关闭实时显示

cogDisplay1.StopLiveDisplay();
5.单张取图
int trigNum;
cogDisplay1.Image = cogAcqFifo.Acquire(out trigNum);

如果取的是彩色图片也可以进行转换

cogDisplay1.Image = CogImageConvert.GetIntensityImage(cogDisplay1.Image, 0, 0, cogDisplay1.Image.Width, cogDisplay1.Image.Height);

//或者
CogImageConvertTool cogImageConvertTool = new CogImageConvertTool();
cogImageConvertTool.InputImage = cogDisplay1.Image;
cogImageConvertTool.Run();
cogDisplay1.Image = cogImageConvertTool.OutputImage;
6.关闭相机
myCogFrameGrabber.Disconnect(false)

3.第三方取相机图片

这里用的是AForge,可以取自带的摄像头和USB摄像头

1.程序集变量
/// <summary>
/// 相机列表
/// </summary>
private FilterInfoCollection filterInfoCollection;
/// <summary>
/// 实时显示线程
/// </summary>
Thread AlwaysShowThread;
2.获取相机的列表
comboBox1.Items.Clear();
//treeView1.Nodes.Clear();
filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (filterInfoCollection.Count == 0)
{
    MessageBox.Show("没有相机,请检查!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
    comboBox1.Items.Clear();
    foreach (FilterInfo item in filterInfoCollection)
    {
        comboBox1.Items.Add(item.Name);
    }
}
3.连接相机
ShutCamera();//关闭之前的相机
if (comboBox1.SelectedIndex >=0)
{
    try
    {
        videoSourcePlayer1.VideoSource = new VideoCaptureDevice(filterInfoCollection[comboBox1.SelectedIndex].MonikerString);
        videoSourcePlayer1.Start();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    button1.Enabled = button2.Enabled = comboBox1.Enabled = false;
}
else
{
    MessageBox.Show("请选择相机!!!","提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
4.关闭并释放摄像头
public void ShutCamera()
{
    if (videoSourcePlayer1.VideoSource != null)
    {
        videoSourcePlayer1.SignalToStop();
        videoSourcePlayer1.WaitForStop();
        videoSourcePlayer1.VideoSource = null;
    }
}
5.实时取像
AlwaysShowThread = new Thread(AlwaysShow);
AlwaysShowThread.Start();

private void AlwaysShow()
{
    while (true)
    {
        Bitmap bitmap = videoSourcePlayer1.GetCurrentVideoFrame();//拍摄
        if (bitmap != null)
        {
            try
            {
                if (radioButton1.Checked)
                {
                    CogImage16Grey cogImage = new CogImage16Grey(bitmap);
                    cogDisplay1.Image = cogImage;
                }
                else
                {
                    CogImage24PlanarColor cogImage24PlanarColor = new CogImage24PlanarColor(bitmap);
                    cogDisplay1.Image = cogImage24PlanarColor;
                }
            }
            catch { }
        }
        else
        {
            return;
        }
    }
}
6.关闭实时取像
if (AlwaysShowThread != null)
{
    AlwaysShowThread.Abort();
}
7.单张取图
if (AlwaysShowThread != null)
{
    AlwaysShowThread.Abort();
}

Bitmap bitmap = videoSourcePlayer1.GetCurrentVideoFrame();//拍摄
if (bitmap != null)
{
    try
    {
        //为真时显示灰色图像
        if (radioButton1.Checked)
        {
            CogImage16Grey cogImage = new CogImage16Grey(bitmap);
            cogDisplay1.Image = cogImage;
        }
        else
        {
            CogImage24PlanarColor cogImage24PlanarColor = new CogImage24PlanarColor(bitmap);
            cogDisplay1.Image = cogImage24PlanarColor;
        }
    }
    catch { }
}
else
{
    MessageBox.Show("取图失败!!!","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值