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;
namespace TIASshot {
internal class IScam : CameraBase {
ICImagingControl _ic;
VCDRangeProperty _brightness;
VCDRangeProperty _gain;
VCDRangeProperty _exposure;
VCDRangeProperty _gamma;
VCDRangeProperty _whiteBalanceBlue;
VCDRangeProperty _whiteBalanceGreen;
VCDRangeProperty _whiteBalanceRed;
/// <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;
//if (!_ic.LoadShowSaveDeviceState("lastSelectedDeviceState.xml")) {
// return false;
//}
//_ic.SaveDeviceStateToFile("DBK33UX178.xml");
_ic.LoadDeviceStateFromFile($"{DeviceName}.xml", true);
if (!_ic.DeviceValid) {
ErrorMsg = "IScamの設定ファイルが見つかりません";
return false;
}
_ic.Sink = new TIS.Imaging.FrameQueueSink(Retrieve, MediaSubtypes.RGB24, 5);
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);
_brightness.Value = Config.GetInt("ISCAM/Brightness");
_gain.Value = Config.GetInt("ISCAM/Gain");
_exposure.Value = Config.GetInt("ISCAM/Exposure");
_gamma.Value = Config.GetInt("ISCAM/Gamma");
_whiteBalanceBlue.Value = Config.GetInt("ISCAM/WBBlue");
_whiteBalanceGreen.Value = Config.GetInt("ISCAM/WBGreen");
_whiteBalanceRed.Value = Config.GetInt("ISCAM/WBRed");
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("CalibrationUpdateInterval") == 0) {
_whiteBalanceBlue.Value = (int)(0.5 + _whiteBalanceBlue.Value * GetRatio((float)whitePatch.Val0, Config.GetFloat("ReferenceB")));
_whiteBalanceGreen.Value = (int)(0.5 + _whiteBalanceGreen.Value * GetRatio((float)whitePatch.Val1, Config.GetFloat("ReferenceG")));
_whiteBalanceRed.Value = (int)(0.5 + _whiteBalanceRed.Value * GetRatio((float)whitePatch.Val2, Config.GetFloat("ReferenceR")));
}
_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;
}
}
}