源代码:
#include
#include "cv.h"
#include "highgui.h"
using namespace std;
#define maxsize 5000
void Grow(IplImage* src, IplImage* src1, int t);
typedef struct
{
int stack[maxsize];
int top;
}mystack;
void initstack(mystack *s){
s->top = 0;
}
int pushstack(mystack *s, int e)//入栈
{
if (s->top >= maxsize)
return 0;
else
{
s->stack[s->top] = e;
s->top++;
return 1;
}
}
int popstack(mystack *s, int *e)//出栈
{
if (s->top == 0)
return 0;
else
{
s->top--;
*e = s->stack[s->top];
return 1;
}
}
int main(int argc, char**argv)
{
//src存放原图像,src1存放区域生长后的图像 lab-k-f-one 2lab-k
IplImage* src = cvLoadImage("picture.jpg", 0);
IplImage* src1 = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
src1->origin = 0;
cvZero(src1);
//cout<<"q="<height;
int width = src->width;
int step = src->widthStep;//每行所占字节数
int seedx, seedy;
////cout<imageData;
uchar* src_data = (uchar*)src->imageData;
//取区域质心
int m00 = 0, m01 = 0, m10 = 0;
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
m00 += src_data[i*step + j];
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
{
m10 += i*src_data[i*step + j];
m01 += j*src_data[i*step + j];
}
seedx = m10 / m00;
seedy = m01 / m00;
pushstack(&stackx, seedx);
pushstack(&stacky, seedy);
int nCurrX;//当前点
int nCurrY;
int xx;
int yy;
while (stackx.top > 0 && stacky.top>0)
{
//popstack(&stackx,&stackx.top);
//popstack(&stackx,&stackx.top);
nCurrX = stackx.stack[stackx.top - 1]; //返回栈顶数据
nCurrY = stacky.stack[stacky.top - 1];
stackx.top--;
stacky.top--;
//seedd.pop(); //栈顶数据出栈
//cout<= 0) && (yy >= 0) && (yy < height) && (src1_data[yy*step + xx] == 0) && abs(src_data[yy*step + xx] - src_data[nCurrY*step + nCurrX]) < t1)
{
// 堆栈的尾部指针后移一位
// 象素(xx,yy) 压入栈
pushstack(&stackx, xx);
pushstack(&stacky, yy);
// 把象素(xx,yy)设置成逻辑()
// 同时也表明该象素处理过
src1_data[yy*step + xx] = 255;
}
}
}
}
实验效果: