Esempio n. 1
0
 public static Tuple <Mat, FrameMetaData> GetMetaMatTuple(Mat mat, FrameMetaData metaData)
 {
     return(Tuple.Create(item1: mat, item2: metaData));
 }
Esempio n. 2
0
 public static Tuple <Mat, FrameMetaData>[] GetStreamOutput(Mat mat, FrameMetaData metaData)
 {
     return(new Tuple <Mat, FrameMetaData>[1] {
         Tuple.Create(item1: mat, item2: metaData)
     });
 }
Esempio n. 3
0
                public void Run()
                {
                    Console.WriteLine("Run was called");
                    while (true)
                    {
                        if (state == BasicCamLoopState.Waiting)
                        {
                            isMessageDequeueSuccess = messageQueue.TryDequeue(out ButtonCommands message);
                            if (isMessageDequeueSuccess)
                            {
                                Console.WriteLine("message received on camera: {0}", message);
                                state = handler.UpdateState(state: state, button: message);

                                if (state == BasicCamLoopState.Streaming && !managedCamera.IsStreaming())
                                {
                                    ResetCounter();
                                    managedCamera.BeginAcquisition();
                                }
                                continue;
                            }

                            Thread.Sleep(100);
                            continue;
                        }

                        else if (state == BasicCamLoopState.Streaming)
                        {
                            try
                            {
                                using (IManagedImage rawImage = managedCamera.GetNextImage())
                                {
                                    imageCtr += 1;
                                    long frameID   = rawImage.ChunkData.FrameID;
                                    long timestamp = rawImage.ChunkData.Timestamp;

                                    if (imageCtr == 1)
                                    {
                                        currFrameMetaData = new FrameMetaData(streamCtr: imageCtr, frameID: frameID, timestamp: timestamp);
                                        continue;
                                    }

                                    prevFrameMetaData = currFrameMetaData;
                                    currFrameMetaData = new FrameMetaData(streamCtr: imageCtr, frameID: frameID, timestamp: timestamp);

                                    if (imageCtr % enqueueRate == 0)
                                    {
                                        Mat fullMat = new Mat(rows: inputSize.Height, cols: inputSize.Width,
                                                              type: Emgu.CV.CvEnum.DepthType.Cv8U, channels: 1, data: rawImage.DataPtr, step: inputSize.Width);

                                        if (isResizeNeeded)
                                        {
                                            Mat resizedMat = new Mat(size: outputSize, type: Emgu.CV.CvEnum.DepthType.Cv8U, channels: 1);
                                            CvInvoke.Resize(src: fullMat, dst: resizedMat, dsize: outputSize, interpolation: Emgu.CV.CvEnum.Inter.Linear);
                                            Tuple <Mat, FrameMetaData>[] output = Util.GetStreamOutput(mat: resizedMat, metaData: currFrameMetaData);
                                            streamQueue.Enqueue(item: output);
                                            fullMat.Dispose();
                                        }
                                        else
                                        {
                                            Tuple <Mat, FrameMetaData>[] output = Util.GetStreamOutput(mat: fullMat, metaData: currFrameMetaData);
                                            streamQueue.Enqueue(item: output);
                                        }
                                    }
                                }
                            }
                            catch (SpinnakerException ex)
                            {
                                Console.WriteLine("Error in SimpleStreamingLoop: {0}", ex.Message);
                            }

                            isMessageDequeueSuccess = messageQueue.TryDequeue(out ButtonCommands message);
                            if (isMessageDequeueSuccess)
                            {
                                state = handler.UpdateState(state: state, button: message);
                            }
                        }

                        else if (state == BasicCamLoopState.Recording)
                        {
                            try
                            {
                                using (IManagedImage rawImage = managedCamera.GetNextImage())
                                {
                                    imageCtr += 1;
                                    long frameID   = rawImage.ChunkData.FrameID;
                                    long timestamp = rawImage.ChunkData.Timestamp;

                                    if (imageCtr == 1)
                                    {
                                        currFrameMetaData = new FrameMetaData(streamCtr: imageCtr, frameID: frameID, timestamp: timestamp);
                                        continue;
                                    }

                                    prevFrameMetaData = currFrameMetaData;
                                    currFrameMetaData = new FrameMetaData(streamCtr: imageCtr, frameID: frameID, timestamp: timestamp);

                                    if (imageCtr % enqueueRate == 0)
                                    {
                                        Mat fullMat = new Mat(rows: inputSize.Height, cols: inputSize.Width,
                                                              type: Emgu.CV.CvEnum.DepthType.Cv8U, channels: 1, data: rawImage.DataPtr, step: inputSize.Width);

                                        if (isResizeNeeded)
                                        {
                                            Mat resizedMat = new Mat(size: outputSize, type: Emgu.CV.CvEnum.DepthType.Cv8U, channels: 1);
                                            CvInvoke.Resize(src: fullMat, dst: resizedMat, dsize: outputSize, interpolation: Emgu.CV.CvEnum.Inter.Linear);
                                            Tuple <Mat, FrameMetaData>[] output = Util.GetStreamOutput(mat: resizedMat, metaData: currFrameMetaData);
                                            streamQueue.Enqueue(item: output);
                                            fullMat.Dispose();
                                        }
                                        else
                                        {
                                            Tuple <Mat, FrameMetaData>[] output = Util.GetStreamOutput(mat: fullMat, metaData: currFrameMetaData);
                                            streamQueue.Enqueue(item: output);
                                        }
                                    }
                                }
                            }
                            catch (SpinnakerException ex)
                            {
                                Console.WriteLine("Error in SimpleStreamingLoop: {0}", ex.Message);
                            }

                            isMessageDequeueSuccess = messageQueue.TryDequeue(out ButtonCommands message);
                            if (isMessageDequeueSuccess)
                            {
                                state = handler.UpdateState(state: state, button: message);
                            }
                        }

                        else if (state == BasicCamLoopState.Exit)
                        {
                            return;
                        }
                    }
                }
