Posted on 2009-07-27 21:52
千張 閱讀(12732)
評論(8) 編輯 收藏 引用 所屬分類:
圖像處理
這周是實習的第一周,天天要早起,天天擠地鐵,沒想到,自己就這樣成為上班一族了

。第一周比較累,每天到公司就像夢游似的,晚睡的習慣一下也改不過來,嚴重睡眠不足,在公司中午也睡不了覺,所以到下午兩點左右的時候特別困,好幾天我都坐在電腦前打瞌睡

。
公司在做一個地面互動系統的項目,我的任務是從圖像中提取出腳印的輪廓,拼接相近的腳印,當做一個腳印處理,并返回拼接后每個腳印的位置,我返回的是包含腳印的矩形,采用OpenCV實現的。我們采用了是紅外線裝置,所以拍攝出來的圖像噪聲比較少,二值化時采用合適的閾值,效果就比較好。
OpenCV文檔中有輪廓提取的例程,這里就詳細不介紹了,我的步驟包括預處理(濾波處理)->灰度化->二值化->Canny邊緣檢測->輪廓提取,可以將二值化這一步省略,二值化很依賴閾值,光照對其影響較大。
下面就貼下拼接的代碼:(若兩矩形相交或中心比較接近,就拼接這兩個矩形)
在CSDN上看到一個測試兩矩形是否相交的特簡單的方法:
/****************************************************************************/
矩形A和B
矩形 A (x1,y1),(x2,y2); //矩形的左上角和右下角坐標
矩形 B (x3,y3),(x4,y4);
設 m = (x1>x4) | (x2<x3)
n = (y2<y3) | (y1>y4)
if(m | n)
{
//不相交
}
else
{
//相交
}
/****************************************************************************/
我就把它寫成代碼了,如下:


.
//在二值圖像上查找contour
cvFindContours( m_pTempImg,storage,&cont,sizeof(CvContour),
CV_RETR_LIST,CV_CHAIN_APPROX_NONE, cvPoint(0,0) );






/**//**********************************將位置相近的輪廓合并***************************************/
BOOL m,n;
int mark = 0,diffX,diffY;
CvRect temp;
vector< CvRect >::iterator iter ;
for( ;cont;cont = cont->h_next )

{
rect = ( (CvContour*)cont ) -> rect;
if( rect.height * rect.width > AREASMALL && ( rect.height * rect.width ) < (m_pContourImg->width * m_pContourImg->height-AREALARGE ))

{
if( 0 == mark )

{
FootSeq.push_back( rect );
mark = 1;
}
else

{
for( iter= FootSeq.begin();iter != FootSeq.end();iter++ ) //for1

{
m = (rect.x > ((*iter).x + (*iter).width)) || ((rect.x + rect.width) < (*iter).x);
n = ((rect.y+rect.height) < (*iter).y) || (rect.y > ((*iter).y + (*iter).height));
if( m || n ) //兩矩形不相交

{
diffX = ( rect.x+rect.width/2)-((*iter).x+(*iter).width/2 );
diffY = ( rect.y+rect.height/2)-((*iter).y+(*iter).height/2 );
if ( (int)( diffX*diffX + diffY*diffY ) < DISTANCE )

{
//保存原值
temp.x = ( *iter ).x;
temp.y = ( *iter ).y;
temp.width = ( *iter ).width;
temp.height = ( *iter ).height;

//合并新的矩形,覆蓋原矩形
(*iter).x = ( rect.x > (*iter).x) ? (*iter).x : rect.x;
(*iter).y = ( rect.y > (*iter).y) ? (*iter).y : rect.y;
(*iter).width = ( (rect.x + rect.width) > (temp.x + temp.width)) ? (rect.x + rect.width - (*iter).x) : ((temp.x + temp.width)-(*iter).x );
(*iter).height = ( (rect.y + rect.height) > (temp.y + temp.height)) ? (rect.y + rect.height - (*iter).y) : ((temp.y + temp.height)-(*iter).y );
break;
}
}//不相交,若不合并,則繼續與下一個輪廓比較
else //相交則合并

{
//保存原值
temp.x = ( *iter ).x;
temp.y = ( *iter ).y;
temp.width = ( *iter ).width;
temp.height = ( *iter ).height;

//合并新的矩形,覆蓋原矩形
(*iter).x = ( rect.x > (*iter).x) ? (*iter).x : rect.x;
(*iter).y = ( rect.y > (*iter).y) ? (*iter).y : rect.y;
(*iter).width = ( (rect.x + rect.width) > (temp.x + temp.width)) ? (rect.x + rect.width - (*iter).x) : ((temp.x + temp.width)-(*iter).x );
(*iter).height = ( (rect.y + rect.height) > (temp.y + temp.height)) ? (rect.y + rect.height - (*iter).y) : ((temp.y + temp.height)-(*iter).y );
break;
}//相交
}

if( iter == FootSeq.end() )
FootSeq.push_back( rect ); //如果FootSeq中沒有與cont可合并的矩形,就將該矩形存儲,作為新的矩形。
}//end else (mark == 1)
} //end if (面積符合要求)
}//end for (cont == NULL)