首页 > 代码库 > 初学视觉学习笔记----保存并显示图片(打开两个相机)
初学视觉学习笔记----保存并显示图片(打开两个相机)
以下代码可以实现的功能:
打开两个相机;抓拍图片以及获取图片的其他信息(如创建的时间);图像处理并把结果显示在hWindowCountrl;把图像在PicBox上显示(图片适应窗口大小);
查看原图(窗口适应图片的大小)//我觉得这个方法不太好用,再学习学习,找机会修改一下。
用到的相机:
USB相机,S500A3B高拍仪(或者笔记本自带相机)
楼主还是一个小白,程序可能写的不好,但是均在自己电脑上运行成功^^,有需要的可以按需自取。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;
using System.Threading;
using System.Drawing.Imaging;
namespace 分别打开两个相机
{
public partial class Form1 : Form
{
Thread t;
public bool tFlag = false;
//halcon变量
public HTuple hv_ExpDefaultWinHandle1;
public HTuple hv_ExpDefaultWinHandle2;
public HObject GPY_Image = null;
public HObject USB_Image = null;
public HTuple USB_AcqHandle = null;
public HTuple GPY_AcqHandle = null;
public HTuple USB_Width = new HTuple();
public HTuple USB_Height = new HTuple();
public HTuple GPY_Width = new HTuple();
public HTuple GPY_Height = new HTuple();
//图片属性
HTuple hv_Pointer = null, hv_Type = null, hv_Width1 = null;
HTuple hv_Height1 = null, hv_MSecond = null, hv_Second = null;
HTuple hv_Minute = null, hv_Hour = null, hv_Day = null;
HTuple hv_YDay = null, hv_Month = null, hv_Year = null;
//截图变量
public HTuple hv_ExpDefaultWinHandle3;
public HTuple WindowHandle;
public HTuple WindowHandle1;
public HObject savepic;
public HObject ho_Image1;
public bool grabImg = false;
//图片保存的路径
public string filename = "C:/Users/like/Documents/Visual Studio 2015/Projects/分别打开两个相机/savepic/SavePic.jpg";
//图像处理
HTuple hv_ExpDefaultWinHandle4;
HSystem sys = new HSystem();
HObject ho_Regions = null;
//本例程打开了两个相机。侧重点 在打开USB相机,并获取USB相机的图片,然后进行图像处理。
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
hv_ExpDefaultWinHandle1 = hWindowControl1.HalconWindow;
hv_ExpDefaultWinHandle2 = hWindowControl2.HalconWindow;
hv_ExpDefaultWinHandle3 = hWindowControl3.HalconWindow;
hv_ExpDefaultWinHandle4 = hWindowControl4.HalconWindow;
HOperatorSet.GenEmptyObj(out USB_Image);
HOperatorSet.GenEmptyObj(out GPY_Image);
HOperatorSet.GenEmptyObj(out savepic);
HOperatorSet.GenEmptyObj(out ho_Regions);
//这句话很重要,设置颜色的数量,没有这句话图像处理结果显示为空
HOperatorSet.SetColored(hv_ExpDefaultWinHandle4, 12);
}
//结束程序
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (tFlag == true)
{
t.Abort();
}
if (MessageBox.Show("是否要退出本系统?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
this.Dispose(true);
this.Close();
Application.Exit();
base.OnClosing(e); //这句话有什么用,我不知道。。
}
}
//USB相机初始化程序
public void InitCameraUSB()
{
HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
-1, "false", "default", "[1] USB Camera", 0, -1, out USB_AcqHandle);
USB_Height = 480; //相机分辨率
USB_Width = 640;
HOperatorSet.SetPart(hv_ExpDefaultWinHandle1, 0, 0, USB_Height, USB_Width); //设置图像显示部分适应窗口
HOperatorSet.SetPart(hv_ExpDefaultWinHandle3, 0, 0, USB_Height, USB_Width);
}
//高拍仪的初始化
public void InitCameraGPY()
{
HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
-1, "false", "default", "[2] S500A3B", 0, -1, out GPY_AcqHandle);
GPY_Height = 2000;
GPY_Width = 2000;
HOperatorSet.SetPart(hv_ExpDefaultWinHandle2, 0, 0, GPY_Height, GPY_Width);
}
//USB相机的打开程序
public void StartUSB()
{
HOperatorSet.GrabImageStart(USB_AcqHandle, -1);
while (true)
{
try
{
USB_Image.Dispose();
HOperatorSet.GrabImageAsync(out USB_Image, USB_AcqHandle, -1);
HOperatorSet.DispObj(USB_Image, hv_ExpDefaultWinHandle1); //视频实时显示
if (grabImg == true) //grabImg是抓拍图片的标志,令grabImg=true时抓拍图片
{
grabImg = false;
HOperatorSet.DispObj(USB_Image, hv_ExpDefaultWinHandle3); //抓拍图显示
//把抓拍到的图片保存到计算机
HOperatorSet.WriteImage(USB_Image, "jpg", 0, filename);
}
}
catch
{
continue;
}
}
}
//高拍仪启动程序
public void StartGPY()
{
HOperatorSet.GrabImageStart(GPY_AcqHandle, -1);
while (true) //高拍仪实时显示
{
try
{
USB_Image.Dispose();
HOperatorSet.GrabImageAsync(out GPY_Image, GPY_AcqHandle, -1);
HOperatorSet.DispObj(GPY_Image, hv_ExpDefaultWinHandle2);
}
catch
{
continue;
}
}
}
//获取拍的照片的相关参数
public void getmessage()
{
HOperatorSet.GetImagePointer1(savepic, out hv_Pointer, out hv_Type, out hv_Width1,out hv_Height1);
HOperatorSet.GetImageTime(savepic, out hv_MSecond, out hv_Second, out hv_Minute, out hv_Hour, out hv_Day, out hv_YDay, out hv_Month, out hv_Year);
}
//图像处理过程,添加一些其他halcon算子,可以实现更复杂的图像处理。
public void threshold()
{
HOperatorSet.ClearWindow(hv_ExpDefaultWinHandle4);
ho_Regions.Dispose();
HOperatorSet.AutoThreshold(savepic, out ho_Regions, 4);
HOperatorSet.DispObj(ho_Regions, hv_ExpDefaultWinHandle4);
HOperatorSet.WriteString(hv_ExpDefaultWinHandle4, "处理图像");
}
//打开USB相机的事件
private void button1_Click(object sender, EventArgs e)
{
t = new Thread(new ThreadStart(StartUSB));
try
{
InitCameraUSB(); //初始化相机
}
catch
{
MessageBox.Show("打开USB相机失败", "请重试");
}
t.Start();
tFlag = true;
}
//打开高拍仪的事件
private void button2_Click(object sender, EventArgs e)
{
t = new Thread(new ThreadStart(StartGPY));
try
{
InitCameraGPY(); //初始化相机
}
catch
{
MessageBox.Show("打开高拍仪失败", "请重试");
}
t.Start();
}
//图像处理事件
private void button3_Click_1(object sender, EventArgs e)
{
threshold();
}
//获取图片信息事件
private void button4_Click(object sender, EventArgs e)
{
getmessage();
textBox1.Text = null;
textBox1.Text = "Pointer:" + hv_Pointer + "\r\n" + "Type :" + hv_Type + "\r\n" + "长宽 :" + hv_Height1 + "X" + hv_Width1 + "\r\n"+"PicTime:"+ "\r\n" + hv_Year + "年" + hv_Month + "月" + hv_Day + "日" + hv_Hour + "时" + hv_Minute + "分" + hv_Second + "秒";
}
//把原图放在PicBox内的事件
//方法:
//step1: picturebox 放在 panel里面
//step2: panel的AutoScroll 设置为true
private void button5_Click(object sender, EventArgs e)
{
HOperatorSet.GetImagePointer1(savepic, out hv_Pointer, out hv_Type, out hv_Width1, out hv_Height1);
pictureBox2.Width = hv_Width1;
pictureBox2.Height = hv_Height1;
HOperatorSet.OpenWindow(0, 0, pictureBox2.Width, pictureBox2.Height, pictureBox2.Handle, "visible", "", out WindowHandle1);
HOperatorSet.DispObj(savepic, WindowHandle1);
HOperatorSet.WriteString(WindowHandle1, "测试完成");
//下次再添加滚轮放大图片的效果
}
//抓取图片事件
private void button6_Click(object sender, EventArgs e)
{
grabImg = true; //抓取图片标志
//把保存的图片的内容读取到savepic中,为了下一步获取图片属性做准备
savepic.Dispose();
HOperatorSet.ReadImage(out savepic, filename);
}
//把图片放在PicBox内的事件
private void button7_Click(object sender, EventArgs e)
{
//用halcon的OpenWindow在PicBox内打开一个窗口
HOperatorSet.OpenWindow(0, 0, pictureBox1.Width, pictureBox1.Height, pictureBox1.Handle, "visible", "", out WindowHandle);
HOperatorSet.DispObj(savepic, WindowHandle);
}
}
}
提出问题:
http://www.ihalcon.com/read-7207.html
问题详情在帖子里有说T T,有没有大神可以帮我解解惑~
保存图片的功能可以改进
初学视觉学习笔记----保存并显示图片(打开两个相机)