C语言实现对图像的二值化

/*************************************************************************  
    *   该函数用于对图像进行阈值分割运算  
    *   参数:  
    *       LPSTR   lpDIBBits         -   指向源DIB图像指针  
    *       LONG     lWidth               -   源图像宽度(象素数)  
    *       LONG     lHeight             -   源图像高度(象素数)  
    ************************************************************************/  
   
BOOL   ImageChangeProc::ThresholdDIB(LPSTR   lpDIBBits,LONG   lWidth,   LONG   lHeight)  
{  
   
//   指向源图像的指针  
LPSTR lpSrc;  
   
//   指向缓存图像的指针  
LPSTR lpDst;  
   
//   指向缓存DIB图像的指针  
LPSTR lpNewDIBBits;  
HLOCAL   hNewDIBBits;  
   
//循环变量  
long   i;  
long   j;  
   
unsigned   char   pixel;  
long   lHistogram[256];  
   
//阈值,最大灰度值与最小灰度值,两个区域的平均灰度值  
unsigned   char   Threshold,NewThreshold,MaxGrayValue,MinGrayValue,Temp1GrayValue,Temp2GrayValue;  
   
//用于计算区域灰度平均值的中间变量  
long   lP1,lP2,lS1,lS2;  
   
//迭代次数  
int   IterationTimes;  
   
LONG   lLineBytes;  
hNewDIBBits   =   LocalAlloc(LHND,   lWidth   *   lHeight);  
   
if   (hNewDIBBits   ==   NULL)  
{  
//   分配内存失败  
return   FALSE;  
}  
   
//   锁定内存  
lpNewDIBBits   =   (char   *   )LocalLock(hNewDIBBits);  
   
//   初始化新分配的内存  
lpDst   =   (char   *)lpNewDIBBits;  
memset(lpDst,   (BYTE)255,   lWidth   *   lHeight);  
   
lLineBytes   =   WIDTHBYTES(lWidth   *   8);  
   
for   (i   =   0;   i   <   256;i++)  
{  
lHistogram[i]=0;  
}  
   
//获得直方图  
MaxGrayValue   =   0;  
MinGrayValue   =   255;  
for   (i   =   0;i   <   lWidth   ;i++)  
{  
for(j   =   0;j   <   lHeight   ;j++)  
{  
lpSrc   =   (char   *)lpDIBBits   +   lLineBytes   *   j   +   i;  
   
pixel   =   (unsigned   char)*lpSrc;  
   
lHistogram[pixel]++;  
//修改最大,最小灰度值  
if(MinGrayValue   >   pixel)  
{  
MinGrayValue   =   pixel;  
}  
if(MaxGrayValue   <   pixel)  
{  
MaxGrayValue   =   pixel;  
}  
}  
}  
   
//迭代求最佳阈值  
NewThreshold   =   (MinGrayValue   +   MaxGrayValue)/2;  
Threshold   =   0;  
   
for(IterationTimes   =   0;   Threshold   !=   NewThreshold   &&   IterationTimes   <   1000;IterationTimes   ++)  
{  
Threshold   =   NewThreshold;  
lP1   =0;  
lP2   =0;  
lS1   =   0;  
lS2   =   0;  
//求两个区域的灰度平均值  
for   (i   =   MinGrayValue;i   <=Threshold;i++)  
{  
lP1   +=   lHistogram[i]*i;  
lS1   +=   lHistogram[i];  
}  
   
for   (i   =   Threshold+1;i<MaxGrayValue;i++)  
{  
lP2   +=   lHistogram[i]*i;  
lS2   +=   lHistogram[i];  
}  
if(lS1==0||lS2==0)  
{  
//   释放内存  
                          LocalUnlock(hNewDIBBits);  
                          LocalFree(hNewDIBBits);  
                          return   FALSE;  
}  
Temp1GrayValue   =   (unsigned   char)(lP1   /   lS1);  
Temp2GrayValue   =   (unsigned   char)(lP2   /   lS2);  
NewThreshold   =     (Temp1GrayValue   +   Temp2GrayValue)/2;  
}  
   
//根据阈值将图像二值化  
for   (i   =   0;i   <   lWidth   ;i++)  
{  
for(j   =   0;j   <   lHeight   ;j++)  
{  
lpSrc   =   (char   *)lpDIBBits   +   lLineBytes   *   j   +   i;  
lpDst   =   (char   *)lpNewDIBBits   +   lLineBytes   *   j   +   i;  
pixel   =   (unsigned   char)*lpSrc;  
   
if(pixel   <=   Threshold)  
{  
*lpDst   =   (unsigned   char)0;  
}  
else  
{  
*lpDst   =   (unsigned   char)255;  
}  
}  
}  
   
//   复制图像  
memcpy(lpDIBBits,   lpNewDIBBits,   lWidth   *   lHeight);  
   
//   释放内存  
LocalUnlock(hNewDIBBits);  
LocalFree(hNewDIBBits);  
   
//   返回  
return   TRUE;  
}  

