图像的阈值分割技术

全局阈值分割


#include "BMP.h"


BMP_Image* GlobalSegmentation(BMP_Image* Image_In)
{

    BMP_Image* Image_GlobalSegmentation;
  	Image_GlobalSegmentation = (BMP_Image*)malloc(sizeof(BMP_Image));


    Image_GlobalSegmentation->width = Image_In->width;
    Image_GlobalSegmentation->height = Image_In->height;
	Image_GlobalSegmentation->biBitCount = 1;
	Image_GlobalSegmentation->imageRgbQuad = (BMP_RgbQuad*)malloc(sizeof(BMP_RgbQuad)*2);
	Image_GlobalSegmentation->imageData = 	(unsigned char*)malloc(Image_In->height*Image_In->width/8);

	 Image_GlobalSegmentation->imageRgbQuad[0].rgbBlue =     0;
	 Image_GlobalSegmentation->imageRgbQuad[0].rgbGreen = 	 0;
	 Image_GlobalSegmentation->imageRgbQuad[0].rgbRed = 	 0;
	 Image_GlobalSegmentation->imageRgbQuad[0].rgbReserved = 0;

	 Image_GlobalSegmentation->imageRgbQuad[1].rgbBlue = 	 255;
	 Image_GlobalSegmentation->imageRgbQuad[1].rgbGreen = 	 255;
	 Image_GlobalSegmentation->imageRgbQuad[1].rgbRed =	     255;
	 Image_GlobalSegmentation->imageRgbQuad[1].rgbReserved = 0;

	int i,tmp,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6,tmp7,tmp8;

    int Th=135;


    for (i=0; i<Image_In->height*Image_In->width; i=i+8)
    {
		/********* Begin *********/
        if(Image_In->imageData[i] > Th)    tmp1 = 1; else tmp1 = 0;
        if(Image_In->imageData[i+1] > Th)  tmp2 = 1; else tmp2 = 0;
        if(Image_In->imageData[i+2] > Th)  tmp3 = 1; else tmp3 = 0;
        if(Image_In->imageData[i+3] > Th)  tmp4 = 1; else tmp4 = 0;
        if(Image_In->imageData[i+4] > Th)  tmp5 = 1; else tmp5 = 0;
        if(Image_In->imageData[i+5] > Th)  tmp6 = 1; else tmp6 = 0;
        if(Image_In->imageData[i+6] > Th)  tmp7 = 1; else tmp7 = 0;
        if(Image_In->imageData[i+7] > Th)  tmp8 = 1; else tmp8 = 0;
        tmp = tmp1*128 + tmp2*64 + tmp3*32 + tmp4*16 + tmp5*8 + tmp6*4  + tmp7*2  + tmp8*1;
        Image_GlobalSegmentation->imageData[i/8] = tmp;



		/********* End *********/
	}
    return Image_GlobalSegmentation;
}

局部阈值分割


#include "BMP.h"


