c語言實現灰階圖轉換為二值圖

來源:互聯網
上載者:User

標籤:

將上篇得到的灰階圖轉換為二值圖,讀取像素資料,低於某一值置0,否則設定為255,為得到更好的效果不同圖片應採用不同的值

  1 /*  2 2015年6月2日11:16:22  3 灰階圖轉換為二值圖  4 blog:http://www.cnblogs.com/wd1001/  5 */  6 #include<stdio.h>  7 #include<malloc.h>  8 #include<stdlib.h>  9 /* 10 位元影像頭結構 11 */ 12 #pragma pack(1) 13 typedef struct tagBITMAPFILEHEADER 14 { 15     unsigned char bfType[2];//檔案格式 16     unsigned long bfSize;//檔案大小 17     unsigned short bfReserved1;//保留 18     unsigned short bfReserved2; 19     unsigned long bfOffBits; //DIB資料在檔案中的位移量 20 }fileHeader; 21 #pragma pack() 22 /* 23 位元影像資料資訊結構 24 */ 25 #pragma pack(1) 26 typedef struct tagBITMAPINFOHEADER 27 { 28     unsigned long biSize;//該結構的大小 29     long biWidth;//檔案寬度 30     long biHeight;//檔案高度 31     unsigned short biPlanes;//平面數 32     unsigned short biBitCount;//顏色位元 33     unsigned long biCompression;//壓縮類型 34     unsigned long biSizeImage;//DIB資料區大小 35     long biXPixPerMeter; 36     long biYPixPerMeter; 37     unsigned long biClrUsed;//多少色彩索引表 38     unsigned long biClrImporant;//多少重要顏色 39 }fileInfo; 40 #pragma pack() 41 /* 42 調色盤結構 43 */ 44 #pragma pack(1) 45 typedef struct tagRGBQUAD 46 { 47     unsigned char rgbBlue; //藍色分量亮度 48     unsigned char rgbGreen;//綠色分量亮度 49     unsigned char rgbRed;//紅色分量亮度 50     unsigned char rgbReserved; 51 }rgbq; 52 #pragma pack() 53  54 int main() 55 { 56     int i,j; 57     unsigned char ImgData[1000]; 58     FILE * fpGray,* fpBin; 59     fileHeader * fh; 60     fileInfo * fi; 61     rgbq * fq; 62  63     if((fpGray=fopen("G:/vc6.0/work/22.bmp","rb"))==NULL) 64     { 65         printf("開啟檔案失敗"); 66         exit(0); 67     } 68      69     if((fpBin=fopen("G:/vc6.0/work/33.bmp","wb"))==NULL) 70     { 71         printf("建立檔案失敗"); 72         exit(0); 73     } 74     //讀取灰階圖資料 75     fh=(fileHeader *)malloc(sizeof(fileHeader)); 76     fi=(fileInfo *)malloc(sizeof(fileInfo)); 77     fq=(rgbq *)malloc(256*sizeof(rgbq)); 78     fread(fh,sizeof(fileHeader),1,fpGray); 79     fread(fi,sizeof(fileInfo),1,fpGray); 80     fread(fq,sizeof(rgbq),256,fpGray); 81     //將頭資訊寫入 82     fwrite(fh,sizeof(fileHeader),1,fpBin); 83     fwrite(fi,sizeof(fileInfo),1,fpBin); 84     fwrite(fq,sizeof(rgbq),256,fpBin); 85     //灰階值低於閾值則置0 86     for(i=0;i<(fi->biHeight);i++) 87     { 88         for(j=0;(j<(fi->biWidth+3)/4*4);j++) 89         { 90             fread(&ImgData[j],1,1,fpGray); 91             if(ImgData[j]>142) 92                 ImgData[j]=255; 93             else 94                 ImgData[j]=0; 95         } 96         fwrite(ImgData,1,j,fpBin); 97     } 98     free(fh); 99     free(fi);100     free(fq);101     fclose(fpBin);102     fclose(fpGray);103     printf("success\n");104     return 0;105 }

結果:

明天考四級了,考完再寫剩下的

c語言實現灰階圖轉換為二值圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.