影象處理演算法基礎(七)---形態學邊界提取
基本的演算法:膨脹腐蝕,開操作,閉操作,擊中擊不中變換
邊界提取主要涉及腐蝕和求補集操作
程式碼如下:
int picProcessBasics::IMGEdgeExtracting(IplImage* pImg,IplImage* pDestImg,double threshold,int pos)
{
if(NULL == pImg || NULL == pDestImg)
return -1;
if(pDestImg->nChannels!=1 )
{
cout<<"It's not gray image!"<<endl;
return -1;
}
IplImage* tempImage= cvCreateImage(cvGetSize(pImg), 8, 1);
if(pImg->nChannels != 1)
{
cvCvtColor(pImg,tempImage,CV_RGB2GRAY);
}
else
cvCopy(pImg,tempImage,0);
//轉換成二值影象
cvThreshold(tempImage, pDestImg, threshold, 255, CV_THRESH_BINARY);
IplConvKernel *element = cvCreateStructuringElementEx( pos*2+1, pos*2+1, pos, pos, CV_SHAPE_RECT, 0 );
cvErode( tempImage, tempImage, element, 1); // 侵蝕,磨蝕
cvReleaseStructuringElement( &element );
for(int i = 0; i < tempImage->height; i++){
for(int j = 0; j < tempImage->width; j++){
pDestImg->imageData[pDestImg->widthStep * i + j ]=pDestImg->imageData[pDestImg->widthStep * i + j ] - tempImage->imageData[tempImage->widthStep * i + j ];
}
}
cvReleaseImage(&tempImage);
return 0;
}
處理前圖片:
提取邊界的圖片: