对焦测距模块的例程有一个整体的特点(前三个例程),就是他们的流程基本相同。
前三个例程的目的都是用三维重建来体现最终的效果,其主要步骤如下:
1.读取图像,将图像转变为多通道图像,并建立循环,在循环中access_channel访问每个通道图像。
2.depth_from_focus使用多个焦点级别提取深度。得到相应的深度图像Depth,如果得到的图像不够理想,可以进行其他操作达到想要的效果图像ImageDepth。
3.select_grayvalues_from_channels使用索引图像选择多通道图像的灰度值。得到图像SharpImage,同理可进行其他操作达到想要的效果图像SharpImage。
4.dev_set_paint ([‘3d_plot’,‘texture’])定义灰度值输出模式。
5.将图像ImageDepth 与图像SharpImage通过compose2构成一个双通道图像,并显示出来。
例程1:
dev_close_window ()
dev_update_off ()
Sequence := [1:10]
Names := 'dff/focus_' + (Sequence$'.2')
read_image (Image, Names)
channels_to_image (Image, Image)
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
for I := 1 to 10 by 1
access_channel (Image, Image1, I)
dev_display (Image1)
disp_message (WindowHandle, 'Focus series (' + I + '/10)', 'window', 12, 12, 'black', 'true')
wait_seconds (0.5)
endfor
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
depth_from_focus (Image, Depth, Confidence, 'highpass', 'next_maximum')
* Construct sharp image
mean_image (Depth, DepthHighConf, 51, 51)
select_grayvalues_from_channels (Image, DepthHighConf, SharpImage)
* Smooth depth map
scale_image_max (DepthHighConf, ImageScaleMax)
mean_image (ImageScaleMax, DepthMean, 51, 51)
dev_display (SharpImage)
disp_message (WindowHandle, 'Sharp image', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 3D reconstruction
dev_set_paint (['3d_plot','texture'])
compose2 (DepthMean, SharpImage, TexturedImage)
dev_display (TexturedImage)
disp_message (WindowHandle, 'Textured depth image', 'window', 12, 12, 'black', 'true')
例程4:通过读取图像,转变为多通道图像并在循环中访问每个通道图像,通过depth_from_focus得到深度图像,经过均值滤波和二值化,特征选择得到相应的区域,从而得到图像中体现深度不同的边缘的区域图像。
Names := []
dev_close_window ()
for i := 1 to 10 by 1
Names := [Names,'dff/focus_' + (i$'.2')]
endfor
read_image (Image, Names)
channels_to_image (Image, Image)
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
* Access a channel of a multichannel image
for i := 1 to 10 by 1
access_channel (Image, Image1, i)
endfor
depth_from_focus (Image, Depth, Confidence, 'highpass', 'next_maximum')
* Smooth by averaging
mean_image (Depth, DepthHighConf, 11, 11)
scale_image_max (DepthHighConf, ImageScaleMax)
mean_image (ImageScaleMax, DepthMean, 51, 51)
threshold (DepthMean, Resistor, 158, 255)
connection (Resistor, ConnectedRes)
* Select regions of a given shape
select_shape_std (ConnectedRes, FinalRes, 'max_area', 0)
threshold (DepthMean, Solder, 125, 158)
connection (Solder, ConnectedSol)
select_shape_std (ConnectedSol, SelectedSol, 'max_area', 0)
* Fill up holes
fill_up (SelectedSol, FinalSol)
dev_set_line_width (3)
dev_set_draw ('margin')
dev_display (Image)
dev_set_color ('red')
dev_display (FinalSol)
dev_set_color ('blue')
dev_display (FinalRes)