public class InfraImage : IDisposable
{
private string exe;
private string img;
private string outPut;
private int width;
private int height;
private Mat mat;
public InfraImage(string img)
{
this.exe = AppDomain.CurrentDomain.BaseDirectory+@"\温度解析\dji_irp.exe";
if (!File.Exists(exe))
{
throw new Exception($"温度解析程序 {exe} 不存在");
}
this.img = img;
if (!File.Exists(img))
{
throw new Exception($"图像 {img} 不存在");
}
outPut = Path.Combine(Path.GetDirectoryName(img), Path.GetFileNameWithoutExtension(img) + ".raw");
if (!File.Exists(outPut))
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
convertImg(img, outPut);
stopwatch.Stop();
Console.WriteLine($"{stopwatch.ElapsedMilliseconds}毫秒");
}
System.Windows.Size imageSize = FileInfoHelper.GetImageSizee(img);
width = (int)imageSize.Width;
height = (int)imageSize.Height;
var res = File.ReadAllBytes(outPut);
mat = new Mat(width, height, MatType.CV_32FC1, res);
}
public void getMaxMinTemp(out double min, out double max)
{
mat.MinMaxIdx(out double _min, out double _max);
min = _min;
max = _max;
Console.WriteLine($"{min} {max}");
}
public double getAverTemp()
{
var mean = mat.Mean();
return mat.Mean().Val0;
}
public float getTemp(int row, int col)
{
return mat.Get<float>(row, col);
}
public void Dispose()
{
if (!mat.IsDisposed)
{
mat.Dispose();
}
}
private void convertImg(string img, string outPut)
{
Process process = new Process();
process.StartInfo.FileName = exe;
process.StartInfo.Arguments = $"dji_irp.exe -s {img} -a measure -o {outPut} --measurefmt float32";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
string outputData = string.Empty;
string errorData = string.Empty;
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.OutputDataReceived += (sender, e) =>
{
outputData += (e.Data + "\n");
};
process.ErrorDataReceived += (sender, e) =>
{
errorData += (e.Data + "\n");
};
process.WaitForExit();
string output = outputData;
string error = errorData;
}
}