Halcon-表面檢測-----確定光度立體法系統的光源的方向

Geeyoo發表於2019-07-12

對應程式:
determine_tilt_slant_photometric_stereo.hdev

目標:通過使用具有朗伯反射的球體的影像來確定光度立體法系統的光源的方向。

思路為:

圖片:
在這裡插入圖片描述

程式碼:

* Initializations
dev_update_off ()
dev_close_window ()
dev_open_window_fit_size (0, 0, 640, 494, 500, 500, WindowHandle1)
Color := 'green'
dev_set_color (Color)
dev_set_draw ('margin')
dev_set_line_width (3)
set_display_font (WindowHandle1, 14, 'mono', 'true', 'false')
ApproxSphereRadius := 200
* 
* Read the shaded images of the sphere
ImageFiles := 'photometric_stereo/shaded_sphere_' + [1:6]$'02'
read_image (Images, ImageFiles)
channels_to_image (Images, MultichannelImage)
count_channels (MultichannelImage, NumImages)
dev_display (Images)
* 
* Determine the position and the radius of the sphere
* from the multichannel image
disp_message (WindowHandle1, 'Detect the position of the sphere ...', 'window', 12, 12, 'black', 'true')
gen_circle_contour_xld (ContourCircle, 200, 200, ApproxSphereRadius, 0, 6.28318, 'positive', 1)
create_scaled_shape_model_xld (ContourCircle, 'auto', 0.0, 0.0, 'auto', 0.9, 1.5, 'auto', 'auto', 'ignore_color_polarity', 1, ModelID)
find_scaled_shape_model (MultichannelImage, ModelID, 0.0, 0.0, .9, 1.1, .95, 1, .5, 'least_squares_high', 0, .9, RowCenter, ColumnCenter, Angle, Scale, MatchScore)
SphereRadius := ApproxSphereRadius * Scale
gen_cross_contour_xld (CenterCross, RowCenter, ColumnCenter, 15, 0.0)
dev_display (CenterCross)
dev_display_shape_matching_results (ModelID, Color, RowCenter, ColumnCenter, Angle, Scale, Scale, 0)
disp_continue_message (WindowHandle1, 'black', 'true')
clear_shape_model (ModelID)
stop ()
Color := 'blue'
dev_set_color (Color)
* 
* Compute the tilt and slant angles of the light source:
* The parameters MinGray and MaxGray are used to select
* the gray values used for processing. Those values must
* be selected in a way that:
* 1. The background of the scene is excluded from processing
* 2. Overexposed and underexposed parts of the sphere are
*    excluded from processing
* 3. The selected region is as large as possible in order
*    to ensure a robust estimation of the orientation of
*    the light source
MinGray := 170
MaxGray := 255
measure_light_source_orientation_from_sphere (MultichannelImage, SphereRadius, RowCenter, ColumnCenter, MinGray, MaxGray, Tilt, Slant)
* 
* Display the results
get_window_extents (WindowHandle1, Row, Column, Width, Height)
dev_open_window (0, Width + 12, Width, Height, 'white', WindowHandle2)
dev_set_part (0, 0, 493, 639)
set_display_font (WindowHandle2, 14, 'mono', 'true', 'false')
PointO := [RowCenter,ColumnCenter]
PointE := PointO + [0,SphereRadius + 10]
gen_contour_polygon_xld (LineContour, [PointO[0],PointE[0]], [PointO[1],PointE[1]])
gen_arrow_contour_xld (ArrowContour, PointE[0], PointE[1], PointO[0], PointO[1] + 50, 20, 20)
hom_mat2d_identity (HomMat2DIdentity)
gen_circle_contour_xld (ContSphere, RowCenter, ColumnCenter, SphereRadius, 0, 6.28318, 'positive', 1)
gen_contour_polygon_xld (LineContourBottom, [RowCenter + SphereRadius,RowCenter + SphereRadius], [0,639])
for Index := 1 to NumImages by 1
    access_channel (MultichannelImage, Image, Index)
    hom_mat2d_rotate (HomMat2DIdentity, rad(Tilt[Index - 1]), RowCenter, ColumnCenter, HomMat2DRotateTilt)
    affine_trans_contour_xld (ArrowContour, ArrowTilt, HomMat2DRotateTilt)
    affine_trans_point_2d (HomMat2DRotateTilt, PointE[0], PointE[1], QxTilt, QyTilt)
    if (Tilt[Index - 1] >= 0)
        PointOrder := 'positive'
    else
        PointOrder := 'negative'
    endif
    gen_circle_contour_xld (ContCircleTilt, RowCenter, ColumnCenter, 100, 0, rad(Tilt[Index - 1]), PointOrder, 1)
    * 
    hom_mat2d_rotate (HomMat2DIdentity, rad(Slant[Index - 1]), RowCenter, ColumnCenter, HomMat2DRotateSlant)
    affine_trans_contour_xld (ArrowContour, ArrowSlant, HomMat2DRotateSlant)
    affine_trans_point_2d (HomMat2DRotateSlant, PointE[0], PointE[1], QxSlant, QySlant)
    gen_circle_contour_xld (ContCircleSlant, RowCenter, ColumnCenter, 100, 0, rad(Slant[Index - 1]), 'positive', 1)
    * 
    dev_set_window (WindowHandle1)
    dev_display (Image)
    dev_display (LineContour)
    dev_display (ContCircleTilt)
    dev_display (ArrowTilt)
    disp_message (WindowHandle1, 'Top view (' + Index + '/' + NumImages + ')', 'window', 12, 12, 'black', 'true')
    disp_message (WindowHandle1, '0°', 'image', PointE[0], PointE[1], 'black', 'true')
    disp_message (WindowHandle1, 'Tilt = ' + Tilt[Index - 1]$'+4.1f' + '°', 'image', QxTilt, QyTilt - 80, 'black', 'true')
    dev_set_window (WindowHandle2)
    dev_clear_window ()
    dev_set_color ('black')
    dev_display (LineContourBottom)
    dev_display (ContSphere)
    dev_set_color (Color)
    dev_display (LineContour)
    dev_display (ArrowSlant)
    dev_display (ContCircleSlant)
    disp_message (WindowHandle2, 'Cross section (' + Index + '/' + NumImages + ')', 'window', 12, 12, 'black', 'true')
    disp_message (WindowHandle2, '0°', 'image', PointE[0], PointE[1], 'black', 'true')
    disp_message (WindowHandle2, 'Slant = ' + Slant[Index - 1]$'+4.1f' + '°', 'image', QxSlant, QySlant - 80, 'black', 'true')
    dev_set_window (WindowHandle1)
    if (Index < NumImages)
        disp_continue_message (WindowHandle2, 'black', 'true')
        stop ()
    endif
endfor

用到的幾個運算元:

參考資料:
[1]: https://blog.csdn.net/weixin_34391445/article/details/94513702

相關文章