// Use this for initialization void Start() { Image <Bgr, Byte> img = new Image <Bgr, byte>(640, 240); String openclStr = "None"; if (CvInvoke.HaveOpenCL) { //StringBuilder builder = new StringBuilder(); using (VectorOfOclPlatformInfo oclPlatformInfos = OclInvoke.GetPlatformInfo()) { if (oclPlatformInfos.Size > 0) { OclPlatformInfo platformInfo = oclPlatformInfos[0]; openclStr = platformInfo.ToString(); } } } CvInvoke.PutText(img, String.Format("Emgu CV for Unity {0}", Emgu.Util.Platform.OperationSystem), new System.Drawing.Point(10, 60), Emgu.CV.CvEnum.FontFace.HersheyDuplex, 1.0, new MCvScalar(0, 255, 0)); CvInvoke.PutText(img, String.Format("OpenCL: {0}", openclStr), new System.Drawing.Point(10, 120), Emgu.CV.CvEnum.FontFace.HersheyDuplex, 1.0, new MCvScalar(0, 0, 255)); Texture2D texture = TextureConvert.ImageToTexture2D(img, FlipType.Vertical); this.GetComponent <GUITexture>().texture = texture; this.GetComponent <GUITexture>().pixelInset = new Rect(-img.Width / 2, -img.Height / 2, img.Width, img.Height); }
// Use this for initialization void Start() { Mat img = new Mat(new Size(640, 240), DepthType.Cv8U, 3); img.SetTo(new MCvScalar()); String openclStr = "None"; if (CvInvoke.HaveOpenCL) { //StringBuilder builder = new StringBuilder(); using (VectorOfOclPlatformInfo oclPlatformInfos = OclInvoke.GetPlatformsInfo()) { if (oclPlatformInfos.Size > 0) { PlatformInfo platformInfo = oclPlatformInfos[0]; openclStr = platformInfo.ToString(); } } } CvInvoke.PutText(img, String.Format("Emgu CV for Unity {0}", Emgu.Util.Platform.OperationSystem), new System.Drawing.Point(10, 60), Emgu.CV.CvEnum.FontFace.HersheyDuplex, 1.0, new MCvScalar(0, 255, 0)); CvInvoke.PutText(img, String.Format("OpenCL: {0}", openclStr), new System.Drawing.Point(10, 120), Emgu.CV.CvEnum.FontFace.HersheyDuplex, 1.0, new MCvScalar(0, 0, 255)); Texture2D texture = TextureConvert.InputArrayToTexture2D(img, FlipType.Vertical); RenderTexture(texture); ResizeTexture(texture); }
public void TestOclChangeDefaultDevice() { if (CvInvoke.HaveOpenCL && CvInvoke.UseOpenCL) { using (VectorOfOclPlatformInfo oclPlatformInfos = Ocl.OclInvoke.GetPlatformsInfo()) { if (oclPlatformInfos.Size > 0) { for (int i = 0; i < oclPlatformInfos.Size; i++) { Ocl.PlatformInfo platformInfo = oclPlatformInfos[i]; for (int j = 0; j < platformInfo.DeviceNumber; j++) { Ocl.Device device = platformInfo.GetDevice(j); Trace.WriteLine(String.Format("{0}Setting device to {1}", Environment.NewLine, device.Name)); //OclDevice d = new OclDevice(); //d.Set(device.NativeDevicePointer); Ocl.Device defaultDevice = Ocl.Device.Default; defaultDevice.Set(device.NativeDevicePointer); Trace.WriteLine(String.Format("Current OpenCL default device: {0}", defaultDevice.Name)); UMat m = new UMat(2048, 2048, DepthType.Cv8U, 3); m.SetTo(new MCvScalar(100, 100, 100)); CvInvoke.GaussianBlur(m, m, new Size(3, 3), 3); Stopwatch watch = Stopwatch.StartNew(); m.SetTo(new MCvScalar(100, 100, 100)); CvInvoke.GaussianBlur(m, m, new Size(3, 3), 3); watch.Stop(); Trace.WriteLine(String.Format("Device '{0}' time: {1} milliseconds", defaultDevice.Name, watch.ElapsedMilliseconds)); CvInvoke.OclFinish(); } } } Trace.WriteLine(Environment.NewLine + "Disable OpenCL"); CvInvoke.UseOpenCL = false; UMat m2 = new UMat(2048, 2048, DepthType.Cv8U, 3); m2.SetTo(new MCvScalar(100, 100, 100)); CvInvoke.GaussianBlur(m2, m2, new Size(3, 3), 3); Stopwatch watch2 = Stopwatch.StartNew(); m2.SetTo(new MCvScalar(100, 100, 100)); CvInvoke.GaussianBlur(m2, m2, new Size(3, 3), 3); watch2.Stop(); Trace.WriteLine(String.Format("CPU time: {0} milliseconds", watch2.ElapsedMilliseconds)); CvInvoke.UseOpenCL = true; } } }
protected override View OnCreateDialogView() { LayoutInflater inflator = LayoutInflater.FromContext(this.Context); View dialog = inflator.Inflate(Resource.Layout.opencl_preference, null); _openCLRadioGroup = dialog.FindViewById <RadioGroup>(Resource.Id.opencl_preference_radio_group); AppPreference preference = new AppPreference(); RadioButton checkedButton = null; RadioButton cpuButton = new RadioButton(this.Context); cpuButton.Text = "CPU (no OpenCL)"; _openCLRadioGroup.AddView(cpuButton); //int selectedIdx = -1; if (preference.UseOpenCL == false) { checkedButton = cpuButton; } cpuButton.Click += (sender, args) => { preference.UseOpenCL = false; //Toast.MakeText(this.Context, "cpu clicked", ToastLength.Short).Show(); }; String selectedDeviceName = preference.OpenClDeviceName; if (selectedDeviceName == null && CvInvoke.HaveOpenCL && preference.UseOpenCL) { selectedDeviceName = Emgu.CV.Ocl.Device.Default.Name; } if (CvInvoke.HaveOpenCL) { using (VectorOfOclPlatformInfo oclPlatformInfos = Emgu.CV.Ocl.OclInvoke.GetPlatformsInfo()) { if (oclPlatformInfos.Size > 0) { for (int i = 0; i < oclPlatformInfos.Size; i++) { Emgu.CV.Ocl.PlatformInfo platformInfo = oclPlatformInfos[i]; for (int j = 0; j < platformInfo.DeviceNumber; j++) { Emgu.CV.Ocl.Device device = platformInfo.GetDevice(j); RadioButton deviceButton = new RadioButton(this.Context); deviceButton.Text = "OpenCL: " + device.Name; if (preference.UseOpenCL == true && device.Name.Equals(selectedDeviceName)) { checkedButton = deviceButton; } _openCLRadioGroup.AddView(deviceButton); //counter++; deviceButton.Click += (sender, args) => { preference.UseOpenCL = true; preference.OpenClDeviceName = device.Name; //Toast.MakeText(this.Context, device.Name + " clicked", ToastLength.Short).Show(); }; } } } } } if (checkedButton != null) { _openCLRadioGroup.Check(checkedButton.Id); } //_openCLRadioGroup.in /* * _openCLToggleButton.Checked = preference.UseOpenCL; * * _openCLToggleButton.CheckedChange += (sender, args) => * { * bool isChecked = args.IsChecked; * * if (isChecked && !CvInvoke.HaveOpenCL) * { * _openCLToggleButton.Checked = false; * Toast.MakeText(Context, "No OpenCL compatible device found.", ToastLength.Long).Show(); * isChecked = false; * } * * preference.UseOpenCL = isChecked; * }; */ return(dialog); }