Пример #1
0
        /// <summary>
        /// Check if the camera can be triggered by a software
        /// </summary>
        /// <returns>True if it can, false if not</returns>
        public bool CheckSoftwareTriggerPresence()
        {
            const uint TriggerInquiry      = 0x530;
            uint       triggerInquiryValue = cam.ReadRegister(TriggerInquiry);

            if ((triggerInquiryValue & 0x10000) != 0x10000)
            {
                return(false);
            }

            return(true);
        }
Пример #2
0
        static bool CheckSoftwareTriggerPresence(ManagedCamera cam)
        {
            const uint k_triggerInq = 0x530;
            uint regVal = cam.ReadRegister(k_triggerInq);

            if ((regVal & 0x10000) != 0x10000)
            {
                return false;
            }

            return true;
        }
Пример #3
0
        static bool CheckSoftwareTriggerPresence(ManagedCamera cam)
        {
            const uint k_triggerInq = 0x530;
            uint       regVal       = cam.ReadRegister(k_triggerInq);

            if ((regVal & 0x10000) != 0x10000)
            {
                return(false);
            }

            return(true);
        }
Пример #4
0
        static bool PollForTriggerReady(ManagedCamera cam)
        {
            const uint SoftwareTrigger = 0x62C;

            uint softwareTriggerValue = 0;

            do
            {
                softwareTriggerValue = cam.ReadRegister(SoftwareTrigger);
            }while ((softwareTriggerValue >> 31) != 0);

            return(true);
        }
Пример #5
0
        static bool PollForTriggerReady(ManagedCamera cam)
        {
            const uint k_softwareTrigger = 0x62C;

            uint regVal = 0;

            do
            {
                regVal = cam.ReadRegister(k_softwareTrigger);
            }while ((regVal >> 31) != 0);

            return(true);
        }
Пример #6
0
        static bool PollForTriggerReady(ManagedCamera cam)
        {
            const uint k_softwareTrigger = 0x62C;

            uint regVal = 0;

            do
            {
                regVal = cam.ReadRegister(k_softwareTrigger);
            }
            while ((regVal >> 31) != 0);

            return true;
        }
Пример #7
0
        static void Main(string[] args)
        {
            PrintBuildInfo();

            const int NumImages          = 10;
            bool      useSoftwareTrigger = true;

            ManagedBusManager busMgr = new ManagedBusManager();
            uint numCameras          = busMgr.GetNumOfCameras();

            Console.WriteLine("Number of cameras detected: {0}", numCameras);

            // Finish if there are no cameras
            if (numCameras == 0)
            {
                Console.WriteLine("Not enough cameras!");
                Console.WriteLine("Press Enter to exit...");
                Console.ReadLine();
                return;
            }

            ManagedPGRGuid guid = busMgr.GetCameraFromIndex(0);

            ManagedCamera cam = new ManagedCamera();

            cam.Connect(guid);

            // Power on the camera
            const uint CameraPower      = 0x610;
            const uint CameraPowerValue = 0x80000000;

            cam.WriteRegister(CameraPower, CameraPowerValue);

            const Int32 MillisecondsToSleep  = 100;
            uint        cameraPowerValueRead = 0;

            // Wait for camera to complete power-up
            do
            {
                System.Threading.Thread.Sleep(MillisecondsToSleep);

                cameraPowerValueRead = cam.ReadRegister(CameraPower);
            }while ((cameraPowerValueRead & CameraPowerValue) == 0);

            // Get the camera information
            CameraInfo camInfo = cam.GetCameraInfo();

            PrintCameraInfo(camInfo);

            if (!useSoftwareTrigger)
            {
                // Check for external trigger support
                TriggerModeInfo triggerModeInfo = cam.GetTriggerModeInfo();
                if (triggerModeInfo.present != true)
                {
                    Console.WriteLine("Camera does not support external trigger!");
                    Console.WriteLine("Press enter to exit...");
                    Console.ReadLine();
                    return;
                }
            }

            // Get current trigger settings
            TriggerMode triggerMode = cam.GetTriggerMode();

            // Set camera to trigger mode 0
            // A source of 7 means software trigger
            triggerMode.onOff     = true;
            triggerMode.mode      = 0;
            triggerMode.parameter = 0;

            if (useSoftwareTrigger)
            {
                // A source of 7 means software trigger
                triggerMode.source = 7;
            }
            else
            {
                // Triggering the camera externally using source 0.
                triggerMode.source = 0;
            }

            // Set the trigger mode
            cam.SetTriggerMode(triggerMode);

            // Poll to ensure camera is ready
            bool retVal = PollForTriggerReady(cam);

            if (retVal != true)
            {
                Console.WriteLine("Poll for trigger read failed!");
                Console.WriteLine("Press enter to exit...");
                Console.ReadLine();
                return;
            }

            // Get the camera configuration
            FC2Config config = cam.GetConfiguration();

            // Set the grab timeout to 5 seconds
            config.grabTimeout = 5000;

            // Set the camera configuration
            cam.SetConfiguration(config);

            // Camera is ready, start capturing images
            cam.StartCapture();

            if (useSoftwareTrigger)
            {
                if (CheckSoftwareTriggerPresence(cam) == false)
                {
                    Console.WriteLine("SOFT_ASYNC_TRIGGER not implemented on this camera!  Stopping application\n");
                    Console.WriteLine("Press enter to exit...");
                    Console.ReadLine();
                    return;
                }
            }
            else
            {
                Console.WriteLine("Trigger the camera by sending a trigger pulse to GPIO%d.\n",
                                  triggerMode.source);
            }

            ManagedImage rawImage = new ManagedImage();

            for (int iImageCount = 0; iImageCount < NumImages; iImageCount++)
            {
                if (useSoftwareTrigger)
                {
                    // Check that the trigger is ready
                    retVal = PollForTriggerReady(cam);

                    Console.WriteLine("Press the Enter key to initiate a software trigger.\n");
                    Console.ReadLine();

                    // Fire software trigger
                    retVal = FireSoftwareTrigger(cam);
                    if (retVal != true)
                    {
                        Console.WriteLine("Error firing software trigger!");
                        Console.WriteLine("Press enter to exit...");
                        Console.ReadLine();
                        return;
                    }
                }

                try
                {
                    // Retrieve an image
                    cam.RetrieveBuffer(rawImage);
                }
                catch (FC2Exception ex)
                {
                    Console.WriteLine("Error retrieving buffer : {0}", ex.Message);
                    continue;
                }

                Console.WriteLine(".\n");
            }

            Console.WriteLine("Finished grabbing images");

            // Stop capturing images
            cam.StopCapture();

            // Turn off trigger mode
            triggerMode.onOff = false;
            cam.SetTriggerMode(triggerMode);

            // Disconnect the camera
            cam.Disconnect();

            Console.WriteLine("Done! Press enter to exit...");
            Console.ReadLine();
        }
