Exemple #1
0
        /// <summary>
        /// returns the given image as a byte array
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="image_width"></param>
        /// <param name="image_height"></param>
        /// <returns></returns>
        private static byte[] GetImageData(string filename,
                                           ref int image_width,
                                           ref int image_height)
        {
            byte[] img = null;

            if (filename.ToLower().EndsWith(".pgm"))
            {
                img = image.loadFromPGM(filename, ref image_width, ref image_height, 1);
            }
            else
            {
                Bitmap bmp = (Bitmap)Bitmap.FromFile(filename);
                if (bmp != null)
                {
                    image_width  = bmp.Width;
                    image_height = bmp.Height;
                    img          = new byte[image_width * image_height * 3];

                    // get the as a byte array
                    BitmapArrayConversions.updatebitmap(bmp, img);

                    // convert the image to mono format (1 byte per pixel)
                    img = image.monoImage(img, image_width, image_height);
                }
            }

            return(img);
        }
Exemple #2
0
        private Bitmap OverlayImage(Bitmap bmp, Bitmap overlay, int overlay_width)
        {
            usage.Update("Overlay image, SurveyorVisionStereoGtk, OverlayImage");
            Bitmap result = (Bitmap)bmp.Clone();

            byte[] image_data = new byte[result.Width * result.Height * 3];
            BitmapArrayConversions.updatebitmap(result, image_data);

            byte[] overlay_data = new byte[overlay.Width * overlay.Height * 3];
            BitmapArrayConversions.updatebitmap(overlay, overlay_data);

            int overlay_height = overlay.Height * overlay_width / overlay.Width;

            for (int y = 0; y < overlay_height; y++)
            {
                int yy = y * overlay.Height / overlay_height;
                for (int x = 0; x < overlay_width; x++)
                {
                    int xx = x * overlay.Width / overlay_width;
                    int n1 = ((yy * overlay.Width) + xx) * 3;
                    int n2 = ((y * result.Width) + x) * 3;
                    for (int col = 0; col < 3; col++)
                    {
                        image_data[n2 + col] = overlay_data[n1 + col];
                    }
                }
            }

            BitmapArrayConversions.updatebitmap_unsafe(image_data, result);
            return(result);
        }
/*
 *      private Bitmap OverlayImage(Bitmap bmp, Bitmap overlay, int overlay_width)
 *      {
 *          Bitmap result = (Bitmap)bmp.Clone();
 *          byte[] image_data = new byte[result.Width * result.Height * 3];
 *          BitmapArrayConversions.updatebitmap(result, image_data);
 *
 *          byte[] overlay_data = new byte[overlay.Width * overlay.Height * 3];
 *          BitmapArrayConversions.updatebitmap(overlay, overlay_data);
 *
 *          int overlay_height = overlay.Height * overlay_width / overlay.Width;
 *
 *          for (int y = 0; y < overlay_height; y++)
 *          {
 *              int yy = y * overlay.Height / overlay_height;
 *              for (int x = 0; x < overlay_width; x++)
 *              {
 *                  int xx = x * overlay.Width / overlay_width;
 *                  int n1 = ((yy * overlay.Width) + xx) * 3;
 *                  int n2 = ((y * result.Width) + x) * 3;
 *                  for (int col = 0; col < 3; col++)
 *                      image_data[n2 + col] = overlay_data[n1 + col];
 *              }
 *          }
 *
 *          BitmapArrayConversions.updatebitmap_unsafe(image_data, result);
 *          return (result);
 *      }
 */

        private void DisplayImage(Bitmap img, Bitmap default_image, bool is_left)
        {
            Bitmap disp_image = null;

            switch (display_type)
            {
            case DISPLAY_RAW: { disp_image = default_image; break; }

            case DISPLAY_CALIBRATION_DOTS: { disp_image = edges; break; }

            case DISPLAY_CALIBRATION_GRID: { disp_image = linked_dots; break; }

            case DISPLAY_CALIBRATION_DIFF: { disp_image = grid_diff; break; }

            case DISPLAY_RECTIFIED: { if (is_left)
                                      {
                                          disp_image = rectified[0];
                                      }
                                      else
                                      {
                                          disp_image = rectified[1];
                                      } break; }
            }

            if ((calibration_pattern == null) || (disp_image == null))
            {
                disp_image = default_image;
            }

            if (calibration_pattern == null)
            {
                if (is_left)
                {
                    if (calibration_map[0] != null)
                    {
                        if (stereo_features == null)
                        {
                            disp_image = rectified[0];
                        }
                        else
                        {
                            disp_image = stereo_features;
                        }
                    }
                }
                else
                {
                    if (calibration_map[1] != null)
                    {
                        disp_image = rectified[1];
                    }
                }
            }

            byte[] image_data = new byte[disp_image.Width * disp_image.Height * 3];
            BitmapArrayConversions.updatebitmap(disp_image, image_data);
            BitmapArrayConversions.updatebitmap_unsafe(image_data, img);
        }
