图像处理应用程序的框架实现

 使用 Windows Forms 和 Halcon 库来处理图像,包含图像处理操作,读取图片、阈值分割等

在MainWindow里

using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 第一个联合项目
{
//枚举 ImagePro
    public enum ImagePro
    { 
      读取图片=1,
      阈值分割=2,
      打散 =3,
      特征筛选=4,
      显示文本=5,
      计算数量=6,
      模板匹配=7,
      显示图片和对象=8,
      自己定义的=9,
    }
    public partial class MainWindow : Form
    {
        /// <summary>
        /// imagePro 数组:这个数组定义了处理流程的顺序。    
        /// </summary>
        public ImagePro[] imagePro;
        public MainWindow()
        {
            InitializeComponent();
            //imagePro=new ImagePro[6] { ImagePro.读取图片, ImagePro.阈值分割, ImagePro.打散, ImagePro.特征筛选, ImagePro.显示图片和对象, ImagePro.显示文本 };
            imagePro = new ImagePro[1] { ImagePro.读取图片 };
        }
// RunPro 方法:根据 imagePro 数组中的操作执行不同的图像处理步骤。
        public void RunPro(ImagePro[]pro,string file,HWindow hWindow)
        {
            for (int i = 0; i < pro.Length; i++)
            {
                switch (pro[i])
                {
                    case ImagePro.读取图片:

                        Algorithm.ImageHelper.ReadImage(out Image, file, hWindow);
                        Console.WriteLine("我执行了读取图片");
                        break;
                    case ImagePro.阈值分割:
                        Console.WriteLine("我执行了阈值分割");
                        break;
                    case ImagePro.打散:
                        Console.WriteLine("我执行了打散");
                        break;
                    case ImagePro.特征筛选:
                        Console.WriteLine("我执行了特征筛选");
                        break;
                    case ImagePro.显示文本:
                        Console.WriteLine("我执行了显示文本");
                        break;
                    case ImagePro.计算数量:
                        Console.WriteLine("我执行了计算数量");
                        break;
                    case ImagePro.模板匹配:
                        Console.WriteLine("我执行了模板匹配");
                        break;
                    case ImagePro.显示图片和对象:
                        Console.WriteLine("我执行了显示图片和对象");
                        break;
                    case ImagePro.自己定义的:
                        Console.WriteLine("我执行了自己定义的");
                        break;
                }
            }
        }

       // button_run_Click 方法:从指定目录读取文件,并按顺序执行图像处理操作。
        private void button_run_Click(object sender, EventArgs e)
        {

            FileInfo[] files = new DirectoryInfo(fileName).GetFiles();
            for (int i = 0; i < files.Length; i++)
            {
                RunPro(imagePro, files[i].FullName, hWindowControl_yuanshi.HalconWindow);
                System.Windows.Forms.Application.DoEvents();
                Thread.Sleep(1000);  
            }

         
        }

        public string fileName;
        public HObject Image;
 // button_setReadImage_Click 方法:显示一个对话框来选择图像文件路径。
        private void button_setReadImage_Click(object sender, EventArgs e)
        {
            读取图片设置 frm=new 读取图片设置();
            frm.ShowDialog();
            fileName= frm.GetFilePath();
            Console.WriteLine(fileName);
        }
    }
}

 在ImageHelper.cs里

