VisionMaster4.2使用OpenCV
VisionMaster4.2使用OpenCV确保 OpenCvSharpExtern.dll 与该目录下的 VmModuleProxy.exe 在同一层级。C:\Program Files\VisionMaster4.2.0\Applications\VmModuleProxy\x64using System;using System.Text;using System.Windows.Forms;using Script.Methods;using OpenCvSharp;public partial class UserScript:ScriptMethods,IProcessMethods{//the count of process//执行次数计数intprocessCount;/// summary/// Initialize the fields value when compiling/// 预编译时变量初始化/// /summarypublicvoidInit(){//You can add other global fields here//变量初始化其余变量可在该函数中添加processCount0;}/// summary/// Enter the process function when running code once/// 流程执行一次进入Process函数/// /summary/// returns/returnspublic boolProcess(){//You can add your codes here, for realizing your desired function//每次执行将进入该函数此处添加所需的逻辑流程处理// 读取图像Mat img1newMat(D:\\halcon2.png,ImreadModes.Color);Cv2.ImWrite(D:\\show.jpg,img1);returntrue;}}VisionMaster和OpenCVSharp互转Mat和图像变量新版本using System;using System.Text;using System.Windows.Forms;using Script.Methods;using OpenCvSharp;using System.Runtime.InteropServices;public partial class UserScript:ScriptMethods,IProcessMethods{//the count of process//执行次数计数intprocessCount;/// summary/// Initialize the fields value when compiling/// 预编译时变量初始化/// /summarypublicvoidInit(){//You can add other global fields here//变量初始化其余变量可在该函数中添加processCount0;}/// summary/// Enter the process function when running code once/// 流程执行一次进入Process函数/// /summary/// returns/returnspublic boolProcess(){//You can add your codes here, for realizing your desired function//每次执行将进入该函数此处添加所需的逻辑流程处理//实例化ImageData类型图像ImageData imgnewImageData();GetImageValue(in0,ref img);ImageData imgOutnewImageData();Mat srcImageMat.Zeros(img.Heigth,img.Width,MatType.CV_8UC1);Rect rectnewRect(0,0,300,300);if(img.PixelFormatImagePixelFormate.MONO8){//开辟内存空间IntPtr grayPtrMarshal.AllocHGlobal(img.Width*img.Heigth);//向内存空间中写入数据Marshal.Copy(img.Buffer,0,grayPtr,img.Buffer.Length);//imagedata转MatsrcImagenewMat(img.Heigth,img.Width,MatType.CV_8UC1,grayPtr);//设置ROIMat imageROInewMat(srcImage,rect);Mat dstImageMat.Zeros(imageROI.Height,imageROI.Width,MatType.CV_8UC1);//调用OpenCV中的接口进行图像处理Cv2.Threshold(imageROI,dstImage,10,120,ThresholdTypes.Otsu);//将处理后的图像拷贝到原图ROI区域dstImage.CopyTo(imageROI);// 修复正确地将Mat转换为byte数组byte[]databnew byte[srcImage.Width*srcImage.Height];// 方法1使用GetArray如果可用// 如果OpenCvSharp版本支持使用下面的代码// srcImage.GetArray(out datab);// 方法2使用GetGenericIndexer推荐if(srcImage.Type()MatType.CV_8UC1){var indexersrcImage.GetGenericIndexerbyte();for(inti0;isrcImage.Height;i){for(intj0;jsrcImage.Width;j){datab[i*srcImage.Widthj]indexer[i,j];}}}//mat转ImageDataimgOut.Bufferdatab;imgOut.WidthsrcImage.Width;imgOut.HeigthsrcImage.Height;imgOut.PixelFormatImagePixelFormate.MONO8;//用完记得释放指针Marshal.FreeHGlobal(grayPtr);}SetImageValue(imageOut,imgOut);returntrue;}}旧版本//实例化ImageData类型图像ImageDataimgnewImageData();GetImageValue(in0,refimg);ImageDataimgOutnewImageData();MatsrcImageMat.Zeros(img.Heigth,img.Width,MatType.CV_8UC1);RectrectnewRect(0,0,300,300);if(img.PixelFormatImagePixelFormate.MONO8){//开辟内存空间IntPtrgrayPtrMarshal.AllocHGlobal(img.Width*img.Heigth);//向内存空间中写入数据Marshal.Copy(img.Buffer,0,grayPtr,img.Buffer.Length);//imagedata转MatsrcImagenewMat(img.Heigth,img.Width,MatType.CV_8UC1,grayPtr);//设置ROIMatimageROInewMat(srcImage,rect);MatdstImageMat.Zeros(imageROI.Height,imageROI.Width,MatType.CV_8UC1);//调用OpenCV中的接口进行图像处理Cv2.Threshold(imageROI,dstImage,10,120,ThresholdTypes.Otsu);//将处理后的图像拷贝到原图ROI区域dstImage.CopyTo(imageROI);byte[]databnewByte[srcImage.Width*srcImage.Height];//mat转ImageDatasrcImage.GetArray(0,0,datab);imgOut.Bufferdatab;imgOut.WidthsrcImage.Width;imgOut.HeigthsrcImage.Height;imgOut.PixelFormatImagePixelFormate.MONO8;//用完记得释放指针Marshal.FreeHGlobal(grayPtr);}SetImageValue(imageOut,imgOut);
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2431549.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!