private void DrawPreview()
        {
            try
            {
                ImageCaptureInfo copy = imageCaptureInfo;
                copy.captureSizeX = previewPictureBox.Width;
                copy.captureSizeY = previewPictureBox.Height;

                //Show something in the preview
                previewImage = CaptureImageFullPreview(ref copy);
                float crop_size_x = copy.actual_crop_size_x;
                float crop_size_y = copy.actual_crop_size_y;

                lastFullCapture = previewImage;
                //Draw selection rectangle
                DrawCaptureRectangleBitmap();

                //Compute image crop coordinates according to selection rectangle

                //Get raw image size from imageCaptureInfo.actual_crop_size to compute scaling between raw and rectangle coordinates

                //Console.WriteLine("SIZE X: {0}, SIZE Y: {1}", imageCaptureInfo.actual_crop_size_x, imageCaptureInfo.actual_crop_size_y);

                imageCaptureInfo.crop_coordinate_left   = selectionRectanglePreviewBox.Left * (crop_size_x / previewPictureBox.Width);
                imageCaptureInfo.crop_coordinate_right  = selectionRectanglePreviewBox.Right * (crop_size_x / previewPictureBox.Width);
                imageCaptureInfo.crop_coordinate_top    = selectionRectanglePreviewBox.Top * (crop_size_y / previewPictureBox.Height);
                imageCaptureInfo.crop_coordinate_bottom = selectionRectanglePreviewBox.Bottom * (crop_size_y / previewPictureBox.Height);

                copy.crop_coordinate_left   = selectionRectanglePreviewBox.Left * (crop_size_x / previewPictureBox.Width);
                copy.crop_coordinate_right  = selectionRectanglePreviewBox.Right * (crop_size_x / previewPictureBox.Width);
                copy.crop_coordinate_top    = selectionRectanglePreviewBox.Top * (crop_size_y / previewPictureBox.Height);
                copy.crop_coordinate_bottom = selectionRectanglePreviewBox.Bottom * (crop_size_y / previewPictureBox.Height);

                Bitmap full_cropped_capture = CaptureImageFullPreview(ref copy, useCrop: true);
                croppedPreviewPictureBox.Image = full_cropped_capture;
                lastFullCroppedCapture         = full_cropped_capture;

                copy.captureSizeX = captureSize.Width;
                copy.captureSizeY = captureSize.Height;

                //Show matching bins for preview
                var        capture = CaptureImage();
                List <int> dummy;
                List <int> dummy2;
                int        black_level      = 0;
                var        features         = FeatureDetector.featuresFromBitmap(capture, out dummy, out black_level, out dummy2);
                int        tempMatchingBins = 0;
                var        isLoading        = FeatureDetector.compareFeatureVector(features.ToArray(), FeatureDetector.listOfFeatureVectorsEng, out tempMatchingBins, -1.0f, false);

                lastFeatures           = features;
                lastDiagnosticCapture  = capture;
                lastMatchingBins       = tempMatchingBins;
                matchDisplayLabel.Text = Math.Round((Convert.ToSingle(tempMatchingBins) / Convert.ToSingle(FeatureDetector.listOfFeatureVectorsEng.GetLength(1))), 4).ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.ToString());
            }
        }
        private List <int> featuresAtScreenCenter()
        {
            try
            {
                Stopwatch stopwatch = Stopwatch.StartNew();

                var        capture = CaptureImage();
                List <int> max_per_patch;
                List <int> min_per_patch;
                int        black_level            = 0;
                var        features               = FeatureDetector.featuresFromBitmap(capture, out max_per_patch, out black_level, out min_per_patch);
                float      new_avg_transition_max = 0.0f;
                int        tempMatchingBins       = 0;
                var        isLoading              = FeatureDetector.compareFeatureVectorTransition(features.ToArray(), FeatureDetector.listOfFeatureVectorsEng, max_per_patch, min_per_patch, -1.0f, out new_avg_transition_max, out tempMatchingBins, 0.8f, false, BlackLevel);//FeatureDetector.isGameTransition(capture, 30);

                lastFeatures = features;

                currentlyPaused = isLoading;

                if (saveDiagnosticImages && snapshotMilliseconds <= 0)
                {
                    snapshotMilliseconds = milliSecondsBetweenSnapshots;

                    snapshotFrameCount = frameCount;

                    segmentSnapshots.Add(capture);
                    segmentMatchingBins.Add(tempMatchingBins);
                    segmentFeatureVectors.Add(features);
                    segmentFrameCounts.Add(frameCount);
                }


                stopwatch.Stop();

                msElapsed.Add(stopwatch.ElapsedMilliseconds);
                if (msElapsed.Count > 20)
                {
                    long sum = 0;

                    foreach (var ms in msElapsed)
                    {
                        sum += ms;
                    }
                    sum /= msElapsed.Count;
                    msElapsed.Clear();
                    Console.WriteLine("DetectMatch (avg): " + sum + "ms");
                }

                if (tempMatchingBins >= 250 && tempMatchingBins <= 420 && saveDiagnosticImages)
                {
                    System.IO.Directory.CreateDirectory(DiagnosticsFolderName + "imgs_features_interesting");

                    try
                    {
                        capture.Save(DiagnosticsFolderName + "imgs_features_interesting/img_" + frameCount + "_" + tempMatchingBins + ".jpg", ImageFormat.Jpeg);
                    }
                    catch
                    {
                    }

                    saveFeatureVectorToTxt(features, "features_" + frameCount + "_" + tempMatchingBins + ".txt", DiagnosticsFolderName + "imgs_features_interesting");

                    //lastSaveFrame = frameCount;
                }


                if (currentlyPaused)
                {
                    //only save if we haven't saved for at least 10 frames, just for diagnostics to see if any false positives are in there.
                    //or if we haven't seen a paused frame for at least 30 frames.
                    if ((frameCount > (lastSaveFrame + 10) || (frameCount - lastPausedFrame) > 30) && saveDiagnosticImages && false)
                    {
                        System.IO.Directory.CreateDirectory(DiagnosticsFolderName + "imgs_stopped");

                        try
                        {
                            capture.Save(DiagnosticsFolderName + "imgs_stopped/img_" + frameCount + "_" + tempMatchingBins + ".jpg", ImageFormat.Jpeg);
                        }
                        catch
                        {
                        }

                        saveFeatureVectorToTxt(features, "features_" + frameCount + "_" + tempMatchingBins + ".txt", DiagnosticsFolderName + "features_stopped");

                        lastSaveFrame = frameCount;
                    }

                    lastPausedFrame = frameCount;
                }
                else
                {
                    //save if we haven't seen a running frame for at least 30 frames (to detect false runs - e.g. aku covering "loading"
                    if ((frameCount - lastRunningFrame) > 10 && saveDiagnosticImages && false)
                    {
                        System.IO.Directory.CreateDirectory(DiagnosticsFolderName + "imgs_running");
                        try
                        {
                            capture.Save(DiagnosticsFolderName + "imgs_running/img_" + frameCount + "_" + tempMatchingBins + ".jpg", ImageFormat.Jpeg);
                        }
                        catch
                        {
                        }

                        saveFeatureVectorToTxt(features, "features_" + frameCount + "_" + tempMatchingBins + ".txt", DiagnosticsFolderName + "features_running");

                        lastSaveFrame = frameCount;
                    }
                    lastRunningFrame = frameCount;
                }

                frameCount++;
                //histogramMatches.Add(matchingHistograms);
                try
                {
                    Invoke(new Action(() =>
                    {
                        try
                        {
                            imageDisplay.Image = capture;
                            imageDisplay.Size  = new Size(captureSize.Width, captureSize.Height);
                            //imageDisplay.BackgroundImage = capture;
                            imageDisplay.Refresh();
                            matchDisplayLabel.Text = tempMatchingBins.ToString();
                            //requiredMatchesTxt.Text = numberOfBinsCorrect.ToString();
                            pausedDisplay.BackColor = isLoading ? Color.Red : Color.Green;

                            pauseCountLabel.Text = pauseSegmentList.Items.Count.ToString();
                            capture.Dispose();
                        }
                        catch
                        {
                        }
                    }));
                }
                catch
                {
                    //if the form is closing, we just do nothing.
                }
                matchingBins = tempMatchingBins;
                return(features);
            }
            catch (Exception)
            {
                Console.WriteLine("TESTESTESTEST");
                return(new List <int>());
            }
            //bmp.Dispose();
        }