Halcon 学习笔记3 仿射变换

像素的减少

开运算(较少)

腐蚀(去除更多)

对灰度图像的开运算或腐蚀 相当于将灰度图像变暗

像素增加

闭运算(较少)

膨胀(较多)

对灰度图像的闭运算或膨胀 相当于将灰度图像变亮

仿射变换

另外一种仿射变换

* This example demonstrates an application from the pharmaceutical
* industry. The task is to check the content of automatically filled
* blisters. The first image (reference) is used to locate the chambers
* within a blister shape as a reference model, which is then used to
* realign the subsequent images along to this reference shape. Using
* blob analysis the content of each chamber is segmented and finally
* classified by a few shape features.
*
dev_close_window ()
dev_update_off ()
read_image (ImageOrig, ‘blister/blister_reference‘)
dev_open_window_fit_image (ImageOrig, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 14, ‘mono‘, ‘true‘, ‘false‘)
dev_set_draw (‘margin‘)
dev_set_line_width (3)
*
* In the first step, we create a pattern to cut out the chambers in the
* subsequent blister images easily.
access_channel (ImageOrig, Image1, 1)
threshold (Image1, Region, 90, 255)
shape_trans (Region, Blister, ‘convex‘)
orientation_region (Blister, Phi)//计算角度Phi
area_center (Blister, Area1, Row, Column)//获取面积和中心点坐标
*将原来的(Row, Column, Phi)表示为(Row, Column, 0),也就是将角度调正
*输出变换矩阵HomMat2D
vector_angle_to_rigid (Row, Column, Phi, Row, Column, 0, HomMat2D)
affine_trans_image (ImageOrig, Image2, HomMat2D, ‘constant‘, ‘false‘)
gen_empty_obj (Chambers)
for I := 0 to 4 by 1
    Row := 88 + I * 70
    for J := 0 to 2 by 1
        Column := 163 + J * 150
        gen_rectangle2 (Rectangle, Row, Column, 0, 64, 30)
        concat_obj (Chambers, Rectangle, Chambers)
    endfor
endfor
affine_trans_region (Blister, Blister, HomMat2D, ‘nearest_neighbor‘)
difference (Blister, Chambers, Pattern)
union1 (Chambers, ChambersUnion)
orientation_region (Blister, PhiRef)
PhiRef := rad(180) + PhiRef
area_center (Blister, Area2, RowRef, ColumnRef)
*
*
* Each image read will be aligned to this pattern and reduced to the area of interest,
* which is the chambers of the blister
Count := 6
for Index := 1 to Count by 1
    read_image (Image, ‘blister/blister_‘ + Index$‘02‘)
    threshold (Image, Region, 90, 255)
    connection (Region, ConnectedRegions)
    select_shape (ConnectedRegions, SelectedRegions, ‘area‘, ‘and‘, 5000, 9999999)
    shape_trans (SelectedRegions, RegionTrans, ‘convex‘)
    *
    * Align pattern along blister of image
    orientation_region (RegionTrans, Phi)
    area_center (RegionTrans, Area3, Row, Column)
    vector_angle_to_rigid (Row, Column, Phi, RowRef, ColumnRef, PhiRef, HomMat2D)
    affine_trans_image (Image, ImageAffineTrans, HomMat2D, ‘constant‘, ‘false‘)
    *
    * Segment pills
    reduce_domain (ImageAffineTrans, ChambersUnion, ImageReduced)
    decompose3 (ImageReduced, ImageR, ImageG, ImageB)
    var_threshold (ImageB, Region, 7, 7, 0.2, 2, ‘dark‘)
    connection (Region, ConnectedRegions0)
    closing_rectangle1 (ConnectedRegions0, ConnectedRegions, 3, 3)
    fill_up (ConnectedRegions, RegionFillUp)
    select_shape (RegionFillUp, SelectedRegions, ‘area‘, ‘and‘, 1000, 99999)
    opening_circle (SelectedRegions, RegionOpening, 4.5)
    connection (RegionOpening, ConnectedRegions)
    select_shape (ConnectedRegions, SelectedRegions, ‘area‘, ‘and‘, 1000, 99999)
    shape_trans (SelectedRegions, Pills, ‘convex‘)
    *
    * Classify segmentation results and display statistics
    count_obj (Chambers, Number)
    gen_empty_obj (WrongPill)
    gen_empty_obj (MissingPill)
    for I := 1 to Number by 1
        select_obj (Chambers, Chamber, I)
        intersection (Chamber, Pills, Pill)
        area_center (Pill, Area, Row1, Column1)
        if (Area > 0)
            min_max_gray (Pill, ImageB, 0, Min, Max, Range)
            if (Area < 3800 or Min < 60)
                concat_obj (WrongPill, Pill, WrongPill)
            endif
        else
            concat_obj (MissingPill, Chamber, MissingPill)
        endif
    endfor
    *
    dev_clear_window ()
    dev_display (ImageAffineTrans)
    dev_set_color (‘forest green‘)
    count_obj (Pills, NumberP)
    count_obj (WrongPill, NumberWP)
    count_obj (MissingPill, NumberMP)
    dev_display (Pills)
    if (NumberMP > 0 or NumberWP > 0)
        disp_message (WindowHandle, ‘Not OK‘, ‘window‘, 12, 12 + 600, ‘red‘, ‘true‘)
    else
        disp_message (WindowHandle, ‘OK‘, ‘window‘, 12, 12 + 600, ‘forest green‘, ‘true‘)
    endif
    *
    Message := ‘# Correct pills: ‘ + (NumberP - NumberWP)
    Message[1] := ‘# Wrong pills  :  ‘ + NumberWP
    Message[2] := ‘# Missing pills:  ‘ + NumberMP
    *
    Colors := gen_tuple_const(3,‘black‘)
    if (NumberWP > 0)
        Colors[1] := ‘red‘
    endif
    if (NumberMP > 0)
        Colors[2] := ‘red‘
    endif
    disp_message (WindowHandle, Message, ‘window‘, 12, 12, Colors, ‘true‘)
    dev_set_color (‘red‘)
    dev_display (WrongPill)
    dev_display (MissingPill)
    if (Index < Count)
        disp_continue_message (WindowHandle, ‘black‘, ‘true‘)
    endif
    stop ()
