栈实现种子区域生长法代码如下:
#include "stdafx.h" #include "highgui.h" #include <iostream> #include "cv.h" //#include <stack> //头文件里有栈顶指针 using namespace std; using namespace cv; #define maxsize 5000 //typedef struct//保存种子像素 //可以学习下用结构体来存放坐标 //{ // int x; // int y; //}seedpoint; void Grow(IplImage* src,IplImage* src1, int t1); 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) { ////if(seedd.empty()) //// printf("TRUE"); ////else //// printf("false"); ////getchar(); //src存放原图像,src1存放区域生长后的图像 lab-k-f-one 2lab-k IplImage* src = cvLoadImage("lab-k-f-one.jpg",0); IplImage* src1 = cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1); src1->origin=0; cvZero(src1); //cout<<"q="<<q<<endl; Grow( src, src1, 20); //cout<<"q1="<<q<<endl; cvNamedWindow("原图片"); cvNamedWindow("区域生长图片"); cvShowImage("原图片",src); cvShowImage("区域生长图片",src1); cvSaveImage("one区域.jpg",src1); cvReleaseImage(&src); cvReleaseImage(&src1); cvWaitKey(0); Mat I=imread("one区域.jpg"); cvtColor(I,I,CV_BGR2GRAY); Mat contours; Canny(I,contours,125,350); threshold(contours,contours,128,255,THRESH_BINARY); namedWindow("原图"); imshow("原图",I); namedWindow("one区域轮廓"); imshow("one区域轮廓",contours); imwrite("one区域轮廓.jpg",contours); waitKey(); } void Grow(IplImage* src,IplImage* src1, int t1) { mystack stackx; //两个栈分别存放X Y坐标 mystack stacky; initstack( & stackx); //初始化 initstack( & stacky); static int nDx[]={-1,0,1,0}; static int nDy[]={0,1,0,-1}; int height = src->height; int width = src->width; int step = src->widthStep;//每行所占字节数 int seedx,seedy; ////cout<<height<<endl; ////cout<<width<<endl; ////cout<<step<<endl; uchar* src1_data =(uchar*)src1->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<<point.x<<endl; for (int k=0; k<4; k++) { // 4邻域象素的坐标 xx = nCurrX + nDx[k]; yy = nCurrY + nDy[k]; if ( (xx < width) && (xx>=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); src1_data[yy*step+xx] = 255 ; /// } } } }
</pre><pre code_snippet_id="388778" snippet_file_name="blog_20140612_3_4592399" name="code" class="html">