Halcon随机贴图生成缺陷图片脚本

halcon随机贴图生成缺陷图片,用于深度学习训练:

cs 复制代码
read_image (Image, 'C:/Users/61082/Desktop/bentiiamge/omron/S06-1211/ok/ok_images/D246B_CPFNNUBA8LT0SX_AAA_S2412001793_C1216_1733895885320066.jpg')
get_image_size (Image, Width, Height)
gen_rectangle1 (Rectangle, 22, 17, 89, 47)
reduce_domain (Image, Rectangle, ImageReduced)
crop_domain (ImageReduced, ImagePart)
for i:=0 to 100 by 1
    Rand:=rand(3)
    paster_Row:=int(Rand[0]*Height)
    paster_Col:=int(Rand[1]*Width)
    Angle:=int(Rand[2]*360)
    
    minImageWidth:=sqrt(Width*Width+Height*Height)
    minImageHeight:=sqrt(Width*Width+Height*Height)
    vector_angle_to_rigid ( Height/2,Width/2, 0, minImageHeight/2, minImageHeight/2, rad(Angle), HomMat2D)
    *获取图像的通道数
    count_channels (ImagePart, Channels)
    *如果是单通道图像
    if (Channels==1)
        *生成一个画布
        gen_image_const (Image1, 'byte', minImageWidth, minImageHeight)
        *将image放到画布上
        overpaint_gray (Image1, ImagePart)
        *图片旋转
        affine_trans_image (Image1, ImageAffineTrans, HomMat2D, 'constant', 'false')
        get_image_size (ImageAffineTrans, Width1, Height1)
        dev_clear_window ()
        dev_display (ImageAffineTrans)
        *如果是三通道彩色图像
    endif 
    if (Channels==3)
        *分解三通道图像
        decompose3 (ImagePart, Image11, Image2, Image3)
        *生成三个画布
        gen_image_const (Image4, 'byte', minImageWidth, minImageHeight)
        gen_image_const (Image5, 'byte', minImageWidth, minImageHeight)
        gen_image_const (Image6, 'byte', minImageWidth, minImageHeight)
        *依次将三个单通道图像overpaint到画布上
        overpaint_gray (Image4, Image11)
        overpaint_gray (Image5, Image2)
        overpaint_gray (Image6, Image3)
        *仿射变换单通道图像
        affine_trans_image (Image4, ImageAffineTrans1, HomMat2D, 'constant', 'false')
        affine_trans_image (Image5, ImageAffineTrans2, HomMat2D, 'constant', 'false')
        affine_trans_image (Image6, ImageAffineTrans3, HomMat2D, 'constant', 'false')
        *三个单通道图像合成彩色图像
        compose3 (ImageAffineTrans1, ImageAffineTrans2, ImageAffineTrans3, rotateImage)
        dev_clear_window ()
        dev_display (rotateImage)
    endif
    
    dev_update_off ()
    
    *rgb分离
    decompose3 (Image, ImageR, ImageG, ImageB)
    decompose3 (rotateImage, ImagePartR, ImagePartG, ImagePartB)
    
    *小图去背景
    threshold (ImagePartR, Regions1, 210, 255)
    connection (Regions1, ConnectedRegions2)
    select_shape_std (ConnectedRegions2, SelectedRegions3, 'max_area', 70)
    fill_up (SelectedRegions3, SelectedRegions3)
    smallest_rectangle1 (SelectedRegions3, Row1, Column1, Row2, Column2)
    get_region_points (SelectedRegions3, oriPartRows, oriPartColumns)
    count:=|oriPartRows|
    subRs:=gen_tuple_const(count, Row1)
    subCs:=gen_tuple_const(count, Column1)
    PartRows:=oriPartRows-subRs
    PartColumns:=oriPartColumns-subCs
    if ((paster_Row+(Row2-Row1)>=Height) or (paster_Col+(Column2-Column1)>=Width))
        continue
    endif
    startRs:=gen_tuple_const(count, paster_Row)
    startCs:=gen_tuple_const(count, paster_Col)
    *将所有像素坐标加上偏移量
    Rows:=startRs+PartRows
    Cols:=startCs+PartColumns
    
    *r绘制
    get_grayval (ImagePartR, oriPartRows, oriPartColumns, Grayval)
    copy_image (ImageR, DupImageR)
    set_grayval (DupImageR, Rows, Cols, Grayval)
    
    
    *g绘制
    get_grayval (ImagePartG, oriPartRows, oriPartColumns, Grayval1)
    copy_image (ImageG, DupImageG)
    set_grayval (DupImageG, Rows, Cols, Grayval1)
    
    *b绘制
    get_grayval (ImagePartB, oriPartRows, oriPartColumns, Grayval2)
    copy_image (ImageB, DupImageB)
    set_grayval (DupImageB, Rows, Cols, Grayval2)
    compose3 (DupImageR, DupImageG, DupImageB, MultiChannelImage)
    
    dev_clear_window()
    dev_display(MultiChannelImage)
    
    wait_seconds(1)
endfor
相关推荐
CoovallyAIHub5 分钟前
方案 | 动车底部零部件检测实时流水线检测算法改进
深度学习·算法·计算机视觉
CoovallyAIHub7 分钟前
方案 | 光伏清洁机器人系统详细技术实施方案
深度学习·算法·计算机视觉
lxmyzzs11 分钟前
【图像算法 - 14】精准识别路面墙体裂缝:基于YOLO12与OpenCV的实例分割智能检测实战(附完整代码)
人工智能·opencv·算法·计算机视觉·裂缝检测·yolo12
大千AI助手2 小时前
SWE-bench:真实世界软件工程任务的“试金石”
人工智能·深度学习·大模型·llm·软件工程·代码生成·swe-bench
盼小辉丶3 小时前
PyTorch生成式人工智能——使用MusicGen生成音乐
pytorch·python·深度学习·生成模型
Tiger Z5 小时前
《动手学深度学习v2》学习笔记 | 1. 引言
pytorch·深度学习·ai编程
胡耀超19 小时前
DataOceanAI Dolphin(ffmpeg音频转化教程) 多语言(中国方言)语音识别系统部署与应用指南
python·深度学习·ffmpeg·音视频·语音识别·多模态·asr
HUIMU_20 小时前
DAY12&DAY13-新世纪DL(Deeplearning/深度学习)战士:破(改善神经网络)1
人工智能·深度学习
mit6.82421 小时前
[1Prompt1Story] 注意力机制增强 IPCA | 去噪神经网络 UNet | U型架构分步去噪
人工智能·深度学习·神经网络
Coovally AI模型快速验证21 小时前
YOLO、DarkNet和深度学习如何让自动驾驶看得清?
深度学习·算法·yolo·cnn·自动驾驶·transformer·无人机