1. c語言的問題 關於「圖」黑白圖像 最後一句的s[j]哪來的,-'0'是什麼意思
這個s是字元串,你看到倒數3行那個scanf("%s",s);了吧,%s就是字元串,s[j]就是當成數組處理的。-『0』是減去字元0的ASCLL碼值,為48
2. 純C語言實現圖像處理
這個不難吧?雖然我不用c,可是說下怎麼做,首先打開一個BMP文件,然後按照bmp格式解析到一個數組中,接著進行二值化(這個有很多方法),最後將數組中的數據寫入一個新的BMP文件就可以了。
3. 如何用C語言對圖像加入高斯雜訊和椒鹽雜訊
自己產生[x,y] 2維隨機數,用分布函數為權,取用點的個數就可以了。如考慮中心對稱,用極座標[r,sita] 也許更方便。
2維椒鹽雜訊,即產生[x,y,1] 和 [x,y,0], 1 是白點,0是黑點。
計算機的偽隨機數,就是白雜訊,均勻分布。
4. svd分解 c語言實現
/*
本程序在linux g++下編譯通過
bool svd(vector<vector<double> > A, int K, vector<vector<double> > &U, vector<double> &S, vector<vector<double> > &V);
A: 輸入待分解矩陣
K: 輸入,取前K大奇異值及奇異向量
U[0],U[1],...,U[K-1]: 前K大奇異值對應的左奇異向量
S[0],S[1],...,S[K-1]: 前K大奇異值 S[0]>=S[1]>=...>=S[K-1]
V[0],V[1],...,V[K-1]: 前K大奇異值對應的右奇異向量
*/
#include <cmath>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <vector>
using namespace std;
const int MAX_ITER=100000;
const double eps=0.0000001;
double get_norm(double *x, int n){
double r=0;
for(int i=0;i<n;i++)
r+=x[i]*x[i];
return sqrt(r);
}
double normalize(double *x, int n){
double r=get_norm(x,n);
if(r<eps)
return 0;
for(int i=0;i<n;i++)
x[i]/=r;
return r;
}
inline double proct(double*a, double *b,int n){
double r=0;
for(int i=0;i<n;i++)
r+=a[i]*b[i];
return r;
}
void orth(double *a, double *b, int n){//|a|=1
double r=proct(a,b,n);
for(int i=0;i<n;i++)
b[i]-=r*a[i];
}
bool svd(vector<vector<double> > A, int K, vector<vector<double> > &U, vector<double> &S, vector<vector<double> > &V){
int M=A.size();
int N=A[0].size();
U.clear();
V.clear();
S.clear();
S.resize(K,0);
U.resize(K);
for(int i=0;i<K;i++)
U[i].resize(M,0);
V.resize(K);
for(int i=0;i<K;i++)
V[i].resize(N,0);
srand(time(0));
double *left_vector=new double[M];
double *next_left_vector=new double[M];
double *right_vector=new double[N];
double *next_right_vector=new double[N];
int col=0;
for(int col=0;col<K;col++){
double diff=1;
double r=-1;
while(1){
for(int i=0;i<M;i++)
left_vector[i]= (float)rand() / RAND_MAX;
if(normalize(left_vector, M)>eps)
break;
}
for(int iter=0;diff>=eps && iter<MAX_ITER;iter++){
memset(next_left_vector,0,sizeof(double)*M);
memset(next_right_vector,0,sizeof(double)*N);
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
next_right_vector[j]+=left_vector[i]*A[i][j];
r=normalize(next_right_vector,N);
if(r<eps) break;
for(int i=0;i<col;i++)
orth(&V[i][0],next_right_vector,N);
normalize(next_right_vector,N);
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
next_left_vector[i]+=next_right_vector[j]*A[i][j];
r=normalize(next_left_vector,M);
if(r<eps) break;
for(int i=0;i<col;i++)
orth(&U[i][0],next_left_vector,M);
normalize(next_left_vector,M);
diff=0;
for(int i=0;i<M;i++){
double d=next_left_vector[i]-left_vector[i];
diff+=d*d;
}
memcpy(left_vector,next_left_vector,sizeof(double)*M);
memcpy(right_vector,next_right_vector,sizeof(double)*N);
}
if(r>=eps){
S[col]=r;
memcpy((char *)&U[col][0],left_vector,sizeof(double)*M);
memcpy((char *)&V[col][0],right_vector,sizeof(double)*N);
}else{
cout<<r<<endl;
break;
}
}
delete [] next_left_vector;
delete [] next_right_vector;
delete [] left_vector;
delete [] right_vector;
return true;
}
void print(vector<vector<double> > &A){
}
int main(){
int m=10;
int n=8;
int k=5;
//分解一個10*8的矩陣A,求其前5個奇異值和奇異向量
srand(time(0));
vector<vector<double> > A;
A.resize(m);
for(int i=0;i<m;i++){
A[i].resize(n);
for(int j=0;j<n;j++)
A[i][j]=(float)rand()/RAND_MAX-0.5;
}
cout<<"A="<<endl;
for(int i=0;i<A.size();i++){
for(int j=0;j<A[i].size();j++){
cout<<setw(12)<<A[i][j]<<' ';
}
cout<<endl;
}
cout<<endl;
vector<vector<double> > U;
vector<double> S;
vector<vector<double> > V;
svd(A,k,U,S,V);
cout<<"U="<<endl;
for(int i=0;i<U[0].size();i++){
for(int j=0;j<U.size();j++){
cout<<setw(12)<<U[j][i]<<' ';
}
cout<<endl;
}
cout<<endl;
cout<<"S="<<endl;
for(int i=0;i<S.size();i++){
cout<<setw(7)<<S[i]<<' ';
}
cout<<endl;
cout<<"V="<<endl;
for(int i=0;i<V[0].size();i++){
for(int j=0;j<V.size();j++){
cout<<setw(12)<<V[j][i]<<' ';
}
cout<<endl;
}
return 0;
}
5. 如何用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://topic.csdn.net/t/20030909/13/2240079.html
6. 關於C語言處理圖像的問題
#include <Bitmap.h>//引用處理圖片的頭文件
FILE *hFile = NULL;//文件指針
int LoadBitmapFile ( char *fileName, BITMAP_IMAGE_PTR bitmap )
{
int hImageFile = 0; //文件讀取成功的標識
unsigned char *workingBuffer = 0; //保存讀取的圖片
hImageFile = _open ( fileName, _O_RDONLY ); //以只讀方式打開圖片文件,成功返回1,失敗返回-1
if ( hImageFile==-1 )//如果打開失敗,則輸出警告
{
WriteLogFile ( "LoadBitmapFile function error! \nCan not open the bitmap.\n" );
return 0;
}
這只是最基本的打開圖片文件,跟打開普通文件沒什麼區別,後面要對圖片進行處理的話沒有圖形學的知識會很困難,如果想進一步研究,建議學習一下計算機圖形學
7. 怎樣用C語言實現數字圖像處理的局部直方圖均衡演算法
1、ctrl+alt+l,自動色階。 2、手動調色階,用黑場和白場吸取色樣。 3、曲線調色。 4、圖像調整,匹配顏色,勾選中和選項。 方法很多的,學會靈活運用喲
8. C語言打開圖像文件後讀取像素
C語言打開圖像文件後運用以下代碼就可以讀取像素,具體如下:
#ifndef IMAGE_H
#define IMAGE_H
void image_info(FILE* file);
void image_save(FILE *file);
void image_gray();
void image_binarization();
void image_opposite();
void image_channel(); //抽取RGB通道
void image_bright();//改變圖像亮度
typedef struct BMP
{
//14位元組
unsigned short bfType; //文件標識 2位元組 必須為BM
unsigned int bfSize; //文件大小 4位元組
unsigned short bfReserved1; //保留,每位元組以"00"填寫 2位元組
unsigned short bfReserved2; //同上 2位元組
unsigned int bfOffBits; //記錄圖像數據區的起始位置(圖象數據相對於文件頭位元組的偏移量)。 4位元組
//40位元組
unsigned int biSize; //表示本結構的大小 4位元組
int biWidth; //點陣圖的寬度 4位元組
int biHeight; //點陣圖的高度 4位元組
unsigned short biPlanes; //永遠為1 , 2位元組
unsigned short biBitCount; //點陣圖的位數 分為1 4 8 16 24 32 2位元組
unsigned int biCompression; //壓縮說明 4位元組
unsigned int biSizeImage; //表示點陣圖數據區域的大小以位元組為單位 4位元組
int biXPelsPerMeter; //用象素/米表示的水平解析度 4位元組
int biYPelsPerMeter; //用象素/米表示的垂直解析度 4位元組
unsigned int biClrUsed; //點陣圖使用的顏色索引數 4位元組
unsigned int biClrImportant; //對圖象顯示有重要影響的顏色索引的數目 4位元組
} BMP;
int line_byte;
unsigned char *imagedata;
extern BMP bmp;
extern int line_byte;
extern unsigned char *imagedata;
#endif
//image_rw.c文件
#include<stdio.h>
#include<stdlib.h>
#include"image.h"
void image_info(FILE *file)
{
int times=3; //輸入文件名次數。
char bmp_name[10]; //文件名
printf("\nplease enter a file name for reading:");
do
{
if (times<3)
{
printf("\nplease enter a file name for reading again:");
}
fflush(stdin);
gets(bmp_name);
//printf("\n%s",bmp_name);
file=fopen(bmp_name,"rb+"); //打開一個文件進行讀寫操作。
--times;
if (file==NULL)
{
printf("\nerror opening %s for reading! ",bmp_name);
}
else
{
break;
}
}
while(times!=0);
if (times==0)
{
printf("\nsorry, shutdown!");
exit(1);
}
//讀取圖像信息
fseek(file,0L,0); //讀取圖像文件類型
fread(&bmp,sizeof(BMP),1,file);
printf("\n bmp tpye: %u",bmp.bfType);
printf("\n bmp size: %u",bmp.bfSize);
printf("\n bmp reserved1: %u",bmp.bfReserved1);
printf("\n bmp reserved2: %u",bmp.bfReserved2);
printf("\n bmp offBits: %u",bmp.bfOffBits);
printf("\n bmp bisize: %u",bmp.biSize);
printf("\n bmp biWidth: %d",bmp.biWidth);
printf("\n bmp biHeight: %d",bmp.biHeight);
printf("\n bmp biplans: %u",bmp.biPlanes);
printf("\n bmp biBitCount: %u",bmp.biBitCount);
printf("\n bmp biCompression: %u",bmp.biCompression);
printf("\n bmp biSizeImage: %u",bmp.biSizeImage);
printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);
printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);
printf("\n bmp biClrUsed: %u",bmp.biClrUsed);
printf("\n bmp biClrImportant: %u\n",bmp.biClrImportant);
line_byte=(bmp.biWidth*bmp.biBitCount/8+3)/4*4; //獲得圖像數據每行的數據個數
//printf("dfsa%u",bmp.line_byte);
//bmp.imagedata=NULL;
imagedata=(unsigned char*)malloc(bmp.biSizeImage);
fseek(file,(long)bmp.bfOffBits,0);
fread(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);
fclose(file);
}
//保存圖像
void image_save(FILE *file)
{
int times=3; //輸入文件名次數。
char bmp_name[10]; //文件名
//int i; //記錄數據區個數
printf("\nplease enter a file name for writeing:");
do
{
if (times<3)
{
printf("\nplease enter a file name for writeing again:");
}
fflush(stdin);
gets(bmp_name);
printf("\n%s",bmp_name);
file=fopen(bmp_name,"wb+"); //打開一個文件進行讀寫操作。
--times;
if (file==NULL)
{
printf("\nerror opening %s for writing",bmp_name);
}
else
{
break;
}
}
while(times!=0);
if (times==0)
{
printf("\nsorry, shutdown!");
exit(1);
}
//寫文件頭
printf("\n%s",bmp_name);
fseek(file,0L,0); //圖像文件類型
fwrite(&(bmp.bfType),sizeof(short),1,file);
printf("\n bmp tpye: %d",bmp.bfType);
fseek(file,2L,0); //圖像文件大小
fwrite(&(bmp.bfSize),sizeof(int),1,file);
printf("\n bmp size: %d",bmp.bfSize);
fseek(file,6L,0); //圖像文件保留字1
fwrite(&(bmp.bfReserved1),sizeof(short),1,file);
printf("\n bmp reserved1: %d",bmp.bfReserved1);
fseek(file,8L,0); //圖像文件保留字2
fwrite(&(bmp.bfReserved2),sizeof(short),1,file);
printf("\n bmp reserved2: %d",bmp.bfReserved2);
fseek(file,10L,0);//數據區的偏移量
fwrite(&(bmp.bfOffBits),sizeof(short),1,file);
printf("\n bmp offBits: %d",bmp.bfOffBits);
fseek(file,14L,0);//文件頭結構大小
fwrite(&(bmp.biSize),sizeof(int),1,file);
printf("\n bmp bisize: %d",bmp.biSize);
fseek(file,18L,0);//圖像的寬度
fwrite(&(bmp.biWidth),sizeof(int),1,file);
printf("\n bmp biWidth: %d",bmp.biWidth);
fseek(file,22L,0);//圖像的高度
fwrite(&(bmp.biHeight),sizeof(int),1,file);
printf("\n bmp biHeight: %d",bmp.biHeight);
fseek(file,24L,0);//圖像的面數
fwrite(&(bmp.biPlanes),sizeof(short),1,file);
printf("\n bmp biplans: %d",bmp.biPlanes);
fseek(file,28L,0);//圖像一個像素的位元組數
fwrite(&(bmp.biBitCount),sizeof(short),1,file);
printf("\n bmp biBitCount: %d",bmp.biBitCount);
fseek(file,30L,0);//圖像壓縮信息
fwrite(&(bmp.biCompression),sizeof(short),1,file);
printf("\n bmp biCompression: %d",bmp.biCompression);
fseek(file,34L,0);//圖像數據區的大小
fwrite(&(bmp.biSizeImage),sizeof(int),1,file);
printf("\n bmp biSizeImage: %d",bmp.biSizeImage);
fseek(file,38L,0);//水平解析度
fwrite(&(bmp.biXPelsPerMeter),sizeof(int),1,file);
printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);
fseek(file,42L,0);//垂直解析度
fwrite(&(bmp.biYPelsPerMeter),sizeof(int),1,file);
printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);
fseek(file,46L,0);//顏色索引數
fwrite(&(bmp.biClrUsed),sizeof(int),1,file);
printf("\n bmp biClrUsed: %d",bmp.biClrUsed);
fseek(file,50L,0);//重要顏色索引數
fwrite(&(bmp.biClrImportant),sizeof(int),1,file);
printf("\n bmp biClrImportant: %d\n",bmp.biClrImportant);
fseek(file,(long)(bmp.bfOffBits),0);
fwrite(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);
fclose(file);
}
//pixProcess.c文件
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include"image.h"
//灰度化
void image_gray()
{
int i,j;
unsigned char tmp;
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
tmp=0.11*(*(imagedata+i*line_byte+j*3+0))+0.59*(*(imagedata+i*line_byte+j*3+1))+0.3*(*(imagedata+i*line_byte+j*3+2));
imagedata[i*line_byte+j*3+0]=tmp;
imagedata[i*line_byte+j*3+1]=tmp;
imagedata[i*line_byte+j*3+2]=tmp;
//printf("\nnidsfh%d %d",i,j);
}
}
}
//二值化
void image_binarization()
{
int i,j;
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte;j++)
{
if ((*(imagedata+i*line_byte+j))<128)
{
imagedata[i*line_byte+j]=0;
}
else
{
imagedata[i*line_byte+j]=255;
}
}
}
}
void image_opposite() //反相
{
int i,j;
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte;j++)
{
imagedata[i*line_byte+j]=abs(255-imagedata[i*line_byte+j]);
}
}
}
void image_channel() //抽取RGB通道
{
int i,j;
char rgb;
printf("\nplease enter a char(r/g/b): ");
fflush(stdin);
scanf("%c",&rgb);
if (rgb=='b')
{
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
imagedata[i*line_byte+3*j+1]=0;
imagedata[i*line_byte+3*j+2]=0;
}
}
}
else if(rgb=='g')
{
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
imagedata[i*line_byte+3*j]=0;
imagedata[i*line_byte+3*j+2]=0;
}
}
}
else
{
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
imagedata[i*line_byte+3*j]=0;
imagedata[i*line_byte+3*j+1]=0;
}
}
}
}
void image_bright()//改變圖像亮度
{
int level;
int i,j;
printf("\n please enter the level of brightness[-255 to 255] :");
fflush(stdin);
scanf("%d",&level);
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte;j++)
{
if (level>=0)
{
if ((imagedata[i*line_byte+j]+level)>255)
imagedata[i*line_byte+j]=255;
else
imagedata[i*line_byte+j]+=level;
}
else
{
if ((imagedata[i*line_byte+j]-abs(level))<0)
imagedata[i*line_byte+j]=0;
else
imagedata[i*line_byte+j]+=level;
}
}
}
}
//void image_create() //創建一幅24位BMP圖像文件。
//{
//main.c文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include"image.h"
BMP bmp;
int main()
{
FILE *file=NULL;
int choose;
char gono;
do
{
image_info(file); //imagedata已經分配了動態內存,但是沒有釋放
printf("\n 1.image_opposite");
printf("\n 2.image_gray");
printf("\n 3.image_binarization");
printf("\n 4.image_channel");
printf("\n 5.image_brightness");
//printf("6.image_opposite");
//printf("7.image_opposite");
printf("\nchoose your options:");
fflush(stdin);
scanf("%d",&choose);
switch(choose)
{
case 1:
image_opposite();
image_save(file);
free(imagedata);
break;
case 2:
image_gray();
image_save(file);
free(imagedata);
break;
case 3:
image_binarization();
image_save(file);
free(imagedata);
break;
case 4:
image_channel();
image_save(file);
free(imagedata);
break;
case 5:
image_bright();
image_save(file);
free(imagedata);
break;
default:
printf("\n wrong choose!");
}
printf("\nlet's go on?(y/n):");
fflush(stdin);
scanf("%c",&gono);
if (gono=='n')
{
printf("\nbye bye!");
break;
}
}
while(1);
return 0;
}
9. 請問C語言讀取圖像轉換為二維數組程序有什麼問題,怎麼修改
FileName=fileDlg.GetFileName ();FILE *fp=fopen(FileName,"rb");//二進制讀方式打開指定的圖像文件fread(&FileHead, sizeof(BITMAPFILEHEADER), 1,fp); //讀取文件頭,文件指針自動後移fread(&InfoHead, sizeof(BITMAPINFOHEADER), 1,fp);//讀取信息頭,文件指針自動後移//獲取圖像寬、高、每像素所佔位數等信息bmpWidth = InfoHead.biWidth;bmpHeight = InfoHead.biHeight;//下面完成圖像數據向內存數組的存儲ImageData=new unsigned char*[bmpHeight];if(InfoHead.biBitCount==24){for (int i=0;i<bmpHeight;i++){ImageData[i]=new unsigned char[(bmpWidth*3+3)/4*4];}for (int k=0;k<bmpHeight;k++ ){for(int j=0;j<(bmpWidth*3+3)/4*4;j++){fread(&ImageData[k][j],1,1,fp);//上面完成動態二維數組的申請,這里實際讀取圖像數據}}fclose(fp);//關閉文件
10. 用C語言編寫灰度圖像直方圖變換增強
參考一下這個程序:
#define IMAGEWIDTH 80
#define IMAGEHEIGHT 80
void Histogram(unsigned char *pImage,int nWidth,int nHeight,float fHisto[256]);
void Enhance(unsigned char *pImage,unsigned char *pImage1,int nWidth,int nHeight,float fHisto[256],float fHisto1[256]);
unsigned char dbImage[IMAGEWIDTH*IMAGEHEIGHT];
unsigned char dbTargetImage[IMAGEWIDTH*IMAGEHEIGHT];
float fHistogram[256],fHistogram1[256];
unsigned char lut[256];
/* 直方圖統計程序 */
int main()
{
... //准備一幅灰度圖片到dbImage
Histogram(dbImage,IMAGEWIDTH,IMAGEHEIGHT,fHistogram); //做直方圖統計
Enhance(dbImage,dbTargetImage,IMAGEWIDTH,IMAGEHEIGHT,fHistogram,fHistogram1); //直方圖增強輸出到dbTargetImage
...//完成後顯示
}
void Histogram(unsigned char *pImage,int nWidth,int nHeight,float fHisto[256])
{
int i,j;
unsigned int uWork;
unsigned char *pWork;
for ( i=0;i<256;i++ ) fHisto[i]=0.0f;
pWork=pImage;
for ( i=0;i<nHeight;i++ )
{
for ( j=0;j<nWidth;j++,pWork++ )
{
uWork=(unsigned int)(*pWork);
fHisto[uWork]++;
}
}
uWork=nWidth*nHeight;
for ( i=0;i<256;i++ )
{
fHisto[i]/=uWork;
fHisto[i]*=100;
}
}
void Enhance(unsigned char *pImage,unsigned char *pImage1,int nWidth,int nHeight,float fHisto[256],float fHisto1[256])
{
int i,j;
unsigned int uWork;
unsigned char *pWork,*pWork1;
for ( i=0;i<256;i++ )
fHisto1[i]=fHisto[i]/100;
for ( i=1;i<256;i++ )
fHisto1[i]+=fHisto1[i-1];
for ( i=0;i<256;i++ )
lut[i]=fHisto1[i]*256;
for ( i=0;i<256;i++ )
if ( lut[i]>=256 )
lut[i]=255;
pWork=pImage; pWork1=pImage1;
for ( i=0;i<nHeight;i++ )
for ( j=0;j<nWidth;j++,pWork++,pWork1++ )
(*pWork1)=lut[(*pWork)];
}