Exemple #4
0
 /// <summary>
 /// updates the given image object from a bitmap object
 /// </summary>
 /// <param name="bmp"></param>
 /// <param name="img"></param>
 public static void setBitmap(Bitmap bmp, Gtk.Image img, ref byte[] bmp_data)
 {
     if (bmp_data == null)
     {
         bmp_data = new byte[bmp.Width * bmp.Height * 3];
     }
     BitmapArrayConversions.updatebitmap(bmp, bmp_data);
     setBitmap(bmp_data, bmp.Width, bmp.Height, img);
 }
Exemple #5
0
 public static void setBitmap(Bitmap bmp, Gtk.Image img)
 {
     try
     {
         byte[] bmp_data = new byte[bmp.Width * bmp.Height * 3];
         BitmapArrayConversions.updatebitmap(bmp, bmp_data);
         setBitmap(bmp_data, bmp.Width, bmp.Height, img);
     }
     catch
     {
     }
 }
Exemple #6
0
        public void updateVision(Image input_img, bool leftImage)
        {
            int cameraIndex = 0;

            Byte[] disp_bmp_data = bmp_data_left;
            if (!leftImage)
            {
                cameraIndex   = 1;
                disp_bmp_data = bmp_data_right;
            }

            Byte[] ary;

            try
            {
                if (captureState[cameraIndex] == 1)
                {
                    if (disp_bmp_data == null)
                    {
                        if (leftImage)
                        {
                            bmp_data_left = new Byte[input_img.Width * input_img.Height * 3];
                            disp_bmp_data = bmp_data_left;
                        }
                        else
                        {
                            bmp_data_right = new Byte[input_img.Width * input_img.Height * 3];
                            disp_bmp_data  = bmp_data_right;
                        }
                    }

                    ary = disp_bmp_data;
                    BitmapArrayConversions.updatebitmap((Bitmap)(input_img.Clone()), ary);

                    captureState[cameraIndex] = 2;
                }

                if (leftImage)
                {
                    left_camera_running = true;
                }
                else
                {
                    right_camera_running = true;
                }
            }
            catch
            {
            }
        }
