Bootstrap

切分图像的halcon代码

* Matching 01: ************************************************
* Matching 01: BEGIN of generated code for model initialization
* Matching 01: ************************************************

* Matching 01: Obtain the model image
read_image (Image, 'C:/Users/Administrator/Desktop/OK-A/0.png')

* Matching 01: Build the ROI from basic regions
gen_rectangle1 (ModelRegion, 129.303, 288.983, 665.821, 790.853)

* Matching 01: Reduce the model template
reduce_domain (Image, ModelRegion, TemplateImage)

* Matching 01: Create and train the shape model
create_generic_shape_model (ModelID)
* Matching 01: set the model parameters
set_generic_shape_model_param (ModelID, 'metric', 'use_polarity')
train_generic_shape_model (TemplateImage, ModelID)

*write_shape_model(ModelID, 'fuck.shm')
read_shape_model( 'fuck.shm',ModelID)

* Matching 01: Get the model contour for transforming it later into the image
get_shape_model_contours (ModelContours, ModelID, 1)

* Matching 01: Support for displaying the model
* Matching 01: Get the reference position
area_center (ModelRegion, ModelRegionArea, RefRow, RefColumn)
vector_angle_to_rigid (0, 0, 0, RefRow, RefColumn, 0, HomMat2D)
affine_trans_contour_xld (ModelContours, TransContours, HomMat2D)

* Matching 01: Display the model contours
dev_display (Image)
dev_set_color ('green')
dev_set_draw ('margin')
dev_display (ModelRegion)
dev_display (TransContours)
stop ()

* Matching 01: END of generated code for model initialization
* Matching 01: ************************************************

* Matching 01: BEGIN of generated code for model application
* Matching 01: Set the search paramaters
set_generic_shape_model_param (ModelID, 'angle_start', rad(-20))
set_generic_shape_model_param (ModelID, 'angle_end', rad(20))
set_generic_shape_model_param (ModelID, 'border_shape_models', 'false')

* Matching 01: Get template dimensions from ModelRegion
smallest_rectangle1 (ModelRegion, Row1, Column1, Row2, Column2)
TemplateHeight := Row2 - Row1
TemplateWidth := Column2 - Column1

* Matching 01: Loop over all specified test images
TestImages := ['C:/Users/Administrator/Desktop/OK-A/24.png']
for T := 0 to 0 by 1
    * 
    * Matching 01: Obtain the test image
    read_image (Image, TestImages[T])
    * 
    * Matching 01: Find the model
    find_generic_shape_model (Image, ModelID, MatchResultID, NumMatchResult)
    * 
    * Matching 01: Retrieve results
    for I := 0 to NumMatchResult-1 by 1
        * 
        * Matching 01: Display the detected match
        dev_display (Image)
        get_generic_shape_model_result_object (MatchContour, MatchResultID, I, 'contours')
        dev_set_color ('green')
        dev_display (MatchContour)
        * 
        * Matching 01: Retrieve parameters of the detected match
        get_generic_shape_model_result (MatchResultID, I, 'row', Row)
        get_generic_shape_model_result (MatchResultID, I, 'column', Column)
        get_generic_shape_model_result (MatchResultID, I, 'angle', Angle)
        get_generic_shape_model_result (MatchResultID, I, 'scale_row', ScaleRow)
        get_generic_shape_model_result (MatchResultID, I, 'scale_column', ScaleColumn)
        get_generic_shape_model_result (MatchResultID, I, 'hom_mat_2d', HomMat2D)
        get_generic_shape_model_result (MatchResultID, I, 'score', Score)
        
        * Matching 01: Calculate cropping region centered at match position
        HalfHeight := TemplateHeight / 2
        HalfWidth := TemplateWidth / 2
        CropRow1 := Row - HalfHeight
        CropRow2 := Row + HalfHeight
        CropCol1 := Column - HalfWidth
        CropCol2 := Column + HalfWidth
        
        * Matching 01: Create cropping region
        gen_rectangle1 (CropRegion, CropRow1, CropCol1, CropRow2, CropCol2)
        
        * Matching 01: Crop the image
        reduce_domain (Image, CropRegion, CroppedImage)
        crop_domain (CroppedImage, CroppedResult)
        
        * Matching 01: Save the cropped image

        tuple_string (I, '.3f', SVal)
        FileName:= 'C:/Users/Administrator/Desktop/OK-A/match_24' + SVal
        write_image (CroppedResult, 'png', 0, FileName)
        
        stop ()
    endfor
endfor

* Matching 01: *******************************************
* Matching 01: END of generated code for model application
* Matching 01: *******************************************

;