private void Initialize() { try { // SenseManagerを生成する senseManager = PXCMSenseManager.CreateInstance(); if (senseManager == null) { throw new Exception("SenseManagerの生成に失敗しました"); } // 3Dスキャンを有効にする var sts = senseManager.Enable3DScan(); if (sts < pxcmStatus.PXCM_STATUS_NO_ERROR) { throw new Exception("初期化に失敗しました"); } // パイプラインを初期化する sts = senseManager.Init(); if (sts < pxcmStatus.PXCM_STATUS_NO_ERROR) { throw new Exception("初期化に失敗しました"); } // ミラー表示にする senseManager.QueryCaptureManager().QueryDevice().SetMirrorMode( PXCMCapture.Device.MirrorMode.MIRROR_MODE_HORIZONTAL); // 3Dスキャンの初期化 Initialize3dScan(); } catch (Exception ex) { MessageBox.Show(ex.Message); Close(); } }
public void StreamColorDepth(String scanType) /* Stream Color and Depth Synchronously or Asynchronously */ { bool sts = true; PXCM3DScan.Configuration scan_config = new PXCM3DScan.Configuration(); String statusString; /* Create an instance of the PXCSenseManager interface */ PXCMSenseManager pp = PXCMSenseManager.CreateInstance(); if (pp == null) { form.UpdateStatus("Failed to create sense manager"); return; } if (pp.captureManager == null) { form.UpdateStatus("Capture manager does not exist"); return; } if (!form.IsModeLive()) { pp.captureManager.SetFileName(form.GetFileName(), form.IsModeRecord()); } /* Set Input Source */ PXCMCapture.DeviceInfo dinfo2 = form.GetCheckedDevice(); if (form.IsModeLive() || form.IsModeRecord()) { pp.captureManager.FilterByDeviceInfo(dinfo2); } if (form.IsModeRecord()) { // Delay recording frames until the scan starts pp.captureManager.SetPause(true); } else if (!form.IsModeLive()) { // Disable real-time mode if we are playing back a file // to ensure that frames are not skipped. pp.captureManager.SetRealtime(false); } /* Set Color & Depth Resolution */ PXCMCapture.Device.StreamProfile cinfo = form.GetColorConfiguration(); if (cinfo.imageInfo.format != 0) { Single cfps = cinfo.frameRate.max; pp.EnableStream(PXCMCapture.StreamType.STREAM_TYPE_COLOR, cinfo.imageInfo.width, cinfo.imageInfo.height, cfps); } PXCMCapture.Device.StreamProfile dinfo = form.GetDepthConfiguration(); if (dinfo.imageInfo.format != 0) { Single dfps = dinfo.frameRate.max; pp.EnableStream(PXCMCapture.StreamType.STREAM_TYPE_DEPTH, dinfo.imageInfo.width, dinfo.imageInfo.height, dfps); } /* Initialization */ FPSTimer timer = new FPSTimer(form); if (form.IsModeLive()) { form.UpdateStatus("Initializing..."); } /* Enable the 3D Scan video module */ pxcmStatus result = pp.Enable3DScan(); if (result != pxcmStatus.PXCM_STATUS_NO_ERROR) { pp.Close(); pp.Dispose(); form.UpdateStatus("Enable3DScan() returned " + result); return; } /* Initialize the camera system */ result = pp.Init(); form.UpdateStatus(""); device = pp.captureManager.device; if (result >= pxcmStatus.PXCM_STATUS_NO_ERROR && device != null) { bool bAutoExpAndWBChanged = false; bool bAutoExposureEnabled = true; bool bAutoWhiteBalanceEnabled = true; /* Setup the scanning configuration */ if (scanType == "Object") { scan_config.mode = PXCM3DScan.ScanningMode.OBJECT_ON_PLANAR_SURFACE_DETECTION; } else if (scanType == "Face") { scan_config.mode = PXCM3DScan.ScanningMode.FACE; } else if (scanType == "Body") { scan_config.mode = PXCM3DScan.ScanningMode.BODY; } else if (scanType == "Head") { scan_config.mode = PXCM3DScan.ScanningMode.HEAD; } else if (scanType == "Full") { scan_config.mode = PXCM3DScan.ScanningMode.VARIABLE; } /* Select the Targeting Options */ scan_config.options = PXCM3DScan.ReconstructionOption.NONE; if (form.isSolidificationSelected()) { scan_config.options |= (PXCM3DScan.ReconstructionOption.SOLIDIFICATION); } if (form.isTextureSelected()) { scan_config.options |= (PXCM3DScan.ReconstructionOption.TEXTURE); } if (form.isLandmarksSelected()) { scan_config.options |= (PXCM3DScan.ReconstructionOption.LANDMARKS); } //scan_config.useMarker = form.isUseMarkerChecked(); scan_config.flopPreviewImage = form.isFlopPreviewImageSelected(); /* Try to initialize the scanning system */ PXCM3DScan scan = pp.Query3DScan(); sts = false; if (scan == null) { form.UpdateStatus("3DScan module not found."); } else { result = scan.SetConfiguration(scan_config); if (result < pxcmStatus.PXCM_STATUS_NO_ERROR) { scan.Dispose(); // Show the configuration related error code switch (result) { case pxcmStatus.PXCM_STATUS_FEATURE_UNSUPPORTED: form.UpdateStatus("Configuration not supported."); break; case pxcmStatus.PXCM_STATUS_ITEM_UNAVAILABLE: form.UpdateStatus("Face module not found."); break; default: form.UpdateStatus("SetConfiguration returned an error."); break; } } else { sts = true; } } // Conditionally finish the initialization and enter the main loop if (sts == true) { // Subscribe to recieve range and tracking alerts scan.Subscribe(OnAlert); Projection projection = new Projection( pp.session, device, dinfo.imageInfo); Boolean bScanning = false; Boolean bPlaybackStarted = false; form.Invoke(new Action(() => form.SetButtonState(sample3dscan.cs.MainForm.ButtonState.Ce_SSd))); while (form.reconstruct_requested || !form.GetStopState()) { if (form.GetScanRequested()) /* one time latch */ { form.Invoke(new Action(() => form.SetScanRequested(false))); // Delay recording frames until the start of the scan is requested if (form.IsModeRecord()) { pp.captureManager.SetPause(false); } // If the max tri/vert controls are enabled, // use the set values. Otherwise, disabled decimation // by setting the values to zero. /* * scan_config.maxTriangles = form.getMaxTrianglesEnabledChecked() * ? form.getMaxTriangles() : 0; * scan_config.maxVertices = form.getMaxVerticesEnabledChecked() * ? form.getMaxVertices() : 0; */ // Request that the scan starts as soon as possible scan_config.startScan = true; scan.SetConfiguration(scan_config); /* Update the status bar to help users understand what the detector is looking for */ if (form.IsModeLive()) { if (scan_config.mode == PXCM3DScan.ScanningMode.OBJECT_ON_PLANAR_SURFACE_DETECTION) { form.UpdateStatus("Object not detected. Place object on flat surface in center of view."); } } } else if (form.reconstruct_requested) { sts = SaveMesh(scan); } /* Get preview image from the 3D Scan video module */ if (!form.GetStopState()) { /* Wait until a frame is ready: Synchronized or Asynchronous */ if (pp.AcquireFrame() < pxcmStatus.PXCM_STATUS_NO_ERROR) { projection.Dispose(); if (!form.IsModeLive()) { form.Invoke(new Action(() => form.EndScan())); sts = SaveMesh(scan); } break; } /* Get preview image from the 3D Scan video module */ PXCMImage preview_image = scan.AcquirePreviewImage(); pp.ReleaseFrame(); /* Display Image and Status */ if (preview_image != null) { form.SetBitmap(preview_image); if (scan.IsScanning()) { statusString = "Scanning"; timer.Tick(statusString + " "); if (bScanning == false) // Lazy initializer { bScanning = true; // One way latch // Once the scanning process starts, we want to enable the Reconstruct button form.Invoke(new Action(() => form.SetButtonState(sample3dscan.cs.MainForm.ButtonState.Ce_ESe))); // Object, head and body scanning with a rear facing camera involves walking // around the target, which effectivly exposes the camera to the full // environment, similar to a panorama. To avoid undesirable color // inconsistencies (realted to the response of the auto-exposure/wb changes), // it usually works best to disable them. // Note that these property changes are restored (below). if (device.deviceInfo.orientation == PXCMCapture.DeviceOrientation.DEVICE_ORIENTATION_REAR_FACING && scan_config.mode != PXCM3DScan.ScanningMode.FACE && form.IsModeLive()) { bAutoExpAndWBChanged = true; bAutoExposureEnabled = device.QueryColorAutoExposure(); device.SetColorAutoExposure(false); bAutoWhiteBalanceEnabled = device.QueryColorAutoWhiteBalance(); device.SetColorAutoWhiteBalance(false); } } } else { if (!form.IsModeLive() && !form.IsModeRecord()) // In playback mode, automatically request the scan { if (bPlaybackStarted == false) // Lazy initializer { bPlaybackStarted = true; // One way latch form.scan_requested = true; form.Invoke(new Action(() => form.StartScanning(false))); form.Invoke(new Action(() => form.SetButtonState(sample3dscan.cs.MainForm.ButtonState.Ce_ESe))); } } else { if (!form.GetStopState()) { if (isScanReady(form.landmarksChecked())) { form.Invoke(new Action(() => form.EnableReconstruction(true))); } else { form.Invoke(new Action(() => form.EnableReconstruction(false))); } } } } preview_image.Dispose(); } } } projection.Dispose(); scan.Dispose(); } // Restore the default camera properties if (bAutoExpAndWBChanged) { device.SetColorAutoExposure(bAutoExposureEnabled); device.SetColorAutoWhiteBalance(bAutoWhiteBalanceEnabled); } device.Dispose(); device = null; } else { try { form.UpdateStatus(result + ""); } catch { } sts = false; } if (sts) { try { form.UpdateStatus(""); } catch { } } pp.Close(); pp.Dispose(); try { form.Invoke(new Action(() => form.ResetStop())); } catch { } }
private void PopulateDepthMenus(ToolStripMenuItem device_item) { PXCMSession.ImplDesc desc = new PXCMSession.ImplDesc(); desc.group = PXCMSession.ImplGroup.IMPL_GROUP_SENSOR; desc.subgroup = PXCMSession.ImplSubgroup.IMPL_SUBGROUP_VIDEO_CAPTURE; desc.iuid = devices_iuid[device_item]; desc.cuids[0] = PXCMCapture.CUID; DepthMenu.DropDownItems.Clear(); PXCMCapture capture; PXCMCapture.DeviceInfo dinfo2 = GetCheckedDevice(); PXCMSenseManager pp = session.CreateSenseManager(); if (pp == null) { return; } if (pp.Enable3DScan() < pxcmStatus.PXCM_STATUS_NO_ERROR) { return; } PXCM3DScan s = pp.Query3DScan(); if (s == null) { return; } PXCMVideoModule m = s.QueryInstance <PXCMVideoModule>(); if (m == null) { return; } if (session.CreateImpl <PXCMCapture>(desc, out capture) >= pxcmStatus.PXCM_STATUS_NO_ERROR) { PXCMCapture.Device device = capture.CreateDevice(dinfo2.didx); if (device != null) { PXCMCapture.Device.StreamProfileSet profile = new PXCMCapture.Device.StreamProfileSet();; PXCMCapture.Device.StreamProfile color_profile = GetColorConfiguration(); if (((int)dinfo2.streams & (int)PXCMCapture.StreamType.STREAM_TYPE_DEPTH) != 0) { for (int p = 0; ; p++) { if (device.QueryStreamProfileSet(PXCMCapture.StreamType.STREAM_TYPE_DEPTH, p, out profile) < pxcmStatus.PXCM_STATUS_NO_ERROR) { break; } PXCMCapture.Device.StreamProfile sprofile = profile[PXCMCapture.StreamType.STREAM_TYPE_DEPTH]; bool bFound = false; int i = 0; PXCMVideoModule.DataDesc inputs; while ((m.QueryCaptureProfile(i++, out inputs) >= pxcmStatus.PXCM_STATUS_NO_ERROR)) { if ((sprofile.imageInfo.height == inputs.streams.depth.sizeMax.height) && (sprofile.imageInfo.width == inputs.streams.depth.sizeMax.width) && (sprofile.frameRate.max == inputs.streams.depth.frameRate.max) && (color_profile.frameRate.max == inputs.streams.depth.frameRate.max)) { bFound = true; } } if (bFound) { if (sprofile.options != (PXCMCapture.Device.StreamOption) 0x20000) // do not show Depth Confidence { ToolStripMenuItem sm1 = new ToolStripMenuItem(ProfileToString(sprofile), null, new EventHandler(Depth_Item_Click)); profiles[sm1] = sprofile; DepthMenu.DropDownItems.Add(sm1); } } } } device.Dispose(); } capture.Dispose(); } m.Dispose(); pp.Dispose(); if (DepthMenu.DropDownItems.Count > 0) { (DepthMenu.DropDownItems[DepthMenu.DropDownItems.Count - 1] as ToolStripMenuItem).Checked = true; } }
private void PopulateColorMenus(ToolStripMenuItem device_item) { OperatingSystem os_version = Environment.OSVersion; PXCMSession.ImplDesc desc = new PXCMSession.ImplDesc(); desc.group = PXCMSession.ImplGroup.IMPL_GROUP_SENSOR; desc.subgroup = PXCMSession.ImplSubgroup.IMPL_SUBGROUP_VIDEO_CAPTURE; desc.iuid = devices_iuid[device_item]; desc.cuids[0] = PXCMCapture.CUID; profiles.Clear(); ColorMenu.DropDownItems.Clear(); PXCMCapture capture; PXCMCapture.DeviceInfo dinfo2 = GetCheckedDevice(); PXCMSenseManager pp = session.CreateSenseManager(); if (pp == null) { return; } if (pp.Enable3DScan() < pxcmStatus.PXCM_STATUS_NO_ERROR) { return; } PXCM3DScan s = pp.Query3DScan(); if (s == null) { return; } PXCMVideoModule m = s.QueryInstance <PXCMVideoModule>(); if (m == null) { return; } int count = 0; if (session.CreateImpl <PXCMCapture>(desc, out capture) >= pxcmStatus.PXCM_STATUS_NO_ERROR) { PXCMCapture.Device device = capture.CreateDevice(dinfo2.didx); if (device != null) { PXCMCapture.Device.StreamProfileSet profile = new PXCMCapture.Device.StreamProfileSet();; if (dinfo2.streams.HasFlag(PXCMCapture.StreamType.STREAM_TYPE_COLOR)) { for (int p = 0; ; p++) { if (device.QueryStreamProfileSet(PXCMCapture.StreamType.STREAM_TYPE_COLOR, p, out profile) < pxcmStatus.PXCM_STATUS_NO_ERROR) { break; } PXCMCapture.Device.StreamProfile sprofile = profile[PXCMCapture.StreamType.STREAM_TYPE_COLOR]; // Only populate profiles which are supported by the module bool bFound = false; int i = 0; PXCMVideoModule.DataDesc inputs; PXCMImage.PixelFormat format = PXCMImage.PixelFormat.PIXEL_FORMAT_RGB32; if (dinfo2.orientation != PXCMCapture.DeviceOrientation.DEVICE_ORIENTATION_REAR_FACING) { format = PXCMImage.PixelFormat.PIXEL_FORMAT_RGB24; } while ((m.QueryCaptureProfile(i++, out inputs) >= pxcmStatus.PXCM_STATUS_NO_ERROR)) { if ((sprofile.imageInfo.height == inputs.streams.color.sizeMax.height) && (sprofile.imageInfo.width == inputs.streams.color.sizeMax.width) && (sprofile.frameRate.max == inputs.streams.color.frameRate.max) && (sprofile.imageInfo.format == format) && (0 == (sprofile.options & PXCMCapture.Device.StreamOption.STREAM_OPTION_UNRECTIFIED))) { bFound = true; if (dinfo2.orientation != PXCMCapture.DeviceOrientation.DEVICE_ORIENTATION_REAR_FACING) { // Hide rear facing resolutions when the front facing camera is connected... if (sprofile.imageInfo.width == 640) { bFound = false; } } // On Windows 7, filter non-functional 30 fps modes if (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor == 1) { if (sprofile.frameRate.max == 30) { bFound = false; } } } } if (bFound) { ToolStripMenuItem sm1 = new ToolStripMenuItem(ProfileToString(sprofile), null, new EventHandler(Color_Item_Click)); profiles[sm1] = sprofile; ColorMenu.DropDownItems.Add(sm1); count++; } } } device.Dispose(); } capture.Dispose(); } m.Dispose(); pp.Dispose(); if (count > 0) { (ColorMenu.DropDownItems[ColorMenu.DropDownItems.Count - 1] as ToolStripMenuItem).Checked = true; } }
private void Initialize() { try { // SenseManagerを生成する senseManager = PXCMSenseManager.CreateInstance(); if ( senseManager == null ) { throw new Exception( "SenseManagerの生成に失敗しました" ); } // 3Dスキャンを有効にする var sts = senseManager.Enable3DScan(); if ( sts < pxcmStatus.PXCM_STATUS_NO_ERROR ) { throw new Exception( "初期化に失敗しました" ); } // パイプラインを初期化する sts = senseManager.Init(); if ( sts < pxcmStatus.PXCM_STATUS_NO_ERROR ) { throw new Exception( "初期化に失敗しました" ); } // ミラー表示にする senseManager.QueryCaptureManager().QueryDevice().SetMirrorMode( PXCMCapture.Device.MirrorMode.MIRROR_MODE_HORIZONTAL ); // 3Dスキャンの初期化 Initialize3dScan(); } catch ( Exception ex ) { MessageBox.Show( ex.Message ); Close(); } }