C#与halcon联合 缩放移动自适应图像

读取图片

//获取文件路径下的图片
        public HObject GetImgFromPath(string imgPath)
        {
            HObject L_Img;
            HOperatorSet.GenEmptyObj(out L_Img);//清空图片
            L_Img.Dispose();//释放
            HOperatorSet.ReadImage(out L_Img, imgPath);//读取图片存入到l_img
            return L_Img;
        }

拉伸显示

//图片拉伸显示
        public void  ImgIsStretchDisplay(HObject L_Img, HTuple Hwindow)
        {
            HTuple hv_Width,hv_Height;
            HOperatorSet.ClearWindow(Hwindow);//清空画面
            HOperatorSet.GetImageSize(L_Img, out hv_Width, out hv_Height);//获取图片大小规格
            HOperatorSet.SetPart(Hwindow, 0, 0, hv_Height, hv_Width);//设置窗体的规格
            HOperatorSet.DispObj(L_Img, Hwindow);//显示图片

        }

不拉伸,原图比例显示

//图片不拉伸显示
        public void ImgIsNotStretchDisplay(HObject L_Img, HTuple Hwindow)
        {
            HTuple hv_Width, hv_Height;
            HTuple win_Width, win_Height, win_Col, win_Row, cwin_Width, cwin_Height;
            HOperatorSet.ClearWindow(Hwindow);
            HOperatorSet.GetImageSize(L_Img, out hv_Width, out hv_Height);//获取图片大小规格
            HOperatorSet.GetWindowExtents(Hwindow, out win_Row, out win_Col, out win_Width, out win_Height);//获取窗体大小规格
            cwin_Height = 1.0 * win_Height / win_Width * hv_Width;//宽不变计算高          
            if (cwin_Height > hv_Height)//宽不变高能容纳
            {
                cwin_Height = 1.0 * (cwin_Height - hv_Height) / 2;
                HOperatorSet.SetPart(Hwindow, -cwin_Height, 0, cwin_Height + hv_Height, hv_Width);//设置窗体的规格
            }
            else//高不变宽能容纳
            {
                cwin_Width = 1.0 * win_Width / win_Height * hv_Height;//高不变计算宽
                cwin_Width = 1.0 * (cwin_Width - hv_Width) / 2;
                HOperatorSet.SetPart(Hwindow, 0, -cwin_Width, hv_Height, cwin_Width + hv_Width);//设置窗体的规格
            }
            HOperatorSet.DispObj(L_Img, Hwindow);//显示图片
        }    

图像缩放

解决缩太小崩溃问题,联合鼠标滚轮事件

//图片缩小放大,配合鼠标滚轮事件
        public  void ImgZoom(HObject L_Img, HTuple Hwindow, int Delta = 0)
        {
            HTuple Zoom, Row, Col, L_Button;
            HTuple hv_Width, hv_Height;
            HTuple Row0, Column0, Row00, Column00, Ht, Wt;
            HTuple[] Now_Pos = new HTuple[4];
            if (Delta > 0)//鼠标滚动格值,一般120
            {
                Zoom = 1.2;//向上滚动,放大倍数
            }
            else
            {
                Zoom = 0.8;//向下滚动,缩小倍数
            }
            HOperatorSet.GetMposition(Hwindow, out Row, out Col, out L_Button);//获取当前鼠标的位置
            HOperatorSet.GetPart(Hwindow, out Row0, out Column0, out Row00, out Column00);//获取当前窗体的大小规格
            HOperatorSet.GetImageSize(L_Img, out hv_Width, out hv_Height);//获取图片大小规格
            Ht = Row00 - Row0;
            Wt = Column00 - Column0;
            if (Ht * Wt < 32000 * 32000 || Zoom == 1.2)
            {
                Now_Pos[0] = (Row0 + ((1 - (1.0 / Zoom)) * (Row - Row0)));
                Now_Pos[1] = (Column0 + ((1 - (1.0 / Zoom)) * (Col - Column0)));
                Now_Pos[2] = Now_Pos[0] + (Ht / Zoom);
                Now_Pos[3] = Now_Pos[1] + (Wt / Zoom);
                HOperatorSet.SetPart(Hwindow, Now_Pos[0], Now_Pos[1], Now_Pos[2], Now_Pos[3]);
                HOperatorSet.ClearWindow(Hwindow);
                HOperatorSet.DispObj(L_Img, Hwindow);
            }
            else
            {
                ImgIsNotStretchDisplay(L_Img, Hwindow);//不拉伸显示
            }

        }

图像移动,实时移动

解决移动太快图像刷新闪烁问题

HTuple oldRow, oldColumn;
        //鼠标按下去拖着图像移动,配合鼠标坐标按下与移动事件
        public void MouseDownMoveImg(HObject L_Img, HTuple Hwindow)
        {
            HTuple row1, col1, row2, col2, Row, Column, Button;
            HOperatorSet.GetMposition(Hwindow, out Row, out Column, out Button);
            double RowMove = Row - oldRow; 
            double ColMove = Column - oldColumn;
            HOperatorSet.GetPart(Hwindow, out row1, out col1, out row2, out col2);//得到当前的窗口坐标
            HOperatorSet.SetPart(Hwindow, row1 - RowMove, col1 - ColMove, row2 - RowMove, col2 - ColMove);

            //防止刷新图片太快的时候闪烁
            HOperatorSet.SetSystem("flush_graphic", "false");
            HOperatorSet.ClearWindow(Hwindow);
            HOperatorSet.SetSystem("flush_graphic", "true");
            //

            HOperatorSet.DispObj(L_Img, Hwindow);
        }

        public void SaveMouseDownPosition(HTuple Hwindow)
        {
            HTuple Button;
            HOperatorSet.GetMposition(Hwindow, out oldRow, out oldColumn, out Button);

        }

图像自适应

这里我们采用不拉伸显示函数

//设置自动适应
        private void tsMAutoFit_Click(object sender, EventArgs e)
        {
            try
            {
                ImgIsNotStretchDisplay(ho_Image1, hWindowControl1.HalconWindow);
            }
            catch (Exception)
            {
                MessageBox.Show("自适应失败,请插入图像!");
            }          
        }
相关推荐
机器视觉李小白3 天前
Halcon 机器视觉案例 之 药剂液面高度测量
图像处理·笔记·halcon·机器视觉
机器视觉李小白14 天前
HALCON 算子 之 阈值分割算子
笔记·halcon·机器视觉
小菠萝090816 天前
【Halcon】hough_lines_dir和hough_lines
图像处理·halcon
小菠萝09081 个月前
Halcon lines_gauss
图像处理·人工智能·计算机视觉·halcon
liyuanbhu1 个月前
Halcon HImage 与 Qt QImage 的相互转换(修订版)
qt·计算机视觉·halcon
凌霜残雪2 个月前
Halcon基础-瓶盖带角度的OCR批量识别
ocr·halcon
code bean2 个月前
【halcon/C#】弧度和角度的转换
开发语言·c#·halcon
arvinzd3 个月前
Halcon基础系列1-基础算子
halcon
仰望大佬0073 个月前
HalconDotNet实现二维码识别功能详解
图像处理·计算机视觉·halcon·halcondotnet
仰望大佬0073 个月前
HalconDotNet实现OCR详解
c#·ocr·halcon·halcondotnet