基本信息
源码名称:halcon联合c#制作模版
源码大小:1.69M
文件格式:.rar
开发语言:C#
更新时间:2024-06-07
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 5 元 
   源码介绍
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HalconDotNet;
using One.Common;

namespace SetMarkModel
{
    public partial class SetModelMarkForm : Form
    {
        HTuple handle;
        HObject ho_A11bmp;
        HTuple w;
        HTuple h;
        HObject LastDispImage;
        HObject LastRegion=null;
        string TestConfigFile = Application.StartupPath "\\Config.ini";
        public SetModelMarkForm()
        {
            InitializeComponent();
            HOperatorSet.OpenWindow(0, 0, pictureBox1.Width, pictureBox1.Height, pictureBox1.Handle, "", "", out handle);
            
        }
        public void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            ImageScale(LastDispImage, e, LastRegion);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string filename = string.Empty;
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog()==DialogResult.OK)
            {
                filename = openFileDialog.FileName;
            }
            Bitmap bitmap = new Bitmap(filename);
            HImageConvertFromBitmap32(bitmap,out ho_A11bmp);
            DisplayImage(ho_A11bmp);
           
        }
        private void HImageConvertFromBitmap32(Bitmap bmp, out HObject Himage)
        {
            HOperatorSet.GenEmptyObj(out Himage);
            BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
            Himage.Dispose();
            try
            {
                HOperatorSet.GenImageInterleaved(out Himage, bmpData.Scan0, "bgrx", bmp.Width, bmp.Height, -1, "byte", bmp.Width, bmp.Height, 0, 0, -1, 0);
                bmp.UnlockBits(bmpData);
                bmp.Dispose();
            }
            catch (Exception ex)
            {
                Himage.Dispose();
            }
        }
        private void DisplayImage(HObject hObject)
        {
            HOperatorSet.SetColor(handle, "green");
            HOperatorSet.SetLineWidth(handle, 2);
            // HOperatorSet.GenRectangle1(out ho_Rectangle1, 258.61, 1337.1, 3627.04, 4846.13);
            //HOperatorSet.ReduceDomain(ho_A11bmp, ho_Rectangle1,out ho_ImageReduced);
            HOperatorSet.GetImageSize(hObject, out w, out h);
            HOperatorSet.SetPart(handle, 2, 2, h, w);
            HOperatorSet.DispObj(hObject, handle);
            LastDispImage = hObject;
           
            label4.Text = trackBar3.Value.ToString();
            label3.Text= trackBar4.Value.ToString();
        }
        public enum RoiType
        {
            Rectangle,Cir
        }
        public RoiType roiType;
        private void DrawRoi(RoiType roiType,HObject image)
        {
            if (roiType==RoiType.Cir)
            {
                HOperatorSet.DrawCircle(handle, out HTuple row, out HTuple col, out HTuple r);
                HOperatorSet.GenCircle(out HObject cir, row, col, r);
                HOperatorSet.ReduceDomain(image,cir,out HObject cirimage);
                HOperatorSet.ClearWindow(handle);
                DisplayImage(cirimage);
            }
            else if (roiType==RoiType.Rectangle)
            {
                HOperatorSet.DrawRectangle1(handle, out HTuple row, out HTuple col, out HTuple row2,out HTuple col2);
                HOperatorSet.GenRectangle1(out HObject rec, row, col, row2,col2);
                HOperatorSet.ReduceDomain(image, rec, out HObject recimage);
                HOperatorSet.ClearWindow(handle);
                DisplayImage(recimage);
            }
           
        }
        private void ImageScale(HObject NowImage, MouseEventArgs e, HObject region = null)
        {
            HTuple Zoom, Row, Col, Button;
            HTuple Row0, Column0, Row00, Column00, Ht, Wt, r1, c1, r2, c2;
            if (e.Delta > 0)
            {
                Zoom = 1.5;
            }
            else
            {
                Zoom = 0.5;
            }
            HOperatorSet.GetMposition(handle, out Row, out Col, out Button);
            HOperatorSet.GetPart(handle, out Row0, out Column0, out Row00, out Column00);
            Ht = Row00 - Row0;
            Wt = Column00 - Column0;
            if (Ht * Wt < 32000 * 32000 || Zoom == 1.5)//普通版halcon能处理的图像最大尺寸是32K*32K。如果无限缩小原图像,导致显示的图像超出限制,则会造成程序崩溃
            {
                r1 = (Row0 ((1 - (1.0 / Zoom)) * (Row - Row0)));
                c1 = (Column0 ((1 - (1.0 / Zoom)) * (Col - Column0)));
                r2 = r1 (Ht / Zoom);
                c2 = c1 (Wt / Zoom);
                HOperatorSet.SetPart(handle, r1, c1, r2, c2);
                HOperatorSet.ClearWindow(handle);
                try
                {
                    HOperatorSet.DispObj(NowImage, handle);
                    LastDispImage = NowImage;
                    if (region!=null)
                    {
                        DisplayRegion(region);
                    }
                }
                catch (HalconException)
                {

                    return;
                }

            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (ho_A11bmp==null)
            {
                return;
            }
            DrawRoi(roiType,ho_A11bmp);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            roiType = RoiType.Cir;
            radioButton1.Checked = true;
            label1.Text = this.trackBar1.Value.ToString();
            label2.Text = this.trackBar2.Value.ToString();
            if (!File.Exists(TestConfigFile))
            {
                File.Create(TestConfigFile);
            }
            this.pictureBox1.MouseWheel = new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseWheel);
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                roiType = RoiType.Cir;
                radioButton2.Checked = false;
            }
            else
            {
                roiType = RoiType.Rectangle;
                radioButton1.Checked = false;
            }
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            label1.Text = this.trackBar1.Value.ToString();
            SelectRegions(LastDispImage, this.trackBar1.Value, this.trackBar2.Value);
        }
        private void DisplayRegion(HObject region)
        {
            HOperatorSet.DispObj(region,handle);
            LastRegion = region;
        }
        /// <summary>
        /// 阈值选择显示
        /// </summary>
        /// <param name="LowThreold">最低阈值</param>
        /// <param name="HighThresold">最高阈值</param>
        private void SelectRegions(HObject image,int LowThreold, int HighThresold)
        {
            if (LowThreold > HighThresold)
            {
                return;
            }
            HOperatorSet.ClearWindow(handle);
            HOperatorSet.DispObj(image,handle);
            LastDispImage = image;
            //HOperatorSet.GenEmptyObj(out ho_Region);
            try
            {   
                HOperatorSet.Threshold(image, out HObject ho_OpenRegion, LowThreold, HighThresold);
                DisplayRegion(ho_OpenRegion);
                LastRegion = ho_OpenRegion;
                   // HOperatorSet.OpeningRectangle1(ho_OpenRegion, out ho_Region, 10, 250);
            }
            catch (HalconException)
            {
                return;
            }
            HOperatorSet.SetColor(handle, "green");
            //HOperatorSet.DispObj(ho_Region, handle);
        }
        HObject FillUpRegion;
        /// <summary>
        /// 面积选择显示
        /// </summary>
        /// <param name="LowArea">最小面积</param>
        /// <param name="HighArea">最大面积</param>
        private void SelectArea(int LowArea, int HighArea)
        {
            HOperatorSet.AreaCenter(LastDispImage, out HTuple arear, out HTuple row, out HTuple hTuple);
            trackBar3.Maximum = arear.I;
            trackBar4.Maximum = arear.I;
            //trackBar3.Value = 0;
            trackBar4.Value = trackBar4.Maximum;
            if (LowArea > HighArea)
            {
                return;
            }
            //HOperatorSet.CloseWindow(handle);
            //HOperatorSet.OpenWindow(0, 0, pictureBox1.Width, pictureBox1.Height, pictureBox1.Handle, "", "", out handle);
            HOperatorSet.ClearWindow(handle);
            HOperatorSet.DispObj(LastDispImage, handle);
            //HOperatorSet.DispObj(ho_Regions, handle);
           // HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            //HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
          //  ho_ConnectedRegions.Dispose();
            HOperatorSet.Connection(LastRegion, out HObject ho_ConnectedRegions);
            //ho_SelectedRegions.Dispose();
            try
            {
                HOperatorSet.SelectShape(ho_ConnectedRegions, out HObject ho_SelectedRegions, "area",
                "and", LowArea, HighArea);
                //LastRegion = ho_SelectedRegions;
                HOperatorSet.FillUp(ho_SelectedRegions, out  FillUpRegion);
                //LastRegion = FillUpRegion;
                HOperatorSet.ClearWindow(handle);
                HOperatorSet.SetColor(handle, "red");
                HOperatorSet.DispObj(LastDispImage, handle);
                HOperatorSet.DispObj(FillUpRegion, handle);
                //LastRegion = FillUpRegion;
            }
            catch (Exception)
            {

                return;
            }
           
        }
        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            label2.Text = this.trackBar2.Value.ToString();
            SelectRegions(LastDispImage,trackBar1.Value,trackBar2.Value);
        }

        private void trackBar3_Scroll(object sender, EventArgs e)
        {
            //HOperatorSet.GetImageSize(LastDispImage, out w, out h);
           
            label4.Text = trackBar3.Value.ToString();
            label3.Text = trackBar4.Value.ToString();
            SelectArea(trackBar3.Value,trackBar4.Value);
        }
        HObject ModelPic;
        HTuple ModelID;
        string FileName = string.Empty;
        private void SaveFileName()
        {
           FileFunc.WriteIniData(TestConfigFile, "FileSet", "取料模板保存路径", FileName);
        }
        private string ReadFileName
        {
            get { return FileFunc.ReadIniData(TestConfigFile, "FileSet", "取料模板保存路径"); }
        }
        private void button3_Click(object sender, EventArgs e)
        {
           
            HOperatorSet.ReduceDomain(ho_A11bmp,LastRegion,out ModelPic);
            HOperatorSet.CreateShapeModel(ModelPic, "auto", (new HTuple(0)).TupleRad(), (new HTuple(360)).TupleRad(), "auto", "auto", "use_polarity", "auto", "auto", out ModelID);
            //HOperatorSet.CreateNccModel(ModelPic, "auto", (new HTuple(0)).TupleRad() , (new HTuple(360)).TupleRad(), "auto", "ignore_global_polarity", out ModelID);
            SaveFileDialog saveImageDialog = new SaveFileDialog();
            saveImageDialog.Filter = "Model Files (*.ofm;*.ofm)|*.ofm;*.ofm";
            //openImageDialog.Multiselect = false;
            if (saveImageDialog.ShowDialog() == DialogResult.OK)
            {
                FileName = saveImageDialog.FileName;
                SaveFileName();
                HOperatorSet.WriteObject(LastRegion, saveImageDialog.FileName);
                HOperatorSet.WriteShapeModel(ModelID, saveImageDialog.FileName);
            }
            else
            {
                return;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            HOperatorSet.ReadShapeModel(ReadFileName,out HTuple ModelId);
            HOperatorSet.ReadObject(out HObject hregion,ReadFileName ".hobj");
            HOperatorSet.FindShapeModel(ho_A11bmp, ModelId, new HTuple(0).TupleRad(), new HTuple(360).TupleRad(), 0.2, 1, 0.5,
                "none", 0, 1, out HTuple out_Row1, out HTuple out_Column1, out HTuple out_Angle, out HTuple out_Score);
            HOperatorSet.AreaCenter(hregion,out HTuple area,out HTuple row,out HTuple col);
            HOperatorSet.VectorAngleToRigid(row,col,0,out_Row1,out_Column1,out_Angle,out HTuple home2d);
            HOperatorSet.AffineTransRegion(hregion,out HObject regionaffine,home2d, "nearest_neighbor");
            HOperatorSet.SetDraw(handle,"margin");
            HOperatorSet.SetLineWidth(handle, 2);
            HOperatorSet.DispObj(regionaffine,handle);
            LastRegion = regionaffine;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            LastRegion = FillUpRegion;
        }
    }
}