为了简单,该程序只能处理256级灰度图像!
开发环境:WindowsXP(sp1) + VC6(sp6) + Matlab7.0(sp1)
//调用matlab图像处理工具箱的canny边缘检测
void CImgDemoView::Oncalliptoolboxcanny()
{
int i,j;
unsigned char *lpSrc;
CImgDemoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if(pDoc->m_hDIB == NULL)
return ;
LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->m_hDIB);
LPSTR lpDIBBits=::FindDIBBits (lpDIB);
//读取图像宽高
int cxDIB = (int) ::DIBWidth(lpDIB); // Size of DIB - x
int cyDIB = (int) ::DIBHeight(lpDIB); // Size of DIB - y
long lLineBytes = WIDTHBYTES(cxDIB * 8); // 计算图像每行的字节数
//分配一临时存储空间
unsigned char * temp=new unsigned char[cxDIB*cyDIB*sizeof(unsigned char)];
//先将图像读入临时缓冲区temp以便于后期送入matlab引擎进行处理
//每行
for(i = 0; i < cyDIB; i++)
{
// 每列
for(j = 0; j < cxDIB; j++)
{
// 指向DIB第i行,第j个象素的指针
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (cyDIB - 1 - i) + j;
temp[j*cyDIB+i]=(*lpSrc);
}
}
mclInitializeApplication(NULL,0);
//定义matlab可以识别的矩阵作为输入和输出
mxArray * input=NULL,* output=NULL;
//为其分配内存空间
input=mxCreateNumericMatrix(cyDIB,cxDIB,mxUINT8_CLASS,mxREAL);
output=mxCreateNumericMatrix(cyDIB,cxDIB,mxUINT8_CLASS,mxREAL);
//将temp区数据赋给matlab可以识别的矩阵形式
memcpy((unsigned char *)mxGetPr(input),(unsigned char *)temp,cxDIB*cyDIB*sizeof(unsigned char));
//将输入input和输出output与matlab中变量关联
engPutVariable(ep,"input",input);
//调用matlab工具箱函数进行canny边缘检测
//注意要使用im2uint8将二值边缘图像转换为uint8格式
engEvalString(output=edge(input,'canny');output=im2uint8(output);");
output=engGetVariable(ep,"output");//提取变量
//将边缘检测的结果传回临时缓存区temp
memcpy((unsigned char *)temp,(unsigned char *)mxGetPr(output),cxDIB*cyDIB*sizeof(unsigned char));
//将临时缓冲区temp数据写入图像
//每行
for(i = 0; i < cyDIB; i++)
{
// 每列
for(j = 0; j < cxDIB; j++)
{
// 指向DIB第i行,第j个象素的指针
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (cyDIB - 1 - i) + j;
(*lpSrc)=temp[j*cyDIB+i];
}
}
//释放内存
mxDestroyArray(input); input=0;
mxDestroyArray(output); output=0;
mclTerminateApplication();
//刷新客户区
::GlobalUnlock((HGLOBAL) pDoc->m_hDIB);
Invalidate(TRUE);
}
void CImgDemoView::Oncalliptoolboxhistoeq()
{
int i,j;
unsigned char *lpSrc;
CImgDemoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if(pDoc->m_hDIB == NULL)
return ;
LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->m_hDIB);
LPSTR lpDIBBits=::FindDIBBits (lpDIB);
//读取图像宽高
int cxDIB = (int) ::DIBWidth(lpDIB); // Size of DIB - x
int cyDIB = (int) ::DIBHeight(lpDIB); // Size of DIB - y
long lLineBytes = WIDTHBYTES(cxDIB * 8); // 计算图像每行的字节数
//分配一临时存储空间
unsigned char * temp=new unsigned char[cxDIB*cyDIB*sizeof(unsigned char)];
//先将图像读入临时缓冲区temp以便于后期送入matlab引擎进行处理
//每行
for(i = 0; i < cyDIB; i++)
{
// 每列
for(j = 0; j < cxDIB; j++)
{
// 指向DIB第i行,第j个象素的指针
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (cyDIB - 1 - i) + j;
temp[j*cyDIB+i]=(lpSrc);
}
}
mclInitializeApplication(NULL,0);
//定义matlab可以识别的矩阵作为输入和输出
mxArray * input=NULL,* output=NULL;
//为其分配内存空间
input=mxCreateNumericMatrix(cyDIB,cxDIB,mxUINT8_CLASS,mxREAL);
output=mxCreateNumericMatrix(cyDIB,cxDIB,mxUINT8_CLASS,mxREAL);
//将temp区数据赋给matlab可以识别的矩阵形式
memcpy((unsigned char *)mxGetPr(input),(unsigned char *)temp,cxDIB*cyDIB*sizeof(unsigned char));
//将输入input和输出output与matlab中变量关联
engPutVariable(ep,"input",input);
//调用matlab工具箱函数进行canny边缘检测
//注意要使用im2uint8将二值边缘图像转换为uint8格式
engEvalString(ep,"output=histeq(input);output=im2uint8(output);");
output=engGetVariable(ep,"output");//提取变量
//将边缘检测的结果传回临时缓存区temp
memcpy((unsigned char temp,(unsigned char *)mxGetPr(output),cxDIB*cyDIB*sizeof(unsigned char));
//将临时缓冲区temp数据写入图像
//每行
for(i = 0; i < cyDIB; i++)
{
// 每列
for(j = 0; j < cxDIB; j++)
{
// 指向DIB第i行,第j个象素的指针
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (cyDIB - 1 - i) + j;
(*lpSrc)=temp[j*cyDIB+i];
}
}
//释放内存
mxDestroyArray(input); input=0;
mxDestroyArray(output); output=0;
mclTerminateApplication()
//刷新客户区
::GlobalUnlock((HGLOBAL) pDoc->m_hDIB);
Invalidate(TRUE);
}
void CImgDemoView::Oncalliptoolboxaddsaltnoise()
{
int i,j;
unsigned char *lpSrc;
CImgDemoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if(pDoc->m_hDIB == NULL)
return ;
LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->m_hDIB);
LPSTR lpDIBBits=::FindDIBBits (lpDIB);
//读取图像宽高
int cxDIB = (int) ::DIBWidth(lpDIB); // Size of DIB - x
int cyDIB = (int) ::DIBHeight(lpDIB); // Size of DIB - y
long lLineBytes = WIDTHBYTES(cxDIB * 8); // 计算图像每行的字节数
//分配一临时存储空间
unsigned char * temp=new unsigned char[cxDIB*cyDIB*sizeof(unsigned char)];
//先将图像读入临时缓冲区temp以便于后期送入matlab引擎进行处理
//每行
for(i = 0; i < cyDIB; i++)
{
// 每列
for(j = 0; j < cxDIB; j++)
{
// 指向DIB第i行,第j个象素的指针
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (cyDIB - 1 - i) + j;
temp[j*cyDIB+i]=(*lpSrc);
}
}
mclInitializeApplication(NULL,0);
//定义matlab可以识别的矩阵作为输入和输出
mxArray * input=NULL,* output=NULL;
//为其分配内存空间
input=mxCreateNumericMatrix(cyDIB,cxDIB,mxUINT8_CLASS,mxREAL);
output=mxCreateNumericMatrix(cyDIB,cxDIB,mxUINT8_CLASS,mxREAL);
//将temp区数据赋给matlab可以识别的矩阵形式
memcpy((unsigned char )mxGetPr(input),(unsigned char *)temp,cxDIB*cyDIB*sizeof(unsigned char));
//将输入input和输出output与matlab中变量关联
engPutVariable(ep,"input",input);
//调用matlab工具箱函数进行canny边缘检测
//注意要使用im2uint8将二值边缘图像转换为uint8格式
engEvalString(ep,"output=imnoise(input,'salt & pepper',0.02);output=im2uint8(output);");
output=engGetVariable(ep,"output");//提取变量
//将边缘检测的结果传回临时缓存区temp
memcpy((unsigned char *)temp,(unsigned char *)mxGetPr(output),cxDIB*cyDIB*sizeof(unsigned char));
//将临时缓冲区temp数据写入图像
//每行
for(i = 0; i < cyDIB; i++)
{
// 每列
for(j = 0; j < cxDIB; j++)
{
// 指向DIB第i行,第j个象素的指针
lpSrc = (unsigned char)lpDIBBits + lLineBytes * (cyDIB - 1 - i) + j;
(*lpSrc)=temp[j*cyDIB+i];
}
}
//释放内存
mxDestroyArray(input); input=0;
mxDestroyArray(output); output=0;
mclTerminateApplication();
//刷新客户区
::GlobalUnlock((HGLOBAL) pDoc->m_hDIB);
Invalidate(TRUE);
}