2021亚太数学建模竞赛A题简要思路

摘要

小记一下第一次参加数模赛

思路

1

问题一

用高斯滤波对图像 Pic11 和 Pic1_2 进行降噪处理,用灰度值开运算降低图像 Pic1 3的噪声;对三个图像进行二值化处理,将灰度图像转化为二值化图像,其次用圆形元素对区域进行膨胀,再用改进的 canny 方法提取亚像素精确边缘,对 XLD 轮廓用最小二乘近似线平滑,再根据斜率以及拐点将 XLD 轮廓分割为直线段和圆弧段或椭圆弧,最后根据轮廓的相对位置对轮廓进行排序,并获取XLD轮廓的长度及区域位置

问题二

为达到自标定消除畸变的目的,通过最小二乘近似线平滑、均值滤波降噪、局部阈值分割等算法,将输出的XLD轮廓分割,连接两个标志性的对象元组重复问题一的步骤得到最佳的校准径向畸变参数,用来校准图像的径向畸变由轮廓的相对位置对轮廓进行排序并获取XLD轮廓的长度,最后根据轮廓长度以及实际的物理坐标计算出6个轮廓的真实长度

问题三

依据问题一的模型将 XLD 轮廓分割为直线段和圆弧段或椭圆弧。当分割轮廓为直线段时,用最小二乘法进行直线拟合并用多边形生成XLD轮廓;圆弧段或圆,用 algebraic 方法使轮廓点与生成的圆之间的代数距离最小化;椭圆弧或椭圆,基于 tukey 的算法对轮廓点进行加权并忽略异常值

结果

问题一

1_1

1_2

1_3

问题二

2

问题三

3

代码

问题一

