数字图像处理-编程实现染色体计数 C语言实现

调用的框架:http://blog.chinaunix.net/uid-20622737-id-3173056.html 
实验内容: 
对于下面这幅图像,编程实现染色体计数,并附简要处理流程说明。 
这里写图片描述 
处理步骤: 
1. 读取图像,转换为灰度图像

  1. 平滑滤波去噪

  2. 图像二值化

  3. 对图像进行膨胀

  4. 再次膨胀(视情况而定)

  5. 腐蚀(视情况而定)

  6. 反转

  7. 统计连通区域个数并输出

  8. 保存图像。

void main()
{
    Bitmap *bmp = (Bitmap *)malloc(sizeof(Bitmap));// build an empty bmp to put the img.空变量呈放图片
    Bitmap *dstBmp = (Bitmap *)malloc(sizeof(Bitmap));
    Bitmap *graybmp = (Bitmap *)malloc(sizeof(Bitmap));
    Bitmap *smoothbmp = (Bitmap *)malloc(sizeof(Bitmap));
    Bitmap *binbmp = (Bitmap *)malloc(sizeof(Bitmap));
    Bitmap *temp = (Bitmap *)malloc(sizeof(Bitmap));
    int ret;
    char *path ="C:\\Users\\Ali\\Desktop\\1.bmp";
    ret = ReadBitmap(path, bmp);//读取图片
    char *savePath ="C:\\Users\\Ali\\Desktop\\zuizhong.bmp";
    RGB2Gray(bmp,graybmp);//转换成灰度图
    smooth(graybmp,smoothbmp);
    Gray2BW(smoothbmp,binbmp,155);//二值化
    pengzhang(binbmp, dstBmp);  
    pengzhang(dstBmp, temp);
    fushi(temp,dstBmp);
    qufan(dstBmp,temp);
    /*BYTE *bitmap = temp->imageData;
    int width = temp->width;
    int height = temp->height;
    int *labelmap;
    ConnectedComponentLabeling(bitmap, width, height,labelmap); 有待调整 此处是调用本博客的另一篇文章
    二值图像统计连通区域*/
    SaveBitmap(savePath,temp);//保存图片    
}

 

bmp.h

#ifndef BMP_H_INCLUDED
#define BMP_H_INCLUDED

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

/**
 * 位图文件结构及基本函数定义  打开和保存bmp文件
 */
typedef unsigned short WORD;
typedef unsigned long DWORD;
typedef long LONG;
typedef unsigned char BYTE;

/* 位图文件头结构 14字节 */
typedef struct tagBITMAPFILEHEADER {
    WORD bfType;
    DWORD bfSize;
    WORD bfReserved1;
    WORD bfReserved2;
    DWORD bfOffBits;
} BITMAPFILEHEADER;

/* 位图信息头结构 40字节 */
typedef struct tagBITMAPINFOHEADER {
    DWORD biSize; // 结构长度 40B
    LONG biWidth;
    LONG biHeight;
    WORD biPlanes; // 1
    WORD biBitCount; // 表示颜色要用到的位数
    DWORD biCompression; // 压缩格式
    DWORD biSizeImage; // 位图占用字节数=biWidth'(4的整倍数)*biHeight
    LONG biXPelsPerMeter; // 水平分辨率
    LONG biYPelsPerMeter; // 垂直分辨率
    DWORD biClrUsed; // 本图像用到的颜色数
    DWORD biClrImportant; // 本图像的重要颜色数
} BITMAPINFOHEADER;

/* 调色板 4字节 */
typedef struct tagRGBQUAD {
    BYTE rgbBlue;
    BYTE rgbGreen;
    BYTE rgbRed;
    BYTE rgbReserved;
} RGBQUAD;

/* 定义图像信息 */
typedef struct tagBITMAPINFO {
    BITMAPINFOHEADER bmiHeader;
    RGBQUAD bmiColors[1];
} BITMAPINFO;

