# 第五章 图像运算

## 图像的代数运算

### 加法运算

$\overline{g}(x,y)=\frac{1}{K}\sum_{i=1}^k{g_i(x,y)} \\ E\{{\overline{g}(x,y)}\}=f(x,y) \\ \\$

add_image(Image1, Image2 : ImageResult : Mult, Add : )

$g'=(g1+g2)*Mult+Add$

dev_close_window ()
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width*1.5, Height*1.5, 'black', WindowHandle)
dev_display (Image)
rgb1_to_gray (Image, GrayImage)
* 将数据类型转换为实数
convert_image_type (GrayImage, ImageConverted, 'real')
* 复制图片，保护原图
copy_image (ImageConverted, DupImage)
* 生成空图像数组
gen_empty_obj (ImageNoiseArray)
* 循环生成白噪声图存入数组
for i:=1 to 10 by 1
concat_obj (ImageNoiseArray, ImageNoise, ImageNoiseArray)
endfor
* 叠加图像
for i:=2 to 10 by 1
if(i=2)
* 选择前两张图片相加
select_obj (ImageNoiseArray, ObjectSelected, 1)
select_obj (ImageNoiseArray, ObjectSelected1, 2)
add_image (ObjectSelected, ObjectSelected1, ImageResult, 1, 0)
else
* 循环叠加图片
select_obj (ImageNoiseArray, ObjectSelected2, i)
add_image (ObjectSelected2, ImageResult, ImageResult, 1, 0)
endif
endfor

* 对生成的图片求平均值
scale_image (ImageResult, ImageScaled, 0.1, 10)


read_image (Image, 'patras')
Width:=720
Height:=480
* 裁剪两张图片得到形同大小区域
crop_part (Image, ImagePart, 0, 0, Width, Height)
crop_part (Image1, ImagePart1, 0, 0, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (ImagePart)
* 设置字体
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
disp_message (WindowHandle, 'Image', 'window', 20, 20, 'black', 'true')
stop ()
dev_display (ImagePart1)
disp_message (WindowHandle, 'Image1', 'window', 20, 20, 'black', 'true')
stop ()
add_image (ImagePart, ImagePart1, ImageResult, 0.5, 0)
dev_display (ImageResult)
disp_message (WindowHandle, 'Composite Image', 'window', 20, 20, 'black', 'true')


### 图像减法

HLACON实现图像减法的算子如下

sub_image(ImageMinuend, ImageSubtrahend : ImageSub : Mult, Add : )

$g'=(g1-g2)*Mult+Add$

### 图像乘法

HALCON中实现图像乘法的算子如下

mult_image(Image1, Image2 : ImageResult : Mult, Add : )

$g'=g1*g2*Mult+Add$

dev_close_window ()
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)
* 将图像转为单通道
rgb1_to_gray (Image1, GrayImage)
* 图像相乘
mult_image (Image, GrayImage, ImageResult, 0.005, 0)


### 图像除法

HALCON中实现图像除法的算子如下

div_image(Image1, Image2 : ImageResult : Mult, Add : )

$g'=g1/g2*Mult+Add$

dev_close_window ()
dev_update_off ()
* 创建灰度斜坡图
gen_image_gray_ramp (ImageGrayRamp, 0.2, 0.2, 128, 256, 256, 512, 512)
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
* 设置字体
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_display (Image)
disp_message (WindowHandle, 'Origin Image', 'window', 12, 12, 'black', 'true')
stop ()
dev_display (ImageGrayRamp)
disp_message (WindowHandle, 'Gray Value Image', 'window', 12, 12, 'black', 'true')
stop ()
* 图像相除
div_image (Image, ImageGrayRamp, ImageResult, 255, 0)
dev_display (ImageResult)
disp_message (WindowHandle, 'Resulting Image', 'window', 12, 12, 'black', 'true')


### 图像逻辑运算（位操作）

read_image (Image, 'fabrik')
rgb1_to_gray (Image1, GrayImage)
* 按位与
bit_and (Image, GrayImage, ImageAnd)
* 按位或
bit_or (Image, GrayImage, ImageOr)
* 按位异或
bit_xor (Image2, Image3, ImageXor)
* 按位取反
bit_not (Image2, ImageNot)
* 按位左移
bit_lshift (Image3, ImageLShift, 3)
* 按位右移
bit_rshift (Image3, ImageRShift, 3)


## 图像的几何变换

### 图像几何变换的一般表达式

1. 点变换
图像处理其实就是针对图像的每个像素点的处理，点处理分为几种，有比例变换、原点变换、绕x/y/x=y轴翻转、剪移等

2. 直线变换——两个点的变换
判断两条直线平行与否只需要判断直线上的两个点即可

3. 单位正方形变换
在单位正方形和平行四边形之间建立映射关系，达到互相转换

### 仿射变换

$\begin{bmatrix}u \\ \upsilon\end{bmatrix}=A\begin{bmatrix}x \\ y \\1 \end{bmatrix}=\begin{bmatrix}a_2 & a_1 & a_0 \\ b_2 & b_1 & b_0 \end{bmatrix}\begin{bmatrix}x \\ y \\1 \end{bmatrix}$

