Halcon 傅里叶变换

计算一个与矩形31x31滤波器相同大小的圆形均值滤波器。为此,我们必须将圆的直径设置为31*sqrt(4/pi) = 34.97975。

* 由于相位相关是循环的,负平移会导致图像的右下方出现峰值。
* 如果平移在一个或两个方向上接近于0,那么local_max_sub_pix中的插值表达式将因此访问错误的值,因为它的边界处理(不是循环的)。
* 为了获得在所有情况下都正确的平移,我们周期性地移动相位相关,使零平移对应于图像的中心。
* 然后修改local_max_sub_pix返回的坐标。
* 最后计算出来的结果才是平移的正确值。

RowOffset := Height / 2
ColumnOffset := Width / 2

get_image_size (ImagePhaseCorrelation, Width, Height)
concat_obj (ImagePhaseCorrelation, ImagePhaseCorrelation, Images2)
concat_obj (Images2, Images2, Images)
tile_images_offset (Images, ImageCyclicShift, [RowOffset - Height,RowOffset - Height,Height / 2,Height / 2], [ColumnOffset- Width,Width / 2,ColumnOffset- Width,Width / 2], [-1,-1,-1,-1], [-1,-1,-1,-1], [-1,-1,-1,-1], [-1,-1,-1,-1], Width, Height)

* 最终计算出来的水平偏移值

Row := RowShifted - RowOffset
Column := ColumnShifted - ColumnOffset

*傅里叶变换是将空间域图像转换为频域图像
*图像处理作用:
*1.图像增强与图像去噪,绝对大部分噪音都是图像高频分量,通过低通滤波器可以滤除高频——噪声
*2.边缘检测——提取图像高频分量
*3.图像压缩可以直接通过傅里叶系数来压缩数据(大部分能量分布在低频谱段,对压缩传输有利,运算次数减少,节省时间)
dev_close_window ()
read_image(lena,'E:/MyHalcon/lena.jpg')
dev_open_window_fit_image (lena, 0, 0, -1, -1, WindowHandle)
dev_display(lena)

*快速傅里叶变换空间域图到频域图
fft_image (lena, ImageFFT)
*Direction:to_freq空间域到频域,from_freq频域到空间域
*Exponent:指数符号 一般向前变换采用-1,向后变换采用1
*Mode:‘dc_center’频域图中心频率为0,‘dc_edge’频域图边角点频率为0
fft_generic (lena, ImageFFT, 'to_freq', -1, 'n', 'dc_center', 'complex')

*快速傅里叶逆变换频域图到空间域图
fft_image_inv (ImageFFT, ImageFFTInv)
fft_generic (ImageFFT, ImageFFTInv, 'from_freq', 1,'sqrt', 'dc_center', 'complex')

*返回复杂图像的功率谱
power_real (ImageFFT, ImageResult)

*生成理想的低通滤波器图像,Frequency决定白色椭圆区域大小(行方向直径=Frequency*图像高 列方向直径=Frequency*图像宽)
gen_lowpass (ImageLowpass, 0.1, 'none', 'dc_center', 512, 512)
*生成理想的高通滤波器图像
gen_highpass (ImageHighpass, 0.1, 'none', 'dc_center', 512, 512)

*频域里卷积图像
convol_fft (ImageFFT, ImageHighpass, ImageConvol)

*检测图像中所有局部最大值(邻域最大值,与联通数有关)
local_max (lena, LocalMaxima)

*使用二项式滤波光滑处理图像,与高斯滤波相近但速度更快
binomial_filter (lena, ImageBinomial, 5, 5)

*提取图像的高频部分,效果图就是原图减去平滑处理的图
highpass_image (lena, Highpass, 3, 3)

*使用滤波对图像进行卷积,FilterMask可以自己建模板
convol_image (lena, ImageResult1, 'sobel', 'mirrored')


************以下有关低通滤波*************
dev_clear_window ()
read_image(lena,'E:/myhalcon/lena.jpg')
rgb1_to_gray (lena, lena)
get_image_size (lena, Width, Height)

gen_lowpass (ImageLowpass, 0.1, 'none', 'dc_center', Width, Height)
fft_generic (lena, ImageFFT, 'to_freq', -1, 'sqrt', 'dc_center', 'complex')
*低通滤波卷积,消除高频区域,获得低频区域
convol_fft (ImageFFT, ImageLowpass, ImageConvol)
*傅里叶逆转换,相当于光滑处理效果
fft_generic (ImageConvol, ImageFFT1, 'from_freq', 1, 'sqrt', 'dc_center', 'byte')

***********以下有关高通滤波
gen_highpass (ImageHighpass, 0.1, 'none', 'dc_center', 512, 512)
fft_generic (lena, ImageFFT2, 'to_freq', 1, 'sqrt', 'dc_center', 'complex')
convol_fft (ImageFFT2, ImageHighpass, ImageConvol1)
fft_generic (ImageConvol1, ImageFFT3, 'from_freq', -1, 'sqrt', 'dc_center', 'byte')

*提取高频部分
highpass_image (lena, Highpass1, 19, 19)
*原图减去高通滤波就是平滑灰度图结果
sub_image (lena, Highpass1, ImageSub, 1, 128)

mean_image (lena, ImageMean, 19, 19)
*原图减去平滑就是高通滤波后灰度图结果
sub_image(lena,ImageMean,ImageSubm,1,128)


stop()
*********消除周期性噪声
* 包含周期性噪声的图像在傅里叶变换后的频域图像有多个中心(亮点),表现为高能量点
* 消除这些不在频域图像中心的其他小亮点,就是在空间域中消除周期性噪声
read_image (zouqi,'E:/myhalcon/zouqi.jpg')
dev_open_window (0, 520, Width, Height, 'black', WindowHandle1)
rgb1_to_gray (zouqi, zouqi)
fft_image (zouqi, ImageFFT4)
power_real (ImageFFT4, PowerSpectrum)
*二项式滤波(配合功率谱算子)
binomial_filter (PowerSpectrum, ImageSmooth, 9, 9)
*阈值化获得高能量点
threshold (ImageSmooth, Region, 100, 1800)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 5, 100)
union1 (SelectedRegions, RegionUnion)
*获得区域内图像域
reduce_domain (ImageSmooth, RegionUnion, ImageReduced)
*获得局部极大值
local_max (ImageReduced, LocalMaxima1)
*膨胀处理区域
dilation_circle (LocalMaxima1, RegionDilation, 9)
*在傅里叶变换中绘制对应区域的灰度值为0
paint_region (RegionDilation, ImageFFT4, ImageFFTFiltered, 0, 'fill')
*傅里叶逆转换,获得去除周期噪声所在区域的频率后的图像
fft_generic (ImageFFTFiltered, ImageFiltered, 'from_freq', 1, 'sqrt', 'dc_center', 'byte')

原文地址:https://www.cnblogs.com/zhengzc/p/12918360.html