/* 定义位图图像 */
typedef struct _Bitmap
{
    BITMAPFILEHEADER bmfh;
    BITMAPINFOHEADER bmih;
    int width;
    int height;
    int bitCount;    // 8 或者24
    int imageSize;    // 图像数据大小(imageSize=height*widthStep)字节
    BYTE* imageData;//排列的图像数据
    int widthStep;    //排列的图像行大小
}Bitmap;

/**
 * 位图创建函数  创建一个Bitmap结构,并为图像数据分配空间
 *
 * 使用方法:
 *     Bitmap *bmp=(Bitmap*)malloc(sizeof(Bitmap));
 *     ret=CreateBitmap(bmp,50,50,3);
 */
int CreateBitmap(Bitmap* bmp, int width, int height, int bitCount)
{
    bmp->width=width;
    bmp->height=height;
    bmp->bmih.biWidth=width;
    bmp->bmih.biHeight=height;

    bmp->widthStep=(int)((width*bitCount+31)/32)*4;        //计算排列的宽度
    bmp->imageSize=bmp->height*bmp->widthStep*sizeof(BYTE);//计算排列的图像大小

    if(bitCount==8)
    {
        bmp->bitCount=8;
        bmp->bmfh.bfType=0x4d42;    //注意是4d42 这个地方折磨我一下午啊
        bmp->bmfh.bfReserved1=0;
        bmp->bmfh.bfReserved2=0;
        bmp->bmih.biBitCount=8;
        bmp->bmih.biSize=40;
        bmp->bmih.biPlanes=1;
        bmp->bmfh.bfSize=54+256*4+height*bmp->widthStep;
        bmp->bmfh.bfOffBits=1078;
        bmp->bmih.biBitCount=8;
        bmp->bmih.biCompression=0;
        bmp->bmih.biSizeImage=bmp->imageSize;
        bmp->bmih.biClrUsed=0;
        bmp->bmih.biClrImportant=0;
        bmp->bmih.biXPelsPerMeter=0;
        bmp->bmih.biYPelsPerMeter=0;
    }
    else if (bitCount==24)
    {
        bmp->bitCount=24;
        bmp->bmfh.bfType=0x4d42;
        bmp->bmih.biBitCount=24;
        bmp->bmfh.bfReserved1=0;
        bmp->bmfh.bfReserved2=0;
        bmp->bmih.biSize=40;
        bmp->bmih.biPlanes=1;
        bmp->bmfh.bfSize=54+height*bmp->widthStep;
        bmp->bmfh.bfOffBits=54;
        bmp->bmih.biBitCount=24;
        bmp->bmih.biSizeImage=bmp->imageSize;
        bmp->bmih.biClrUsed=0;
        bmp->bmih.biCompression=0;
        bmp->bmih.biClrImportant=0;
        bmp->bmih.biXPelsPerMeter=0;
        bmp->bmih.biYPelsPerMeter=0;
    }
    else
    {
        printf("Error(CreateBitmap): only supported 8 or 24 bits bitmap.\n");
        return -1;
    }

    bmp->imageData=(BYTE*)malloc(bmp->imageSize);        //分配数据空间
    if(!(bmp->imageData))
    {
        printf("Error(CreateBitmap): can not allocate bitmap memory.\n");
        return -1;
    }
    return 0;
}

/**
 * 位图指针释放函数  释放位图数据空间
 *
 * 使用方法:
 *     ReleaseBitmap(bmp);
 */
void ReleaseBitmap(Bitmap* bmp)
{
    free(bmp->imageData);
    bmp->imageData=NULL;
    free(bmp);
    bmp=NULL;
}

/**
 * 路径检查函数:是否为BMP文件,是否可读
 * 正确返回0,错误返回-1
 *
 * 使用方法
 *         ret=CheckPath(path);
 */
int CheckPath(char *path)
{
    FILE *fd;
    int len = strlen(path) / sizeof(char);
    char ext[3];
    //check whether the path include the characters "bmp" at end
    strncpy(ext, &path[len - 3], 3);
    if (!(ext[0] == 'b' && ext[1] == 'm' && ext[2] == 'p')) {
        printf("Error(CheckPath): the extension of the file is not bmp.\n");
        return -1;
    }

    //check whether the file can be read or not
    fd = fopen(path, "r");
    if (!fd)
    {
        printf("Error(CheckPath): can not open the file.\n");
        return -1;
    }
    fclose(fd);

    return 0;
}

/**
 * 从文件中读取位图de函数
 * 正确返回0,错误返回-1
 *
 * 使用方法:
 *     bmp=(Bitmap*)malloc(sizeof(Bitmap));
 *     ret=ReadBitmap(path, bmp);
 */
int ReadBitmap(char* path, Bitmap* bmp)
{
    int ret;
    FILE *fd;

    //检查路径是否可读
    ret=CheckPath(path);
    if(ret==-1)
    {
        printf("Error(ReadBitmap): the path of the image is invalid.\n");
        return -1;
    }

    //打开文件
    fd=fopen(path,"rb");
    if(fd==0)
    {
        printf("Error(ReadBitmap): can not open the image.\n");
        return -1;
    }

    //读取文件信息头     14字节
    fread(&(bmp->bmfh.bfType),sizeof(WORD),1,fd);
    fread(&(bmp->bmfh.bfSize),sizeof(DWORD),1,fd);
    fread(&(bmp->bmfh.bfReserved1),sizeof(WORD),1,fd);
    fread(&(bmp->bmfh.bfReserved2),sizeof(WORD),1,fd);
    fread(&(bmp->bmfh.bfOffBits),sizeof(DWORD),1,fd);

    //读取位图信息头    40字节
    fread(&(bmp->bmih.biSize),sizeof(DWORD),1,fd);
    fread(&(bmp->bmih.biWidth),sizeof(DWORD),1,fd);
    fread(&(bmp->bmih.biHeight),sizeof(DWORD),1,fd);
    fread(&(bmp->bmih.biPlanes),sizeof(WORD),1,fd);
    fread(&(bmp->bmih.biBitCount),sizeof(WORD),1,fd);
    fread(&(bmp->bmih.biCompression),sizeof(DWORD),1,fd);
    fread(&(bmp->bmih.biSizeImage),sizeof(DWORD),1,fd);
    fread(&(bmp->bmih.biXPelsPerMeter),sizeof(DWORD),1,fd);
    fread(&(bmp->bmih.biYPelsPerMeter),sizeof(DWORD),1,fd);
    fread(&(bmp->bmih.biClrUsed),sizeof(DWORD),1,fd);
    fread(&(bmp->bmih.biClrImportant),sizeof(DWORD),1,fd);

    //创建位图结构
    ret=CreateBitmap(bmp, bmp->bmih.biWidth, bmp->bmih.biHeight, bmp->bmih.biBitCount);
    if(ret==-1)
    {
        printf("Error(CreateBitmap): can not CreateBitmap.\n");
        return -1;
    }

    //读取图像数据
    //由于4字节对齐格式
    fseek(fd,bmp->bmfh.bfOffBits,SEEK_SET);    //定位到图像数据区
    ret=fread(bmp->imageData,bmp->imageSize,1,fd);
    if(ret==0)
    {
        if(feof(fd))    //if the file pointer point to the end of the file
        {
        }
        if(ferror(fd))  //if error happened while read the pixel data
        {
            printf("Error(ReadBitmap): can not read the pixel data.\n");
            fclose(fd);
            return -1;
        }
    }

    //关闭文件
    fclose(fd);
    return 0;
}

/**
 * 保存位图到文件中去
 * 正确返回0,错误返回-1
 *
 * 使用方法:
 *     bmp=(Bitmap*)malloc(sizeof(Bitmap));
 *     ret=SaveBitmap(path, bmp);
 */
int SaveBitmap(char* path, Bitmap* bmp)
{
    int ret;
    FILE *fd;

    //检查路径是否正确
    int len = strlen(path) / sizeof(char);
    char ext[3];
    //check whether the path include the characters "bmp" at end
    strncpy(ext, &path[len - 3], 3);
    if (!(ext[0] == 'b' && ext[1] == 'm' && ext[2] == 'p'))
    {
        printf("Error(SaveBitmap): the extension of the file is not bmp.\n");
        return -1;
    }

    //打开文件
    fd=fopen(path,"wb");
    if(fd==0)
    {
        printf("Error(SaveBitmap): can not open the image.\n");
        return -1;
    }

    //保存文件信息头     14字节
    fwrite(&(bmp->bmfh.bfType),sizeof(WORD),1,fd);
    fwrite(&(bmp->bmfh.bfSize),sizeof(DWORD),1,fd);
    fwrite(&(bmp->bmfh.bfReserved1),sizeof(WORD),1,fd);
    fwrite(&(bmp->bmfh.bfReserved2),sizeof(WORD),1,fd);
    fwrite(&(bmp->bmfh.bfOffBits),sizeof(DWORD),1,fd);

    //保存位图信息头    40字节
    fwrite(&(bmp->bmih.biSize),sizeof(DWORD),1,fd);
    fwrite(&(bmp->bmih.biWidth),sizeof(DWORD),1,fd);
    fwrite(&(bmp->bmih.biHeight),sizeof(DWORD),1,fd);
    fwrite(&(bmp->bmih.biPlanes),sizeof(WORD),1,fd);
    fwrite(&(bmp->bmih.biBitCount),sizeof(WORD),1,fd);
    fwrite(&(bmp->bmih.biCompression),sizeof(DWORD),1,fd);
    fwrite(&(bmp->bmih.biSizeImage),sizeof(DWORD),1,fd);
    fwrite(&(bmp->bmih.biXPelsPerMeter),sizeof(DWORD),1,fd);
    fwrite(&(bmp->bmih.biYPelsPerMeter),sizeof(DWORD),1,fd);
    fwrite(&(bmp->bmih.biClrUsed),sizeof(DWORD),1,fd);
    fwrite(&(bmp->bmih.biClrImportant),sizeof(DWORD),1,fd);

    //如果为8位,则 保存调色板
    RGBQUAD pal[256];
    int i;
    if(bmp->bitCount==8)
    {
        for(i=0;i<256;i++)
        {
            pal[i].rgbBlue=i;
            pal[i].rgbGreen=i;
            pal[i].rgbRed=i;
            pal[i].rgbReserved=0;
        }
        if(fwrite(pal,sizeof(RGBQUAD)*256,1,fd)!=1)
        {
            printf("Error(SaveBitmap): can not write Color Palette.\n");
            return -1;
        }
    }


    //保存图像数据
    ret=fwrite(bmp->imageData,bmp->imageSize,1,fd);
    if(ret!=1)
    {
        printf("Error(SaveBitmap): can not save the pixel data.\n");
        return -1;
    }

    //关闭文件
    fclose(fd);

    return 0;
}

#endif // BMP_H_INCLUDED
​​​​​​​

basicprocess.h

#ifndef BASICPROCESS_H_
#define BASICPROCESS_H_
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include "bmp.h"
#include <math.h>
/**
 * <font color="#3f7f5f">位图图像基本处理函数  图像格式转换</font>
 */
int RGB2Gray(Bitmap* src, Bitmap* dst)
{
    int ret;
    int n=0,i,j;
    BYTE r,g,b,gray;

    //检查图像格式是否合法
    if(src->bitCount!=24)
    {
        printf("Error(RGB2Gray): the source image must be in RGB format.\n");
        return -1;
    }

    //为dst图像分配数据空间
    ret=CreateBitmap(dst,src->width,src->height,8);
    if(ret==-1)
    {
        printf("Error(RGB2Gray): can't create target image.\n");
        return -1;
    }

    //计算灰度数据
    for(i=0;i<src->height;i++)
    {
        n=0;
        for(j=0;j<src->width*3;j++,n++)
        {
            b=*(src->imageData+src->widthStep*(src->height-1-i)+j);
            j++;
            g=*(src->imageData+src->widthStep*(src->height-1-i)+j);
            j++;
            r=*(src->imageData+src->widthStep*(src->height-1-i)+j);
            gray=(r*19595 + g*38469 + b*7472) >> 16;
            *(dst->imageData+dst->widthStep*(dst->height-1-i)+n)=gray;
        }
    }

    return 0;
}

/**
 * Gray2RGB
 *
 * 使用方法:
 *        bmp=(Bitmap*)malloc(sizeof(Bitmap));
 *        ret=ReadBitmap(path, bmp);
 *        dstbmp=(Bitmap*)malloc(sizeof(Bitmap));
 *        ret=Gray2RGB(bmp,dstbmp);
 */
int Gray2RGB(Bitmap* src, Bitmap* dst)
{
    int ret;
    int n=0,i,j;
    BYTE r;

    //检查图像格式是否合法
    if(src->bitCount!=8)
    {
        printf("Error(Gray2RGB): the source image must be in gray scale.\n");
        return -1;
    }

    //为dst图像分配数据空间
    ret=CreateBitmap(dst,src->width,src->height,24);
    if(ret==-1)
    {
        printf("Error(Gray2RGB): can't create target image.\n");
        return -1;
    }

    //计算灰度数据
    for(i=0;i<src->height;i++)
    {
        n=0;
        for(j=0;j<src->width;j++,n++)
        {
            r=*(src->imageData+src->widthStep*(src->height-1-i)+j);
            *(dst->imageData+dst->widthStep*(dst->height-1-i)+n)=r;
            n++;
            *(dst->imageData+dst->widthStep*(dst->height-1-i)+n)=r;
            n++;
            *(dst->imageData+dst->widthStep*(dst->height-1-i)+n)=r;
        }
    }

    return 0;
}

/**
 * Gray2BW 图像二值化
 *
 * 使用方法:
 *        bmp=(Bitmap*)malloc(sizeof(Bitmap));
 *        ret=ReadBitmap(path, bmp);
 *        dstbmp=(Bitmap*)malloc(sizeof(Bitmap));
 *        ret=Gray2BW(bmp,dstbmp);
 */
int Gray2BW(Bitmap* src, Bitmap* dst, int threshold)
{
    int ret;
    int n=0,i,j;
    BYTE r;

    //检查图像格式是否合法
    if(src->bitCount!=8)
    {
        printf("Error(Gray2BW): the source image must be in gray scale.\n");
        return -1;
    }

    //为dst图像分配数据空间
    ret=CreateBitmap(dst,src->width,src->height,8);
    if(ret==-1)
    {
        printf("Error(Gray2BW): can't create target image.\n");
        return -1;
    }

    //计算灰度数据
    for(i=0;i<src->height;i++)
    {
        for(j=0;j<src->width;j++,n++)
        {
            r=*(src->imageData+src->widthStep*(src->height-1-i)+j);
            if(r>=threshold)
            {
                n=255;
            }
            else
            {
                n=0;
            }
            *(dst->imageData+dst->widthStep*(dst->height-1-i)+j)=n;
        }
    }

    return 0;
}

/**
 *  rgb2hsv
 *  r,g,b values are from 0 to 1
 *  h = [0,360], s = [0,1], v = [0,1]
 *  if s == 0, then h = -1 (undefined)
 *  使用方法:
 *      rgb2hsv(0.2,0.3,0.3,&x,&y,&z);
 */
void rgb2hsv(float R, float G, float B, float *H, float *S, float* V)
{
    float min, max, delta,tmp;
    tmp = R<G?R:G;
    min = tmp<B?tmp:B;
    tmp = R>G?R:G;
    max = tmp>B?tmp:B;
    *V = max; // v

    delta = max - min;

    if( max != 0 )
      *S = delta / max; // s
    else
    {
       // r = g = b = 0 // s = 0, v is undefined
      *S = 0;
      *H = -1;
      return;
    }
    if( R == max )
        *H = ( G - B ) / delta; // between yellow & magenta
   else if( G == max )
        *H = 2 + ( B - R ) / delta; // between cyan & yellow
   else
       *H = 4 + ( R - G ) / delta; // between magenta & cyan

    (*H) *= 60; // degrees
    if( *H < 0 )
       (*H) += 360;
}


/**
 *  hsv2rgb
 *  r,g,b values are from 0 to 1
 *  h = [0,360], s = [0,1], v = [0,1]
 *  if s == 0, then h = -1 (undefined)
 *  使用方法:
 *       hsv2rgb(60,0.3,0.5,&x,&y,&z);
*/
void hsv2rgb(float H, float S, float V, float *R, float *G, float *B)
{
     int i;
    float f, p, q, t;

    if( S == 0 )
    {
        *R =*G = *B = V;
        return;
    }

    H /= 60; // sector 0 to 5
    i = floor( H );
    f = H - i; // factorial part of h
    p = V * ( 1 - S );
    q = V * ( 1 - S * f );
    t = V * ( 1 - S * ( 1 - f ) );

    switch( i )
    {
    case 0:
        *R = V;
        *G = t;
        *B = p;
       break;
    case 1:
       *R = q;
       *G = V;
       *B = p;
       break;
    case 2:
       *R = p;
       *G = V;
       *B = t;
       break;
    case 3:
       *R = p;
       *G = q;
       *B = V;
       break;
    case 4:
       *R = t;
       *G = p;
       *B = V;
       break;
    default: // case 5:
       *R = V;
       *G = p;
       *B = q;
       break;
    }
}



/**
 * 直方图均衡化
 * 返回 0正确 -1错误
 */
int HistEqualization(Bitmap* dstBmp, Bitmap* srcBmp)
{
    return 0;
}

/*
 * 中值滤波 
*/


int MedFilt(Bitmap* dstBmp, Bitmap* srcBmp)
{
    return 0;
}



//平滑去噪

int smooth(Bitmap* src, Bitmap* dst)
{
    int ret;
    int n=0,i,j;
    int sum;

    //为dst图像分配数据空间
    ret=CreateBitmap(dst,src->width,src->height,8);
    if(ret==-1)
    {
        printf("Error(Gray2BW): can't create target image.\n");
        return -1;
    }

    for(i=1;i<src->height-1;i++)
    {
        for(j=1;j<src->width-1;j++)
        {   
            sum = 0;//clear sum
            sum+=*(src->imageData+src->widthStep*(src->height-1-i-1)+j-1);
            sum+=*(src->imageData+src->widthStep*(src->height-1-i-1)+j);
            sum+=*(src->imageData+src->widthStep*(src->height-1-i-1)+j+1);

            sum+=*(src->imageData+src->widthStep*(src->height-1-i)+j-1);
            sum+=*(src->imageData+src->widthStep*(src->height-1-i)+j);// 该点
            sum+=*(src->imageData+src->widthStep*(src->height-1-i)+j+1);

            sum+=*(src->imageData+src->widthStep*(src->height-1-i+1)+j-1);
            sum+=*(src->imageData+src->widthStep*(src->height-1-i+1)+j);
            sum+=*(src->imageData+src->widthStep*(src->height-1-i+1)+j+1);

            *(dst->imageData+dst->widthStep*(dst->height-1-i)+j)=(sum/9);//赋值给目标


        }
    }

    return 0;
}




//膨胀

int pengzhang(Bitmap* src, Bitmap* dst)
{
    int ret;
    int n=0,i,j;
    int r;

    //为dst图像分配数据空间
    ret=CreateBitmap(dst,src->width,src->height,8);
    if(ret==-1)
    {
        printf("Error(Gray2BW): can't create target image.\n");
        return -1;
    }

    for(i=1;i<src->height-1;i++)
    {
        for(j=1;j<src->width-1;j++)
        {   
            r=*(src->imageData+src->widthStep*(src->height-1-i)+j);     
            if(r==0)
            {
            *(dst->imageData+dst->widthStep*(dst->height-1-i-1)+j-1)=0;
            *(dst->imageData+dst->widthStep*(dst->height-1-i-1)+j)=0;
            *(dst->imageData+dst->widthStep*(dst->height-1-i-1)+j+1)=0;

            *(dst->imageData+dst->widthStep*(dst->height-1-i)+j-1)=0;
            *(dst->imageData+dst->widthStep*(dst->height-1-i)+j)=0;
            *(dst->imageData+dst->widthStep*(dst->height-1-i)+j+1)=0;

            *(dst->imageData+dst->widthStep*(dst->height-1-i+1)+j-1)=0;
            *(dst->imageData+dst->widthStep*(dst->height-1-i+1)+j)=0;
            *(dst->imageData+dst->widthStep*(dst->height-1-i+1)+j+1)=0;
            }
            else *(dst->imageData+dst->widthStep*(dst->height-1-i)+j)=r;
        }
    }

    return 0;
}







int fushi(Bitmap* src, Bitmap* dst)
{
    int ret;
    int n=0,i,j;
    int r,sum;

    //为dst图像分配数据空间
    ret=CreateBitmap(dst,src->width,src->height,8);
    if(ret==-1)
    {
        printf("Error(Gray2BW): can't create target image.\n");
        return -1;
    }

    for(i=1;i<src->height-1;i++)
    {
        for(j=1;j<src->width-1;j++)
        {   
            r=*(src->imageData+src->widthStep*(src->height-1-i)+j);     

            if(r==0)
            {
            sum = 0;//clear sum
            sum+=*(src->imageData+src->widthStep*(src->height-1-i-1)+j-1);
            sum+=*(src->imageData+src->widthStep*(src->height-1-i-1)+j);
            sum+=*(src->imageData+src->widthStep*(src->height-1-i-1)+j+1);

            sum+=*(src->imageData+src->widthStep*(src->height-1-i)+j-1);
            sum+=*(src->imageData+src->widthStep*(src->height-1-i)+j);// 该点
            sum+=*(src->imageData+src->widthStep*(src->height-1-i)+j+1);

            sum+=*(src->imageData+src->widthStep*(src->height-1-i+1)+j-1);
            sum+=*(src->imageData+src->widthStep*(src->height-1-i+1)+j);
            sum+=*(src->imageData+src->widthStep*(src->height-1-i+1)+j+1);
            if(sum>=255){
                *(dst->imageData+dst->widthStep*(dst->height-1-i)+j)=255;}
            else {*(dst->imageData+dst->widthStep*(dst->height-1-i)+j)=r;}
            }
            else *(dst->imageData+dst->widthStep*(dst->height-1-i)+j)=255;
        }
    }

    return 0;
}







int qufan(Bitmap* src, Bitmap* dst)
{
    int ret;
    int n=0,i,j;

    //为dst图像分配数据空间
    ret=CreateBitmap(dst,src->width,src->height,8);
    if(ret==-1)
    {
        printf("Error(Gray2BW): can't create target image.\n");
        return -1;
    }
    for(i=1;i<src->height-1;i++)
    {
        for(j=1;j<src->width-1;j++)
        {   

        *(dst->imageData+dst->widthStep*(dst->height-1-i)+j)=255-*(src->imageData+src->widthStep*(src->height-1-i)+j);
        }
    }

    return 0;
}

#endif /* BASICPROCESS_H_ */



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447

效果图 中间过程图太多了,就不上了 这是最后的图。 
这里写图片描述 
废话不多说 直接show 代码 你们看吧。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值