using OpenCvSharp;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
namespace OS.Spin.Common
{
public class Helper
{
/////
///// 检查图片是否存在
/////
/////
//public static void CheckExist(object filePath)
//{
// if (!File.Exists(filePath.ToString()))
// {
// Cache.GetInstance().SaveCount++;
// }
//}
public static double GetProcessUsedMemory()
{
double usedMemory = 0;
usedMemory = Process.GetCurrentProcess().WorkingSet64 / 1024.0 / 1024.0;
return usedMemory;
}
#region 清理非托管资源
[System.Runtime.InteropServices.DllImport("Kernel32")]
private extern static Boolean CloseHandle(IntPtr handle);
#endregion
#region
[DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
///
/// 释放内存
///
public static void ClearMemory()
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
}
#endregion
///
/// 通过传入的图片计算增益值
///
///
///
public static int GetGainValue(string imagePath)
{
try
{
Rect re = new Rect(new OpenCvSharp.Point(1100, 50), new OpenCvSharp.Size(200, 200));
Mat imageRoi = null;
Mat _cameraMat = null;
Mat _mat = new Mat(imagePath);
if (_mat.Channels() == 3)
{
_cameraMat = new Mat(_mat, re);
imageRoi = new Mat(200, 200, MatType.CV_8UC3);
Cv2.CvtColor(_cameraMat, imageRoi, ColorConversionCodes.BGR2GRAY);
}
else
{
imageRoi = new Mat(_mat, re);
}
int gainResult = 0;
double avgGain = Cv2.Mean(imageRoi).Val0;
int th_max = 230, th_min = 130, th_mid = 180;
if (avgGain > th_mid)
{
if (avgGain > th_max)
avgGain = th_max;
gainResult = 20 - (int)((avgGain - th_mid) / 5);
}
else
{
if (avgGain < th_min)
avgGain = th_min;
gainResult = 20 + (int)((th_mid - avgGain) / 5);
}
//imageRoi.Release();
//_cameraMat.Release();
//_mat.Release();
return gainResult;
}
catch (Exception ex)
{
LogisTrac.WriteLog(string.Format("GetGainValue(string imagePath):{0}", ex.Message));
return 0;
}
}
///
/// 通过传入的图片计算增益值
///
///
///
public static int GetGainValue(Mat _mat)
{
try
{
Rect re = new Rect(new OpenCvSharp.Point(1100, 50), new OpenCvSharp.Size(200, 200));
Mat imageRoi = null;
Mat _cameraMat = null;
if (_mat.Channels() == 3)
{
_cameraMat = new Mat(_mat, re);
imageRoi = new Mat(200, 200, MatType.CV_8UC3);
Cv2.CvtColor(_cameraMat, imageRoi, ColorConversionCodes.BGR2GRAY);
}
else
{
imageRoi = new Mat(_mat, re);
}
int gainResult = 0;
double avgGain = Cv2.Mean(imageRoi).Val0;
int th_max = 230, th_min = 130, th_mid = 180;
if (avgGain > th_mid)
{
if (avgGain > th_max)
avgGain = th_max;
gainResult = 20 - (int)((avgGain - th_mid) / 5);
}
else
{
if (avgGain < th_min)
avgGain = th_min;
gainResult = 20 + (int)((th_mid - avgGain) / 5);
}
//imageRoi.Release();
//_cameraMat.Release();
return gainResult;
}
catch (Exception ex)
{
LogisTrac.WriteLog(string.Format("GetGainValue(Mat _mat):{0}", ex.Message));
return 0;
}
}
///
/// 普通图片获取增益值(非Mat)
///
///
///
public static int GetGain(string filePath)
{
try
{
double[] a = new double[3] { 0, 0, 0 };
Bitmap curBitmap = (Bitmap)System.Drawing.Image.FromFile(filePath);
int wide = curBitmap.Width;
int height = curBitmap.Height;
Color srcColor;
double RR = 0;
double GG = 0;
double BB = 0;
for (int y3 = 50; y3 < (50 + 200); y3++)
{
for (int x3 = 1100; x3 < (1100 + 200); x3++)
{
srcColor = curBitmap.GetPixel(x3, y3);
RR = RR + srcColor.R;
GG = GG + srcColor.G;
BB = BB + srcColor.B;
}
}
a[0] = Math.Round(RR / (1100 * 50), 2);
double mean_gain = a[0];
int th_max = 230, th_min = 130, th_mid = 180;
if (mean_gain > th_mid)
{
if (mean_gain > th_max)
mean_gain = th_max;
return 20 - (int)((mean_gain - th_mid) / 5);
}
else
{
if (mean_gain < th_min)
mean_gain = th_min;
return 20 + (int)((th_mid - mean_gain) / 5);
}
}
catch (Exception ex)
{
LogisTrac.WriteLog(string.Format("GetGain(string filePath):{0}", ex.Message));
return 0;
}
}
}
}