Halcon例子

1、均值滤波

* 获取图像
read_image (Image, 'circle_plate')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width / 2, Height / 2, 'black', WindowHandle)
dev_display (Image)
stop ()

* 矩形均值滤波
mean_image (Image, ImageMeanRect, 31, 31)
dev_display (ImageMeanRect)
stop ()

* 频域均值滤波  频域滤波器*图像频域=滤波后图像频域
gen_mean_filter (MeanFilter, 'ellipse', 34.97975, 34.97975, 0, 'n', 'rft', Width, Height)
rft_generic (Image, ImageFFT, 'to_freq', 'none', 'complex', Width)
convol_fft (ImageFFT, MeanFilter, ImageConvol)
rft_generic (ImageConvol, ImageMeanCirc, 'from_freq', 'none', 'byte', Width)
dev_display (ImageMeanCirc)

2、高斯滤波

read_image (Image, 'mreut')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_set_part (0, 0, Height - 1, Width - 1)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_display (Image)
optimize_rft_speed (Width, Height, 'standard')
stop ()

* 频域高斯滤波
gen_gauss_filter (GaussFilter, 3, 3, 0, 'n', 'rft', Width, Height)
rft_generic (Image, ImageFFT, 'to_freq', 'none', 'complex', Width)
convol_fft (ImageFFT, GaussFilter, ImageConvol)
rft_generic (ImageConvol, ImageFiltered, 'from_freq', 'none', 'real', Width)
dev_display (ImageFiltered)
stop ()

* 空间域高斯滤波
derivate_gauss (Image, ImageGauss, 3, 'none')
dev_display (ImageGauss)
stop ()

* 边缘有些差异,中间部分几乎相同
gen_rectangle1 (Rectangle, 20, 20, Height - 21, Width - 21)
sub_image (ImageFiltered, ImageGauss, ImageSub, 1, 0)
min_max_gray (Rectangle, ImageSub, 0, Min, Max, Range)
intensity (Rectangle, ImageSub, Mean, Deviation)
dev_display (ImageSub)
stop ()

3、去除纹理

    read_image (Image, 'plan_01')
    get_image_size (Image, Width, Height)
    dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
    set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
    dev_set_part (0, 0, Height - 1, Width - 1)
    dev_display (Image)
    optimize_fft_speed (Width, Height, 'standard')
    fft_generic (Image, ImageFFT, 'to_freq', -1, 'sqrt', 'dc_center', 'complex')
    dev_open_window (0, Width + 7, Width, Height, 'black', WindowHandle1)
    dev_set_color ('red')
    dev_set_draw ('margin')
    dev_display (ImageFFT)
    stop ()
    * 频域去除纹理区域
    power_real (ImageFFT, PowerSpectrum)
    binomial_filter (PowerSpectrum, ImageSmooth, 9, 9)
    threshold (ImageSmooth, Region, 50, 100000)
    connection (Region, ConnectedRegions)
    select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 5, 200)
    union1 (SelectedRegions, RegionUnion)
    reduce_domain (ImageSmooth, RegionUnion, ImageReduced)
    local_max (ImageReduced, LocalMaxima)
    shape_trans (LocalMaxima, RegionTrans, 'convex')
    hom_mat2d_identity (HomMat2DIdentity)
    hom_mat2d_scale (HomMat2DIdentity, 2.1, 2.1, Height / 2, Width / 2, HomMat2DScale)
    affine_trans_region (RegionTrans, RegionTrans1, HomMat2DScale, 'nearest_neighbor')
    hom_mat2d_scale (HomMat2DIdentity, 1.9, 1.9, Height / 2, Width / 2, HomMat2DScale)
    affine_trans_region (RegionTrans, RegionTrans2, HomMat2DScale, 'nearest_neighbor')
    difference (RegionTrans1, RegionTrans2, RegionDifference)
    reduce_domain (ImageSmooth, RegionDifference, ImageReduced)
    threshold (ImageReduced, Region, 15, 100000)
    reduce_domain (ImageSmooth, Region, ImageReduced)
    local_max (ImageReduced, LocalMaxima2)
    union2 (LocalMaxima, LocalMaxima2, RegionUnion)
    dilation_circle (RegionUnion, RegionDilation, 15.5)
    paint_region (RegionDilation, ImageFFT, ImageFFTFiltered, 0, 'fill')
    dev_display (ImageFFT)
    dev_display (RegionDilation)
    stop ()
    * 去除纹理后图像
    fft_generic (ImageFFTFiltered, ImageFiltered, 'from_freq', 1, 'sqrt', 'dc_center', 'byte')
    dev_display (ImageFiltered)
    stop ()
    * 纹理图
    dev_open_window (0, 2 * Width + 14, Width, Height, 'black', WindowHandle2)
    sub_image (Image, ImageFiltered, ImageTexture, 1, 128)
    dev_display (ImageTexture)

