图像分割(阈值分割和区域生长)vc++代码(5)
时间:2025-04-09
时间:2025-04-09
//获取图像信息 height=pImg->height; width=pImg->width; step=pImg->widthStep; channels=pImg->nChannels; data=(uchar *)pImg->imageData;
//printf("%d \n%d",width,step);
//创建一张与原图大小一样的全黑的图像,用于显示生长范围
int depth=pImg->depth; IplImage* pImg2=cvCreateImage(cvSize(width,height),depth,channels); pImg2->origin=0; cvZero(pImg2);
uchar * data2=(uchar *)pImg2->imageData;
cvNamedWindow( "win1", 1);//创建窗口
cvShowImage( "win1", pImg );//显示图像
EnQu(q,height/2);
EnQu(p,width/2);
while(q->front<q->rear) { i=q->qu[q->front]; j=p->qu[p->front]; if(abs(data[(i-1)*step+j]-data[i*step+j])<18&&(data2[(i-1)*step+j]==0)&&(i-1>0)&&(i-1<height)&&(j>0)&&(j<width))
{
data2[(i-1)*step+j]=255;
EnQu(q,i-1);
EnQu(p,j);
}
if(abs(data[(i+1)*step+j]-data[i*step+j])<18&&(data2[(i+1)*step+j]==0)&&(i+1>0)&&(i+1<height)&&(j>0)&&(j<width))
{data2[(i+1)*step+j]=255;
EnQu(q,i+1);
EnQu(p,j);
}
if(abs(data[i*step+j+1]-data[i*step+j])<18&&(data2[(i)*step+j+1]==0)&&(i>0)&&(i<height)&&(j+1>0)&&(j+1<width))
{
data2[i*step+j+1]=255;