|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2012-11-19 13:04:41
|
显示全部楼层
int32 IMA_SpotPick(TObjBlob *pObjBlobs, int16 bufsize, int32 tar_area, int32 area_thre)
{
TRectCell rc;
int32 blobnum = 0, i, cnt = 0, id;
uint8 full_search = 0;
int32 min_size, min_area, min1;
int32 area_sum;
int32 w1, h1, tmp1;
min_area = tar_area - area_thre;
if (min_area < 256)
min_area = 256;
min_size = 2;
//在ROI内搜索
if (m_Prev_Found)
{
rc.x = m_Prev_CenterX - SPOT_ROI_WITDH/2;
if (rc.x <0)
rc.x = 0;
if (rc.x + SPOT_ROI_WITDH >= IMG_WIDTH)
rc.w = IMG_WIDTH - rc.x;
else
rc.w = SPOT_ROI_WITDH;
rc.y = m_Prev_CenterY - SPOT_ROI_HEIGHT/2;
if (rc.y <0)
rc.y = 0;
if (rc.y + SPOT_ROI_HEIGHT >= IMG_HEIGHT)
rc.h = IMG_HEIGHT - rc.y;
else
rc.h = SPOT_ROI_HEIGHT;
IMA_rl_Encode(&rc, 1, min_size);
IMA_rl_labeling(&m_rlcImage);
blobnum = IMA_rl_getBlob(pObjBlobs, bufsize, min_area);
}
//结果中找匹配的
id = -1;
min1 = 0x7fffff;
area_sum = 0;
for(i=0; i<blobnum; i++)
{
area_sum += pObjBlobs[i].area;
if (pObjBlobs[i].area < tar_area + area_thre)
{
cnt++;
if (abs(pObjBlobs[i].area - tar_area) < min1) //面积判据
{
id = i;
min1 = abs(pObjBlobs[i].area - tar_area);
}
}
}
if (id <0)
full_search = 1;
//没找到, 全屏搜索
if (full_search)
{
rc.x = 0;
rc.y = 0;
rc.w = IMG_WIDTH;
rc.h = IMG_HEIGHT;
IMA_rl_Encode(&rc, 2, min_size); //降分辨率scale
IMA_rl_labeling(&m_rlcImage);
blobnum = IMA_rl_getBlob(pObjBlobs, bufsize, min_area);
//再次在结果中找匹配的
id = -1;
min1 = 0x7fffffff;
area_sum = 0;
for(i=0; i<blobnum; i++)
{
area_sum += pObjBlobs[i].area;
w1 = pObjBlobs[i].ext_rect.right - pObjBlobs[i].ext_rect.left;
h1 = pObjBlobs[i].ext_rect.bottom - pObjBlobs[i].ext_rect.top;
tmp1 = MIN(w1, h1);
if ((pObjBlobs[i].area < tar_area + area_thre) //面积判据
&&(abs(w1-h1) < tmp1/2)) //长宽比判据
{
cnt++;
if (abs(pObjBlobs[i].area - tar_area) < min1)
{
id = i;
min1 = abs(pObjBlobs[i].area - tar_area);
}
}
}
}
//ROI定义
if (id>=0)
{
m_Prev_CenterX = pObjBlobs[id].center_x;
m_Prev_CenterY = pObjBlobs[id].center_y;
m_Prev_Found = 1;
}
else
{
m_Prev_Found = 0;
}
#if 0
//按面积估计,调节二值化阀值
if ((area_sum > (tar_area + area_thre)*2)||(cnt>1)||(cnt==1 && (prev_binThre_adjUp !=0)))
{
IMA_bin_threPercentAdj(-2);
prev_binThre_adjUp = 0;
m_Prev_Found = 0;
}
else if ((full_search!=0) && (m_Prev_Found==0)&&(blobnum>0) &&(cnt==0)&&(prev_binThre_adjUp ==0))
{
IMA_bin_threPercentAdj(1);
prev_binThre_adjUp = 1;
m_Prev_Found = 0;
}
#endif
//Buffer最后一个清0,外部程序可以读出全部Blob
if (blobnum < bufsize-1)
{
IMA_rl_blobInit(pObjBlobs + blobnum);
}
return(id);
}
|
|