using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Algorithm
{

    /// <summary>
    /// 这个类就是我图像处理的算法类
    /// </summary>
    public class ImageHelper
    {
      //定义string数组用于打散颜色
        public static string[] Color=new string[12] {"red","green", "blue" , "cyan", "magenta" , "yellow", "coral", "spring green", "orange", "orange red", "pink", "navy" };
        /// <summary>
        /// 这是一个读取图片 并且在指定窗体上显示的方法
        /// </summary>
        /// <param name="hImage"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
//用于直接从文件路径读取图像,并在指定的 HWindow 控件上显示它。适合于代码中已知文件路径的情况。
        public static bool ReadImage(out HObject hImage, string filename, HWindow window)
        {
            try
            {
                HOperatorSet.ReadImage(out hImage, filename);
                HTuple width, height;
                //获得图片的宽高
                HOperatorSet.GetImageSize(hImage, out width, out height);
                //设置显示范围
                //HWindowControl.HalconWindow -->控件的句柄  设置显示范围
                HOperatorSet.SetPart(window, 0, 0, (height - 1), (width - 1));
                //显示
                HOperatorSet.DispObj(hImage, window);
                return true;

            }
            catch
            {
                hImage = null;
                return false;
            }
        }
//用于通过用户选择文件的对话框来读取图像。适合于需要用户交互选择图像文件的场景。
        public static bool ReadImage(out HObject hImage, OpenFileDialog openFileDialog)
        {
            //给halcon变量置空
            hImage = null;
            //给halcon变量置空
            //HOperatorSet.GenEmptyObj(out hImage);

            // openFileDialog.ShowDialog()  -->返回值 OK 和Cancel
            //返回ok就应该能读取到指定文件的路径
            DialogResult result = openFileDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                HOperatorSet.ReadImage(out hImage, openFileDialog.FileName);
                return true;
            }
            else
            { 
                return false;
            }
        }

        /// <summary>
        /// 只单单显示图片
        /// </summary>
        /// <param name="hImage"></param>
        /// <param name="window"></param>
        /// <returns></returns>
        public static bool HalconWindowShowImage(HObject hImage, HWindow window)
        {
            try
            {
                HTuple width;
                HTuple height;
                HOperatorSet.GetImageSize(hImage, out width, out height);

                //设置显示区域  将图片左上角 和右下角形成的正矩形区域 作为窗体的显示区域
                HTuple LeftTopRow = 0;
                HTuple LeftTopCol = 0;
                HTuple RightTopRow = height;
                HTuple RightTopCol = width;
                HOperatorSet.SetPart(window, LeftTopRow, LeftTopCol, RightTopRow - 1, RightTopCol - 1);

                //显示
                HOperatorSet.DispObj(hImage, window);
                return true;
            }
            catch (Exception)
            {
                return false;
            }  
        }
        /// <summary>
          /// 图片和对象一起显示
          /// </summary>
          /// <param name="hImage"></param>
          /// <param name="Region"></param>
          /// <param name="window"></param>
          /// <param name="color"></param>
          /// <returns></returns>
        public static bool HalconWindowShowImage(HObject hImage, HObject Region, HWindow window, string[] color)
        {
            try
            {
                HTuple width;
                HTuple height;
                HOperatorSet.GetImageSize(hImage, out width, out height);

                //设置显示区域  将图片左上角 和右下角形成的正矩形区域 作为窗体的显示区域
                HTuple LeftTopRow = 0;
                HTuple LeftTopCol = 0;
                HTuple RightTopRow = height;
                HTuple RightTopCol = width;
                HOperatorSet.SetPart(window, LeftTopRow, LeftTopCol, RightTopRow - 1, RightTopCol - 1);

                //显示
                HOperatorSet.DispObj(hImage, window);

                HTuple NUMS;
                HOperatorSet.CountObj(Region, out NUMS);//计算Region的个数
                int index = 0;
                for (int i = 0; i < NUMS; i++, index++)
                {
//使用 color[index] 来设置绘制区域的颜色。color 数组的长度决定了可以使用多少种颜色来区分不同的区域对象。
                    HObject selectRrgion;
                    HOperatorSet.SetColor(window, color[index]);
                    HOperatorSet.SelectObj(Region, out selectRrgion, i + 1);
                    HOperatorSet.DispObj(selectRrgion, window);
                    if (index >= color.Length - 1)
                    {
                        index = 0;
                    }
                }
                return true;
            }
            catch (Exception)
            { 
                return false;
            }
        }
        #region 【阈值分割】
        public static bool Threshold(HObject hImage, out HObject Region, int Min, int Max)
        {
            HOperatorSet.Threshold(hImage, out Region, Min, Max);
            return true;
        }
        public static bool BinaryThreshold(HObject hImage, out HObject Region, 自动阈值分割方法 method, 亮或暗 lightAndDark)
        {
            HTuple use;
            HOperatorSet.BinaryThreshold(hImage, out Region, method.ToString(), lightAndDark.ToString(), out use);
            return true;
        }

        public static bool ThresholdALL(HObject hImage, out HObject Region, ThreSholdDef threSholdDef)
        {
            Region = null;

            switch (threSholdDef._Segment)
            {
                case 阈值分割方式.Threshold:
                    Console.WriteLine("我调用了普通阈值分割  Threshold");
                    return Threshold(hImage, out Region, threSholdDef._MinGray, threSholdDef._MaxGray);

                case 阈值分割方式.BinaryThreshold:
                    Console.WriteLine("我调用了自动阈值分割  BinaryThreshold(");
                    return BinaryThreshold(hImage, out Region, threSholdDef._SegmentMothd, threSholdDef._LightOrDark);
                default:
                    return false;
            }
        }

        #endregion

        /// <summary>
        /// 根据输入区域 生成外接矩形 以及外接矩形的参数
        /// </summary>
        /// <param name="Region"></param>
        /// <param name="ExternalRegion"></param>
        /// <param name="rectangleDef"></param>
        /// <param name="isMargin"></param>
        /// <param name="hWindow"></param>
        public static void GenRectangle(HObject Region, out HObject ExternalRegion,out RectangleDef rectangleDef,bool isMargin,HWindow hWindow)
        {
            if (isMargin == true)
            {
                HOperatorSet.SetDraw(hWindow, "margin");
            }
            else 
            {
                HOperatorSet.SetDraw(hWindow, "fill");
            }
            rectangleDef=new RectangleDef();
            HOperatorSet.SmallestRectangle2(Region, out rectangleDef._CenterRow, out rectangleDef._CenterCol, out rectangleDef.Angle, out rectangleDef.len1, out rectangleDef.len2);
            HOperatorSet.GenRectangle2(out ExternalRegion, rectangleDef._CenterRow, rectangleDef._CenterCol, rectangleDef.Angle, rectangleDef.len1, rectangleDef.len2);
        }
        public static void ShowText(string Content, HWindow hWindow,HTuple ROW, HTuple COL)
        {
            HOperatorSet.DispText(hWindow, Content, "window", ROW, COL, "red", new HTuple(), new HTuple());
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值