private void button4_Click(object sender, EventArgs e)
        {
            /*if (gps == null)
             * {
             *  try
             *  {
             *      gps = new GPSReceiver(comboBox1.Items[comboBox1.SelectedIndex].ToString(), (int)numericUpDown1.Value, true);
             *
             *      gps.StartReceiver();
             *      timer1.Start();
             *  }
             *  catch (System.ArgumentOutOfRangeException ex)
             *  {
             *      MessageBox.Show("Make sure your GPS is connected and choose serial port!!! Starting capture without GPS", "GPS problem!", MessageBoxButtons.OK);
             *  }
             * }
             * else if (!gps.CommPort.IsOpen)
             * {
             *  try
             *  {
             *      gps = new GPSReceiver(comboBox1.Items[comboBox1.SelectedIndex].ToString(), (int)numericUpDown1.Value, true);
             *
             *      gps.StartReceiver();
             *      timer1.Start();
             *  }
             *  catch (System.ArgumentOutOfRangeException ex)
             *  {
             *      MessageBox.Show("Make sure your GPS is connected and choose serial port!!! Starting capture without GPS", "GPS problem!", MessageBoxButtons.OK);
             *  }
             * } */
            try
            {
                StreamReader sr   = new StreamReader("CalibParams.txt");
                string       line = sr.ReadLine();
                string[]     row1 = line.Split(new char[] { ',' });

                LatestCamIntrinsics.IntrinsicMatrix[0, 0] = Convert.ToDouble(row1[0]);
                LatestCamIntrinsics.IntrinsicMatrix[0, 1] = Convert.ToDouble(row1[1]);
                LatestCamIntrinsics.IntrinsicMatrix[0, 2] = Convert.ToDouble(row1[2]);



                LatestCamIntrinsics.IntrinsicMatrix[1, 0] = Convert.ToDouble(row1[3]);
                LatestCamIntrinsics.IntrinsicMatrix[1, 1] = Convert.ToDouble(row1[4]);
                LatestCamIntrinsics.IntrinsicMatrix[1, 2] = Convert.ToDouble(row1[5]);



                LatestCamIntrinsics.IntrinsicMatrix[2, 0] = Convert.ToDouble(row1[6]);
                LatestCamIntrinsics.IntrinsicMatrix[2, 1] = Convert.ToDouble(row1[7]);
                LatestCamIntrinsics.IntrinsicMatrix[2, 2] = Convert.ToDouble(row1[8]);

                line = sr.ReadLine();
                string[] dcoeffs = line.Split(new char[] { ',' });
                LatestCamIntrinsics.DistortionCoeffs[0, 0] = Convert.ToDouble(dcoeffs[0]);
                LatestCamIntrinsics.DistortionCoeffs[1, 0] = Convert.ToDouble(dcoeffs[1]);
                LatestCamIntrinsics.DistortionCoeffs[2, 0] = Convert.ToDouble(dcoeffs[2]);
                LatestCamIntrinsics.DistortionCoeffs[3, 0] = Convert.ToDouble(dcoeffs[3]);

                sr.Close();


                // dumping out the new parameters from the file
                line = "[ " + row1[0] + " , " + row1[1] + " , " + row1[2] + " ]" + "\n" +
                       "[ " + row1[3] + " , " + row1[4] + " , " + row1[5] + " ]" + "\n" +
                       "[ " + row1[6] + " , " + row1[7] + " , " + row1[8] + " ]" + "\n" +
                       "-------------------------------------------------" + "\n" +
                       "[" + dcoeffs[0] + " , " + dcoeffs[1] + " , " + dcoeffs[2] + " , " + dcoeffs[3] + "]";
                MessageBox.Show(line, "New camera Intrinsics and Distortion Coefficients loaded from the file", MessageBoxButtons.OK);
            }
            catch (FileNotFoundException ex) {
                // dumping out the new parameters from the file
                string line = "[ " + Convert.ToString(LatestCamIntrinsics.IntrinsicMatrix[0, 0]) + " , 0 , " + Convert.ToString(LatestCamIntrinsics.IntrinsicMatrix[0, 2]) + " ]" + "\n" +
                              "[ 0 , " + Convert.ToString(LatestCamIntrinsics.IntrinsicMatrix[1, 1]) + " , " + Convert.ToString(LatestCamIntrinsics.IntrinsicMatrix[1, 2]) + " ]" + "\n" +
                              "[ 0 , 0, 1 ]" + "\n" +
                              "-------------------------------------------------" + "\n" +
                              "[" + Convert.ToString(LatestCamIntrinsics.DistortionCoeffs[0, 0]) +
                              " , " + Convert.ToString(LatestCamIntrinsics.DistortionCoeffs[1, 0]) +
                              " , " + Convert.ToString(LatestCamIntrinsics.DistortionCoeffs[2, 0]) +
                              " , " + Convert.ToString(LatestCamIntrinsics.DistortionCoeffs[3, 0]) + "]";
                MessageBox.Show(line, "Could not retrieve intrinsics from file. Using the values previously stored in memory...", MessageBoxButtons.OK);
            };

            if (MessageBox.Show("Do you wish to do a calibration before recording ?", "Calibration Alert!", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                // calibrate first
                calibrator = new Flea3Calibrator(pictureBox1);
                calibrator.startCalibration(comboResolution.SelectedIndex, calibrator.CamIDs[0]);
            }
            else
            {
                recorder = new Flea3Recorder(gps, imu);



                // the file name format: "dd-mm-yyyy.hh-mm-ss"
                String fname = "";
                if (checkRecord.Checked)
                {
                    int day = DateTime.Now.Day;
                    fname = fname + ((day < 10) ? "0" + Convert.ToString(day) + "-" : Convert.ToString(day) + "-");

                    int month = DateTime.Now.Month;
                    fname = fname + ((month < 10) ? "0" + Convert.ToString(month) + "-" : Convert.ToString(month) + "-");

                    int year = DateTime.Now.Year;
                    fname = fname + Convert.ToString(year) + '.';

                    int hour = DateTime.Now.Hour;
                    fname = fname + ((hour < 10) ? "0" + Convert.ToString(hour) + "-" : Convert.ToString(hour) + "-");

                    int minute = DateTime.Now.Minute;
                    fname = fname + ((minute < 10) ? "0" + Convert.ToString(minute) : Convert.ToString(minute));

                    fname = textOutputFolder.Text + "\\" + fname;
                }

                CurrentFileName = fname;


                // ok, starting the recording!
                // using the first camera in the list
                if (recorder.CamIDs.Count() > 0)
                {
                    recorder.startCapture(recorder.CamIDs[0], comboResolution.SelectedIndex, pictureBox1, fname + ".avi", checkRecord.Checked);
                }
                else
                {
                    Console.WriteLine(" No camera found on the bus!!! Capturing aborted!");
                }
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            /*if (gps == null)
            {
                try
                {
                    gps = new GPSReceiver(comboBox1.Items[comboBox1.SelectedIndex].ToString(), (int)numericUpDown1.Value, true);

                    gps.StartReceiver();
                    timer1.Start();
                }
                catch (System.ArgumentOutOfRangeException ex)
                {
                    MessageBox.Show("Make sure your GPS is connected and choose serial port!!! Starting capture without GPS", "GPS problem!", MessageBoxButtons.OK);
                }
            }
            else if (!gps.CommPort.IsOpen)
            {
                try
                {
                    gps = new GPSReceiver(comboBox1.Items[comboBox1.SelectedIndex].ToString(), (int)numericUpDown1.Value, true);

                    gps.StartReceiver();
                    timer1.Start();
                }
                catch (System.ArgumentOutOfRangeException ex)
                {
                    MessageBox.Show("Make sure your GPS is connected and choose serial port!!! Starting capture without GPS", "GPS problem!", MessageBoxButtons.OK);
                }
            } */
            try
            {
                StreamReader sr = new StreamReader("CalibParams.txt");
                string line = sr.ReadLine();
                string[] row1 = line.Split(new char[] {','});

                LatestCamIntrinsics.IntrinsicMatrix[0, 0] = Convert.ToDouble(row1[0]);
                LatestCamIntrinsics.IntrinsicMatrix[0, 1] = Convert.ToDouble(row1[1]);
                LatestCamIntrinsics.IntrinsicMatrix[0, 2] = Convert.ToDouble(row1[2]);

                LatestCamIntrinsics.IntrinsicMatrix[1, 0] = Convert.ToDouble(row1[3]);
                LatestCamIntrinsics.IntrinsicMatrix[1, 1] = Convert.ToDouble(row1[4]);
                LatestCamIntrinsics.IntrinsicMatrix[1, 2] = Convert.ToDouble(row1[5]);

                LatestCamIntrinsics.IntrinsicMatrix[2, 0] = Convert.ToDouble(row1[6]);
                LatestCamIntrinsics.IntrinsicMatrix[2, 1] = Convert.ToDouble(row1[7]);
                LatestCamIntrinsics.IntrinsicMatrix[2, 2] = Convert.ToDouble(row1[8]);

                line = sr.ReadLine();
                string[] dcoeffs = line.Split(new char[] { ',' });
                LatestCamIntrinsics.DistortionCoeffs[0, 0] = Convert.ToDouble(dcoeffs[0]);
                LatestCamIntrinsics.DistortionCoeffs[1, 0] = Convert.ToDouble(dcoeffs[1]);
                LatestCamIntrinsics.DistortionCoeffs[2, 0] = Convert.ToDouble(dcoeffs[2]);
                LatestCamIntrinsics.DistortionCoeffs[3, 0] = Convert.ToDouble(dcoeffs[3]);

                sr.Close();

                // dumping out the new parameters from the file
                line = "[ "+row1[0]+" , "+row1[1] + " , " + row1[2] + " ]" + "\n"+
                       "[ "+row1[3]+" , "+row1[4] + " , " + row1[5] + " ]" + "\n"+
                       "[ "+row1[6]+" , "+row1[7] + " , " + row1[8] + " ]" + "\n"+
                       "-------------------------------------------------" + "\n"+
                       "["+dcoeffs[0]+" , "+ dcoeffs[1] + " , "+ dcoeffs[2] + " , " + dcoeffs[3]+"]";
                MessageBox.Show(line, "New camera Intrinsics and Distortion Coefficients loaded from the file", MessageBoxButtons.OK);
            }
            catch (FileNotFoundException ex) {

                // dumping out the new parameters from the file
                string line = "[ " + Convert.ToString(LatestCamIntrinsics.IntrinsicMatrix[0,0]) + " , 0 , "+Convert.ToString(LatestCamIntrinsics.IntrinsicMatrix[0,2])+ " ]" + "\n" +
                       "[ 0 , " + Convert.ToString(LatestCamIntrinsics.IntrinsicMatrix[1,1]) + " , " + Convert.ToString(LatestCamIntrinsics.IntrinsicMatrix[1,2])+ " ]" + "\n" +
                       "[ 0 , 0, 1 ]" + "\n" +
                       "-------------------------------------------------" + "\n" +
                       "[" + Convert.ToString(LatestCamIntrinsics.DistortionCoeffs[0,0]) +
                       " , " + Convert.ToString(LatestCamIntrinsics.DistortionCoeffs[1,0]) +
                       " , " + Convert.ToString(LatestCamIntrinsics.DistortionCoeffs[2,0]) +
                       " , " + Convert.ToString(LatestCamIntrinsics.DistortionCoeffs[3,0]) + "]";
                MessageBox.Show(line, "Could not retrieve intrinsics from file. Using the values previously stored in memory...",  MessageBoxButtons.OK);

            };

            if (MessageBox.Show("Do you wish to do a calibration before recording ?", "Calibration Alert!", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                // calibrate first
                calibrator = new Flea3Calibrator(pictureBox1);
                calibrator.startCalibration(comboResolution.SelectedIndex, calibrator.CamIDs[0]);
            }
            else
            {
               recorder = new Flea3Recorder(gps, imu);

                // the file name format: "dd-mm-yyyy.hh-mm-ss"
                String fname = "";
                if (checkRecord.Checked)
                {

                    int day = DateTime.Now.Day;
                    fname = fname + ((day < 10) ? "0" + Convert.ToString(day) + "-" : Convert.ToString(day) + "-");

                    int month = DateTime.Now.Month;
                    fname = fname + ((month < 10) ? "0" + Convert.ToString(month) + "-" : Convert.ToString(month) + "-");

                    int year = DateTime.Now.Year;
                    fname = fname + Convert.ToString(year) + '.';

                    int hour = DateTime.Now.Hour;
                    fname = fname + ((hour < 10) ? "0" + Convert.ToString(hour) + "-" : Convert.ToString(hour) + "-");

                    int minute = DateTime.Now.Minute;
                    fname = fname + ((minute < 10) ? "0" + Convert.ToString(minute) : Convert.ToString(minute));

                    fname = textOutputFolder.Text + "\\" + fname;
                }

                CurrentFileName = fname;

                // ok, starting the recording!
                // using the first camera in the list
                if (recorder.CamIDs.Count() > 0)
                {

                    recorder.startCapture(recorder.CamIDs[0], comboResolution.SelectedIndex, pictureBox1, fname+".avi", checkRecord.Checked);
                }
                else
                    Console.WriteLine(" No camera found on the bus!!! Capturing aborted!");

            }
        }