Exemple #7
0
        /// <summary>
        /// main update for stereo correspondence
        /// </summary>
        /// <param name="rectified_left">left image bitmap object</param>
        /// <param name="rectified_right">right image bitmap object</param>
        /// <param name="calibration_offset_x">horizontal offset from calibration, correcting for non-parallel alignment of the cameras</param>
        /// <param name="calibration_offset_y">vertical offset from calibration, correcting for non-parallel alignment of the cameras</param>
        public void Update(
            Bitmap rectified_left,
            Bitmap rectified_right,
            float calibration_offset_x,
            float calibration_offset_y)
        {
            image_width  = rectified_left.Width;
            image_height = rectified_left.Height;
            int pixels = image_width * image_height * 3;

            if (img[0] == null)
            {
                for (int i = 0; i < 4; i++)
                {
                    img[i] = new byte[pixels];
                }
            }
            else
            {
                if (img[0].Length != pixels)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        img[i] = new byte[pixels];
                    }
                }
            }

            BitmapArrayConversions.updatebitmap(rectified_left, img[0]);
            BitmapArrayConversions.updatebitmap(rectified_right, img[1]);

            // convert colour images to mono
            monoImage(img[0], image_width, image_height, 1, ref img[2]);
            monoImage(img[1], image_width, image_height, 1, ref img[3]);

            // main stereo correspondence routine
            Update(img[0], img[1],
                   img[2], img[3],
                   rectified_left.Width, rectified_left.Height,
                   calibration_offset_x, calibration_offset_y);

            if (BroadcastStereoFeatureColours)
            {
                // assign a colour to each feature
                UpdateFeatureColours(img[0], img[1]);
            }

            // send stereo features to connected clients
            BroadcastStereoFeatures();
        }
