Photoshop和Halcon中的极坐标变换

 

极坐标想必学太高中数学的人都听过,通常的坐标系中用(x, y)值来描述一个点的位置,而在极坐标系中,则使用到原点的距离ρ和夹角θ来描述该点的位置。算法

 

我很早就接触了Photoshop,知道Photoshop里面有个极坐标的扭曲滤镜,以下图:函数

 

明白了极坐标(正向、反向)的大体效果是:能将矩形图案变成圆形(环形)图案,或者反过来。优化

 

例如制做这种超现实的特效:spa

 

Photoshop中的“极坐标”滤镜的原理是:以右上角为圆心,旋转之后生成的是一个长宽都是画布两倍的图形,而后将此图形压缩至二分之一,最后将圆心移到画面中央。(注意下面三个图形之间的相对位置)rest

 

Halcon中极坐标的处理可能为了使用方便,将内部算法通过了优化,因此外在表现形式略有不一样。code

 

Halcon中与极坐标变换相关的算子共有7个,不过polar_trans_image已通过时,官方不建议使用,能够看到Halcon对Image、Region、XLD均可以运用极坐标变换和反变换regexp

 

Halcon示例程序中关于极坐标的例子有: inspect_bottle_mouth.hdev。下面的代码亦是根据这个例子精简而来,完整代码以下(注意看注释):blog

 1 *说明:本例由Halcon示例程序 inspect_bottle_mouth.hdev 精简而成,可先参考示例程序再阅读本代码。  2 
 3 dev_set_draw ('margin')  4 dev_set_line_width (2)  5 set_font (3600, '-Courier New-16-*-*-*-*-1-')  6 
 7 list_files ('C:/Users/Administrator/Desktop/bottle', ['files','follow_links','recursive'], ImageFiles)  8 tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)  9 
10 for Index := 1 to |ImageFiles| - 1 by 1
11  read_image (Image, ImageFiles[Index]) 12  get_image_size (Image, Width, Height) 13     threshold (Image, Region, 0, 60) 14  fill_up (Region, RegionFillUp) 15     opening_circle (RegionFillUp, RegionOpening, 8.5) 16  smallest_circle (RegionOpening, Row0, Column0, Radius0) 17  
18     *下面这个函数是本身写的抓圆的函数,细节不表。你也能够用fit_circle_contour_xld  gen_circle_contour_xld实现相似功能 19     find_circle (Image, PartCircleXLD, Regions, Cross, Circle, Row0, Column0, Radius0 + 10, 0, 360, 30, 70, 20, 1, 50, 'negative', 'first', 'inner', 10, 'circle', RowCenter, ColCenter, Radius) 20  dev_display (Image) 21  dev_display (Circle) 22     
23     *该算子对一个图像的圆弧区域进行极坐标变换,圆弧外径是Radius,内径是Radius - 100,即圆弧厚度是100 24     *同理,圆弧展开成矩形后,矩形宽度应该是外弧圆圈的周长,即6.28319 * Radius(周长 = 2π × r) ;矩形高度应该是圆弧厚度,即100 25     polar_trans_image_ext (Image, PolarTransImage, RowCenter, ColCenter, 0, 6.28319, Radius - 100, Radius, 6.28319 * Radius, 100, 'nearest_neighbor') 26     
27     *下面这句仅用于观察image的反向极坐标变换,生成的图片的宽高仍是设置为最原始图像的Width, Height 28     polar_trans_image_inv  (PolarTransImage, XYTransImage, RowCenter, ColCenter, 0, 6.28319, Radius - 100, Radius, Width, Height, 'nearest_neighbor') 29     
30     *mean_image选择主要沿水平方向进行模糊,动态阈值的'not_equal'参数同时筛选出了跟周围比过亮和过暗的区域(由于过暗和过亮都是缺陷) 31     mean_image (PolarTransImage, ImageMean, 500, 3) 32     dyn_threshold (PolarTransImage, ImageMean, Region1, 30, 'not_equal') 33     
34     fill_up_shape (Region1, RegionFillUp1, 'area', 1, 100) 35     *开运算去掉细小干扰 36     opening_circle (RegionFillUp1, RegionOpening1, 1.5) 37  connection (RegionOpening1, ConnectedRegions) 38     
39     *之因此要进行极坐标转换,就是为了这里用'height'来筛选,这是本例使用极坐标变换最关键的缘由 40     select_shape (ConnectedRegions, SelectedRegions, 'height', 'and', 10, 99999) 41     polar_trans_region_inv (SelectedRegions, XYTransRegion, RowCenter, ColCenter, 0, 6.28319, Radius - 100, Radius, 6.28319 * Radius, 100, Width, Height, 'nearest_neighbor') 42  dev_display (Image) 43  dev_display (XYTransRegion) 44     
45  stop () 46 endfor

 

虽然极坐标变换是本例中的核心思路,可是仍有三句很是巧妙的代码,仔细想一想为何这三句代码很巧妙:

31 mean_image (PolarTransImage, ImageMean, 500, 3) 32 dyn_threshold (PolarTransImage, ImageMean, Region1, 30, 'not_equal')
40     select_shape (ConnectedRegions, SelectedRegions, 'height', 'and', 10, 99999)
 

该图极坐标转换后的图像为:图片

 

 

部分处理效果图以下:ci

相关文章
相关标签/搜索