C# Aforge - 图像不一致,有些图像很亮,但大多数不是

C# Aforge - image is not consistent, some images are very bright most of them not

我正在尝试使用 Aforge 获得一致的图像,但有时图像非常明亮,大部分时间都很好。如果你看图片,你会发现两者之间有非常明显的区别。图片应该像第二张图片,这样我就可以对其进行图像处理,但由于它太亮了,有时会把它弄乱。在图片中,您会看到摄像头暴露在外部光线下,但在我们的测试设置中却没有,甚至存在问题。我使用的是罗技 c900 网络摄像头。

我不知道它们为什么不同或如何解决。我尝试了一些方法,比如设置亮度,给它更多时间来设置滤镜但是......代码如下所示:

using AForge.Imaging.Filters;
using AForge.Video;
using AForge.Video.DirectShow;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;

namespace X
{
    public class LiveCamera
    {
        #region Properties
        private static readonly double _maxiumumDurationToTakePicture = 30;
        public static bool RequestPicture { get; private set; } = false;
        public static bool PictureTaken { get; private set; } = false;
        public static bool IsCameraStarted { get; private set; } = false;

        public static VideoCaptureDevice VideoSource { get; private set; }
        public static Bitmap Picture { get; private set; }
        public static CameraConfig CamConfig { get; private set; }
        #endregion

        public static void StartCameraService(CameraConfig camConfig = null)
        {
            LogUtil.Logger.Information($"StartCameraService - starting the camera service");
            try
            {
                CamConfig = CheckCameraConfig(camConfig);
                LogUtil.Logger.Information($"StartCameraService - config loaded: {camConfig}");
                var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                var moniker = Camera.GetCameraMonikerString(videoDevices, CamConfig.CameraName);
                VideoSource = new VideoCaptureDevice(moniker);
                VideoSource.ProvideSnapshots = true;
                VideoSource.VideoResolution = Camera.GetResolution(VideoSource, CamConfig);
                Camera.SetCameraOptions(VideoSource, CamConfig);

                VideoSource.NewFrame += new NewFrameEventHandler(Video_NewFrame);
                
                Thread.Sleep(5000);
                VideoSource.Start();
                Thread.Sleep(200);
                IsCameraStarted = true;
            }
            catch (Exception ex)
            {
                LogUtil.Logger.Error($"StartCameraService - camera service didn't start well: {ex}");
            }
        }

        private static void Video_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            if (RequestPicture)
            {
                Picture = (Bitmap)eventArgs.Frame.Clone();
                if (!(Picture is null))
                {
                    PictureTaken = true;
                }
            }
        }

        public static string GetCameraOutput(string name = "", string path = "")
        {
            if (IsCameraStarted == false)
            {
                return "";
            }

            RequestPicture = true;
            name = CheckName(name);
            path = CheckPath(path);

            LogUtil.Logger.Information($"GetCameraOutput - Taking the picture from the camera service");
            try
            {
                var endTime = DateTime.Now.AddSeconds(_maxiumumDurationToTakePicture);
                while (!PictureTaken)
                {
                    if (DateTime.Now > endTime)
                    {
                        break;
                    }
                }

                SetPictureFilters(CamConfig);
                var absPath = path + name;
                absPath = VerifySavedImage(absPath);
                LogUtil.Logger.Information($"GetCameraOutput - Taking the picture path: {absPath}");
                Thread.Sleep(100);
                Reset();
                return absPath;
            }
            catch (Exception ex)
            {
                LogUtil.Logger.Error($"GetCameraOutput - could not get camera capture: {ex}");
            }
            Reset();
            return "";
        }

        public static void CloseCameraService()
        {
            LogUtil.Logger.Information($"CloseCameraService - Closing the camera service");
            if (IsCameraStarted is false)
            {
                return;
            }

            try
            {
                VideoSource.SignalToStop();
                VideoSource.NewFrame -= new NewFrameEventHandler(Video_NewFrame);
                IsCameraStarted = false;
                Reset();
                Thread.Sleep(1000);
            }
            catch (Exception ex)
            {
                LogUtil.Logger.Error($"CloseCameraService - could not close the camera: {ex}");
            }
        }

        #region Internal
        private static string VerifySavedImage(string path)
        {
            if(Picture is null)
            {
                LogUtil.Logger.Information("The image was null, not able to save the picture.");
            }
            if (!File.Exists(path))
            {
                Picture.Save(path + ".png", ImageFormat.Jpeg);
                return path + ".png";
            }
            else
            {
                var rand = new Random();
                var number = rand.Next(0, 100);
                path = $"{path}_{number}.png";
                Picture.Save(path + ".png", ImageFormat.Jpeg); // ...
                return path;
            }
        }

        private static void Reset()
        {
            RequestPicture = false;
            PictureTaken = false;
            Picture = null;
        }

        private static string CheckName(string name)
        {
            if (name.Equals(""))
            {
                name = Config.Default.DefaultImageName + Guid.NewGuid().ToString();
            }
            return name;
        }

        private static string CheckPath(string path)
        {
            if (path.Equals(""))
            {
                path = Config.Default.DefaultImagePath;
            }
            ResourceFolder.CreateFolderPath(path);
            return path;
        }

        private static CameraConfig CheckCameraConfig(CameraConfig config)
        {
            if (config is null)
            {
                config = ConfigReader.GetConfig("MVPConfig.json").CameraConfig;
            }
            return config;
        }
        #endregion

        #region Picture filters
        private static void SetPictureFilters(CameraConfig config)
        {
            SetBrightnessCorrectionFilter(config.Brightness);
        }

        private static void SetBrightnessCorrectionFilter(int? brightnessFactor)
        {
            if (brightnessFactor is null)
            {
                return;
            }
            var br = new BrightnessCorrection(brightnessFactor.Value);
            Thread.Sleep(50);
            br.ApplyInPlace(Picture);
        }
        #endregion
    }
}

我使用上面的如下

LiveCamera.StartCameraService();
var capture = LiveCamera.GetCameraOutput();
LiveCamera.CloseCameraService();

我正在使用 C# dotnet 框架 4 和 aforge 2.2.5,我查看是否有人遇到过这个问题,但在这个论坛或互联网上找不到任何东西。任何帮助都会非常好!提前致谢。

当我在拍照时检查对焦和曝光的值时,我发现曝光没有设置到它应该设置的值。我通过检查值是否未设置是否应该重新设置来解决这个问题。这似乎可以解决问题。

为什么我不知道第一次没有设置值。特别是因为我给了相机足够的时间等等。