http://blog.csdn.net/wdswei/archive/2010/01/05/5128293.aspx

转载于:https://www.cnblogs.com/xinzhuangzi/archive/2010/06/24/4100584.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
图像二值化是一种常用的图像处理技术,通过将图像中的像素按照灰度值进行分类,将灰度值相近的像素合并,使得图像变得更加清晰。腐蚀是一种常用的图像处理技术,通过将图像中的像素按照某种规则进行去除,使得图像变得更加简单。 下面是一个使用C语言实现图像二值化和腐蚀的代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <math.h> #define WIDTH 640 #define HEIGHT 480 // 定义图像数据类型 typedef unsigned char pixel; // 定义一个图像结构体 typedef struct { pixel *data; int width; int height; } image; // 计算一个像素到左上角的距离 int distance(int x, int y, int width, int height) { return sqrt((x - (width - 1)) * (x - (width - 1)) + (y - (height - 1)) * (y - (height - 1))); } // 二值化函数 void binary(image *img) { int i, j; for (i = 1; i < img->height - 1; i++) { for (j = 1; j < img->width - 1; j++) { if (img->data[i * img->width + j] < img->data[(i - 1) * img->width + j] || img->data[i * img->width + j] < img->data[i * img->width + (j - 1)] || img->data[i * img->width + j] < img->data[(i + 1) * img->width + j] || img->data[i * img->width + j] < img->data[(i + 1) * img->width + (j + 1)]) { img->data[i * img->width + j] = 255; // 将该像素设为白色 } else { img->data[i * img->width + j] = 0; // 将该像素设为黑色 } } } } // 腐蚀函数 void erosion(image *img) { int i, j; for (i = 0; i < img->height; i++) { for (j = 0; j < img->width; j++) { if (img->data[i * img->width + j] == 255) { // 如果当前像素为白色(非背景) int x = i - 1; // 向左移动一个像素的位置(不包括当前位置) int y = j - 1; // 向下移动一个像素的位置(不包括当前位置) while (x >= 0 && y >= 0 && img->data[x * img->width + y] == 255) { // 如果向左和向下移动的像素也为白色(非背景)则去掉这个区域中的像素 img->data[(i + x) * img->width + (j + y)]; // 将黑色值填充进来,这里是临时保存区,会根据需求覆盖原始图像数据或覆盖在新的二进制图像上。替换像素的值可能会覆盖到一些不该覆盖的位置,请注意谨慎处理。在这里只简单使用了0替代白色。可能的结果是去除了一些原本不应该去除的区域。这个代码仅供参考,可能需要根据实际需求进行修改。注意:在处理图像数据时,一定要小心不要覆盖到不应该覆盖的位置。如果需要覆盖原始图像数据,请使用malloc分配新的内存空间。在释放内存时,需要使用free释放。此外,请注意C语言中的指针和内存管理。在实际编程中,应该更加小心地处理这些问题。对于需要更复杂的图像处理任务,可能需要使用专门的库或工具。如果可能的话,可以考虑使用Python等高级语言进行图像处理。但是,C语言在某些情况下仍然是非常有用的工具,特别是在需要与硬件交互或需要优化性能的情况下。希望这个示例代码能够帮助你开始使用C语言进行图像处理。如果你有任何问题或需要进一步的帮助,请随时提问。祝你编程愉快!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值