前言
C#联合halcon 鼠标移动获取图像灰度值。
代码
HObject hImage = null;
HTuple width = new HTuple();
HTuple height = new HTuple();
int pointX = -1;
int pointY = -1;
private void HSImageWindow_HMouseMove(object sender, HMouseEventArgs e)
{
if (hImage != null )
{
HOperatorSet.CountChannels(hImage, out HTuple hChannels);
HTuple grayValue = new HTuple();
pointX = Convert.ToInt32(e.X);
pointY = Convert.ToInt32(e.Y);
if (pointX >= 0 && pointY >= 0
&& pointX < width && pointY< height)
{
HOperatorSet.GetGrayval(hImage, pointY, pointX, out grayValue);
if (hChannels != null && hChannels == 1)
{
Debug.WriteLine($"[X = {pointX},Y = {pointY},Gray = {grayValue.D.ToString("F0")}]");
}
else if (hChannels != null && hChannels == 3)
{
Debug.WriteLine($"[X = {pointX},Y = {pointY},Gray = {grayValue.D.ToString("F0")}]");
}
}
}
else
{
Debug.WriteLine($"[X = {(int)e.X},Y = {(int)e.Y}]");
}
}