endfor

仿射变换 理论

https://www.cnblogs.com/liekkas0626/p/5238564.html 

原文地址:https://www.cnblogs.com/my-cat/p/8206128.html

时间: 2024-11-25 13:47:40

Halcon 学习笔记3 仿射变换的相关文章

Halcon学习笔记之支持向量机(二)

例程:classify_halogen_bulbs.hdev 在Halcon中模式匹配最成熟最常用的方式该署支持向量机了,在本例程中展示了使用支持向量机对卤素灯的质量检测方法.通过这个案例,相信大家可以对支持向量机的使用有一个更加清晰的了解.在相当多的检测和识别的应用中,都可以使用相同的方法来解决分类问题. 图1. 卤素灯图像 大致原理: 一.准备阶段:描述样本 1. 准备好两组卤素灯图像样本,好坏的各若干张图像: 2. 对样本图像进行分割,获取卤素灯关键部位区域: 3. 选择合适的对图像的描述

Halcon学习笔记——条形码的定位与识别

一维码的原理与结构 条码基本原理是利用条纹和间隔或宽窄条纹(间隔)构成二进制的"0"和"1",反映的是某种信息. 一维条码数据结构,分四个区域.组成分别为静区.起始/终止符.校验符.数据符. 一维条码的意识形态结构,分三条. 构成一维码的基本单元是模块,模块是指条码中最窄的条或空: 构成条码的条或空称为一个单元,一个单元包含多个或单个模块: 一个单元包含的模块数量由编码方式决定,即形成了不同的码制. 一维码的定位与识别 基本halcon工具算子,create_bar

Halcon学习笔记——机器视觉应用工程开发思路及相机标定

机器视觉应用工程开发思路 机器视觉应用工程主要可划分为两大部分,硬件部分和软件部分. 1.硬件部分,硬件的选型至关重要,决定了后续工作是否可以正常开展,其中关键硬件部分包括:光源,相机以及镜头. 2.软件部分,目前业内商业库主要有Halcon,康耐视,DALSA,evision,NI等,开源库有OpenCV.其中NI的labview+vision模块. 机器视觉应用工程大致开发思路:       一.获取图像              图像采集可以来源多个途径,用算子read_image去读取图

Halcon学习笔记(一)

一.Halcon编程之图像处理 1.读取图片 1.读取单个图片: 1.1 直接用算子read_image read_image (Image, 'D:/3.tiff') 2.读取多个图片 2.1 定义一个存放图片路径的数组,通过循环读取 imagepath:=[] imagepath[0]:=['D:/1.tiff']imagepath[1]:=['D:/2.tiff']imagepath[2]:=['D:/3.tiff']for Index := 1 to 5 by 1 read_image

halcon学习笔记——摄像机标定

1.read_cam_par( : : CamParFile : CameraParam) 从文件夹中读取相机的内参数. 2.disp_caltab( : : WindowHandle, CalTabDescrFile, CameraParam, CaltabPose, ScaleFac : ) 利用相机内外参数,把标定板模型投影到图像平面,显示标定点和连接线,X,Y轴也被显示出来. 3.vector_to_pose( : : WorldX, WorldY, WorldZ, ImageRow,

halcon学习笔记——(1)单摄像机标定

In the reference manual,operator signatures are visualized in the following way: operator ( iconic input : iconic output : control input : control output ) 在HALCON所有算子中,变量皆是如上格式,即:图像输入:图像输出:控制输入:控制输出. 其中四个参数任意一个可以为空. 控制输入可以是变量.常量.表达式: 控制输出以及图像输入和输出必须

halcon学习笔记&mdash;&mdash;(2)摄像机标定

1.read_cam_par( : : CamParFile : CameraParam)从文件夹中读取相机的内参数.2.disp_caltab( : : WindowHandle, CalTabDescrFile, CameraParam, CaltabPose, ScaleFac : )利用相机内外参数,把标定板模型投影到图像平面,显示标定点和连接线,X,Y轴也被显示出来.3.vector_to_pose( : : WorldX, WorldY, WorldZ, ImageRow, Imag

Halcon学习笔记(2) VS2010 + Halcon12 C#链接相机

1.打开Halcon, 使用图像采集助手获取相机实时图像: 1.1 获取实时图像: 1.2 插入采集实时图像的Halcon代码,并导出: Image_acq.cs代码: // // File generated by HDevelop for HALCON/DOTNET (C#) Version 12.0 // // This file is intended to be used with the HDevelopTemplate or // HDevelopTemplateWPF proje

halcon学习笔记&mdash;&mdash;(3)HALCON标定后的二维测量

1.gen_measure_rectangle2( : : Row, Column, Phi, Length1, Length2, Width, Height,Interpolation : MeasureHandle) 产生测量矩形句柄,控制输入为测量矩形范围的坐标中心,矩形长轴和水平方向夹角范围的弧度,矩形边长的一半,测量图片的宽和高度,输出为测量句柄. 2.measure_pairs(Image : : MeasureHandle, Sigma, Threshold, Transition