read_image (Img, 'Pic1_1.bmp')
dev_close_window ()
get_image_size (Img, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
gauss_filter(Img, ImgGauss, 11)
binary_threshold (ImgGauss, Region,  'smooth_histo', 'dark', UsedThreshold)
boundary (Region, RegionBorder, 'inner')
dilation_circle (RegionBorder, RegionDread_image (Img, 'Pic1_1.bmp')
dev_close_window ()
get_image_size (Img, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
gauss_filter(Img, ImgGauss, 11)
binary_threshold (ImgGauss, Region,  'smooth_histo', 'dark', UsedThreshold)
boundary (Region, RegionBorder, 'inner')
dilation_circle (RegionBorder, RegionDilation, 2.5)
reduce_domain (ImgGauss, RegionDilation, ImgReduced)
edges_sub_pix (ImgReduced, Edges, 'canny', 2, 20, 60)
smooth_contours_xld(Edges, ImgSmoothed, 9)
segment_contours_xld (ImgSmoothed, ContoursSplit, 'lines_circles', 5, 4, 3)
count_obj (ContoursSplit, Number)
dev_display (Img)
dev_set_draw ('margin')
dev_set_color ('white')
dev_update_window ('off')
dev_set_colored (12)
dev_set_line_width (3)
dev_display (ContoursSplit)
sort_contours_xld (ContoursSplit, SortedContours, 'upper_left', 'true', 'column')
count_obj (ContoursSplit, Number)
length_xld(SortedContours,Length)
gen_empty_obj (Line)
for i := 1 to 24 by 1
    select_obj (SortedContours, ObjectSelected, i)
    get_contour_xld (ObjectSelected, Row, Col)
    gen_region_polygon (Region, Row,Col)
    concat_obj (Line, Region, Line)
endfor
X := Col
Y := Rowilation, 2.5)
reduce_domain (ImgGauss, RegionDilation, ImgReduced)
edges_sub_pix (ImgReduced, Edges, 'canny', 2, 20, 60)
smooth_contours_xld(Edges, ImgSmoothed, 9)
segment_contours_xld (ImgSmoothed, ContoursSplit, 'lines_circles', 5, 4, 3)
count_obj (ContoursSplit, Number)
dev_display (Img)
dev_set_draw ('margin')
dev_set_color ('white')
dev_update_window ('off')
dev_set_colored (12)
dev_set_line_width (3)
dev_display (ContoursSplit)
sort_contours_xld (ContoursSplit, SortedContours, 'upper_left', 'true', 'column')
count_obj (ContoursSplit, Number)
length_xld(SortedContours,Length)
gen_empty_obj (Line)
for i := 1 to 24 by 1
    select_obj (SortedContours, ObjectSelected, i)
    get_contour_xld (ObjectSelected, Row, Col)
    gen_region_polygon (Region, Row,Col)
    concat_obj (Line, Region, Line)
endfor
X := Col
Y := Row
read_image (Img, 'Pic1_2.bmp')
dev_close_window ()
get_image_size (Img, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
gauss_filter(Img, ImgGauss, 11)
binary_threshold (ImgGauss, Region,  'smooth_histo', 'dark', UsedThreshold)
boundary (Region, RegionBorder, 'inner')
dilation_circle (RegionBorder, RegionDilation, 2.5)
reduce_domain (ImgGauss, RegionDilation, ImgReduced)
edges_sub_pix (ImgReduced, Edges, 'canny', 2, 20, 60)
smooth_contours_xld(Edges, ImgSmoothed, 9)
segment_contours_xld (ImgSmoothed, ContoursSplit, 'lines_circles', 5, 4, 3)
count_obj (ContoursSplit, Number)
dev_display (Img)
dev_set_draw ('margin')
dev_set_color ('white')
dev_update_window ('off')
dev_set_colored (12)
dev_set_line_width (3)
dev_display (ContoursSplit)
sort_contours_xld (ContoursSplit, SortedContours, 'upper_left', 'true', 'column')
count_obj (ContoursSplit, Number)
length_xld(SortedContours,Length)
gen_empty_obj (Line)
for i := 1 to 84 by 1
    select_obj (SortedContours, ObjectSelected, i)
    get_contour_xld (ObjectSelected, Row, Col)
    gen_region_polygon (Region, Row,Col)
    concat_obj (Line, Region, Line)
endfor
x := round(Col)
y := round(Row)
read_image (Img, 'Pic1_3.bmp')
dev_close_window ()
get_image_size (Img, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
gray_opening_shape (Img, ImgOpen, 16, 16, 'octagon')
mean_image(ImgOpen, ImgMean, 9.3, 9.3)
binary_threshold (ImgMean, Region,  'smooth_histo', 'dark',  UsedThreshold)
boundary (Region, RegionBorder, 'inner')
dilation_circle (RegionBorder, RegionDilation, 4.6)
reduce_domain (ImgMean, RegionDilation, ImgReduced)
edges_sub_pix (ImgReduced, Edges, 'canny', 2, 20, 60)
union_cotangential_contours_xld (Edges, UnionContours, 2, 5, 3.14, 25.0, 10, 2.0, 'attr_forget')
smooth_contours_xld(UnionContours, ImgSmoothed, 9)
segment_contours_xld (ImgSmoothed, ContoursSplit, 'lines_circles', 5, 4, 3)
count_obj (ContoursSplit, Number)
dev_display (Img)
dev_set_draw ('margin')
dev_set_color ('white')
dev_update_window ('off')
dev_set_colored (12)
dev_set_line_width (3)
dev_display (ContoursSplit)
sort_contours_xld (ContoursSplit, SortedContours, 'upper_left', 'true', 'column')
count_obj (ContoursSplit, Number)
length_xld(SortedContours,Length)
gen_empty_obj (Line)
for i := 1 to 120 by 1
    select_obj (SortedContours, ObjectSelected, i)
    get_contour_xld (ObjectSelected, Row, Col)
    gen_region_polygon (Region, Row,Col)
    concat_obj (Line, Region, Line)
endfor
x := round(Col)
y := round(Row)
问题二

dev_update_off ()
read_image (Img, 'Pic2_4.bmp')
dev_close_window ()
dev_open_window_fit_image (Img, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_display (Img)
disp_message (WindowHandle, 'Img with radial distortions', 'window', 0, 0, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
gen_empty_obj (Borders)
for N := 1 to 4 by 1
    read_image (Img, 'Pic2_' + N$'d' + '.bmp')
    edges_sub_pix (Img, ImgBorders, 'canny', 1, 10, 40)
    smooth_contours_xld(ImgBorders, ImgSmoothed, 9)
    segment_contours_xld (ImgSmoothed, SplitBorders, 'lines_circles', 5, 4, 2)
    select_shape_xld (SplitBorders, SelectedBorders, 'contlength', 'and', 30, 100000)
    concat_obj (Borders, SelectedBorders, Borders)
    dev_display (Img)
    dev_set_colored (12)
    dev_display (SelectedBorders)
    disp_message (WindowHandle, 'Edges extracted from image ' + N$'d', 'window', 0, 0, 'black', 'true')
endfor
dev_clear_window ()
dev_set_colored (12)
dev_display (Borders)
disp_message (WindowHandle, 'Collected edges from multiple images', 'window', 0, 0, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
dev_clear_window ()
disp_message (WindowHandle, 'Performing self-calibration...', 'window', 0, 0, 'black', 'true')
radial_distortion_self_calibration (Borders, CalibrationBorders, 1296, 972, 0.08, 42, 'division', 'fixed', 0, CamParMultiImage)
dev_clear_window ()
dev_set_colored (12)
dev_display (CalibrationBorders)
disp_message (WindowHandle, 'Edges used for calibration', 'window', 0, 0, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
change_radial_distortion_cam_par ('fixed', CamParMultiImage, 0, CamParMultiImageRect)
read_image (ImgCir, 'Pic2_1.bmp')
get_domain (ImgCir, Domain)
change_radial_distortion_image (ImgCir, Domain, ImgCirRectified, CamParMultiImage, CamParMultiImageRect)
get_image_size (ImgCir, WidthCir, HeightCir)
dev_open_window_fit_image (ImgCir, 0, 0, -1, -1, WindowHandleCir)
set_display_font (WindowHandleCir, 16, 'mono', 'true', 'false')
dev_display (ImgCir)
mean_image(ImgCir, MeanCir, 61, 61)
dyn_threshold(ImgCir, MeanCir, SegCir, 5, 'dark')
fill_up(SegCir, SegFillUpCir)
connection(SegFillUpCir, segConnectCir)
select_shape(segConnectCir, SelectedRegionsCir, ['area','circularity'], 'and', [500, 0.7], [20000, 1])
smallest_circle(SelectedRegionsCir, RowCir, ColumnCir, RadiusCir)
area_center(SelectedRegionsCir, AreaCir, Row1Cir, Column1Cir)
CRadius:=sqrt(AreaCir/3.1415926)
RowSize := mean(0.5 / CRadius)
read_image (Img, 'Pic2_4.bmp')
get_domain (Img, Domain)
change_radial_distortion_image (Img, Domain, ImageRectified, CamParMultiImage, CamParMultiImageRect)
dev_close_window ()
get_image_size (ImageRectified, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
gauss_filter(Img, ImgGauss, 11)
binary_threshold (ImgGauss, Region,  'smooth_histo', 'dark', UsedThreshold)
boundary (Region, RegionBorder, 'inner')
dilation_circle (RegionBorder, RegionDilation, 2.5)
reduce_domain (ImgGauss, RegionDilation, ImgReduced)
edges_sub_pix (ImgReduced, Borders, 'canny', 2, 20, 60)
smooth_contours_xld(Borders, ImgSmoothed, 9)
dev_display (ImgSmoothed)
dev_set_draw ('margin')
dev_set_color ('white')
dev_update_window ('off')
dev_set_colored (12)
dev_set_line_width (3)
sort_contours_xld (ImgSmoothed, SortedContours, 'upper_left', 'true', 'column')
count_obj (ImgSmoothed, Number)
length_xld(SortedContours,Length)
RealSize := Length * RowSize
AllSzie := sum(RealSize)
问题三

read_contour_xld_dxf (DxfContours, 'EdgeContour.dxf', [], [], DxfStatus)
segment_contours_xld (DxfContours, ContoursSplit, 'lines_circles', 5, 4, 3)
sort_contours_xld (ContoursSplit, SortedContours, 'upper_left', 'true', 'column')
count_obj (SortedContours, Number)
dev_display (DxfContours)
dev_set_draw ('margin')
dev_set_color ('white')
dev_update_window ('off')
dev_set_colored (12)
dev_set_line_width (3)
dev_display (SortedContours)
dev_set_colored (3)
dev_set_line_width (3)
ColBL := []
RowsBL := []
ColEL := []
RowsEL := []
Lines := []
ColsC := []
RowsC := []
RadiiC := []
PhisC := []
ColsSC := []
RowsSC := []
ColsEC := []
RowsEC := []
ColsE := []
RowsE := []
RadiiE := []
PhisE := []
ColsSE := []
RowsSE := []
ColsEE := []
RowsEE := []
k:=0
J := []
for I := 1 to Number by 1
   select_obj (SortedContours, ObjectSelected, I)
   get_contour_global_attrib_xld (ObjectSelected, 'cont_approx', Attrib)
    if(Attrib == -1)
        k := k+1
        J := [J,k]
        fit_line_contour_xld (ObjectSelected, 'tukey', -1, 2, 6, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
        gen_contour_polygon_xld (ContLine, [RowBegin,RowEnd], [ColBegin,ColEnd])
        RowsBL := [RowsBL,RowBegin]
        RowsEL := [RowsEL,RowEnd]
        ColBL := [ColBL,ColBegin]
        ColEL := [ColEL,ColEnd]
        length_xld(ContLine,LengthL)
        Lines := [Lines, LengthL]
        dev_display (ContLine)
    elseif(Attrib == 1)
        J := [J,0]
        fit_circle_contour_xld (ObjectSelected, 'algebraic', -1, 6, 0, 3, 2, RowC, ColumnC, RadiusC, StartPhiC, EndPhiC, PointOrderC)
        gen_circle_contour_xld (ContCircle, RowC, ColumnC, RadiusC, 0, rad(360), PointOrderC, 1.0)
        RowsC := [RowsC,RowC]
        ColsC := [ColsC,ColumnC]
        RadiiC := [RadiiC,RadiusC]
        PhisC := [PhisC,(EndPhiC-StartPhiC)*180/3.14159]
        ColsSC := [ColsSC,ColumnC+RadiusC*cos(StartPhiC)]
        RowsSC := [RowsSC,RowC+RadiusC*sin(StartPhiC)]
        ColsEC := [ColsEC,ColumnC+RadiusC*cos(EndPhiC)]
        RowsEC := [RowsEC,RowC+RadiusC*sin(EndPhiC)]
        dev_display (ContCircle)
    else
        fit_ellipse_contour_xld(ObjectSelected, 'ftukey',-1, 6, 0, 2, 3, 2, RowE, ColumnE, RadiusE, RaE, RbE, StartPhiE, EndPhiE, PointOrderE)
        gen_ellipse_contour_xld(ContEllipse, RowE, ColumnE, RadiusE, RaE, RbE, 0, rad(360), PointOrderE, 1.0)
        RowsE := [RowsE,RowE]
        ColsE := [ColsE,ColumnE]
        RadiiE := [RadiiE,RadiusE]
        PhisE := [PhisE,(EndPhiE-StartPhiE)*180/3.14159]
        ColsSE := [ColsSE,ColumnE+RadiusE*cos(StartPhiE)]
        RowsSE := [RowsSE,RowE+RadiusE*sin(StartPhiE)]
        ColsEE := [ColsEE,ColumnE+RadiusE*cos(EndPhiE)]
        RowsEE := [RowsEE,RowE+RadiusE*sin(EndPhiE)]
        dev_display (ContEllipse)
        J:=[J,-1]
    endif
endfor
matlab代码

clear;
clc;
a = xlsread('Edge1.xlsx');
b = xlsread('Edge2.xlsx');
FID = dxf_open('EdgeContour.dxf');
dxf_polyline(FID, a(:,1), a(:,2), zeros(length(a),1));
dxf_polyline(FID, b(:,1), b(:,2), zeros(length(b),1));
dxf_close(FID);

DXFLib 库地址: https://www.mathworks.com/matlabcentral/fileexchange/33884-dxflib

- ETX   Thank you for reading -
  • Copyright: All posts on this blog except otherwise stated, All adopt CC BY-NC-ND 4.0 license agreement. Please indicate the source of reprint!