Photoshop和Halcon中的極坐標變換


 

極坐標想必學過高中數學的人都聽過,一般的坐標系中用(x, y)值來描述一個點的位置,而在極坐標系中,則使用到原點的距離ρ和夾角θ來描述該點的位置。

 

我很早就接觸了Photoshop,知道Photoshop里面有個極坐標的扭曲濾鏡,如下圖:

 

明白了極坐標(正向、反向)的大致效果是:能將矩形圖案變成圓形(環形)圖案,或者反過來。

 

例如制作這種超現實的特效:

 

Photoshop中的“極坐標”濾鏡的原理是:以右上角為圓心,旋轉以后生成的是一個長寬都是畫布兩倍的圖形,然后將此圖形壓縮至二分之一,最后將圓心移到畫面中央。(注意下面三個圖形之間的相對位置)

 

Halcon中極坐標的處理可能為了使用方便,將內部算法經過了優化,所以外在表現形式略有不同。

 

Halcon中與極坐標變換相關的算子共有7個,不過polar_trans_image已經過時,官方不建議使用,可以看到Halcon對Image、Region、XLD都可以運用極坐標變換和反變換

 

Halcon示例程序中關於極坐標的例子有: inspect_bottle_mouth.hdev。下面的代碼亦是根據這個例子精簡而來,完整代碼如下(注意看注釋):

 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)
 
        

該圖極坐標轉換后的圖像為:

 

 

部分處理效果圖如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM