区域生长代码修改,该如何解决
区域生长代码修改
代码贴上,结果不对啊,球大婶路过帮忙修改啊 ,嘎嘎
------解决方案--------------------
unsigned char * pUnRegion = (unsigned char * )img->imageData;
memset(pUnRegion,0,sizeof(unsigned char)*nWidth*nHeight);
修改为:
unsigned char * pUnRegion = new unsigned char[nWidth*nHeight];
memset(pUnRegion,0,sizeof(unsigned char)*nWidth*nHeight);
最后:delete []pUnRegion ;
试试看。
代码贴上,结果不对啊,球大婶路过帮忙修改啊 ,嘎嘎
- C/C++ code
#include "highgui.h" #include "cv.h" #include <cvaux.h> /************************************************************************ * /说明 * 对应象素为生长区域,表示为非生长区域 * 区域生长一般包含三个比较重要的问题: * 1. 种子点的选取 * 2. 生长准则 * 3. 终止条件 * 可以认为,这三个问题需要具体分析,而且每个问题解决的好坏直接关系到 * 区域生长的结果。 * 本函数的种子点选取为图像的中心,生长准则是相邻象素的象素值小于 * nThreshold, ()终止条件是一直进行到再没有满足生长准则需要的象素时为 ************************************************************************* */ // 在这个代码中,它认为这张图片就是一个区域,选取了中间点为种子点。 void RegionGrow(IplImage *img, int nThreshold) { static int nDx[]={-1,0,1,0}; static int nDy[]={0,1,0,-1}; nThreshold = 20; // 图象的长宽大小 int nWidth = img->width; int nHeight = img->height ; // 图像在计算机在存储中的实际大小 //CSize sizeImageSave = ((nWidth+3)/4 )*4 ; // 图像在内存中每一行象素占用的实际空间 int nSaveWidth = img->widthStep ; // 初始化 unsigned char * pUnRegion = (unsigned char * )img->imageData; memset(pUnRegion,0,sizeof(unsigned char)*nWidth*nHeight); //设置种子点为图像的中心 int nSeedX = nWidth /2 , nSeedY = nHeight/2 ; // 定义堆栈,存储坐标并分配空间 int * pnGrowQueX = new int [nWidth*nHeight]; int * pnGrowQueY = new int [nWidth*nHeight]; // 图像数据的指针 unsigned char * pUnchInput =(unsigned char * )img->imageData; // 定义堆栈的起点和终点 // 当nStart=nEnd, 表示堆栈中只有一个点 int nStart = 0; int nEnd = 0 ; // 把种子点的坐标压入栈 pnGrowQueX[nEnd] = nSeedX; pnGrowQueY[nEnd] = nSeedY; // 设置当前正在处理的象素 int nCurrX = 0; int nCurrY = 0; // 循环控制变量 int k = 0; // 图象的横纵坐标,用来对当前象素的邻域进行遍历 int xx = 0; int yy = 0; while (nStart <= nEnd) { // 当前种子点的坐标 nCurrX = pnGrowQueX[nStart]; nCurrY = pnGrowQueY[nStart]; // 对当前点的邻域进行遍历 for (k = 0; k < 4; ++k) { // 4邻域象素的坐标 xx = nCurrX + nDx[k]; yy = nCurrY + nDy[k]; // 判断象素(xx,yy) 是否在图像内部 // 判断象素(xx,yy) 是否已经处理过 // pUnRegion[yy*nWidth+xx]==0 表示还没有处理 // 生长条件:判断象素(xx,yy)和当前象素(nCurrX,nCurrY) 象素值差的绝对值 if ( (xx < nWidth) && (xx>=0) && (yy<nHeight) && (yy>=0) && (pUnRegion[yy*nWidth+xx]==0) && abs(pUnchInput[yy*nSaveWidth+xx] - pUnchInput[nCurrY*nSaveWidth+nCurrX])<nThreshold ) { // 堆栈的尾部指针后移一位 nEnd++; // 象素(xx,yy) 压入栈 pnGrowQueX[nEnd] = xx; pnGrowQueY[nEnd] = yy; // 把象素(xx,yy)设置成逻辑() // 同时也表明该象素处理过 pUnRegion[yy*nWidth+xx] = 255 ; } } nStart++; } // 释放内存 delete []pnGrowQueX; delete []pnGrowQueY; pnGrowQueX = NULL ; pnGrowQueY = NULL ; img->imageData = (char *)pUnRegion; } int main() { IplImage *img = cvLoadImage("E:\\111.jpg"); RegionGrow(img,10); cvShowImage("区域生长",img);//载入转化后的图像 cvWaitKey(0); }
------解决方案--------------------
unsigned char * pUnRegion = (unsigned char * )img->imageData;
memset(pUnRegion,0,sizeof(unsigned char)*nWidth*nHeight);
修改为:
unsigned char * pUnRegion = new unsigned char[nWidth*nHeight];
memset(pUnRegion,0,sizeof(unsigned char)*nWidth*nHeight);
最后:delete []pUnRegion ;
试试看。