Пример #8
0
        public FlyCapture()
        {
            ColorProcessing = ColorProcessingAlgorithm.Default;
            source          = Observable.Create <FlyCaptureDataFrame>((observer, cancellationToken) =>
            {
                return(Task.Factory.StartNew(() =>
                {
                    lock (captureLock)
                    {
                        ManagedCamera camera;
                        using (var manager = new ManagedBusManager())
                        {
                            var guid = manager.GetCameraFromIndex((uint)Index);
                            camera = new ManagedCamera();
                            camera.Connect(guid);

                            // Power on the camera
                            const uint CameraPower = 0x610;
                            const uint CameraPowerValue = 0x80000000;
                            camera.WriteRegister(CameraPower, CameraPowerValue);

                            // Wait for camera to complete power-up
                            const Int32 MillisecondsToSleep = 100;
                            uint cameraPowerValueRead = 0;
                            do
                            {
                                Thread.Sleep(MillisecondsToSleep);
                                cameraPowerValueRead = camera.ReadRegister(CameraPower);
                            }while ((cameraPowerValueRead & CameraPowerValue) == 0);
                        }

                        var capture = 0;
                        try
                        {
                            // Set frame rate
                            var prop = new CameraProperty(PropertyType.FrameRate);
                            prop.absControl = true;
                            prop.absValue = FramesPerSecond;
                            prop.autoManualMode = false;
                            prop.onOff = true;
                            camera.SetProperty(prop);

                            // Enable/disable blackfly pull up
                            const uint pullUp = 0x19D0;
                            if (EnableBlackflyOutputVoltage)
                            {
                                camera.WriteRegister(pullUp, 0x10000001);
                            }
                            else
                            {
                                camera.WriteRegister(pullUp, 0x10000000);
                            }

                            // Acquisition parameters
                            var colorProcessing = ColorProcessing;
                            var autoExposure = !AutoExposure; // Horrible hack to trigger update inititally
                            var shutter = Shutter;
                            var gain = Gain;

                            // Configure embedded info
                            const uint embeddedInfo = 0x12F8;
                            uint embeddedInfoState = camera.ReadRegister(embeddedInfo);
                            if (EnableEmbeddedFrameCounter)
                            {
                                embeddedInfoState |= (uint)1 << 6;
                            }
                            else
                            {
                                embeddedInfoState &= ~((uint)1 << 6);
                            }

                            if (EnableEmbeddedFrameTimeStamp)
                            {
                                embeddedInfoState |= (uint)1 << 0;
                            }
                            else
                            {
                                embeddedInfoState &= ~((uint)1 << 0);
                            }

                            camera.WriteRegister(embeddedInfo, embeddedInfoState);

                            using (var image = new ManagedImage())
                                using (var notification = cancellationToken.Register(() =>
                                {
                                    Interlocked.Exchange(ref capture, 0);
                                    camera.StopCapture();
                                }))
                                {
                                    camera.StartCapture();
                                    Interlocked.Exchange(ref capture, 1);
                                    while (!cancellationToken.IsCancellationRequested)
                                    {
                                        IplImage output;
                                        BayerTileFormat bayerTileFormat;

                                        if (autoExposure != AutoExposure && AutoExposure)
                                        {
                                            prop = new CameraProperty(PropertyType.AutoExposure);
                                            prop.autoManualMode = true;
                                            prop.onOff = true;
                                            camera.SetProperty(prop);
                                            autoExposure = AutoExposure;

                                            // Shutter
                                            prop = new CameraProperty(PropertyType.Shutter);
                                            prop.absControl = true;
                                            prop.autoManualMode = true;
                                            prop.onOff = true;
                                            camera.SetProperty(prop);

                                            // Shutter
                                            prop = new CameraProperty(PropertyType.Gain);
                                            prop.absControl = true;
                                            prop.autoManualMode = true;
                                            prop.onOff = true;
                                            camera.SetProperty(prop);

                                            autoExposure = AutoExposure;
                                        }
                                        else if (autoExposure != AutoExposure && !AutoExposure)
                                        {
                                            shutter = -0.1f; // Hack
                                            gain = -0.1f;

                                            autoExposure = AutoExposure;
                                        }

                                        if (shutter != Shutter && !AutoExposure)
                                        {
                                            // Figure out max shutter time given current frame rate
                                            var info = camera.GetPropertyInfo(PropertyType.Shutter);
                                            var delta = info.absMax - info.absMin;

                                            prop = new CameraProperty(PropertyType.Shutter);
                                            prop.absControl = true;
                                            prop.absValue = Shutter * delta + info.absMin;
                                            prop.autoManualMode = false;
                                            prop.onOff = true;
                                            camera.SetProperty(prop);

                                            shutter = Shutter;
                                        }

                                        if (gain != Gain && !AutoExposure)
                                        {
                                            // Figure out max shutter time given current frame rate
                                            var info = camera.GetPropertyInfo(PropertyType.Shutter);
                                            var delta = info.absMax - info.absMin;

                                            prop = new CameraProperty(PropertyType.Gain);
                                            prop.absControl = true;
                                            prop.absValue = Gain * delta + info.absMin;;
                                            prop.autoManualMode = false;
                                            prop.onOff = true;
                                            camera.SetProperty(prop);

                                            gain = Gain;
                                        }

                                        try { camera.RetrieveBuffer(image); }
                                        catch (FC2Exception ex)
                                        {
                                            if (capture == 0)
                                            {
                                                break;
                                            }
                                            else if (IgnoreImageConsistencyError && ex.CauseType == ErrorType.ImageConsistencyError)
                                            {
                                                continue;
                                            }
                                            else
                                            {
                                                throw;
                                            }
                                        }

                                        if (image.pixelFormat == PixelFormat.PixelFormatMono8 ||
                                            image.pixelFormat == PixelFormat.PixelFormatMono16 ||
                                            (image.pixelFormat == PixelFormat.PixelFormatRaw8 &&
                                             (image.bayerTileFormat == BayerTileFormat.None ||
                                              colorProcessing == ColorProcessingAlgorithm.NoColorProcessing)))
                                        {
                                            unsafe
                                            {
                                                bayerTileFormat = image.bayerTileFormat;
                                                var depth = image.pixelFormat == PixelFormat.PixelFormatMono16 ? IplDepth.U16 : IplDepth.U8;
                                                var bitmapHeader = new IplImage(new Size((int)image.cols, (int)image.rows), depth, 1, new IntPtr(image.data));
                                                output = new IplImage(bitmapHeader.Size, bitmapHeader.Depth, bitmapHeader.Channels);
                                                CV.Copy(bitmapHeader, output);
                                            }
                                        }
                                        else
                                        {
                                            unsafe
                                            {
                                                bayerTileFormat = BayerTileFormat.None;
                                                output = new IplImage(new Size((int)image.cols, (int)image.rows), IplDepth.U8, 3);
                                                using (var convertedImage = new ManagedImage(
                                                           (uint)output.Height,
                                                           (uint)output.Width,
                                                           (uint)output.WidthStep,
                                                           (byte *)output.ImageData.ToPointer(),
                                                           (uint)(output.WidthStep * output.Height),
                                                           PixelFormat.PixelFormatBgr))
                                                {
                                                    convertedImage.colorProcessingAlgorithm = colorProcessing;
                                                    image.Convert(PixelFormat.PixelFormatBgr, convertedImage);
                                                }
                                            }
                                        }

                                        observer.OnNext(new FlyCaptureDataFrame(output, image.imageMetadata, bayerTileFormat));
                                    }
                                }
                        }
                        finally
                        {
                            // Power off the camera
                            const uint CameraPower = 0x610;
                            const uint CameraPowerValue = 0x00000000;
                            camera.WriteRegister(CameraPower, CameraPowerValue);

                            if (capture != 0)
                            {
                                camera.StopCapture();
                            }
                            camera.Disconnect();
                            camera.Dispose();
                        }
                    }
                },
                                             cancellationToken,
                                             TaskCreationOptions.LongRunning,
                                             TaskScheduler.Default));
            })
                              .PublishReconnectable()
                              .RefCount();
        }