halcon裁切并导入数据集

create_dict (WindowDict)

*** 测试 ***
*图片路径
*总路径
DataDir := 'D:/HNQM_JS_ZC/RF_OCR'
* D:/HNQM_JS_ZC/RF_OCR/Train/CFY/test_hui
* HalconImageDir:= DataDir + '/IMAGE/POS/0915_2'
HalconImageDir:= DataDir + '/Train/CFY/test_hui'
*数据集路径
*/Train/CFY/dl_dataset/DLDataset_cfy_0915.hdict
*D:/HNQM_JS_ZC/RF_OCR/Train/CFY/test_hui/remove_0D/avag1000.hdict
PillBagHdictFile := DataDir + '/Train/CFY/test_hui/remove_0D/avag1000.hdict'

*读取数据集
read_dict (PillBagHdictFile, [], [], DLDataset)
*dataset字典,与DLDataset格式相同
create_dict (Dict_Dataset)
get_dict_tuple(DLDataset,'image_dir',image_dir)
get_dict_tuple(DLDataset,'class_ids',dl_class_ids)
get_dict_tuple(DLDataset,'class_names',dl_class_names)
get_dict_tuple(DLDataset,'samples',dl_sample)



********************************************

create_dict (Dict_Dataset)
set_dict_tuple (Dict_Dataset, 'class_ids',dl_class_ids )
set_dict_tuple (Dict_Dataset, 'class_names',dl_class_names )
*图片路径,也需要对应修改
set_dict_tuple (Dict_Dataset, 'image_dir', 'D:/HNQM_JS_ZC/RF_OCR/Train/CFY/test_hui/remove_0D/img')

************************************************

*保存的要导出的数据集
dl_sample_export:=dl_sample  
*最后所有的数据在这里
Dict_tuple:=[]
sample_sum:=0
*单个文件的路径  sample[i]
for i:=0 to |dl_sample_export|-1 by 1
    *就是一个样本表示一个图片,但是可能有多个标注
    dl_sample_export_i:=dl_sample_export[i]
    
    get_dict_tuple(dl_sample_export_i,'image_file_name',ImageFile)
    read_image (Image, image_dir+ImageFile)
    *获取原始图片宽高
    get_image_size (Image, oriWidth, oriHeight)    
    
    *sample.
    get_dict_tuple(dl_sample_export_i,'bbox_label_id',sample_bbox)
    *将画框提取然后进行裁剪
    get_dict_tuple(dl_sample_export_i,'bbox_row1',row1)
    get_dict_tuple(dl_sample_export_i,'bbox_row2',row2)
    get_dict_tuple(dl_sample_export_i,'bbox_col1',col1)
    get_dict_tuple(dl_sample_export_i,'bbox_col2',col2)
    get_dict_tuple(dl_sample_export_i,'bbox_label_id',class_id)
    
    if(|sample_bbox|==1)
        create_dict(dl_sample_export_label_img)
        set_dict_tuple(dl_sample_export_label_img,'bbox_label_id',class_id)
        set_dict_tuple(dl_sample_export_label_img,'image_id',sample_sum)
        *set_dict_tuple(dl_sample_export_label_img,'image_file_name','save_'+sample_sum+'jpg')
        
         *将画框提取然后进行裁剪
        width:=col2-col1
        hight:=row2-row1
        crop_Image:=Image
        crop_part (crop_Image, ImagePart, row1, col1, width, hight)
        write_image(ImagePart,'jpeg',0,'D:/HNQM_JS_ZC/RF_OCR/Train/CFY/test_hui/remove_0D/avag1000_img_save'+'/save_'+sample_sum+'.jpg')
        
        set_dict_tuple(dl_sample_export_label_img,'image_file_name','save_'+sample_sum+'.jpg')
        
        set_dict_tuple(dl_sample_export_label_img,'bbox_row1',0)
        set_dict_tuple(dl_sample_export_label_img,'bbox_row2',row2-row1)
        set_dict_tuple(dl_sample_export_label_img,'bbox_col1',0)
        set_dict_tuple(dl_sample_export_label_img,'bbox_col2',col2-col1)
        *保存到字典中
        Dict_tuple[sample_sum]:=dl_sample_export_label_img
        sample_sum:=sample_sum+1
    endif
    
    *每一个标注,这里就对应了每一个
    for j:=1 to |sample_bbox|-1 by 1
        *对每一个sample的每一图片进行另存,将其放入数据集
        create_dict(dl_sample_export_label_img)
        set_dict_tuple(dl_sample_export_label_img,'bbox_label_id',class_id[j])
        set_dict_tuple(dl_sample_export_label_img,'image_id',sample_sum)
        *set_dict_tuple(dl_sample_export_label_img,'image_file_name','save_'+sample_sum+'jpg')
        
        *将画框提取然后进行裁剪
        width:=col2[j]-col1[j]
        hight:=row2[j]-row1[j]
        crop_Image:=Image
        crop_part (crop_Image, ImagePart, row1[j], col1[j], width, hight)
        if(width>5)
            write_image(ImagePart,'jpeg',0,'D:/HNQM_JS_ZC/RF_OCR/Train/CFY/test_hui/remove_0D/avag1000_img_save'+'/save_'+sample_sum+'.jpg')
        
            set_dict_tuple(dl_sample_export_label_img,'image_file_name','save_'+sample_sum+'.jpg')
            
            set_dict_tuple(dl_sample_export_label_img,'bbox_row1',0)
            set_dict_tuple(dl_sample_export_label_img,'bbox_row2',row2[j]-row1[j])
            set_dict_tuple(dl_sample_export_label_img,'bbox_col1',0)
            set_dict_tuple(dl_sample_export_label_img,'bbox_col2',col2[j]-col1[j])
            *保存到字典中
            Dict_tuple[sample_sum]:=dl_sample_export_label_img
            sample_sum:=sample_sum+1
        endif
        
    endfor
endfor
set_dict_tuple(Dict_Dataset,'samples',Dict_tuple)
    
*D:/HNQM_JS_ZC/RF_OCR/Train/CFY/test_hui/remove_0D
write_dict (Dict_Dataset, DataDir + '/Train/CFY/test_hui/remove_0D/aveg100.hdict', [], [])

你可能感兴趣的:(AI-CV,python,人工智能,算法)