BMP_Image* LocalSegmentation(BMP_Image* Image_In)
{

    BMP_Image* Image_LocalSegmentation;
  	Image_LocalSegmentation = (BMP_Image*)malloc(sizeof(BMP_Image));


    Image_LocalSegmentation->width = Image_In->width;
    Image_LocalSegmentation->height = Image_In->height;
	Image_LocalSegmentation->biBitCount = 1;
	Image_LocalSegmentation->imageRgbQuad = (BMP_RgbQuad*)malloc(sizeof(BMP_RgbQuad)*2);
	Image_LocalSegmentation->imageData = 	(unsigned char*)malloc(Image_In->height*Image_In->width/8);

	 Image_LocalSegmentation->imageRgbQuad[0].rgbBlue =     0;
	 Image_LocalSegmentation->imageRgbQuad[0].rgbGreen = 	 0;
	 Image_LocalSegmentation->imageRgbQuad[0].rgbRed = 	 0;
	 Image_LocalSegmentation->imageRgbQuad[0].rgbReserved = 0;

	 Image_LocalSegmentation->imageRgbQuad[1].rgbBlue = 	 255;
	 Image_LocalSegmentation->imageRgbQuad[1].rgbGreen = 	 255;
	 Image_LocalSegmentation->imageRgbQuad[1].rgbRed =	     255;
	 Image_LocalSegmentation->imageRgbQuad[1].rgbReserved = 0;

	int i,j,tmp,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6,tmp7,tmp8;

    int Th1=135;
    int Th2=195;
    int Th3=55;
    int Th4=95;

    for (i=0; i<Image_In->height/2; i++)
	    for (j=0; j<Image_In->width/2; j=j+8)
    {
			/********* Begin *********/
			//¶Ô×óÉϽǵÄͼƬ½øÐд¦Àí
            if(Image_In->imageData[i*Image_In->width+j] > Th1)    tmp1 = 1; else tmp1 = 0;
            if(Image_In->imageData[i*Image_In->width+j+1] > Th1)  tmp2 = 1; else tmp2 = 0;
            if(Image_In->imageData[i*Image_In->width+j+2] > Th1)  tmp3 = 1; else tmp3 = 0;
            if(Image_In->imageData[i*Image_In->width+j+3] > Th1)  tmp4 = 1; else tmp4 = 0;
            if(Image_In->imageData[i*Image_In->width+j+4] > Th1)  tmp5 = 1; else tmp5 = 0;
            if(Image_In->imageData[i*Image_In->width+j+5] > Th1)  tmp6 = 1; else tmp6 = 0;
            if(Image_In->imageData[i*Image_In->width+j+6] > Th1)  tmp7 = 1; else tmp7 = 0;
            if(Image_In->imageData[i*Image_In->width+j+7] > Th1)  tmp8 = 1; else tmp8 = 0;
            tmp = tmp1*128 + tmp2*64 + tmp3*32 + tmp4*16 + tmp5*8 + tmp6*4  + tmp7*2  + tmp8*1;
            Image_LocalSegmentation->imageData[(i*Image_In->width+j)/8] = tmp;



			/********* End *********/
	}

    for (i=0; i<Image_In->height/2; i++)
	    for (j=Image_In->width/2; j<Image_In->width; j=j+8)
    {
			/********* Begin *********/
			//¶ÔÓÒÉϽǵÄͼƬ½øÐд¦Àí
            if(Image_In->imageData[i*Image_In->width+j] > Th2)    tmp1 = 1; else tmp1 = 0;
            if(Image_In->imageData[i*Image_In->width+j+1] > Th2)  tmp2 = 1; else tmp2 = 0;
            if(Image_In->imageData[i*Image_In->width+j+2] > Th2)  tmp3 = 1; else tmp3 = 0;
            if(Image_In->imageData[i*Image_In->width+j+3] > Th2)  tmp4 = 1; else tmp4 = 0;
            if(Image_In->imageData[i*Image_In->width+j+4] > Th2)  tmp5 = 1; else tmp5 = 0;
            if(Image_In->imageData[i*Image_In->width+j+5] > Th2)  tmp6 = 1; else tmp6 = 0;
            if(Image_In->imageData[i*Image_In->width+j+6] > Th2)  tmp7 = 1; else tmp7 = 0;
            if(Image_In->imageData[i*Image_In->width+j+7] > Th2)  tmp8 = 1; else tmp8 = 0;
            tmp = tmp1*128 + tmp2*64 + tmp3*32 + tmp4*16 + tmp5*8 + tmp6*4  + tmp7*2  + tmp8*1;
            Image_LocalSegmentation->imageData[(i*Image_In->width+j)/8] = tmp;



			/********* End *********/
	}

    for (i=Image_In->height/2; i<Image_In->height; i++)
	    for (j=0; j<Image_In->width/2; j=j+8)
    {
			/********* Begin *********/
			//¶Ô×óϽǵÄͼƬ½øÐд¦Àí
            if(Image_In->imageData[i*Image_In->width+j] > Th3)    tmp1 = 1; else tmp1 = 0;
            if(Image_In->imageData[i*Image_In->width+j+1] > Th3)  tmp2 = 1; else tmp2 = 0;
            if(Image_In->imageData[i*Image_In->width+j+2] > Th3)  tmp3 = 1; else tmp3 = 0;
            if(Image_In->imageData[i*Image_In->width+j+3] > Th3)  tmp4 = 1; else tmp4 = 0;
            if(Image_In->imageData[i*Image_In->width+j+4] > Th3)  tmp5 = 1; else tmp5 = 0;
            if(Image_In->imageData[i*Image_In->width+j+5] > Th3)  tmp6 = 1; else tmp6 = 0;
            if(Image_In->imageData[i*Image_In->width+j+6] > Th3)  tmp7 = 1; else tmp7 = 0;
            if(Image_In->imageData[i*Image_In->width+j+7] > Th3)  tmp8 = 1; else tmp8 = 0;
            tmp = tmp1*128 + tmp2*64 + tmp3*32 + tmp4*16 + tmp5*8 + tmp6*4  + tmp7*2  + tmp8*1;
            Image_LocalSegmentation->imageData[(i*Image_In->width+j)/8] = tmp;



			/********* End *********/
	}

    for (i=Image_In->height/2; i<Image_In->height; i++)
	    for (j=Image_In->width/2; j<Image_In->width; j=j+8)
    {
			/********* Begin *********/
			//¶ÔÓÒϽǵÄͼƬ½øÐд¦Àí
            if(Image_In->imageData[i*Image_In->width+j] > Th4)    tmp1 = 1; else tmp1 = 0;
            if(Image_In->imageData[i*Image_In->width+j+1] > Th4)  tmp2 = 1; else tmp2 = 0;
            if(Image_In->imageData[i*Image_In->width+j+2] > Th4)  tmp3 = 1; else tmp3 = 0;
            if(Image_In->imageData[i*Image_In->width+j+3] > Th4)  tmp4 = 1; else tmp4 = 0;
            if(Image_In->imageData[i*Image_In->width+j+4] > Th4)  tmp5 = 1; else tmp5 = 0;
            if(Image_In->imageData[i*Image_In->width+j+5] > Th4)  tmp6 = 1; else tmp6 = 0;
            if(Image_In->imageData[i*Image_In->width+j+6] > Th4)  tmp7 = 1; else tmp7 = 0;
            if(Image_In->imageData[i*Image_In->width+j+7] > Th4)  tmp8 = 1; else tmp8 = 0;
            tmp = tmp1*128 + tmp2*64 + tmp3*32 + tmp4*16 + tmp5*8 + tmp6*4  + tmp7*2  + tmp8*1;
            Image_LocalSegmentation->imageData[(i*Image_In->width+j)/8] = tmp;



			/********* End *********/
	}

    return Image_LocalSegmentation;
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值