Esempio n. 4
0
        public static void MergeStreamsThreadInit(MergeStreamsManager manager)
        {
            int  imageCtr = 0;
            bool isMessageDequeueSuccess;
            int  nQueues = manager.input.nInputQueues;

            bool[]          isInputDequeueSuccess = new bool[nQueues];
            StreamLoopState state = StreamLoopState.Waiting;

            Tuple <Mat, FrameMetaData>[][] input;

            while (true)
            {
                UpdateLoopState();
                if (state == StreamLoopState.Waiting)
                {
                    Thread.Sleep(100); continue;
                }
                else if (state == StreamLoopState.Exit)
                {
                    return;
                }

                input = new Tuple <Mat, FrameMetaData> [nQueues][];
                for (int i = 0; i < nQueues; i++)
                {
                    while (true)
                    {
                        isInputDequeueSuccess[i] = manager.input.streamQueues[i].TryDequeue(out input[i]);
                        if (isInputDequeueSuccess[i])
                        {
                            isInputDequeueSuccess[i] = false;
                            break;
                        }
                        else
                        {
                            UpdateLoopState();
                            if (state == StreamLoopState.Streaming)
                            {
                                continue;
                            }
                            else if (state == StreamLoopState.Waiting)
                            {
                                Thread.Sleep(100);
                            }
                            else if (state == StreamLoopState.Exit)
                            {
                                return;
                            }
                        }
                    }
                }

                ProcessInput();
                continue;

                void ProcessInput()
                {
                    imageCtr += 1;

                    if (imageCtr % manager.output.outputChannels[0].enqueueOrDequeueRate != 0)
                    {
                        input[0][0].Item1.Dispose();
                        input[1][0].Item1.Dispose();
                        return;
                    }

                    Mat outputMat = new Mat(size: manager.output.outputChannels[0].imageSize, type: Emgu.CV.CvEnum.DepthType.Cv8U, channels: 1);

                    CvInvoke.VConcat(src1: input[0][0].Item1, src2: input[1][0].Item1, dst: outputMat);
                    FrameMetaData outputMeta = input[0][0].Item2;

                    Tuple <Mat, FrameMetaData>[] output = Util.GetStreamOutput(mat: outputMat, metaData: outputMeta);
                    manager.output.displayQueue.Enqueue(item: output);
                }

                void UpdateLoopState()
                {
                    isMessageDequeueSuccess = manager.messageQueue.TryDequeue(out ButtonCommands message);
                    if (isMessageDequeueSuccess)
                    {
                        if (message == ButtonCommands.BeginStreaming)
                        {
                            Console.WriteLine("BeginStreaming command received in MergeStreamsThread");
                            state = StreamLoopState.Streaming;
                        }
                        else if (message == ButtonCommands.EndStreaming)
                        {
                            Console.WriteLine("EndStreaming command received in MergeStreamsThread");
                            state = StreamLoopState.Waiting;
                        }
                        else if (message == ButtonCommands.Exit)
                        {
                            state = StreamLoopState.Exit;
                        }
                    }
                }
            }
        }
Esempio n. 5
0
 public void ResetCounter()
 {
     imageCtr          = 0;
     prevFrameMetaData = new FrameMetaData();
     currFrameMetaData = new FrameMetaData();
 }