HLAOCN中做仿射变换需要先定义变换矩阵，然后对矩阵进行平移、缩放、旋转等操作后在应用矩阵到图像或区域上，有关算子列举一些在下面

hom_mat2d_identity( : : : HomMat2DIdentity)

hom_mat2d_translate( : : HomMat2D, Tx, Ty : HomMat2DTranslate)

hom_mat2d_scale( : : HomMat2D, Sx, Sy, Px, Py : HomMat2DScale)

hom_mat2d_rotate( : : HomMat2D, Phi, Px, Py : HomMat2DRotate)

affine_trans_region(Region : RegionAffineTrans : HomMat2D, Interpolate : )

affine_trans_image(Image : ImageAffinTrans : HomMat2D, Interpolation, AdaptImageSize : )

dev_close_window ()
dev_open_window (0, 0, 512, 512, 'white', WindowHandle)
dev_set_color ('red')
gen_rectangle1 (Rectangle, 20, 20, 100, 100)
dev_display (Rectangle)
* 定义仿射变换矩阵
hom_mat2d_identity (HomMat2DIdentity)
* 增加平移变换量
hom_mat2d_translate (HomMat2DIdentity, 0, 120, HomMat2DTranslate)
* 应用仿射变换
affine_trans_region (Rectangle, RegionAffineTrans, HomMat2DTranslate, 'nearest_neighbor')
dev_display (RegionAffineTrans)
* 增加缩放变换量
hom_mat2d_scale (HomMat2DTranslate, 1.2, 1.2, 16, 16, HomMat2DScale)
* 应用仿射变换
affine_trans_region (RegionAffineTrans, RegionAffineTrans1, HomMat2DScale, 'nearest_neighbor')
dev_display (RegionAffineTrans1)
* 增加旋转变化量
hom_mat2d_rotate (HomMat2DTranslate, 0.6, 256,256, HomMat2DRotate)
* 偏移一下位置
hom_mat2d_translate (HomMat2DRotate, 150, 50, HomMat2DTranslate1)
* 适当变大
hom_mat2d_scale (HomMat2DTranslate1, 2, 2, 64, 64, HomMat2DScale1)
* 应用仿射变换
affine_trans_region (RegionAffineTrans, RegionAffineTrans2, HomMat2DScale1, 'nearest_neighbor')
dev_display (RegionAffineTrans2)


### 投影变换

hom_vector_to_proj_hom_mat2d( : : Px, Py, Pw, Qx, Qy, Qw, Method : HomMat2D)

### 灰度插值

1. 最近邻插值法

1. 双线性插值法

1. 卷积插值法

dev_update_window ('off')
dev_update_var ('off')
dev_update_time ('off')
dev_update_pc ('off')
dev_set_color ('red')
dev_display (Image)
threshold (Image, Regions, 160, 218)
* 开运算
opening_circle (Regions, RegionOpening, 9)
dev_display (RegionOpening)
disp_message (3600,'Ready to rotate', 'window', 12, 12, 'black', 'true')
stop ()
* 定义仿射变换矩阵
hom_mat2d_identity (HomMat2DIdentity)
Scale:=1 //缩放比例
*循环旋转和缩放
for Phi := 0 to 360 by 1
hom_mat2d_rotate (HomMat2DIdentity, rad(Phi), 256, 256, HomMat2DRotate)
hom_mat2d_scale (HomMat2DRotate, Scale, Scale, 256, 256, HomMat2DScale)
* 对图像和区域应用仿射变换
affine_trans_image (Image, ImageAffinTrans, HomMat2DScale, 'nearest_neighbor', 'false')
affine_trans_region (RegionOpening, RegionAffineTrans, HomMat2DScale, 'nearest_neighbor')
dev_display (ImageAffinTrans)
dev_display (RegionAffineTrans)
* 更改缩放比例
Scale:=Scale/1.005
endfor
stop ()
*循环旋转和缩放
for Phi := 0 to 360 by 1
hom_mat2d_rotate (HomMat2DIdentity, rad(-Phi), 256, 256, HomMat2DRotate)
hom_mat2d_scale (HomMat2DRotate, Scale, Scale, 256, 256, HomMat2DScale)
* 对图像和区域应用仿射变换
affine_trans_image (Image, ImageAffinTrans, HomMat2DScale, 'nearest_neighbor', 'false')
affine_trans_region (RegionOpening, RegionAffineTrans, HomMat2DScale, 'nearest_neighbor')
dev_display (ImageAffinTrans)
dev_display (RegionAffineTrans)
* 更改缩放比例
Scale:=Scale*1.005
endfor
disp_message (3600, '好玩吧', 'window', 12, 12, 'black', 'true')
stop ()
dev_update_window ('on')
dev_update_var ('on')
dev_update_time ('on')
dev_update_pc ('on')


## 图像校正

posted @ 2021-09-12 09:40  Dragonet-Z  阅读(1305)  评论(0编辑  收藏  举报