4、图像畸变矫正

dev_set_draw ('margin')
dev_set_line_width (2)

* 标定
gen_cam_par_area_scan_division (0.016, 0, 2.4E-6, 2.4E-6, 5472/2, 3648/2, \
                                5472, 3648, StartCamPar)
create_calib_data ('calibration_object', 1, 1, CalibDataID)
set_calib_data_cam_param (CalibDataID, 0, [], StartCamPar)
set_calib_data_calib_object (CalibDataID, 0, 'caltab.descr')
for I := 1 to 9 by 1
    read_image (Image, 'images/img_' + I$'02d')
    find_calib_object (Image, CalibDataID, 0, 0, I - 1, [], [])
    get_calib_data_observ_points (CalibDataID, 0, 0, I - 1, Row, Column, Index, Pose)
    get_calib_data_observ_contours (Contours, CalibDataID, 'caltab', 0, 0, I - 1)
    gen_cross_contour_xld (Cross, Row, Column, 6, 0.785398)
    dev_display (Contours)
    dev_display (Cross)
endfor
calibrate_cameras (CalibDataID, Error)

* 畸变矫正
get_calib_data (CalibDataID, 'camera', 0, 'params', CamParam)
for I := 1 to 9 by 1
    read_image (Image, 'images/img_' + I$'02d')
    dev_display(Image)
    stop ()
    
    * 修改径向畸变参数为0
    change_radial_distortion_cam_par ('fixed', CamParam, 0, CamParFixed)
    
    *** method1 by map ***
    gen_radial_distortion_map (MapFixed, CamParam, CamParFixed, 'bilinear')
    map_image (Image, MapFixed, ImageFixed)
    dev_display(ImageFixed)
    stop ()
    
    *** method2 by region ***
    get_domain(Image, Domain)
    change_radial_distortion_image(Image, Domain, ImageRectified, CamParam, CamParFixed)
    dev_display(ImageRectified)
    stop ()
endfor

另外的方式

get_calib_data (CalibDataID, 'camera', 0, 'params', CamParam)

for I := 1 to 9 by 1
    read_image (Image, 'images/img_' + I$'02d')
    get_calib_data (CalibDataID, 'calib_obj_pose', [0,I - 1], 'pose', Pose)
    dev_display (Image)
    
    if(I == 1)
        edges_sub_pix(Image, Edges, 'canny', 1, 20, 40)
        select_contours_xld(Edges, SelectedContours, 'contour_length', 450, 550, -0.5, 0.5)
        gen_region_contour_xld(SelectedContours, Region, 'filled')
        area_center(Region, Area, Row1, Column1)
        tuple_mean(Row1, MeanR)
        tuple_mean(Column1, MeanC)
        Ind := find(Row1[>]MeanR-80 and Row1[<]MeanR+80 and Column1[>]MeanC-80 and Column1[<]MeanC+80, 1)
        CenRow := Row1[Ind]
        CenCol := Column1[Ind]
        cal_dxyz(CamParam, Pose, CenRow, CenCol, 5472, 3648, Scale, Dx, Dy, Dz)
    endif
    set_origin_pose (Pose, Dx, Dy, Dz, PoseNewOrigin)

    * method1
    image_to_world_plane (Image, ImageWorld, CamParam, PoseNewOrigin, 5472, 3648, Scale, 'bilinear')

    * method2
    gen_image_to_world_plane_map (Map, CamParam, PoseNewOrigin, 5472, 3648, 5472, 3648, Scale, 'bilinear')
    map_image (Image, Map, ImageMapped)
    
    dev_clear_window()
    dev_display (ImageWorld)
    stop ()
endfor

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值