// Close the camera.
        static void CameraClose()
        {
            Thread.Sleep(1000);
            // Unsetup event channel.
            Pv.AttrUint32Set(GCamera.Handle, "EventsEnable1", 0);
            Pv.CameraEventCallbackUnRegister(GCamera.Handle, EventDone);

            // Clear the queue.
            Console.WriteLine("Clearing the queue...");
            Pv.CaptureQueueClear(GCamera.Handle);
            // close the camera
            Console.WriteLine("The camera is now closed.");
            Pv.CameraClose(GCamera.Handle);
        }
Exemple #2
0
        public static int LuminanceAdjust(ref tCamera Camera, double mean)
        {
            int result = 0;

            if (mean < lowIlluminance)
            {
                result = -1;
            }
            if (mean > highIlluminance)
            {
                result = 1;
            }
            if (result * lastStatus == -1)//如果上一级过暗,此级过亮(或反之)
            {
                if (gain == 0)
                {
                    //exposuretime = (exposuretime + lastExposuretime) / 2;
                    if ((int)(exposuretime * scale * 0.5) > 0)
                    {
                        scale *= 0.5;
                    }
                }
                else
                {
                    result = 0;//增益为整数,无法调整,直接不进行调整
                }
            }
            if (result == -1)
            {
                //低照度情况
                isNoon = false;
                //if (frame<=200)
                {
                    //自动增加曝光时间或增益来调整图像亮度
                    if (exposuretime >= 40000)
                    {
                        exposuretime = 40000;
                        if (gain < 10)
                        {
                            gain++;
                        }
                        else
                        {
                            gain = 10;
                        }
                    }
                    else if (exposuretime <= 10)
                    {
                        exposuretime = 10;
                    }
                    else if (10000 <= exposuretime && exposuretime < 40000)
                    {
                        exposuretime = exposuretime + (uint)(10000 * scale);
                        gain         = 0;
                    }
                    else if (1000 <= exposuretime && exposuretime < 10000)
                    {
                        exposuretime = exposuretime + (uint)(1000 * scale);
                        gain         = 0;
                    }
                    else if (exposuretime >= 100 && exposuretime < 1000)
                    {
                        exposuretime = exposuretime + (uint)(100 * scale);
                        gain         = 0;
                    }
                    else if (exposuretime >= 10 && exposuretime < 100)
                    {
                        exposuretime = exposuretime + (uint)(10 * scale);
                        gain         = 0;
                    }
                }
                if (exposuretime == 40000 && gain == 10)
                {
                    //当曝光时间和增益调整到最大值后,对低照度图像进行非线性变换调整图像质量
                    if (isNight == false)
                    {
                        isNight = true;
                    }
                    else
                    {
                        result = 0;
                    }
                }
            }
            else if (result == 1)
            {
                //强光照情况
                isNight = false;
                //if (frame<=200)
                {
                    //自动降低曝光时间和增益来降低图像亮度
                    if (gain > 0)
                    {
                        gain = 0;
                    }
                    else
                    {
                        if (exposuretime > 40000)
                        {
                            exposuretime = 40000;
                        }
                        else if (exposuretime <= 10)
                        {
                            exposuretime = 10;
                        }
                        else if (10000 < exposuretime && exposuretime <= 40000)
                        {
                            exposuretime = exposuretime - (uint)(10000 * scale);
                        }
                        else if (1000 < exposuretime && exposuretime <= 10000)
                        {
                            exposuretime = exposuretime - (uint)(1000 * scale);
                        }
                        else if (exposuretime > 100 && exposuretime <= 1000)
                        {
                            exposuretime = exposuretime - (uint)(100 * scale);
                        }
                        else if (exposuretime > 10 && exposuretime <= 100)
                        {
                            exposuretime = exposuretime - (uint)(10 * scale);
                        }
                    }
                }
                if (exposuretime == 10 && gain == 0)
                {
                    //当无法对曝光时间和增益进行调整时对图像进行gamma变换来调整图像亮度
                    if (isNoon == false)
                    {
                        isNoon = true;
                    }
                    else
                    {
                        result = 0;
                    }
                }
            }
            else if (mean < highIlluminance && mean >= lowIlluminance)
            {
                //正常光照情况,不对图像进行任何处理
            }
            if (result != 0)
            {
                Pv.AttrUint32Set(Camera.Handle, "ExposureValue", exposuretime);
                Pv.AttrUint32Set(Camera.Handle, "GainValue", gain);
            }
            lastStatus = result;
            Console.WriteLine("Brightness: {0}, ExposureValue: {1}, GainValue: {2}", mean, exposuretime, gain);
            return(result);
        }
        // Setup the camera up for streaming.
        static bool CameraStart()
        {
            tFrameCallback       lFrameCB = new tFrameCallback(FrameDoneCB);
            tCameraEventCallback lEventCB = new tCameraEventCallback(EventDone);
            UInt32 FrameSize = 0;
            IntPtr Context   = IntPtr.Zero;


            if (Pv.AttrExists(GCamera.Handle, "EventsEnable1") == tErr.eErrNotFound)
            {
                Console.WriteLine("This camera does not support event notifications.");
                return(false);
            }

            // Adjust packet size for optimal performance.
            Pv.CaptureAdjustPacketSize(GCamera.Handle, 8228);

            // Determines how big the frame buffers should be.
            if (Pv.AttrUint32Get(GCamera.Handle, "TotalBytesPerFrame", ref FrameSize) == 0)
            {
                tFrame   Frame   = new tFrame();
                byte[]   Buffer  = new byte[FrameSize];
                GCHandle pBuffer = GCHandle.Alloc(Buffer, GCHandleType.Pinned);

                // Set the frame's fields.
                // Handle to the Camera.
                Frame.Context.Field0 = new IntPtr(GCamera.Handle);
                // Address of the pinned object.
                Frame.ImageBuffer = pBuffer.AddrOfPinnedObject();
                // Buffer size.
                Frame.ImageBufferSize = FrameSize;

                // Setup the event channel.
                Pv.AttrUint32Set(GCamera.Handle, "EventsEnable1", 0);
                Pv.AttrEnumSet(GCamera.Handle, "EventSelector", "AcquisitionStart");
                Pv.AttrEnumSet(GCamera.Handle, "EventNotification", "On");
                Pv.AttrEnumSet(GCamera.Handle, "EventSelector", "AcquisitionEnd");
                Pv.AttrEnumSet(GCamera.Handle, "EventNotification", "On");
                Pv.AttrEnumSet(GCamera.Handle, "EventSelector", "FrameTrigger");
                Pv.AttrEnumSet(GCamera.Handle, "EventNotification", "On");


                if (Pv.CameraEventCallbackRegister(GCamera.Handle, lEventCB, Context) != tErr.eErrSuccess)
                {
                    Console.WriteLine("There was an error accessing the driver.");
                    return(false);
                }

                // Start the capture mode.
                if (Pv.CaptureStart(GCamera.Handle) == 0)
                {
                    if (Pv.AttrFloat32Set(GCamera.Handle, "FrameRate", 5) == 0)
                    {
                        // Set the camera in continuous acquisition mode,and in "Freerun".
                        if (Pv.AttrEnumSet(GCamera.Handle, "FrameStartTriggerMode", "FixedRate") == 0)
                        {
                            // Set the acquisition mode into continuous.
                            if (Pv.CommandRun(GCamera.Handle, "AcquisitionStart") != 0)
                            {
                                // If that fails, reset the camera to non-capture mode.
                                Pv.CaptureEnd(GCamera.Handle);

                                Console.WriteLine("Failed to start.");
                                return(false);
                            }
                            else
                            {
                                // Pin down a copy of the frame structure.
                                GCHandle pFrame = GCHandle.Alloc(Frame, GCHandleType.Pinned);

                                // Enqueue the frame.
                                Pv.CaptureQueueFrame(GCamera.Handle, pFrame.AddrOfPinnedObject(), lFrameCB);

                                return(true);
                            }
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }