跪求 C语言图像分割和边缘检测 程序

Python028

跪求 C语言图像分割和边缘检测 程序,第1张

clude<stdio.h>

int main()

{

int len=0

int len2=0

FILE* stream

FILE* stream1

FILE* stream2

char buf[50]

char buf1[50]

char buf2[50]

char text[1024]

printf("input anfile path to open:")

scanf("%s",buf)

stream=fopen(buf,"r+")

fseek(stream,0,SEEK_END)

len=ftell(stream)

printf("the file %s length is %d!\n",buf,len)

len2 = len/2

printf("intput 2 file name: \n")

scanf("%s%s",buf1,buf2)

fseek(stream,0,SEEK_SET)

stream1=fopen(buf1,"w+")

stream2=fopen(buf2,"w+")

fread(text,len2,1,stream)

fwrite(text,len2,1,stream1)

fread(text,len-len2,1,stream)

fwrite(text,len-len2,1,stream2)

fclose(stream)

fclose(stream1)

fclose(stream2)

return 0

}

文件合并

#include<stdio.h>

int main()

{

int len=0

int len2=0

FILE* stream

FILE* stream1

char buf[50]

char buf1[50]

char text[1024]

printf("input anfile path to open:")

scanf("%s",buf)

stream=fopen(buf,"r+")

fseek(stream,0,SEEK_END)

printf("intput another file name: \n")

scanf("%s",buf1)

stream1=fopen(buf1,"r+")

fseek(stream1,0,SEEK_END)

len=ftell(stream1)

fseek(stream1,0,SEEK_SET)

fread(text,len,1,stream1)

fwrite(text,len,1,stream)

fclose(stream)

fclose(stream1)

remove(buf1)//remove the another file

return 0

}

需要在程序目录下有文件存在

/*************************************************************************

* 该函数用于对图像进行阈值分割运算

* 参数:

* 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 < 256i++)

{

lHistogram[i]=0

}

//获得直方图

MaxGrayValue = 0

MinGrayValue = 255

for (i = 0i < lWidth i++)

{

for(j = 0j < 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 < 1000IterationTimes ++)

{

Threshold = NewThreshold

lP1 =0

lP2 =0

lS1 = 0

lS2 = 0

//求两个区域的灰度平均值

for (i = MinGrayValuei <=Thresholdi++)

{

lP1 += lHistogram[i]*i

lS1 += lHistogram[i]

}

for (i = Threshold+1i<MaxGrayValuei++)

{

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 = 0i < lWidth i++)

{

for(j = 0j < 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://topic.csdn.net/t/20030909/13/2240079.html

#include<math.h>

#include <iomanip.h> 

#include <stdlib.h>

#include <windows.h>

#include <stdio.h>

#include <stdlib.h>

#include <iostream.h>

#include <fstream.h>

//---------------------------------------------------------------------------------------

//以下该模块是完成BMP图像(彩色图像是24bit RGB各8bit)的像素获取,并存在文件名为xiang_su_zhi.txt中

unsigned char *pBmpBuf//读入图像数据的指针

int bmpWidth//图像的宽

int bmpHeight//图像的高

RGBQUAD *pColorTable//颜色表指针

int biBitCount//图像类型,每像素位数

//-------------------------------------------------------------------------------------------

//读图像的位图数据、宽、高、颜色表及每像素位数等数据进内存,存放在相应的全局变量中

bool readBmp(char *bmpName) 

{

    FILE *fp=fopen(bmpName,"rb")//二进制读方式打开指定的图像文件

    if(fp==0)

        return 0

    //跳过位图文件头结构BITMAPFILEHEADER

    fseek(fp, sizeof(BITMAPFILEHEADER),0)

    //定义位图信息头结构变量,读取位图信息头进内存,存放在变量head中

    BITMAPINFOHEADER head  

    fread(&head, sizeof(BITMAPINFOHEADER), 1,fp) //获取图像宽、高、每像素所占位数等信息

    bmpWidth = head.biWidth

    bmpHeight = head.biHeight

    biBitCount = head.biBitCount//定义变量,计算图像每行像素所占的字节数(必须是4的倍数)

    int lineByte=(bmpWidth * biBitCount/8+3)/4*4//灰度图像有颜色表,且颜色表表项为256

    if(biBitCount==8)

    {

        //申请颜色表所需要的空间,读颜色表进内存

        pColorTable=new RGBQUAD[256]

        fread(pColorTable,sizeof(RGBQUAD),256,fp)

    }

    //申请位图数据所需要的空间,读位图数据进内存

    pBmpBuf=new unsigned char[lineByte * bmpHeight]

    fread(pBmpBuf,1,lineByte * bmpHeight,fp)

    fclose(fp)//关闭文件

    return 1//读取文件成功

}

//-----------------------------------------------------------------------------------------

//给定一个图像位图数据、宽、高、颜色表指针及每像素所占的位数等信息,将其写到指定文件中

bool saveBmp(char *bmpName, unsigned char *imgBuf, int width, int height, int biBitCount, RGBQUAD *pColorTable)

{

    //如果位图数据指针为0,则没有数据传入,函数返回

    if(!imgBuf)

        return 0

    //颜色表大小,以字节为单位,灰度图像颜色表为1024字节,彩色图像颜色表大小为0

    int colorTablesize=0

    if(biBitCount==8)

        colorTablesize=1024

    //待存储图像数据每行字节数为4的倍数

    int lineByte=(width * biBitCount/8+3)/4*4

    //以二进制写的方式打开文件

    FILE *fp=fopen(bmpName,"wb")

    if(fp==0)

        return 0

    //申请位图文件头结构变量,填写文件头信息

    BITMAPFILEHEADER fileHead

    fileHead.bfType = 0x4D42//bmp类型

    //bfSize是图像文件4个组成部分之和

    fileHead.bfSize= sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + colorTablesize + lineByte*height

    fileHead.bfReserved1 = 0

    fileHead.bfReserved2 = 0

    //bfOffBits是图像文件前3个部分所需空间之和

    fileHead.bfOffBits=54+colorTablesize

    //写文件头进文件

    fwrite(&fileHead, sizeof(BITMAPFILEHEADER),1, fp)

    //申请位图信息头结构变量,填写信息头信息

    BITMAPINFOHEADER head 

    head.biBitCount=biBitCount

    head.biClrImportant=0

    head.biClrUsed=0

    head.biCompression=0

    head.biHeight=height

    head.biPlanes=1

    head.biSize=40

    head.biSizeImage=lineByte*height

    head.biWidth=width

    head.biXPelsPerMeter=0

    head.biYPelsPerMeter=0

    //写位图信息头进内存

    fwrite(&head, sizeof(BITMAPINFOHEADER),1, fp)

    //如果灰度图像,有颜色表,写入文件 

    if(biBitCount==8)

        fwrite(pColorTable, sizeof(RGBQUAD),256, fp)

    //写位图数据进文件

    fwrite(imgBuf, height*lineByte, 1, fp)

    //关闭文件

    fclose(fp)

    return 1

}

//----------------------------------------------------------------------------------------

//以下为像素的读取函数

void doIt()

{

    //读入指定BMP文件进内存

    char readPath[]="nv.BMP"

    readBmp(readPath)

    //输出图像的信息

    cout<<"width="<<bmpWidth<<" height="<<bmpHeight<<" biBitCount="<<biBitCount<<endl

    

    //循环变量,图像的坐标

    //每行字节数

    int lineByte=(bmpWidth*biBitCount/8+3)/4*4

    //循环变量,针对彩色图像,遍历每像素的三个分量

    int m=0,n=0,count_xiang_su=0

    //将图像左下角1/4部分置成黑色

 

    ofstream outfile("图像像素.txt",ios::in|ios::trunc)

    if(biBitCount==8) //对于灰度图像

    {              

    //------------------------------------------------------------------------------------

    //以下完成图像的分割成8*8小单元,并把像素值存储到指定文本中。由于BMP图像的像素数据是从

    //左下角:由左往右,由上往下逐行扫描的

        int L1=0

        int hang=63

        int lie=0

        //int L2=0

        //int fen_ge=8

        for(int fen_ge_hang=0fen_ge_hang<8fen_ge_hang++)//64*64矩阵行循环

        { 

            for(int fen_ge_lie=0fen_ge_lie<8fen_ge_lie++)//64*64列矩阵循环

            { 

            //--------------------------------------------

                for(L1=hangL1>hang-8L1--)//8*8矩阵行

                {

                    for(int L2=lieL2<lie+8L2++)//8*8矩阵列

                    {

                        m=*(pBmpBuf+L1*lineByte+L2)

                        outfile<<m<<" "

                        count_xiang_su++

                        if(count_xiang_su%8==0)//每8*8矩阵读入文本文件

                        {

                            outfile<<endl

                        }

                    }

                }

            //---------------------------------------------

                hang=63-fen_ge_hang*8//64*64矩阵行变换

                lie+=8//64*64矩阵列变换

                    //该一行(64)由8个8*8矩阵的行组成

            }

            hang-=8//64*64矩阵的列变换

            lie=0//64*64juzhen

        }

    }

    //double xiang_su[2048]

    //ofstream outfile("xiang_su_zhi.txt",ios::in|ios::trunc)

    if(!outfile)

    {

        cout<<"open error!"<<endl

        exit(1)

    }

    else if(biBitCount==24)

    {//彩色图像

        for(int i=0i<bmpHeighti++)

        {

            for(int j=0j<bmpWidthj++)

            {

                for(int k=0k<3k++)//每像素RGB三个分量分别置0才变成黑色

                {

                    //*(pBmpBuf+i*lineByte+j*3+k)-=40

                    m=*(pBmpBuf+i*lineByte+j*3+k)

                    outfile<<m<<" "

                    count_xiang_su++

                    if(count_xiang_su%8==0)

                    {

                        outfile<<endl

                    }

                //n++

                }

                n++

            }

        }

        cout<<"总的像素个素为:"<<n<<endl

        cout<<"----------------------------------------------------"<<endl

    }

    

    //将图像数据存盘

    

    char writePath[]="nvcpy.BMP"//图片处理后再存储

    saveBmp(writePath, pBmpBuf, bmpWidth, bmpHeight, biBitCount, pColorTable)

    //清除缓冲区,pBmpBuf和pColorTable是全局变量,在文件读入时申请的空间

    delete []pBmpBuf

    if(biBitCount==8)

        delete []pColorTable

}

void main()

{

    doIt()

}