Halcon打开单个图片
1、dev_open_file_dialog 打开对话框
2、*获取图像的宽
get_image_size (Image, Width, Height) 3、*创建显示窗口,并设置窗口及绘制参数 dev_open_window_fit_size (0, 0, Width, Height, -1, -1, WindowHandle) *等价于dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
4、*灰度取反
invert_image (Image, ImageInvert) dev_close_window() dev_update_off () dev_open_file_dialog ('read_image', 'default', 'default', Selection) read_image (Image, Selection) get_image_size (Image, Width, Height) dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle) dev_display (Image) *彩色转灰度图 count_channels (Image, Channels) if (Channels == 3) *将图像进行通道分解,分别转换为三个通道的RGB图像 decompose3 (Image, Red, Green, Blue) rgb1_to_gray (Image, Image1) *真彩色转灰度图 elseif (Channels == 4) decompose4 (Image, Red, Green, Blue, Alpha) compose3 (Red, Green, Blue, MultiChannelImage) rgb1_to_gray (MultiChannelImage, Image1) else Image1 := Image endif *取RGB的最小值通道 min_image (Red, Green, ImageMin) min_image (ImageMin, Blue, ImageMin) *使用颜色转换将RGB三个通道图像转化为HSV通道的图像 trans_from_rgb (Red, Green, Blue, Hue1, Saturation1, Intensity1, 'hsi') trans_from_rgb (Red, Green, Blue, Hue2, Saturation2, Value2, 'hsv') *灰度取反 invert_image (Image1, ImageInvert) *二值化 *最大类间方差法(otsu) binary_threshold (Image1, BrightRegion, 'max_separability', 'dark', UsedThreshold) region_to_bin (BrightRegion, BinImage, 0, 255, Width, Height)
Halcon遍历读取图片
1、通用方法
使用list_files和tuple_regexp_select算子
dev_close_window () list_files ('D:/hellowprld/pic', ['files','follow_links','recursive'], 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]) dev_close_window () get_image_size (Image, Width, Height) *dev_open_window (0, 0, Width, Height, 'black', WindowHandle) dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle) dev_display (Image) gen_rectangle1 (Rectangle, 482.313, 458.083, 847.963, 1001.78) reduce_domain (Image, Rectangle, ImageReduced) crop_domain (ImageReduced, ImagePart) mean_image (ImagePart, ImageMean, 20, 20) auto_threshold (ImageMean, Regions, 2) connection (Regions, ConnectedRegions) select_shape_std (ConnectedRegions, SelectedRegions, 'max_area', 70) area_center (SelectedRegions, Area, Row, Column) dev_clear_window () dev_display (ImagePart) dev_display (Image) if (Area < 100000) disp_message (WindowHandle, 'good', 'window', Row, Column, 'black', 'true') else disp_message (WindowHandle, 'bad', 'window', Row, Column, 'black', 'true') endif stop () endfor
2、简易方法
直接for循环
dev_close_window() dev_open_window(0, 0, 512, 512, 'black', WindowHandle) for i:=1 to 7 by 1 read_image (Image, 'image/蚕丝检测' + i + '.bmp') endfor * Main loop: process different images for Index1 := 1 to 7 by 1 read_image (Image, 'image/saw_blade/saw_'+Index1$'02') endfor
画十字线
read_image (Image1, 'D:/match/AOI/基于halcon的AOI自动光学检测1.bmp') get_image_size (Image1, Width, Height) dev_open_window_fit_image (Image1, 0, 0, -1, -1, WindowHandle) dev_display (Image1) *画十字线 set_display_font (WindowHandle, 25, 'mono', 'true', 'false') dev_set_line_width(2) dev_set_color('green') gen_rectangle1 (Rectangle0, Height/2, 0, Height/2, Width) //横线 dev_display(Rectangle0) gen_rectangle1 (Rectangle1, 0, Width/2, Height, Width/2) //竖线 dev_display(Rectangle1)