1.图像采集
2.图像灰度化
3.图像二值化
4.图像形态学闭运算
5.填充所有孔洞
6.计算所有连通域
7.计算区域方向
8.获取区域中心位置坐标
9.计算生成旋转矩阵
10.旋转图像至水平方向
11.几何测量,得到宽度(单位:像素)
本次实验重点知识:仿射变换的应用
笔记本电脑折叠90度,利用其摄像头完成实验平台搭建,在扩展屏上进行代码编辑,笔记本屏幕壁纸更换为白色背景,亮度调节至最高,减少阴影影响,使用自制小纸片。
* Image Acquisition 01: Code generated by Image Acquisition 01
open_framegrabber ('DirectShow', 1, 1, 0, 0, 0, 0, 'default', 8, 'rgb', -1, 'false', 'default', '[0] ', 0, -1, AcqHandle)
grab_image_start (AcqHandle, -1)
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
* dev_update_off ()
while (true)
grab_image_async (Image, AcqHandle, -1)
rgb1_to_gray (Image, GrayImage)
threshold (GrayImage, Regions, 149, 206)
closing_rectangle1 (Regions, RegionOpening, 10, 10)//形态学闭运算,模版为矩形
fill_up (RegionOpening, RegionFillUp)
orientation_region (RegionFillUp, Phi)//区域方向计算,得到弧度Phi
area_center (RegionFillUp, Area, Row, Column) //获得区域坐标,面积
vector_angle_to_rigid (Row, Column, Phi, Row, Column, 0, HomMat2D)//生成旋转矩形
affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')//旋转操作
//以下测量部分代码使用测量助手生成
* Measure 02: Code generated by Measure 02
* Measure 02: Prepare measurement
AmplitudeThreshold := 49
RoiWidthLen2 := 29
set_system ('int_zooming', 'true')
* Measure 02: Coordinates for line Measure 02 [0]
LineRowStart_Measure_02_0 := 241.682
LineColumnStart_Measure_02_0 := 320.184
LineRowEnd_Measure_02_0 := 260.591
LineColumnEnd_Measure_02_0 := 1232.32
* Measure 02: Convert coordinates to rectangle2 type
TmpCtrl_Row := 0.5*(LineRowStart_Measure_02_0+LineRowEnd_Measure_02_0)
TmpCtrl_Column := 0.5*(LineColumnStart_Measure_02_0+LineColumnEnd_Measure_02_0)
TmpCtrl_Dr := LineRowStart_Measure_02_0-LineRowEnd_Measure_02_0
TmpCtrl_Dc := LineColumnEnd_Measure_02_0-LineColumnStart_Measure_02_0
TmpCtrl_Phi := atan2(TmpCtrl_Dr, TmpCtrl_Dc)
TmpCtrl_Len1 := 0.5*sqrt(TmpCtrl_Dr*TmpCtrl_Dr + TmpCtrl_Dc*TmpCtrl_Dc)
TmpCtrl_Len2 := RoiWidthLen2
* Measure 02: Create measure for line Measure 02 [0]
* Measure 02: Attention: This assumes all images have the same size!
gen_measure_rectangle2 (TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2, 1280, 720, 'nearest_neighbor', MsrHandle_Measure_02_0)
* Measure 02: ***************************************************************
* Measure 02: * The code which follows is to be executed once / measurement *
* Measure 02: ***************************************************************
* Measure 02: The image is assumed to be made available in the
* Measure 02: variable last displayed in the graphics window
copy_obj (ImageAffineTrans, Image, 1, 1)
* Measure 02: Execute measurements
measure_pairs (Image, MsrHandle_Measure_02_0, 1, AmplitudeThreshold, 'all', 'all', Row1_Measure_02_0, Column1_Measure_02_0, Amplitude1_Measure_02_0, Row2_Measure_02_0, Column2_Measure_02_0, Amplitude2_Measure_02_0, Width_Measure_02_0, Distance_Measure_02_0)
* Measure 02: Do something with the results
endwhile
close_framegrabber (AcqHandle)
实验效果