HALCON图像坐标与控件坐标相互转换

HALCON图像坐标与控件坐标相互转换

1.控件坐标转换到图像坐标

代码示例:

        public Point ControlPointToHImagePoint(this HSmartWindowControlWPF Halcon, double x, double y)
        {
            // Halcon 控件宽高
            double cHeight = Halcon.ActualHeight;
            double cWidth = Halcon.ActualWidth;
            // Halcon 图像在窗口上显示的区域
            double x0 = Halcon.HImagePart.X;
            double y0 = Halcon.HImagePart.Y;
            double imHeight = Halcon.HImagePart.Height;
            double imWidth = Halcon.HImagePart.Width;
            // 缩放系数
            double ratio_y = imHeight / cHeight;
            double ratio_x = imWidth / cWidth;
            double x1;
            double y1;
            x1 = (ratio_x * x) + x0;
            y1 = (ratio_y * y) + y0;
            return new Point(x1, y1);
        }

2.图像坐标转换到控件坐标

        public Point HImagePointToControlPoint(this HSmartWindowControlWPF Halcon, double x, double y)
        {
            // Halcon 控件宽高
            double cHeight = Halcon.ActualHeight;
            double cWidth = Halcon.ActualWidth;
            // Halcon 图像在窗口上显示的区域
            double x0 = Halcon.HImagePart.X;
            double y0 = Halcon.HImagePart.Y;
            double imHeight = Halcon.HImagePart.Height;
            double imWidth = Halcon.HImagePart.Width;
            // 缩放系数
            double ratio_y = imHeight / cHeight;
            double ratio_x = imWidth / cWidth;
            double x1;
            double y1;
            x1 = (x - x0) / ratio_x;
            y1 = (y - y0) / ratio_y;
            return new Point(x1, y1);
        }

3.HALCON灰度图像转换到byte[]数组

        public static void GetImageGrayValue(HImage ho_Image, out byte[] grayValue)
        {
            HOperatorSet.GetImagePointer1(ho_Image, out HTuple hv_Pointer, out HTuple hv_Type, out HTuple hv_Width, out HTuple hv_Height);
            int len = hv_Width * hv_Height;
            grayValue = new byte[len];
            Marshal.Copy(hv_Pointer, grayValue, 0, len);
            return;
        }

4.HALCON彩色图像转换到byte[]数组

        public static void GetImageMultiValue(HImage ho_Image, out byte[] R, out byte[] G, out byte[] B)
        {
            HOperatorSet.GetImagePointer3(ho_Image, out HTuple hv_PointerRed, out HTuple hv_PointerGreen, out HTuple hv_PointerBlue, out HTuple hv_Type, out HTuple hv_Width, out HTuple hv_Height);
            int len = hv_Width * hv_Height;
            R = new byte[len];
            G = new byte[len];
            B = new byte[len];
            Marshal.Copy(hv_PointerRed, R, 0, len);
            Marshal.Copy(hv_PointerGreen, G, 0, len);
            Marshal.Copy(hv_PointerBlue, B, 0, len);
            return;
        }

你可能感兴趣的:(Halcon,Halcon坐标转换)