Exemple #8
0
        private void update()
        {
            // load calibration data
            stereointerface.loadCalibration(calibration_filename);

            // get image data from bitmaps
            int bytes_per_pixel = 3;
            int image_width     = picLeftImage.Image.Width;
            int image_height    = picLeftImage.Image.Height;

            byte[] fullres_left = new byte[image_width * image_height *
                                           bytes_per_pixel];
            byte[] fullres_right = new byte[fullres_left.Length];
            BitmapArrayConversions.updatebitmap((Bitmap)picLeftImage.Image, fullres_left);
            BitmapArrayConversions.updatebitmap((Bitmap)picRightImage.Image, fullres_right);

            // load images into the correspondence object
            stereointerface.loadImage(fullres_left, image_width, image_height, true, bytes_per_pixel);
            stereointerface.loadImage(fullres_right, image_width, image_height, false, bytes_per_pixel);

            // set the quality of the disparity map
            stereointerface.setDisparityMapCompression(horizontal_compression, vertical_compression);

            clock.Start();

            // perform stereo matching
            stereointerface.stereoMatchRun(0, 8, correspondence_algorithm_type);

            long correspondence_time_mS = clock.Stop();

            txtStereoCorrespondenceTime.Text = correspondence_time_mS.ToString();

            // make a bitmap
            Bitmap depthmap_bmp = new Bitmap(image_width, image_height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            picDepthMap.Image = depthmap_bmp;

            byte[] depthmap = new byte[image_width * image_height * 3];
            stereointerface.getDisparityMap(depthmap, image_width, image_height, 0);

            BitmapArrayConversions.updatebitmap_unsafe(depthmap, depthmap_bmp);
            picDepthMap.Refresh();
        }
        private void timAnimation_Tick(object sender, EventArgs e)
        {
            if ((File.Exists(processed_left_image_filename)) &&
                (File.Exists(processed_right_image_filename)))
            {
                if (picAnimation.Image != null)
                {
                    picAnimation.Image.Dispose();
                }

                if (animation_state)
                {
                    Bitmap bmp = (Bitmap)Bitmap.FromFile(processed_left_image_filename);
                    if (reverse_colours)
                    {
                        byte[] img = new byte[bmp.Width * bmp.Height * 3];
                        BitmapArrayConversions.updatebitmap(bmp, img);
                        BitmapArrayConversions.RGB_BGR(img);
                        BitmapArrayConversions.updatebitmap_unsafe(img, bmp);
                    }
                    picAnimation.Image = bmp;
                }
                else
                {
                    Bitmap bmp = (Bitmap)Bitmap.FromFile(processed_right_image_filename);
                    if (reverse_colours)
                    {
                        byte[] img = new byte[bmp.Width * bmp.Height * 3];
                        BitmapArrayConversions.updatebitmap(bmp, img);
                        BitmapArrayConversions.RGB_BGR(img);
                        BitmapArrayConversions.updatebitmap_unsafe(img, bmp);
                    }
                    picAnimation.Image = bmp;
                }
                picAnimation.Refresh();

                if (chkAnimate.Checked)
                {
                    animation_state = !animation_state;
                }
            }
        }
Exemple #10
0
        private void loadSimulationImages()
        {
            String left_filename  = System.AppDomain.CurrentDomain.BaseDirectory + "\\left_" + Convert.ToString(simulation_file_index) + ".jpg";
            String right_filename = System.AppDomain.CurrentDomain.BaseDirectory + "\\right_" + Convert.ToString(simulation_file_index) + ".jpg";

            captureState[0] = 0;
            captureState[1] = 0;
            if ((File.Exists(left_filename)) && (File.Exists(right_filename)))
            {
                picLeftImage.Image = new Bitmap(left_filename);
                picLeftImage.Refresh();
                left_image = (Bitmap)picLeftImage.Image.Clone();
                if (global_variables.left_bmp == null)
                {
                    global_variables.left_bmp = new Byte[left_image.Width * left_image.Height * 3];
                }
                BitmapArrayConversions.updatebitmap(left_image, global_variables.left_bmp);
                captureState[0] = 2;

                picRightImage.Image = new Bitmap(right_filename);
                picRightImage.Refresh();
                right_image = (Bitmap)picRightImage.Image.Clone();
                if (global_variables.right_bmp == null)
                {
                    global_variables.right_bmp = new Byte[right_image.Width * right_image.Height * 3];
                }
                BitmapArrayConversions.updatebitmap(right_image, global_variables.right_bmp);
                captureState[1] = 2;
            }

            // we need to slow things down, otherwise the simulation runs at lightning speed
            delay_counter++;
            if (delay_counter > 2)
            {
                delay_counter = 0;
                simulation_file_index++;
                if (simulation_file_index >= output_file_index)
                {
                    simulation_file_index = 1;
                }
            }
        }
Exemple #11
0
        public void updateVisionRight(Image input_img, bool leftImage)
        {
            try
            {
                if (captureState[1] == 1)
                {
                    if (global_variables.right_bmp == null)
                        global_variables.right_bmp = new Byte[input_img.Width * input_img.Height * 3];

                    right_image = (Bitmap)input_img.Clone();
                    BitmapArrayConversions.updatebitmap(right_image, global_variables.right_bmp);
                    captureState[1] = 2;
                }
                right_camera_running = true;
            }
            catch //(Exception ex)
            {
                //MessageBox.Show("updateVisionRight/" + ex.Message);
            }
        }
        /// <summary>
        /// is the given bitmap a blank frame ?
        /// </summary>
        /// <param name="bmp">bitmap object</param>
        /// <param name="step_size">sampling step size</param>
        /// <returns>true if blank</returns>
        protected static bool IsBlankFrame(Bitmap bmp, int step_size)
        {
            bool is_blank = true;

            if (bmp != null)
            {
                byte[] image = new byte[bmp.Width * bmp.Height * 3];
                BitmapArrayConversions.updatebitmap(bmp, image);

                int i = image.Length - 1;
                while ((is_blank) && (i > 0))
                {
                    if (image[i] != 0)
                    {
                        is_blank = false;
                    }
                    i -= step_size;
                }
                image = null;
            }
            return(is_blank);
        }
Exemple #13
0
        /// <summary>
        /// add some text to the given image
        /// </summary>
        /// <param name="img">colour image into which to insert th text</param>
        /// <param name="img_width">width of the image</param>
        /// <param name="img_height">height of the image</param>
        /// <param name="text">text to be added</param>
        /// <param name="font">font style</param>
        /// <param name="font_size">font size</param>
        /// <param name="r">red</param>
        /// <param name="g">green</param>
        /// <param name="b">blue</param>
        /// <param name="position_x">x coordinate at which to insert the text</param>
        /// <param name="position_y">y coordinate at which to insert the text</param>
        public static void AddText(byte[] img, int img_width, int img_height,
                                   String text,
                                   String font, int font_size,
                                   int r, int g, int b,
                                   float position_x, float position_y)
        {
            Bitmap screen_bmp = new Bitmap(img_width, img_height,
                                           System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            // insert the existing image into the bitmap
            BitmapArrayConversions.updatebitmap_unsafe(img, screen_bmp);

            Font       drawFont  = new Font(font, font_size);
            SolidBrush drawBrush = new SolidBrush(Color.FromArgb(r, g, b));

            Graphics grph = Graphics.FromImage(screen_bmp);

            grph.DrawString(text, drawFont, drawBrush, position_x, position_y);
            grph.Dispose();

            // extract the bitmap data
            BitmapArrayConversions.updatebitmap(screen_bmp, img);
        }
Exemple #14
0
        private void timUpdate_Tick(object sender, EventArgs e)
        {
            int processingTime;
            bool readyToUpdate = false;

            if (((left_imageloaded) && (right_imageloaded)) || ((left_camera_running) && (right_camera_running)))
            {
                // get images from the two cameras
                captureCameraImages();

                if (!((left_camera_running) && (right_camera_running)))
                {
                    //update from loaded images
                    picLeftImage.Image = new Bitmap(left_image_filename);
                    picRightImage.Image = new Bitmap(right_image_filename);

                    global_variables.standard_width = picLeftImage.Image.Width;
                    global_variables.standard_height = picLeftImage.Image.Height;

                    // initialise arrays if necessary
                    if (global_variables.left_bmp == null)
                    {
                        global_variables.left_bmp = new Byte[global_variables.standard_width * global_variables.standard_height * 3];
                        global_variables.right_bmp = new Byte[global_variables.standard_width * global_variables.standard_height * 3];
                    }

                    left_image = (Bitmap)picLeftImage.Image;
                    right_image = (Bitmap)picRightImage.Image;

                    // copy bitmap data into byte arrays
                    BitmapArrayConversions.updatebitmap(left_image, global_variables.left_bmp);
                    BitmapArrayConversions.updatebitmap(right_image, global_variables.right_bmp);

                    readyToUpdate = true;
                }
                else
                {
                    //update from live cameras
                    if ((captureState[0] == 2) && (captureState[1] == 2))
                    {
                        // reset the capture state, so the system knows it can begin
                        // grabbing frames once again
                        captureState[0] = 0;
                        captureState[1] = 0;
                        readyToUpdate = true;
                    }
                }

                // we have the images. Now it's time to do something with them...
                if (readyToUpdate)
                {
                    // start timing the stereo processing
                    clock.Start();

                    // find stereo correspondences
                    stereo.stereoMatch(global_variables.left_bmp, global_variables.right_bmp,
                                   global_variables.standard_width, global_variables.standard_height, true);

                    // initialise some bitmaps to show the output of stereo processing
                    if (!outputInitialised)
                    {
                        picOutput.Image = new Bitmap(global_variables.standard_width, global_variables.standard_height, PixelFormat.Format24bppRgb);
                        disp_bmp_data = new Byte[global_variables.standard_width * global_variables.standard_height * 3];
                        outputInitialised = true;
                    }

                    // how long did the processing take ?
                    processingTime = (int)clock.Stop();

                    // show stereo disparities as circles
                    switch(display_type)
                    {
                        case 0:
                            {
                                showDisparities();
                                break;
                            }
                        case 1:
                            {
                                break;
                            }
                        case 2:
                            {
                                showDisparityMap();
                                break;
                            }
                        case 3:
                            {
                                break;
                            }
                        case 4:
                            {
                                break;
                            }
                        case 5:
                            {
                                showDisparityMap();
                                break;
                            }
                    }
                }

            }
        }
        public frmManualOffsetCalibration(
            string left_image_filename,
            string right_image_filename,
            float offset_x,
            float offset_y,
            float scale,
            float rotation_degrees,
            bool parameters_exist,
            bool reverse_colours)
        {
            InitializeComponent();

            this.left_image_filename  = left_image_filename;
            this.right_image_filename = right_image_filename;
            this.offset_x             = offset_x;
            this.offset_y             = offset_y;
            this.scale            = scale;
            this.rotation_degrees = rotation_degrees;
            this.reverse_colours  = reverse_colours;

            if (!parameters_exist)
            {
                LoadPreviousParameters();
                if (left_image_filename != "")
                {
                    this.left_image_filename = left_image_filename;
                }
                if (right_image_filename != "")
                {
                    this.right_image_filename = right_image_filename;
                }
            }

            if ((this.left_image_filename != null) &&
                (this.left_image_filename != ""))
            {
                Console.WriteLine(this.left_image_filename);
                Bitmap bmp = (Bitmap)Bitmap.FromFile(this.left_image_filename);
                if (reverse_colours)
                {
                    byte[] img = new byte[bmp.Width * bmp.Height * 3];
                    BitmapArrayConversions.updatebitmap(bmp, img);
                    BitmapArrayConversions.RGB_BGR(img);
                    BitmapArrayConversions.updatebitmap_unsafe(img, bmp);
                }
                picLeftImage.Image = bmp;
            }
            if ((this.right_image_filename != null) &&
                (this.right_image_filename != ""))
            {
                Bitmap bmp = (Bitmap)Bitmap.FromFile(this.right_image_filename);
                if (reverse_colours)
                {
                    byte[] img = new byte[bmp.Width * bmp.Height * 3];
                    BitmapArrayConversions.updatebitmap(bmp, img);
                    BitmapArrayConversions.RGB_BGR(img);
                    BitmapArrayConversions.updatebitmap_unsafe(img, bmp);
                }
                picRightImage.Image = bmp;
            }

            txtOffsetX.Text  = this.offset_x.ToString();
            txtOffsetY.Text  = this.offset_y.ToString();
            txtScale.Text    = this.scale.ToString();
            txtRotation.Text = this.rotation_degrees.ToString();

            Update();
        }
        protected void GrabLinux()
        {
            string filename = "capture";

            // append temporary files path if specified
            if ((temporary_files_path != null) &&
                (temporary_files_path != ""))
            {
                if (temporary_files_path.EndsWith("/"))
                {
                    filename = temporary_files_path + filename;
                }
                else
                {
                    filename = temporary_files_path + "/" + filename;
                }
            }

            // Extract numbers from the camera device names
            // This is ised to uniquely identify devices so that
            // potentially more than one stereo camera could be running
            // at the same time
            string identifier = "";

            for (int cam = 0; cam < 2; cam++)
            {
                char[] ch = camera_device[cam].ToCharArray();
                for (int i = 0; i < ch.Length; i++)
                {
                    if ((ch[i] >= '0') && (ch[i] <= '9'))
                    {
                        identifier += ch[i];
                    }
                }
            }
            filename += identifier;

            string command_str = "fswebcam -q -d " + camera_device[0] + "," + camera_device[1];

            command_str += " -r " + image_width.ToString() + "x" + image_height.ToString();
            command_str += " --no-banner";
            command_str += " -S " + skip_frames.ToString();
            if (exposure > 0)
            {
                command_str += " -s brightness=" + exposure.ToString() + "%";
            }
            command_str += " --save " + filename + "_.jpg";

            // fswebcam -q -d /dev/video1,/dev/video2 -r 320x240 --no-banner -S 2 -s brightness=50% --save capture12_.jpg

            if (capture_utility == "v4l2stereo")
            {
                command_str  = "v4l2stereo --dev0 " + camera_device[0] + " ";
                command_str += "--dev1 " + camera_device[1] + " ";
                if (flip_left_image)
                {
                    command_str += "--flipleft ";
                }
                if (flip_right_image)
                {
                    command_str += "--flipright ";
                }
                command_str += "--save " + filename + "_ ";
            }

            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine(command_str);
            Console.WriteLine("");
            Console.WriteLine("");

            string left_image_filename  = filename + "_0.jpg";
            string right_image_filename = filename + "_1.jpg";

            // delete any existing images
            for (int cam = 0; cam < 2; cam++)
            {
                if (File.Exists(filename + "_" + cam.ToString() + ".jpg"))
                {
                    try
                    {
                        File.Delete(filename + "_" + cam.ToString() + ".jpg");
                    }
                    catch
                    {
                    }
                }
            }

            bool    command_succeeded = false;
            Process proc = new Process();

            proc.EnableRaisingEvents = false;
            proc.StartInfo.FileName  = command_str;
            try
            {
                proc.Start();
                command_succeeded = true;
            }
            catch
            {
            }

            if (!command_succeeded)
            {
                Console.WriteLine("Command failed.  fswebcam may not be installed.");
            }
            else
            {
                //proc.WaitForExit();
                proc.Close();

                // wait for the file to appear
                const int timeout_secs    = 10;
                DateTime  start_time      = DateTime.Now;
                int       seconds_elapsed = 0;
                while (((!File.Exists(left_image_filename)) ||
                        (!File.Exists(right_image_filename))) &&
                       (seconds_elapsed < timeout_secs))
                {
                    System.Threading.Thread.Sleep(50);
                    TimeSpan diff = DateTime.Now.Subtract(start_time);
                    seconds_elapsed = (int)diff.TotalSeconds;
                }

                if ((File.Exists(left_image_filename)) &&
                    (File.Exists(right_image_filename)))
                {
                    // grab the data from the captured images
                    Bitmap[] bmp = new Bitmap[2];

                    for (int cam = 0; cam < 2; cam++)
                    {
                        try
                        {
                            bmp[cam] = (Bitmap)Bitmap.FromFile(filename + "_" + cam.ToString() + ".jpg");
                        }
                        catch
                        {
                            bmp[cam] = null;
                        }
                        if (bmp[cam] == null)
                        {
                            break;
                        }

                        image_width  = bmp[cam].Width;
                        image_height = bmp[cam].Height;

                        byte[] raw_image_data = new byte[image_width * image_height * 3];
                        BitmapArrayConversions.updatebitmap(bmp[cam], raw_image_data);
                    }

                    if ((bmp[0] != null) && (bmp[1] != null))
                    {
                        if (calibration_pattern != null)
                        {
                            if (!show_left_image)
                            {
                                SurveyorCalibration.DetectDots(bmp[0], ref edge_detector, calibration_survey[0], ref edges, ref linked_dots, ref grid, ref grid_diff, ref rectified[0]);
                            }
                            else
                            {
                                SurveyorCalibration.DetectDots(bmp[1], ref edge_detector, calibration_survey[1], ref edges, ref linked_dots, ref grid, ref grid_diff, ref rectified[1]);
                            }
                        }

                        RectifyImages(bmp[0], bmp[1]);

                        Process(bmp[0], bmp[1]);

                        // save images to file
                        if (Record)
                        {
                            string path = "";
                            if ((recorded_images_path != null) &&
                                (recorded_images_path != ""))
                            {
                                if (recorded_images_path.EndsWith("/"))
                                {
                                    path = recorded_images_path;
                                }
                                else
                                {
                                    path = recorded_images_path + "/";
                                }
                            }

                            RecordFrameNumber++;
                            DateTime t = DateTime.Now;
                            LogEvent(t, "RAW_L " + stereo_camera_index.ToString() + " raw" + identifier + "_0_" + RecordFrameNumber.ToString() + ".jpg", image_log);
                            LogEvent(t, "RAW_R " + stereo_camera_index.ToString() + " raw" + identifier + "_1_" + RecordFrameNumber.ToString() + ".jpg", image_log);
                            bmp[0].Save(path + "raw" + identifier + "_0_" + RecordFrameNumber.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                            bmp[1].Save(path + "raw" + identifier + "_1_" + RecordFrameNumber.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                            if ((rectified[0] != null) && (rectified[0] != null))
                            {
                                LogEvent(t, "REC_L " + stereo_camera_index.ToString() + " rectified" + identifier + "_0_" + RecordFrameNumber.ToString() + ".jpg", image_log);
                                LogEvent(t, "REC_R " + stereo_camera_index.ToString() + " rectified" + identifier + "_1_" + RecordFrameNumber.ToString() + ".jpg", image_log);

                                rectified[0].Save(path + "rectified" + identifier + "_0_" + RecordFrameNumber.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                                rectified[1].Save(path + "rectified" + identifier + "_1_" + RecordFrameNumber.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < 2; i++)
                        {
                            if (bmp[i] == null)
                            {
                                Console.WriteLine("Warning: Did not acquire image from " + camera_device[i]);
                            }
                        }
                    }
                }
                else
                {
                    if (!File.Exists(left_image_filename))
                    {
                        Console.WriteLine("Timed out: File not found " + left_image_filename + ".");
                    }
                    if (!File.Exists(right_image_filename))
                    {
                        Console.WriteLine("Timed out: File not found " + right_image_filename + ".");
                    }
                }
            }

            if (next_camera != null)
            {
                active_camera = false;
                Pause();
                next_camera.active_camera = true;
                next_camera.Resume();
            }
        }
Exemple #17
0
        public static void MatchImages(
            string left_filename, string right_filename,
            int no_of_masks,
            double a, double b,
            double min_phase_degrees, double max_phase_degrees,
            double[] frequency,
            int max_disparity,
            double min_difference, double max_difference,
            string disparity_map_filename,
            int calibration_offset_x,
            int calibration_offset_y,
            int calibration_scale,
            int vertical_compression,
            int position_search_radius,
            int minimum_intensity,
            int random_rows,
            List <StereoFeature> features)
        {
            // load images
            Bitmap left_bmp  = (Bitmap)Bitmap.FromFile(left_filename);
            Bitmap right_bmp = (Bitmap)Bitmap.FromFile(right_filename);

            // extract data
            byte[] left_img_colour  = new byte[left_bmp.Width * left_bmp.Height * 3];
            byte[] right_img_colour = new byte[right_bmp.Width * right_bmp.Height * 3];
            BitmapArrayConversions.updatebitmap(left_bmp, left_img_colour);
            BitmapArrayConversions.updatebitmap(right_bmp, right_img_colour);

            // convert to mono
            byte[] left_img  = monoImage(left_img_colour, left_bmp.Width, left_bmp.Height);
            byte[] right_img = monoImage(right_img_colour, right_bmp.Width, right_bmp.Height);

            byte[] disparity_map = new byte[left_bmp.Width * left_bmp.Height * 3];

            MatchImages(left_img, right_img, left_bmp.Width, left_bmp.Height,
                        no_of_masks, a, b,
                        min_phase_degrees, max_phase_degrees,
                        frequency,
                        max_disparity,
                        min_difference, max_difference,
                        calibration_offset_x, calibration_offset_y,
                        vertical_compression,
                        position_search_radius,
                        minimum_intensity,
                        random_rows,
                        features,
                        disparity_map);

            Bitmap disparity_bmp = new Bitmap(left_bmp.Width, left_bmp.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            BitmapArrayConversions.updatebitmap_unsafe(disparity_map, disparity_bmp);

            if (disparity_map_filename.ToLower().EndsWith("bmp"))
            {
                disparity_bmp.Save(disparity_map_filename, System.Drawing.Imaging.ImageFormat.Bmp);
            }
            if (disparity_map_filename.ToLower().EndsWith("jpg"))
            {
                disparity_bmp.Save(disparity_map_filename, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            if (disparity_map_filename.ToLower().EndsWith("gif"))
            {
                disparity_bmp.Save(disparity_map_filename, System.Drawing.Imaging.ImageFormat.Gif);
            }
            if (disparity_map_filename.ToLower().EndsWith("png"))
            {
                disparity_bmp.Save(disparity_map_filename, System.Drawing.Imaging.ImageFormat.Png);
            }
        }