编程实现二值图的轮廓提取,点击菜单以后,图像没有任何反应,想让高手给看看哪里编写错了,谢谢!个人感觉是图像灰度的获取有问题,pixel[(j+1)*width+i-1]算法是:挖空内部像素点即可。亮点的8个相邻像素点全部为亮点,则该点为内部点,反之为轮廓点。将所有内部点置为背景点,完成轮廓提取。下面是我的程序:/******************************************************************************
* 作用: 实现轮廓提取
* 参数:
* pixel 原始像素数组
* tempPixel 保存变换后图像的像素数组
* width 原始图像宽度
* height 原始图像高度
******************************************************************************/void CMyfunView::FindContour(BYTE* pixel , BYTE* tempPixel , UINT width , UINT height)
{
int i,j,n1,n2,n3,n4,n5,n6,n7,n8; int size=width*height*4;
tempPixel=new BYTE[size];
//初始化新分配的内存为
memset(tempPixel,255,size); for(j=1;j<height-1;j++)
for(i=1;i<width-1;i++)
{
if(pixel[j*width+i]==0)
{
tempPixel[j*width+i]=0;
n1=pixel[(j+1)*width+i-1];
n2=pixel[(j+1)*width+i];
n3=pixel[(j+1)*width+i+1];
n4=pixel[j*width+i-1];
n5=pixel[j*width+i+1];
n6=pixel[(j-1)*width+i-1];
n7=pixel[(j-1)*width+i];
n8=pixel[(j-1)*width+i+1]; //如果相邻的个点都是黑点
if(n1+n2+n3+n4+n5+n6+n7+n8==0)
tempPixel[j*width+i]=255;
}
else
tempPixel[j*width+i]=pixel[j*width+i];
}
}//下面的是我点击菜单的函数
void CMyfunView::OnConExtract()
{
// TODO: 在此添加命令处理程序代码 FindContour(m_pImageBuffer, m_pImageTempBuffer,m_nPicWidth, m_nPicHeight);
memcpy(m_pImageBuffer, m_pImageTempBuffer,m_nPicWidth*m_nPicHeight*4);
Invalidate();
}
* 作用: 实现轮廓提取
* 参数:
* pixel 原始像素数组
* tempPixel 保存变换后图像的像素数组
* width 原始图像宽度
* height 原始图像高度
******************************************************************************/void CMyfunView::FindContour(BYTE* pixel , BYTE* tempPixel , UINT width , UINT height)
{
int i,j,n1,n2,n3,n4,n5,n6,n7,n8; int size=width*height*4;
tempPixel=new BYTE[size];
//初始化新分配的内存为
memset(tempPixel,255,size); for(j=1;j<height-1;j++)
for(i=1;i<width-1;i++)
{
if(pixel[j*width+i]==0)
{
tempPixel[j*width+i]=0;
n1=pixel[(j+1)*width+i-1];
n2=pixel[(j+1)*width+i];
n3=pixel[(j+1)*width+i+1];
n4=pixel[j*width+i-1];
n5=pixel[j*width+i+1];
n6=pixel[(j-1)*width+i-1];
n7=pixel[(j-1)*width+i];
n8=pixel[(j-1)*width+i+1]; //如果相邻的个点都是黑点
if(n1+n2+n3+n4+n5+n6+n7+n8==0)
tempPixel[j*width+i]=255;
}
else
tempPixel[j*width+i]=pixel[j*width+i];
}
}//下面的是我点击菜单的函数
void CMyfunView::OnConExtract()
{
// TODO: 在此添加命令处理程序代码 FindContour(m_pImageBuffer, m_pImageTempBuffer,m_nPicWidth, m_nPicHeight);
memcpy(m_pImageBuffer, m_pImageTempBuffer,m_nPicWidth*m_nPicHeight*4);
Invalidate();
}
memcpy(m_pImageBuffer, m_pImageTempBuffer,m_nPicWidth*m_nPicHeight*4);这个函数的参数是不是有问题?
应该不是width,而是位图的实际宽度吧
linewidth=(width*8+31)/32*4;
FindContour(BYTE* pixel , BYTE* &tempPixel , UINT width , UINT height);
允许,tempPixel指针被修改,同时在外面就得自行释放内存,否则每调用一次就内存泄露一次。
正规的做法通常在外部分配、在外部释放内存,内部只是对内存中的数据做修改而已。