Search code examples
geometryregionhalcon

Halcon - extend region where it intersects with another region


Left image is my starting situation: red is a region, and black is a XLD rectangle.

The right is what I would like to achive. I would need that wherever the Region touches the Rectangle, the region is stretched out by X pixel.. my approach would be to draw a rectangular region with Width "X" and then unite it "union2()" but I have a difficult time to figure out the Y coordinates of the rectangles. Better yet, I can find the intersections, but I do not know how to find out if it is the "beginning" or the "end" of the intersection, so if it is the top or the bottom of the rectangle.. I was thinking that the first is alway the beginning and then they alternate, but in reality it happens that the intersection returns two values at the same pixel..

enter image description here


Solution

  • Here a demo code

    dev_close_window ()
    dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
    
    * Creation of a Region
    gen_circle (Circle1, 150, 200, 100.5)
    gen_circle (Circle2, 250, 150, 50.5)
    gen_circle (Circle3, 350, 200, 100.5)
    union2 (Circle1, Circle2, RegionUnion1)
    union2 (Circle3, RegionUnion1, Region)
    
    * Creation of a xld
    gen_rectangle2_contour_xld (Rectangle_xld, 250, 210, 0, 100.5, 205.5)
    
    * Convert xld to region
    gen_region_contour_xld (Rectangle_xld, Rectangle_region, 'margin')
    
    * intersection
    intersection (Region, Rectangle_region, RegionIntersection)
    
    * stretch RegionIntersection
    Stretch := 30
    area_center (RegionIntersection, AreaRegionIntersection, RowRegionIntersection, ColumnRegionIntersection)
    
    hom_mat2d_identity (HomMat2DIdentity)
    hom_mat2d_scale (HomMat2DIdentity, 1, -Stretch, RowRegionIntersection, ColumnRegionIntersection, HomMat2DScale)
    affine_trans_region (RegionIntersection, RegionIntersectionStretched, HomMat2DScale, 'nearest_neighbor')
    
    union2 (Region, RegionIntersectionStretched, FinalRegion)
    

    The input

    input

    The output

    stretched intersection