通过定位黑色塑料件的中心位置来定位目标,
代码流程
-
median_image中值滤波,去除亮点和暗点,让图片模糊化
-
threshold 阈值二值化,获取塑料件
-
select_shape 去除细小的干扰
dev_close_window () list_files ('Pic', ['files','follow_links'], ImageFiles) tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles) Row1:=[] Column1:=[] Angle1:=[] Distance:=185 for Index := 0 to |ImageFiles| - 1 by 1 read_image (Image, ImageFiles[Index]) get_image_size (Image, Width, Height) dev_open_window (0, 0, Width, Height, 'black', WindowHandle) dev_display (Image) dev_set_draw ('margin') * Image Acquisition 01: Do something median_image (Image, ImageMedian, 'circle', 3, 'mirrored') threshold (ImageMedian, Regions, 0, 60) select_shape (Regions, SelectedRegions, 'area', 'and', 7000, 99999) shape_trans (SelectedRegions, RegionTrans, 'convex') area_center (RegionTrans, Area, Row, Column) gen_cross_contour_xld (Cross, Row, Column, 16, rad(45)) *极轴的角度(与水平轴方向) orientation_region (RegionTrans, Phi) OriAngle:=deg(Phi) dev_set_color ('red') dev_set_draw ('margin') dev_set_line_width (1) Length := 80 disp_arrow (WindowHandle, Row, Column, Row + cos(Phi + 1.5708)* Length, Column + sin(Phi + 1.5708) * Length, 3) stop() *============测量矩形区域================= Length1:=56 Length2:=14 lineColumn:=Column+Distance gen_circle (Circle, Row, Column, Distance) *圆弧的起始角度为极轴方向对应的角度Phi:2.00711 *圆弧的跨度(需要事先知道,可以计算得出)AngleExtend:2.73167 AngleExtend:=2.73167 ArcAngle:=Phi+AngleExtend *确定两条直线的夹角:圆弧跨度计算 *angle_ll (Row, Column, Row, lineColumn, Row, Column, CenterRow, CenterColumn, Angle2) *Angle22:=deg(Angle2) * A:=360-(angle-Angle22) * Ac:=rad(A) *AngleC:=Phi+Ac *get_points_ellipse (AngleC, Row, Column, 0, Distance, Distance, RowPoint, ColPoint) *gen_cross_contour_xld (Cross2, RowPoint, ColPoint, 6, Angle2) *disp_line (WindowHandle, Row, Column, Row, Column+Distance) get_points_ellipse (ArcAngle, Row, Column, 0, Distance, Distance, RowPoint, ColPoint) *水平线 gen_contour_polygon_xld (Contour, [Row,Row], [Column,lineColumn]) *极轴方向直线 gen_contour_polygon_xld (Contour, [Row,Row + cos(Phi + 1.5708) * Distance], [Column,Column + sin(Phi + 1.5708) * Distance]) *测量区域,连接中心点直线 gen_contour_polygon_xld (Contour, [Row,RowPoint], [Column,ColPoint]) *创建一个测量矩形区域 gen_rectangle2 (ROI_0, RowPoint, ColPoint, rad(OriAngle+6), Length1, Length2)
*放平,将Phi角度变为0 vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(0), HomMat2D) affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false') affine_trans_region (ROI_0, RegionAffineTrans, HomMat2D, 'nearest_neighbor') stop() area_center (RegionAffineTrans, Area1, Row2, Column2) orientation_region (RegionAffineTrans, Phi1) NewAngle:=deg(Phi1) *一下就是通过定位之后的其操作 gen_measure_rectangle2 (Row2, Column2, Phi1, 100, 20, Width, Height, 'nearest_neighbor', MeasureHandle1) measure_pairs (ImageAffineTrans, MeasureHandle1, 1.6, 23, 'all', 'all', RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, InterDistance)
gen_cross_contour_xld (Cross1, RowEdgeSecond, ColumnEdgeSecond, 6, 0.785398)
stop() endfor
read_image
median_image
threshold (ImageMedian, Regions, 0, 60)
shape_trans (SelectedRegions, RegionTrans, 'convex')
gen_cross_contour_xld (Cross, Row, Column, 16, rad(45))
*极轴的角度(与水平轴方向),最终是将这个角度转成0
orientation_region (RegionTrans, Phi)
disp_arrow (WindowHandle, Row, Column, Row + cos(Phi + 1.5708)* Length, Column + sin(Phi + 1.5708) * Length, 3)
gen_circle (Circle, Row, Column, Distance)
*极轴方向直线
gen_contour_polygon_xld (Contour, [Row,Row + cos(Phi + 1.5708) * Distance], [Column,Column + sin(Phi + 1.5708) * Distance])
*测量区域,连接中心点直线
gen_contour_polygon_xld (Contour, [Row,RowPoint], [Column,ColPoint])
gen_cross_contour_xld (Cross1, RowEdgeSecond, ColumnEdgeSecond, 6, 0.785398)
例图
标签:Distance,Phi,Column,测量,contour,例子,Blob,gen,Row From: https://www.cnblogs.com/ken0137/p/18360869