标签:window 算法 pix system init memory unsigned eth 方法
//选择视频设备
public void InitVideoDevice()
{
try
{
if (cam != null)
cam.Dispose();
//读取参数
int VIDEODEVICE = 1; // zero based index of video capture device to use
const int VIDEOWIDTH = 640;// 是用默认(最大)分辨率
const int VIDEOHEIGHT = 480; // Depends on video device caps
const int VIDEOBITSPERPIXEL = 24; // BitsPerPixel values determined by device
cam = new Capture(VIDEODEVICE, VIDEOWIDTH, VIDEOHEIGHT, VIDEOBITSPERPIXEL, picPreview );
}
catch
{
MessageBox.Show("摄像头打开错误,请首先确保摄像头连接并至少支持1024*768分辨率!");
}
}
public FormMain()
{
InitializeComponent();
//构造摄像头数据 foreach (DsDevice ds in DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice)) { cbCam.Items.Add(ds.Name); } //初始化摄像头 InitVideoDevice();
}
private void timer_Tick(object sender, EventArgs e)
{
// Release any previous buffer
if (m_ip != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(m_ip);
m_ip = IntPtr.Zero;
}
// capture image
try
{
m_ip = cam.Click();
}
catch
{
//do nothing,允许丢帧 TODO:是否改成继承上一帧更好
}
Bitmap b = new Bitmap(cam.Width, cam.Height, cam.Stride, PixelFormat.Format24bppRgb, m_ip);
// If the image is upsidedown
b.RotateFlip(RotateFlipType.RotateNoneFlipY);
srcImage = b;
if (picPreview.Image != null)
picPreview.Image.Dispose();
//调用clr+opencv图像处理模块
MemoryStream ms = new MemoryStream();
b.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bytes = ms.GetBuffer();
Bitmap bitmap = client.testMethod(bytes);
//显示结果
picPreview.Image = bitmap;
}
// Release any previous buffer
if (m_ip != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(m_ip);
m_ip = IntPtr.Zero;
}
// capture image
try
{
m_ip = cam.Click();
}
catch
{
//do nothing,允许丢帧 TODO:是否改成继承上一帧更好
}
Bitmap b = new Bitmap(cam.Width, cam.Height, cam.Stride, PixelFormat.Format24bppRgb, m_ip);
// If the image is upsidedown
b.RotateFlip(RotateFlipType.RotateNoneFlipY);
srcImage = b;
if (picPreview.Image != null)
picPreview.Image.Dispose();
//调用clr+opencv图像处理模块
MemoryStream ms = new MemoryStream();
b.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bytes = ms.GetBuffer();
Bitmap bitmap = client.testMethod(bytes);
Bitmap^ GOClrClass::testMethod(cli::array<unsigned char>^ pCBuf1)
{
////////////////////////////////将输入cli::array<unsigned char>转换为cv::Mat/////////////////////////
pin_ptr<System::Byte> p1 = &pCBuf1[0];
unsigned char* pby1 = p1;
cv::Mat img_data1(pCBuf1->Length,1,CV_8U,pby1);
cv::Mat img_object = cv::imdecode(img_data1,IMREAD_UNCHANGED);
if (!img_object.data)
return nullptr;
////////////////////////////////////////////OpenCV的算法处理过程////////////////////////////////////
cvtColor(img_object,img_object,COLOR_BGR2GRAY);
cvtColor(img_object,img_object,COLOR_GRAY2BGR);
Mat drawing = img_object.clone();
/////////////////////////将cv::Mat转换为Bitmap(只能传输cv_8u3格式数据)///////////////////////////////
if (!drawing.data)
return nullptr;
Bitmap^ bitmap = MatToBitmap(drawing);
return bitmap;
}
private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl.SelectedIndex == 1)//只有在预览的时候打开图像处理
timer.Enabled = true;
else
timer.Enabled = false;
}
标签:window 算法 pix system init memory unsigned eth 方法
原文地址:https://www.cnblogs.com/jsxyhelu/p/9567597.html