halcon截取图片并保存为图片

halcon截取图片并保存为图片,代码如下:

**********************批量图片截取***************
InputTuple := ['True','2','6','2','1676.714,1319.375','90','2','2149.448,1323.449','90','2','1682.962,4337.366','90','2','2133.115,4334.441','90','2','1281.720,4557.761','270','2','1750.190,4557.761','270','','','','','','','5','1849.229,1280.023','90','5','2306.869,1287.910','90','5','1816.291,4346.614','90','5','2287.365,4349.672','90','5','1412.514,4582.036','270','5','1877.470,4575.921','270','','','','','','','300','200']
*遍历文件夹
list_files ('E:/测试图片/焊点/2D/OriginNG', 'files', Files)
Images1:=[]
Images2:=[]
tuple_number (InputTuple, InputTuple)  
TabCount := InputTuple[2]
TabWidth := InputTuple[51]
TabHeight := InputTuple[52]  
for j := 0 to |Files|-1 by 1  
    tuple_strchr (Files[j], '-', Position)
    tuple_substr (Files[j], Position+1, Position+2, Substring)
    tuple_number (Substring, Number)
    tuple_int (Number, Int)
    if(Int==2)
        Images1:=Files[j]
        read_image (InputImage1,Images1)
        for i := 0 to TabCount-1 by 1       
        lib_convert_point (Cross, InputTuple[3*i+4], TabCenter)
        crop_rectangle1 (InputImage1, TabImage, TabCenter[0]-(TabWidth+TabHeight)*0.5, TabCenter[1]-(TabWidth+TabHeight)*0.5, TabCenter[0]+(TabWidth+TabHeight)*0.5, TabCenter[1]+(TabWidth+TabHeight)*0.5)
        lib_white_balance (TabImage, TabImage, 80, 80, 80)
        rotate_image (TabImage, TabImage, InputTuple[3*i+5], 'constant')      
        write_image (TabImage, 'bmp', 0, 'E:/Images//1_' + i$'.4d' +j+ '.bmp')   
       endfor
    endif
    if(Int==5)
        Images2:=Files[j]
        read_image (InputImage2,Images2)
        for i := 0 to TabCount-1 by 1
        lib_convert_point (Cross, InputTuple[3*i+28], TabCenter)
        crop_rectangle1 (InputImage2, TabImage, TabCenter[0]-(TabWidth+TabHeight)*0.5, TabCenter[1]-(TabWidth+TabHeight)*0.5, TabCenter[0]+(TabWidth+TabHeight)*0.5, TabCenter[1]+(TabWidth+TabHeight)*0.5)
        lib_white_balance (TabImage, TabImage, 80, 80, 80)
        rotate_image (TabImage, TabImage, InputTuple[3*i+29], 'constant')
        write_image (TabImage, 'bmp', 0, 'E:/Images//2_' + i$'.4d' +j+ '.bmp')       
    endfor
    endif
endfor
stop ()

关键代码,截取图片,并保存到文件夹中,代码如下:

read_image (InputImage1,Images1)
        for i := 0 to TabCount-1 by 1
        lib_convert_point (Cross, InputTuple[3*i+4], TabCenter)
        *切出一个或多个矩形图像区域
        crop_rectangle1 (InputImage1, TabImage, TabCenter[0]-(TabWidth+TabHeight)*0.5, TabCenter[1]-(TabWidth+TabHeight)*0.5, TabCenter[0]+(TabWidth+TabHeight)*0.5, TabCenter[1]+(TabWidth+TabHeight)*0.5)
        lib_white_balance (TabImage, TabImage, 80, 80, 80)
        *图像绕中心旋转(根据需求是否写)
        rotate_image (TabImage, TabImage, InputTuple[3*i+5], 'constant')    
        *将图像数据保存为图像文件  
        write_image (TabImage, 'bmp', 0, 'E:/Images//1_' + i$'.4d' +j+ '.bmp')   

lib_convert_point (Cross, String, Point)函数,通过String获取某个点位置坐标。

String=['1676.714,1319.375']

gen_empty_obj (Cross)
Point := number(split(String,','))

if (|Point| != 2)
    Point := [0,0]
else
    gen_cross_contour_xld (Cross, Point[0], Point[1], 100, 0)
endif

return ()

lib_white_balance (TabImage, TabImage, 80, 80, 80)自定义函数,处理截取的图片。

get_region_points (Image, Rows, Columns)
decompose3 (Image, ImageR, ImageG, ImageB)
get_grayval (ImageR, Rows, Columns, GrayvalR)
get_grayval (ImageG, Rows, Columns, GrayvalG)
get_grayval (ImageB, Rows, Columns, GrayvalB)
ScaleR := TargetR/mean(GrayvalR)
ScaleG := TargetG/mean(GrayvalG)
ScaleB := TargetB/mean(GrayvalB)
scale_image (ImageR, ImageScaledR, ScaleR, 0)
scale_image (ImageG, ImageScaledG, ScaleG, 0)
scale_image (ImageB, ImageScaledB, ScaleB, 0)
compose3 (ImageScaledR, ImageScaledG, ImageScaledB, ImageBalance)
return ()

需要处理的图片:

halcon截取图片并保存为图片_第1张图片

halcon处理后的图片:

halcon截取图片并保存为图片_第2张图片

 总结:本文是批量截取各个图片,我的代码中是处理的2和5结尾的图片(-2.jpg和-5.jpg),每张原图截取6张图片保存到本地。

你可能感兴趣的:(Halcon图像处理基础,halcon)