(1) 对核酸管图像进行灰度化、阈值分割和连通域分析;
(2) 筛选出待检测的区域,并对该区域进行变换校正;
(3) 进一步获取待检测的ROI区域,并根据几何特征和阈值条件,来对核酸管外观进行检测;
(4) 将检测结果写入txt文档。
*统计核酸管外观正常的数组
P_Tuple:=[]
*统计核酸管外观缺陷的数组
N_Tuple:=[]
*判断文件是否存在
file_path:= './核酸管外观缺陷检测统计1.txt'
file_exists (file_path, file_exist)
if (file_exist==1)
delete_file (file_path)
endif
open_file (file_path, 'output', FileHandle)
*获取文件路径列表
list_files ('./Test_img/', ['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
img_path:=ImageFiles[Index]
*img_path:='./Test_img/Image_20230420091226458.bmp'
*文件名拆分
parse_filename (img_path, BaseName, Extension, Directory)
*读取核酸管图片
read_image (Image, img_path)
*灰度化
rgb1_to_gray (Image, GrayImage)
*阈值分割
threshold (GrayImage, Region, 30, 145)
*填充孔洞
fill_up (Region, RegionFillUp)
*连通区域分析
connection (RegionFillUp, ConnectedRegions)
*获取面积
area_center (ConnectedRegions, Area, Row, Column)
*通过面积筛选区域
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 500000, 800000)
*获取区域外接矩形
smallest_rectangle2 (SelectedRegions, Row1, Column1, Phi, Length1, Length2)
*创建变换模型
vector_angle_to_rigid (Row1, Column1, abs(Phi), Row1, Column1, acos(0.0), HomMat2D)
*进行区域变换
affine_trans_region (SelectedRegions, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
*获取区域外接矩形(平行于坐标轴)
smallest_rectangle1 (RegionAffineTrans, Row11, Column11, Row12, Column12)
*裁剪区域
clip_region (RegionAffineTrans, RegionClipped1, Row11, Column12-200, Row12, Column12)
*获取区域外接矩形(平行于坐标轴)
smallest_rectangle1 (RegionClipped1, Row21, Column21, Row22, Column22)
*裁剪区域
clip_region (RegionClipped1, RegionClipped2, int((Row21+Row22)/2.0)-20, Column12-200, int((Row21+Row22)/2.0)+20, Column12)
*获取区域外接矩形(平行于坐标轴)
smallest_rectangle1 (RegionClipped2, Row31, Column31, Row32, Column32)
distance:=Column32-Column31
if(distance>=180)
dev_clear_window ()
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width/2, Height/2, 'black', WindowHandle)
dev_display (Image)
*设置颜色
dev_set_color ('green')
*显示字符
set_tposition (WindowHandle, 10, 5) //设置文本光标1的位置
write_string (WindowHandle, '核酸管外观正常!')
set_tposition (WindowHandle, 50, 5) //设置文本2光标的位置
write_string (WindowHandle, ['核酸管外观正常的图片名:',BaseName])
dev_close_window ()
P_Tuple:=[P_Tuple,1]
fwrite_string(FileHandle,[Index,'核酸管外观正常的图片名:',BaseName])
fnew_line(FileHandle)
else
dev_clear_window ()
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width/2, Height/2, 'black', WindowHandle)
dev_display (Image)
*设置颜色
dev_set_color ('green')
*显示字符
set_tposition (WindowHandle, 10, 5) //设置文本光标1的位置
write_string (WindowHandle, '核酸管外观有缺陷!')
set_tposition (WindowHandle, 50, 5) //设置文本2光标的位置
write_string (WindowHandle, ['核酸管外观缺陷的图片名:',BaseName])
dev_close_window ()
N_Tuple:=[N_Tuple,0]
fwrite_string(FileHandle,[Index,'核酸管外观缺陷的图片名:',BaseName])
fnew_line(FileHandle)
endif
endfor
tuple_length (P_Tuple, P_Length)
tuple_length (N_Tuple, N_Length)
Yield_Rate:= real(P_Length)/real(N_Length+P_Length)
fwrite_string(FileHandle,['核酸管良品率:',Yield_Rate])
fnew_line(FileHandle)
close_file (FileHandle)
(1) 单张图片检测结果:
(2) 部分图片检测结果: