金属圆柱求取倾斜角度

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

方法1:

     


  
    count_seconds (Seconds1)
    read_image (Image, '9_7750_f3d301de6764d94.jpg')
    get_image_size (Image, Width, Height)
    dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
    dev_display (Image)
    rgb1_to_gray (Image, GrayImage)
    dev_display (GrayImage)  
 
    threshold (GrayImage, Region, 75, 100)
    closing_circle (Region, RegionClosing, 5)
    gen_contour_region_xld (RegionClosing, Contours, 'border')
    segment_contours_xld (Contours, ContoursSplit, 'lines', 50, 10, 10) 
    
    *擬合水平線
    select_contours_xld (ContoursSplit, SelectedContours0, 'direction', rad(-5), rad(5), rad(-0.5), rad(0.5))
    select_shape_xld (SelectedContours0, Stamps, 'row', 'and', 20, 35)
    length_xld (Stamps, Length0)
    tuple_max (Length0, Max0)
    gen_empty_obj (EmptyObject0)
    count_obj (SelectedContours0, Number0)
    for i := 1 to Number0 by 1
        select_obj (SelectedContours0, ObjectSelected, i)
        length_xld (ObjectSelected, Length0)
        if (Length0=Max0)
            concat_obj (EmptyObject0, ObjectSelected, EmptyObject0)
        endif
    endfor
    
    *擬合垂直線
    select_contours_xld (ContoursSplit, SelectedContours1, 'direction', rad(85), rad(95),rad(-0.5), rad(0.5))    
    length_xld (SelectedContours1, Length1)
    tuple_max (Length1, Max1)
    gen_empty_obj (EmptyObject1)
    count_obj (SelectedContours1, Number1)   
    for j := 1 to Number1 by 1
        select_obj (SelectedContours1, ObjectSelected, j)
        length_xld (ObjectSelected, Length1)
        if (Length1=Max1)
            concat_obj (EmptyObject1, ObjectSelected, EmptyObject1)
        endif
    endfor    
    fit_line_contour_xld (EmptyObject0, 'tukey', -1, 0, 5, 2, RowBegin0, ColBegin0, RowEnd0, ColEnd0, Nr0, Nc0, Dist0)
    gen_region_line(RegionLine0, RowBegin0, ColBegin0, RowEnd0, ColEnd0)
    fit_line_contour_xld (EmptyObject1, 'tukey', -1, 0, 5, 2, RowBegin1, ColBegin1, RowEnd1, ColEnd1, Nr1, Nc1, Dist1)
    gen_region_line(RegionLine1, RowBegin1, ColBegin1, RowEnd1, ColEnd1)
    
    *求交點
    intersection_lines (RowBegin1, ColBegin1, RowEnd1, ColEnd1, RowBegin0, ColBegin0, RowEnd0, ColEnd0, Row, Column, IsOverlapping)   
    
    dev_clear_window()
    dev_display (Image)
    dev_set_color ('magenta')
    dev_set_line_width (2)
    distance_pp(Row,Column,RowEnd0, ColEnd0, DistanceEnd)
    distance_pp(Row,Column,RowBegin0, ColBegin0, DistanceBegin)
    if (DistanceBegin > DistanceEnd)
        disp_line(WindowHandle,RowBegin0, ColBegin0,Row, Column)
    else
        disp_line(WindowHandle,RowEnd0, ColEnd0,Row, Column)
    endif
    distance_pp(Row,Column,RowEnd1, ColEnd1, DistanceEnd)
    distance_pp(Row,Column,RowBegin1, ColBegin1, DistanceBegin)
    if (DistanceBegin > DistanceEnd)
        disp_line(WindowHandle,RowBegin1, ColBegin1,Row, Column)
    else
        disp_line(WindowHandle,RowEnd1, ColEnd1,Row, Column)
    endif
    *計算角度
    dev_set_color ('green')
    gen_region_points(Region,Row, Column)
    gen_cross_contour_xld (Cross, Row, Column, 20, rad(45))
    dev_display(Cross)
    angle_ll (RowBegin1, ColBegin1, RowEnd1, ColEnd1, RowBegin0, ColBegin0, RowEnd0, ColEnd0, Angle)
    AngleDeg := deg(Angle)
     
    count_seconds (Seconds2)
    disp_message (WindowHandle, 'CT : ' + ((Seconds2-Seconds1)*1000)+' mSec', 'image', 10 , 10, 'slate blue', 'true')  
    disp_message (WindowHandle, 'AngleDeg : ' + AngleDeg+' 度', 'image', 60 , 10, 'slate blue', 'true')  
  
    stop()

在这里插入图片描述
方法2:

list_files ('', ['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)
for Index := 0 to |ImageFiles| - 1 by 1
    read_image (Image, ImageFiles[Index])
    * Image Acquisition 01: Do something
    count_seconds (Seconds3)
    mean_image (Image, ImageMean, 30, 30)
    dyn_threshold (Image, ImageMean, RegionDynThresh, 15, 'light')
    closing_circle (RegionDynThresh, RegionClosing1, 12.5)
    fill_up (RegionClosing1, RegionFillUp)
    connection (RegionFillUp, ConnectedRegions)
    select_shape_std (ConnectedRegions, SelectedRegions, 'max_area', 70)
    opening_rectangle1 (SelectedRegions, RegionOpening, 100, 10)
    skeleton (RegionOpening, Skeleton)
    gen_contours_skeleton_xld (Skeleton, Contours1, 1, 'filter')
    fit_line_contour_xld (Contours1, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
    gen_region_line (RegionLines, RowBegin, ColBegin, RowEnd, ColEnd)
    opening_rectangle1 (SelectedRegions, RegionOpening1, 10, 100)
    dilation_circle (RegionOpening, RegionDilation, 12.5)
    difference (RegionOpening1, RegionDilation, RegionDifference)
    connection (RegionDifference, ConnectedRegions1)
    select_shape_std (ConnectedRegions1, SelectedRegions1, 'max_area', 70)
    skeleton (SelectedRegions1, Skeleton1)
    gen_contours_skeleton_xld (Skeleton1, Contours2, 1, 'filter')
    fit_line_contour_xld (Contours2, 'tukey', -1, 0, 5, 2, RowBegin1, ColBegin1, RowEnd1, ColEnd1, Nr, Nc, Dist)
    gen_region_line (RegionLines1, RowBegin1, ColBegin1, RowEnd1, ColEnd1)
    intersection_lines (RowBegin1, ColBegin1, RowEnd1, ColEnd1, RowBegin, ColBegin, RowEnd, ColEnd, Row, Column, IsOverlapping)
    angle_ll (RowEnd1, ColEnd1,RowBegin1, ColBegin1,RowBegin, ColBegin, RowEnd, ColEnd,   Angle)
    gen_cross_contour_xld (Cross, Row, Column, 20, rad(45))
    count_seconds (Seconds4)
    dev_display(Image)
    dev_set_line_width (3)
    dev_set_color ('red')
    dev_display(RegionLines)
    dev_display(RegionLines1)
    dev_set_color ('green')
    dev_display(Cross)
    AngleDeg := deg(Angle)
    disp_message (WindowHandle, 'CT : ' + ((Seconds4-Seconds3)*1000)+' mSec', 'image', 10 , 10, 'slate blue', 'true')  
    disp_message (WindowHandle, 'AngleDeg : ' + AngleDeg+' 度', 'image', 60 , 10, 'slate blue', 'true')
    stop()
endfor

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/cashmood/article/details/111942046