from: 魔棒工具--RegionGrow算法简介

struct Node
{
int x;
int y;
Node* next;
};
void MyTreasureBox::RegionGrow(const IplImage* src, IplImage* dst, int seedx, int seedy, int threshold, bool flag)
{
if(!src || src->nChannels != 1)return ;
int width = src->width;
int height = src->height;
int srcwidthstep = src->widthStep;
uchar* img = (uchar*)src->imageData;
//成长区域
cvZero(dst);
//标记每个像素点是否被计算过
IplImage* M = cvCreateImage(cvSize(width, height), 8, 1);
int Mwidthstep = M->widthStep;
cvZero(M);
M->imageData[seedy * Mwidthstep + seedx] = 1; //种子点位置为1,其它位置为0
CvScalar cur = CV_RGB(255,255,255);
cvSet2D(dst, seedy, seedx, cur);
//队列的两端
int start = 0;
int end = 1;
Node *queue = new Node;
queue->x = seedx;
queue->y = seedy;
queue->next = NULL;
Node *first = queue;
Node *last = queue;
while (end - start > 0)
{
int x = first->x;
int y = first->y;
uchar pixel = (uchar)img[y * srcwidthstep + x];
for (int yy = -1; yy<=1; yy++)
{
for (int xx = -1; xx<=1; xx++)
{
if(flag)
if ( abs(yy) && abs(xx))
continue;
int cx = x + xx;
int cy = y + yy;
if (cx >= 0 && cx <width && cy >=0 && cy < height)
{
if (abs(img[cy * srcwidthstep + cx] - pixel) <= threshold && M->imageData[cy * Mwidthstep + cx] != 1)
{
Node *node = new Node;
node->x = cx;
node->y = cy;
node->next = NULL;
end++;
last->next = node;
last = node;
M->imageData[cy * Mwidthstep + cx] = 1;
cvSet2D(dst, cy, cx, cur);
}
}
}
}
Node* temp = first;
first = first->next;
delete temp;
start++;
}
cvReleaseImage(&M);
}

2652



被折叠的 条评论
为什么被折叠?



