Newer
Older
TIASshot / TIASshot / IScam.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TIS.Imaging;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
using System.IO;

namespace TIASshot {
    internal class IScam : CameraBase {
        ICImagingControl _ic;
        VCDRangeProperty _brightness;
        VCDRangeProperty _gain;
        VCDRangeProperty _exposure;
        VCDRangeProperty _gamma;
        VCDRangeProperty _whiteBalanceBlue;
        VCDRangeProperty _whiteBalanceGreen;
        VCDRangeProperty _whiteBalanceRed;
        FrameQueueSink _queueSink;
        FrameSnapSink _snapSink;

        /// <summary>
        /// IScamコンストラクタ
        /// </summary>
        /// <param name="form"></param>
        /// <param name="ic"></param>
        public IScam(Form1 form, ICImagingControl ic, string deviceName) : base(form) {
            _ic = ic;
            DeviceName = deviceName;
        }

        /// <summary>
        /// IScam接続
        /// </summary>
        /// <returns></returns>
        public override bool Connect() {
            if (!BootCheck()) return false;

            var configFile = $"{DeviceName}.xml";
            if (!File.Exists(configFile)) {
                ErrorMsg = $"{configFile}が見つかりません";
                return false;
            }
            _ic.LoadDeviceStateFromFile(configFile, true);
            if (!_ic.DeviceValid) {
                ErrorMsg = $"設定ファイル{configFile}の読み込みに失敗しました";
                return false;
            }
            //if (!_ic.LoadShowSaveDeviceState(configFile)) {
            //    return false;
            //}

            _queueSink = new FrameQueueSink(Retrieve, MediaSubtypes.RGB24, 5);
            _snapSink = new FrameSnapSink(MediaSubtypes.RGB24);
            _ic.Sink = _queueSink;
            DeviceName = _ic.DeviceCurrent.Name;
            SerialNumber = _ic.DeviceCurrent.GetSerialNumber();
            _brightness = _ic.VCDPropertyItems.Find<VCDRangeProperty>(VCDGUIDs.VCDID_Brightness, VCDGUIDs.VCDElement_Value);
            _gain = _ic.VCDPropertyItems.Find<VCDRangeProperty>(VCDGUIDs.VCDID_Gain, VCDGUIDs.VCDElement_Value);
            _exposure = _ic.VCDPropertyItems.Find<VCDRangeProperty>(VCDGUIDs.VCDID_Exposure, VCDGUIDs.VCDElement_Value);
            _gamma = _ic.VCDPropertyItems.Find<VCDRangeProperty>(VCDGUIDs.VCDID_Gamma, VCDGUIDs.VCDElement_Value);
            _whiteBalanceBlue = _ic.VCDPropertyItems.Find<VCDRangeProperty>(VCDGUIDs.VCDID_WhiteBalance, VCDGUIDs.VCDElement_WhiteBalanceBlue);
            _whiteBalanceGreen = _ic.VCDPropertyItems.Find<VCDRangeProperty>(VCDGUIDs.VCDID_WhiteBalance, VCDGUIDs.VCDElement_WhiteBalanceGreen);
            _whiteBalanceRed = _ic.VCDPropertyItems.Find<VCDRangeProperty>(VCDGUIDs.VCDID_WhiteBalance, VCDGUIDs.VCDElement_WhiteBalanceRed);
            Debug.WriteLine($"Exposure range: {_exposure.RangeMin} to {_exposure.RangeMax}  value={_exposure.Value}" );
            Debug.WriteLine($"Gain range: {_gain.RangeMin} to {_gain.RangeMax}  value={_gain.Value}");
            Debug.WriteLine($"Brightness range: {_brightness.RangeMin} to {_brightness.RangeMax}  value={_brightness.Value}");
            Debug.WriteLine($"Gamma range: {_gamma.RangeMin} to {_gamma.RangeMax}  value={_gamma.Value}");
            Debug.WriteLine($"White Balance Blue range: {_whiteBalanceBlue.RangeMin} to {_whiteBalanceBlue.RangeMax}  value={_whiteBalanceBlue.Value}");
            Debug.WriteLine($"White Balance Green range: {_whiteBalanceGreen.RangeMin} to {_whiteBalanceGreen.RangeMax}  value={_whiteBalanceGreen.Value}");
            Debug.WriteLine($"White Balance Red range: {_whiteBalanceRed.RangeMin} to {_whiteBalanceRed.RangeMax}  value={_whiteBalanceRed.Value}");

            _ic.LiveStart();
            return true;
        }

        /// <summary>
        /// IScam切断
        /// </summary>
        public override void Disconnect() {
            _ic.LiveStop();
        }

        /// <summary>
        /// フレーム取得処理
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        private FrameQueuedResult Retrieve(IFrameQueueBuffer buffer) {
            var frameType = buffer.FrameType;

            using (Mat img = Mat.FromPixelData(frameType.Height, frameType.Width, MatType.CV_8UC3, buffer.GetIntPtr())) {
                using (Mat imgt = img.T()) {
                    _bmps[_bmpIndex] = imgt.ToBitmap();

                    if (_calibrating > 0) {
                        var whitePatch = Cv2.Mean(imgt, _chartMasks[12]);
                        Debug.WriteLine($"White patch R {whitePatch.Val2:.00} G {whitePatch.Val1:.00} B {whitePatch.Val0:.00}");
                        if (_calibrating % Config.GetInt("Calib/UpdateInterval") == 0) {
                            _whiteBalanceBlue.Value = (int)(0.5 + _whiteBalanceBlue.Value * GetRatio((float)whitePatch.Val0, Config.GetFloat("Calib/Reference/B")));
                            _whiteBalanceGreen.Value = (int)(0.5 + _whiteBalanceGreen.Value * GetRatio((float)whitePatch.Val1, Config.GetFloat("Calib/Reference/G")));
                            _whiteBalanceRed.Value = (int)(0.5 + _whiteBalanceRed.Value * GetRatio((float)whitePatch.Val2, Config.GetFloat("Calib/Reference/R")));
                        }
                        _calibrating--;
                        if (_calibrating == 0) {
                            Debug.WriteLine($"White Balance Blue range: {_whiteBalanceBlue.RangeMin} to {_whiteBalanceBlue.RangeMax}  value={_whiteBalanceBlue.Value}");
                            Debug.WriteLine($"White Balance Green range: {_whiteBalanceGreen.RangeMin} to {_whiteBalanceGreen.RangeMax}  value={_whiteBalanceGreen.Value}");
                            Debug.WriteLine($"White Balance Red range: {_whiteBalanceRed.RangeMin} to {_whiteBalanceRed.RangeMax}  value={_whiteBalanceRed.Value}");

                            CalcTcc(imgt);
                        }
                    }

                    if (!_calibrated && _calibrating == 0) {
                        DetectChart(imgt);
                    }
                }
            }
            _form.ShowImage(_bmps[_bmpIndex]);
            _bmpIndex = (_bmpIndex + 1) % 2;
            if (_bmps[_bmpIndex] != null) _bmps[_bmpIndex].Dispose();

            return FrameQueuedResult.ReQueue;
        }

        /// <summary>
        /// 撮影
        /// </summary>
        /// <param name="numImages"></param>
        /// <param name="interval"></param>
        protected override void Shot(int numImages = 1, int interval = 0) {
            _ic.LiveStop();
            _ic.Sink = _snapSink;
            _ic.LiveStart();
            var snapSink = _ic.Sink as FrameSnapSink;

            _shots.Clear();
            snapSink.SnapSingle(TimeSpan.FromSeconds(5));   // 最初のフレームを捨てる

            var thread = new Thread(() => SaveThread(numImages));
            thread.Start();

            var filename = Config.GetString("File/ShotTime");
            using (var csv = new StreamWriter(Path.Combine(_saveFolder, filename))) {
                csv.WriteLine("Shot,Time(ms)");

                var watch = Stopwatch.StartNew();
                for (int i = 0; i < numImages; i++) {
                    var shotTime = watch.ElapsedMilliseconds;
                    csv.WriteLine($"{i+1},{shotTime}");
                    _form.ShowMessage($"撮影 {i + 1} / {numImages} 枚目");
                    var buffer = snapSink.SnapSingle(TimeSpan.FromSeconds(5));

                    using (Mat img = Mat.FromPixelData(buffer.FrameType.Height, buffer.FrameType.Width, MatType.CV_8UC3, buffer.GetIntPtr())) {
                        _shots.Add(img.T());
                    }
                    while (watch.ElapsedMilliseconds < interval * (i+1) && i < numImages - 1) {
                        Thread.Sleep(1);
                    }
                }
            }

            _ic.LiveStop();
            _ic.Sink = _queueSink;
            _ic.LiveStart();
        }
    }
}