1. 程式人生 > >Halcon實戰記錄之一《劃分影象區域為中心,四邊,四角》

Halcon實戰記錄之一《劃分影象區域為中心,四邊,四角》

在做影象處理的時候,我們有時候對影象的不同區域的處理方式不一樣,我這裡寫下我最近使用的方法,將一副影象分成三個區域–中心,四邊,四角,如下圖:

這裡寫圖片描述

邊的最小寬度和高度佔影象的比例為Ratio,例如Rato等於0.1,即中心佔影象的0.8的比例。

1、獲取中心區域:

void GetImageRegion_Center (HObject *ho_Region, HTuple hv_imWidth, HTuple hv_imHeight, HTuple hv_Ratio)
{
    GenRectangle1(&(*ho_Region), hv_imHeight*hv_Ratio, hv_imWidth*hv_Ratio
, (hv_imHeight*(1-hv_Ratio))-1, (hv_imWidth*(1-hv_Ratio))-1); }

2、獲取四邊區域:

void GetImageRegion_Side (HObject *ho_Region, HTuple hv_imWidth, HTuple hv_imHeight, HTuple hv_Ratio)
{
    // Local iconic variables
    HObject  ho_Rectangle_top, ho_Rectangle_bot, ho_Rectangle_lft;
    HObject  ho_Rectangle_rgt, ho_ObjectsConcat;
    //top
GenRectangle1(&ho_Rectangle_top, 0, hv_imWidth*hv_Ratio, (hv_imHeight*hv_Ratio)-1, (hv_imWidth*(1-hv_Ratio))-1); //bottom GenRectangle1(&ho_Rectangle_bot, hv_imHeight*(1-hv_Ratio),hv_imWidth*hv_Ratio, hv_imHeight-1, (hv_imWidth*(1-hv_Ratio))-1); //left GenRectangle1(&ho_Rectangle_lft, hv_imHeight*hv_Ratio, 0
, (hv_imHeight*(1-hv_Ratio)
)-1, (hv_imWidth*hv_Ratio)-1); //right GenRectangle1(&ho_Rectangle_rgt, hv_imHeight*hv_Ratio, hv_imWidth*(1-hv_Ratio), (hv_imHeight*(1-hv_Ratio))-1, hv_imWidth-1); ConcatObj(ho_Rectangle_top, ho_Rectangle_bot, &ho_ObjectsConcat); ConcatObj(ho_ObjectsConcat, ho_Rectangle_lft, &ho_ObjectsConcat); ConcatObj(ho_ObjectsConcat, ho_Rectangle_rgt, &ho_ObjectsConcat); Union1(ho_ObjectsConcat, &(*ho_Region)); }

3、獲取四角區域:

void GetImageRegion_Corner (HObject *ho_Region, HTuple hv_imWidth, HTuple hv_imHeight, HTuple hv_Ratio)
{
    // Local iconic variables
    HObject  ho_Rectangle_lt, ho_Rectangle_lb, ho_Rectangle_rt;
    HObject  ho_Rectangle_rb, ho_ObjectsConcat;
    //left top
    GenRectangle1(&ho_Rectangle_lt, 0, 0, (hv_imHeight*hv_Ratio)-1, (hv_imWidth*hv_Ratio)-1);
    //left bottom
    GenRectangle1(&ho_Rectangle_lb, hv_imHeight*(1-hv_Ratio), 0, hv_imHeight-1, (hv_imWidth*hv_Ratio)-1);
    //right top
    GenRectangle1(&ho_Rectangle_rt, 0, hv_imWidth*(1-hv_Ratio), (hv_imHeight*hv_Ratio)-1, hv_imWidth-1);
    //right bottom
    GenRectangle1(&ho_Rectangle_rb, hv_imHeight*(1-hv_Ratio), hv_imWidth*(1-hv_Ratio), hv_imHeight-1, hv_imWidth-1);

    ConcatObj(ho_Rectangle_lt, ho_Rectangle_lb, &ho_ObjectsConcat);
    ConcatObj(ho_ObjectsConcat, ho_Rectangle_rt, &ho_ObjectsConcat);
    ConcatObj(ho_ObjectsConcat, ho_Rectangle_rb, &ho_ObjectsConcat);
    Union1(ho_ObjectsConcat, &(*ho_Region));
}

注意:四邊和四角的區域各有四個,返回的是聯合區域。