using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
using OTS.WinFormsUI.Docking;
using OTSDataType;
using OTSSysMgrTools;
using static OTSMeasureApp.OTSMeasureStatusWindow;
namespace OTSMeasureApp
{
public partial class frmSpecialGrayParticle : DockContent
{
//国际化
OTSSysMgrTools.Language lan;
Hashtable table;
//测量主窗体
public OTSIncAMeasureAppForm m_MeasureAppForm;
public OTSMeasureStatusWindow MeasureStatuWindow;
OTSImageData m_ImageData = null;
NLog.Logger log ;
public frmSpecialGrayParticle(OTSIncAMeasureAppForm m_MeasureForm, OTSMeasureStatusWindow MeasureStatuForm)
{
log = NLog.LogManager.GetCurrentClassLogger();
InitializeComponent();
m_MeasureAppForm = m_MeasureForm;
MeasureStatuWindow = MeasureStatuForm;
//国际化
lan = new OTSSysMgrTools.Language(this);
table = lan.GetNameTable(this.Name);
}
private Bitmap bseImg;
///
/// 获取当前的BSE原图
///
public Bitmap BseImg { get => bseImg; set => bseImg = value; }
private byte[] bBseData;
private byte[] originalBseData;
///
/// 获取当前的BSE原图数据
///
public byte[] GetBBseData()
{
return bBseData;
}
///
/// 获取当前的BSE原图数据
///
public void SetBBseData(byte[] value)
{
bBseData = value;
originalBseData = value;
}
private void RestoreOriginalBseData()
{
bBseData = originalBseData;
}
//去背景灰度最小值
private int bseGrayMinValue=-1;
///
/// 去背景灰度最小值
///
public int BseGrayMinValue { get => bseGrayMinValue; set => bseGrayMinValue = value; }
//去背景灰度最大值
private int bseGrayMaxValue = -1;
///
/// 去背景灰度最大值
///
public int BseGrayMaxValue { get => bseGrayMaxValue; set => bseGrayMaxValue = value; }
private void frmSpecialGrayParticle_Load(object sender, EventArgs e)
{
if (BseImg != null)
{
pbBSEImage.Image = BseImg;
}
if (BseGrayMinValue >-1)
{
nuDownGrayStart.Value = BseGrayMinValue;
tbGrayStart.Value = BseGrayMinValue;
txtGrayStart.Text = BseGrayMinValue.ToString();
}
if (BseGrayMaxValue >-1)
{
nuDownGrayEnd.Value = BseGrayMaxValue;
tbGrayEnd.Value = BseGrayMaxValue;
txtGrayEnd.Text = BseGrayMaxValue.ToString();
}
if (m_ImageData == null)
{
m_ImageData = new OTSImageData(MeasureStatuWindow, m_MeasureAppForm);
}
}
#region BSE图去背景
///
/// BSE图去背景
///
/// BSE原图
/// 开始灰度值
/// 结束灰度值
///
protected Bitmap RemoveBseGray(Bitmap bInput,int grayStart, int grayEnd)
{
int imgWidth = bInput.Width;
int imgHeight = bInput.Height;
//转换颜色
List colorMapTemp = new List();
for (int i = 0; i <= grayStart; i++)
{
ColorMap colorMap = new ColorMap();
string colorName = "#" + Color.FromArgb(i, i, i).Name.ToString();
colorMap.OldColor = ColorTranslator.FromHtml(colorName);
colorMap.NewColor = Color.White;
colorMapTemp.Add(colorMap);
}
for (int i = grayEnd; i <= 255; i++)
{
ColorMap colorMap = new ColorMap();
string colorName = "#" + Color.FromArgb(i, i, i).Name.ToString();
colorMap.OldColor = ColorTranslator.FromHtml(colorName);
colorMap.NewColor = Color.White;
colorMapTemp.Add(colorMap);
}
if (colorMapTemp.Count > 0)
{
Bitmap cutbitmap = new Bitmap(bInput.Width, bInput.Height);
//创建Graphics对象
Graphics g = Graphics.FromImage(cutbitmap);
//生成的图像大小
int width = bInput.Width;
int height = bInput.Height;
//编辑被着急图像所要显示的位置
Rectangle DrawRect = new Rectangle(0, 0, imgWidth, imgHeight);
//编辑输出画布中着色的位置
Rectangle ShowRect = new Rectangle(0, 0, imgWidth, imgHeight);
ImageAttributes attr = new ImageAttributes();
attr.SetRemapTable(colorMapTemp.ToArray());
//从输入图像中截图至临时图像中
g.DrawImage(bInput, ShowRect, 0, 0, DrawRect.Width, DrawRect.Height, GraphicsUnit.Pixel, attr);
g.Dispose();
MemoryStream ms = new MemoryStream();
cutbitmap.Save(ms, ImageFormat.Png);
cutbitmap.Dispose();
//返回图像对象
return (Bitmap)Image.FromStream(ms);
}
else
{
return null;
}
}
#endregion
protected void ShowRemoveBGImage(int startGray,int endGray)
{
try
{
//获取电镜中图像大小
int m_iWidth=0, m_iHeight=0;
string str = m_MeasureAppForm.m_ProjParam.GetBSEImageResolution();
string[] sArray = str.Split('X');
if (sArray[0] != "" && sArray[1] != "")
{
m_iWidth = Convert.ToInt32(sArray[0]);
m_iHeight = Convert.ToInt32(sArray[1]);
}
byte[] cBseData=new byte[m_iHeight*m_iWidth];
var bfResult = m_ImageData.GetSpecialGrayImage(bBseData,m_iWidth, m_iHeight, startGray,endGray, ref cBseData);
bBseData = cBseData;
//取图不成功就返回
if (!bfResult) { return; }
Bitmap bitmap = Imagepro.ToGrayBitmap(cBseData, m_iWidth, m_iHeight);
ShowBSEImage(bitmap);
}
catch (Exception ex)
{
log.Error("(LZMeasureStatusWindow.ShowRemoveBGImage_Click) " + ex.ToString());
}
}
#region 显示去背景BSE图
public void ShowBSEImage(Bitmap BseImg)
{
if (BseImg != null)
{
pbBSEImage.Image = BseImg;
pbBSEImage.Refresh();
}
}
#endregion
private void nuDwonGrayStart_ValueChanged(object sender, EventArgs e)
{
RemoveBseGrayValueChanged();
}
private void nuDownGrayEnd_ValueChanged(object sender, EventArgs e)
{
RemoveBseGrayValueChanged();
}
protected void RemoveBseGrayValueChanged()
{
int grayStart = (int)nuDownGrayStart.Value;
int grayEnd = (int)nuDownGrayEnd.Value;
if (grayStart <= grayEnd)
{
Bitmap reBseImg = RemoveBseGray(BseImg, grayStart, grayEnd);
if (reBseImg != null)
{
ShowBSEImage(reBseImg);
}
}
else
{
nuDownGrayEnd.Value = nuDownGrayStart.Value;
}
txtGrayStart.Text = nuDownGrayStart.Value.ToString();
txtGrayEnd.Text = nuDownGrayEnd.Value.ToString();
tbGrayStart.Value = (int)nuDownGrayStart.Value;
tbGrayEnd.Value = (int)nuDownGrayEnd.Value;
}
private void tbGrayStart_Scroll(object sender, EventArgs e)
{
int grayStart = (int)tbGrayStart.Value;
int grayEnd = (int)tbGrayEnd.Value;
if (grayStart <= grayEnd)
{
Bitmap reBseImg = RemoveBseGray(BseImg, grayStart, grayEnd);
if (reBseImg != null)
{
ShowBSEImage(reBseImg);
}
}
else
{
tbGrayStart.Value = tbGrayStart.Value;
tbGrayEnd.Value = tbGrayStart.Value;
}
txtGrayStart.Text = tbGrayStart.Value.ToString();
txtGrayEnd.Text = tbGrayEnd.Value.ToString();
nuDownGrayStart.Value = (int)tbGrayStart.Value;
nuDownGrayEnd.Value = (int)tbGrayEnd.Value;
}
private void tbGrayEnd_Scroll(object sender, EventArgs e)
{
int grayStart = (int)tbGrayStart.Value;
int grayEnd = (int)tbGrayEnd.Value;
if (grayStart <= grayEnd)
{
txtGrayStart.Text = grayStart.ToString();
txtGrayEnd.Text = grayEnd.ToString();
Bitmap reBseImg = RemoveBseGray(BseImg, grayStart, grayEnd);
if (reBseImg != null)
{
ShowBSEImage(reBseImg);
}
}
else
{
tbGrayEnd.Value = tbGrayStart.Value;
txtGrayEnd.Text = grayStart.ToString();
}
nuDownGrayStart.Value = tbGrayStart.Value;
nuDownGrayEnd.Value = tbGrayEnd.Value;
}
private void btnYes_Click(object sender, EventArgs e)
{
RestoreOriginalBseData();
BseGrayMinValue = Convert.ToInt32(txtGrayStart.Text);
BseGrayMaxValue = Convert.ToInt32(txtGrayEnd.Text);
COTSImageProcParam cOTSImgProc = new COTSImageProcParam();
//remove the low end gray pixel
//CIntRange cIntRange = new CIntRange();
//cIntRange.SetStart(0);
//cIntRange.SetEnd(BseGrayMinValue);
//cOTSImgProc.SetBGGray(cIntRange);
var reBseImg= ShowRemoveBGImage(bBseData,BseGrayMinValue, bseGrayMaxValue);
ShowBSEImage(reBseImg);
//remove the high end gray pixel
//cIntRange.SetStart(BseGrayMaxValue);
//cIntRange.SetEnd(255);
//cOTSImgProc.SetBGGray(cIntRange);
//ShowRemoveBGImage(cOTSImgProc);
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
#region BSE图去背景
protected Bitmap ShowRemoveBGImage(byte[] bBseData,int grayStart,int grayEnd)
{
try
{
int m_iWidth = 0;
int m_iHeight = 0;
//获取电镜中图像大小
string str = m_MeasureAppForm.m_ProjParam.GetBSEImageResolution();
string[] sArray = str.Split('X');
if (sArray[0] != "" && sArray[1] != "")
{
m_iWidth = Convert.ToInt32(sArray[0]);
m_iHeight = Convert.ToInt32(sArray[1]);
}
//去背景图
byte[] cBseData = null;
//获取图像数据
var imagefun = new OTSBSEImageFun();
bool bfResult = imagefun.GetBSEImage(bBseData, m_iHeight, m_iWidth, grayStart, grayEnd, ref cBseData);
if (bfResult)
{
Bitmap reImg= Imagepro.ToGrayBitmap(cBseData, m_iWidth, m_iHeight);
return reImg;
}
else
{
return null;
}
}
catch (Exception ex)
{
log.Error("(ShowRemoveBGImage_Click):" + ex.ToString());
}
return null;
}
#endregion
}
}