C#Halcon图像处理畸变校正之曲面校正

图像校正场景一般有两种,其一由镜头本身或安装角度引起,其二是被拍摄物品本身引起

理论处理流程

我的处理处理流程

1,加载网格校正图像

2,确定符合条件的网格区域

3,显示网格鞍点

4,显示网格线

5,确定最终需要扭曲校正的图像

6,显示校正后图像

7,加载需要校正的图像

8,显示校正后图像效果

显而易见,校正后的图像在字符识别,缺陷检测,尺寸测量等方面检测效果要优于原图。

附上源码

(注意,源码仅供流程参考,在实战项目过程中,方法最好封装成类亦或者Dll文件,参数可以适当开放方便后期调试,映射关系需保存本地,软件重启需重新读取参数)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ViewControl;
using HalconDotNet;
using System.Reflection.Emit;
using static System.Net.Mime.MediaTypeNames;
namespace DeepLearningTest1
{
    public partial class Form1 : Form
    {
        HalconView HW;

        HObject HIMage = new HObject(), ho_ImageReduced=new HObject(),ho_ConnectingLines=new HObject(), ho_Map=new HObject();
        HObject ho_ImageMapped =new HObject();
        HObject ho_GridRegion;
        HTuple  hv_Row = new HTuple(), hv_Col = new HTuple();


        public Form1()
        {
            InitializeComponent();
            HW = new HalconView();
            HW.HWindowControl.BackColor = Color.White;
            splitContainer1.Panel2.Controls.Add(HW);
            HW.Dock = DockStyle.Fill;
        }

       

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                OpenFileDialog openFileDialog = new OpenFileDialog();
                //openFileDialog.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
                openFileDialog.Filter = "图片文件(*.bmp;*.jpg;*.gif;*.png;*.tiff;*.tif)|*.bmp;*.jpg;*.gif;*.png;*.tiff;*.tif";
                openFileDialog.RestoreDirectory = true;
                openFileDialog.FilterIndex = 1;
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    label3.Text = openFileDialog.FileName;
                    HOperatorSet.ReadImage(out HIMage, label3.Text);
                    
                    HW.DispImage(HIMage, true);
                }
               
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载图片失败  " + ex.ToString());
            }

        }
      
        /// <summary>
        /// 1 确定整流网格区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (!HIMage.IsInitialized()) { MessageBox.Show("图片为空"); return; }
            
            HOperatorSet.GenEmptyObj(out ho_GridRegion);            
            ho_GridRegion.Dispose();
            HOperatorSet.FindRectificationGrid(HIMage, out ho_GridRegion, 25,10);
            ho_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(HIMage, ho_GridRegion, out ho_ImageReduced);
            HW.DispObject(ho_GridRegion);
        }
        /// <summary>
        ///2 确定网格点。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            //---------------------2-----------------------------
            //确定网格点。
            HTuple hv_Threshold = new HTuple();
            HObject  ho_SaddlePoints;
            HOperatorSet.GenEmptyObj(out ho_SaddlePoints);
            hv_Row.Dispose(); hv_Col.Dispose();
            HOperatorSet.SaddlePointsSubPix(ho_ImageReduced, "facet", 1.5,
                5, out hv_Row, out hv_Col);
            ho_SaddlePoints.Dispose();
            HOperatorSet.GenCrossContourXld(out ho_SaddlePoints, hv_Row, hv_Col, 6, 0.785398);
            HOperatorSet.SetColor(HW.HalconWindow, "red");
            HW.DispObject(HIMage);
            HW.DispObject(ho_SaddlePoints);
        }
        /// <summary>
        /// 网格线区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            HTuple hv_SigmaConnectGridPoints = new HTuple();
            HTuple hv_MaxDist = new HTuple();
            //------------------------3------------------------
            //确定网格线。
            ho_ConnectingLines.Dispose();
            HOperatorSet.ConnectGridPoints(ho_ImageReduced, out ho_ConnectingLines, hv_Row, hv_Col, 0.9, 5.0);
            HW.DispObject(HIMage);
            HW.DispObject(ho_ConnectingLines);
        }

        /// <summary>
        /// 输出映射关系
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {

            HObject  ho_Meshes;
            HOperatorSet.GenEmptyObj(out ho_Meshes);
            HTuple hv_GridSpacing = new HTuple();
            ho_Map.Dispose(); ho_Meshes.Dispose();
            HOperatorSet.GenGridRectificationMap(ho_ImageReduced, 20, out ho_Map,
                out ho_Meshes, hv_GridSpacing, 0, hv_Row, hv_Col, "bilinear");
            HW.DispObject(HIMage);
            HW.DispObject(ho_Meshes);
        }

        /// <summary>
        /// 映射原图像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {

            ho_ImageMapped.Dispose();
            HOperatorSet.MapImage(ho_ImageReduced, ho_Map, out ho_ImageMapped);
            HW.DispObject(ho_ImageMapped);
        }
        /// <summary>
        /// 加载其他图像映射
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button7_Click(object sender, EventArgs e)
        {
            ho_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(HIMage, ho_GridRegion, out ho_ImageReduced);
            ho_ImageMapped.Dispose();
            HOperatorSet.MapImage(ho_ImageReduced, ho_Map, out ho_ImageMapped);
            HW.DispObject(ho_ImageMapped);

        }
    }
}
相关推荐
Ring__Rain13 分钟前
C++ 标准模板库STL--Pair
开发语言·c++·算法
Galeoto14 分钟前
alloc complex data in c, and access in fortran
c语言·开发语言·算法
夜半被帅醒19 分钟前
JavaSpring AI与阿里云通义大模型的集成使用Java Data Science Library(JDSL)进行数据处理
java·人工智能·阿里云
夜半被帅醒27 分钟前
【JAVA】神经网络的基本结构和前向传播算法
java·神经网络·算法
中杯可乐多加冰1 小时前
【玩转OCR | 腾讯云智能结构化OCR应用探索和场景实践】
人工智能·深度学习·信息可视化·云计算·ocr·腾讯云·玩转腾讯云ocr
ROBOT玲玉1 小时前
PaddleOCROCR关键信息抽取训练过程
人工智能·ocr
TANGLONG2222 小时前
【初阶数据结构与算法】排序算法总结篇(每个小节后面有源码)(直接插入、希尔、选择、堆、冒泡、快速、归并、计数以及非递归快速、归并排序)
java·c语言·数据结构·c++·算法·面试·排序算法
怀念无所不能的你2 小时前
洛谷P2814 家谱(c嘎嘎)
c语言·数据结构·c++·算法·map·并查集
feifeikon4 小时前
机器学习DAY3续:逻辑回归、极大似然、梯度下降 (逻辑回归完)
人工智能·机器学习·逻辑回归
贝多财经4 小时前
高频生活场景带动低频金融服务,美团企业版点燃场景金融建设引擎
人工智能·金融·生活