具体的步骤在算法中有讲解。
/************************************************************************/
/* 查找物体的边界,输出已排序的边界序列 适应于单一区域 */
/************************************************************************/
//若能够输出边界点的序列则是比较有用的
#include
#include
#include
#include
using namespace std;
int main(){
IplImage * image,*image2;
image = cvLoadImage("E:\\image\\mapleleaf.tif",0);
cvNamedWindow("image",1);
cvShowImage("image",image);
image2 = cvCreateImage(cvSize(image->width, image->height),image->depth,1);
cvZero(image2);//image2 赋值为0
//寻找区域的左上角点
CvPoint startPoint = cvPoint(0,0);
bool bFindStartpoint = false;
int i ,j;
unsigned char * ptr,*dst;
stack board;//奇数位存储x坐标,偶数位存储y坐标
//当前扫描点
CvPoint currentPoint = cvPoint(0,0);
//邻域的8个点的方向
int directions[8][2] = {{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
int beginDirection = 0;
bool bFindBoardpoint = false;//寻找到邻域的边界点的判定
for (i = 0 ; i< image->height && bFindStartpoint == false; i++)
{
for (j = 0 ; j< image->width && bFindStartpoint == false; j++)
{
ptr = (unsigned char *)(image->imageData + i*image->widthStep + j);
if (*ptr == 255)
{
startPoint.x = j;
startPoint.y = i;
bFindStartpoint = true;
//cout<<"x: " << j <<"y : " <imageData + (currentPoint.y + directions[beginDirection][1])* image->widthStep + currentPoint.x + directions[beginDirection][0]);
if (*ptr == 255)
{
bFindBoardpoint = true;
currentPoint.x += directions[beginDirection][0];
currentPoint.y += directions[beginDirection][1];
/************************************************************************/
/* 此处添加序列存储的代码 */
/************************************************************************/
//一、将边界存储到图片中
dst = (unsigned char *)image2->imageData + currentPoint.y * image2->widthStep + currentPoint.x;
*dst = 255;
//二、将边界点的序列存储到一个堆栈中
board.push(currentPoint.x);
board.push(currentPoint.y);
if (currentPoint.x == startPoint.x && currentPoint.y == startPoint.y )
{
bFindStartpoint = true;
}
//改变下次首先开始扫描的方向
beginDirection -= 2;
if (beginDirection < 0)
{
beginDirection += 8;
}
}
else
{
beginDirection ++;
beginDirection = beginDirection%8;
}
}
//cout<<"currentPoint "<
/************************************************************************/
/* 轮廓跟踪算法获取物体的轮廓序列 生成边界链码 */
/************************************************************************/
#include
#include
#include
#include
using namespace std;
int main(){
IplImage * image,*image2,*image3;
image = cvLoadImage("E:\\image\\bottle2.tif",0);
cvNamedWindow("image",1);
cvShowImage("image",image);
image2 = cvCreateImage(cvSize(image->width, image->height),image->depth,1);
image3 = cvCreateImage(cvSize(image->width, image->height),image->depth,1);
cvZero(image2);//image2 赋值为0
cvZero(image3);
//寻找区域的左上角点
CvPoint startPoint = cvPoint(0,0);
bool bFindStartpoint = false;
int i ,j;
unsigned char * ptr,*dst;
stack board;//奇数位存储x坐标,偶数位存储y坐标
//当前扫描点
CvPoint currentPoint = cvPoint(0,0);
//邻域的8个点的方向
int directions[8][2] = {{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
int beginDirection = 0;
bool bFindBoardpoint = false;//寻找到邻域的边界点的判定
for (i = 0 ; i< image->height && bFindStartpoint == false; i++)
{
for (j = 0 ; j< image->width && bFindStartpoint == false; j++)
{
ptr = (unsigned char *)(image->imageData + i*image->widthStep + j);
if (*ptr == 255)
{
startPoint.x = j;
startPoint.y = i;
bFindStartpoint = true;
//cout<<"x: " << j <<"y : " <imageData + (currentPoint.y + directions[beginDirection][1])* image->widthStep + currentPoint.x + directions[beginDirection][0]);
if (*ptr == 255)
{
bFindBoardpoint = true;
currentPoint.x += directions[beginDirection][0];
currentPoint.y += directions[beginDirection][1];
/************************************************************************/
/* 此处添加序列存储的代码 */
/************************************************************************/
//一、将边界存储到图片中
dst = (unsigned char *)image2->imageData + currentPoint.y * image2->widthStep + currentPoint.x;
*dst = 255;
//二、将边界点的序列存储到一个堆栈中
board.push(currentPoint.x);
board.push(currentPoint.y);
if (currentPoint.x == startPoint.x && currentPoint.y == startPoint.y )
{
bFindStartpoint = true;
}
//改变下次首先开始扫描的方向
beginDirection -= 2;
if (beginDirection < 0)
{
beginDirection += 8;
}
}
else
{
beginDirection ++;
beginDirection = beginDirection%8;
}
}
//cout<<"currentPoint "<imageData + i*image->widthStep + j;
*ptr = 255;
}
cvNamedWindow("image3",1);
cvSaveImage("E:\\image\\bottle2lianma.bmp",image3);
cvShowImage("image3",image3);
cvWaitKey(0);
return 0;
}
转自http://blog.csdn.net/renshengrumenglibing/article/details/7251072