您的位置:首页 > 其它

图像处理算法基础(七)---形态学边界提取

2015-12-25 16:15 477 查看
形态学一般是使用二值图像,进行边界提取,骨架提取,孔洞填充,角点提取,图像重建。

基本的算法:膨胀腐蚀,开操作,闭操作,击中击不中变换

边界提取主要涉及腐蚀和求补集操作



代码如下:

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;

}

处理前图片:



提取边界的图片:




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: