# 数字图像处理:BMP 图像处理(控制台程序)
# 实验任务书:
编写一个 VC++ 的控制台程序。要求如下:
一、打开一个 BMP 图像文件(图像大小不要太大)。
二、显示图像文件的数据。
对于读入的图像文件,显示位图数据 (选择一部分区域显示即可)。用 256 色或 256 级灰度图像进行验证。有两种显示方式:从上到下,从下到上。自己验证。
三、计算图像的直方图 **(可以只显示灰度级 100~200 的直方图,为 0 的不显示;大小的处理,先归一化,再乘以 50**)
对于 256 级灰度级的图像进行计算。存入一个 256 元素的数组中,并显示其数据。用图形的方式显示(数据为 0 的不显示,如果值太大,按比例缩小一下)。
四、图像的增亮或减暗
输入一个数字,如果是正数对图像进行增亮,如果是负数对图像进行减暗。
用其他软件进行查看,源图像和被增亮或减暗的图像进行对比,是不是达到了预想的效果。
五、main () 函数的要求
有选择。
main () 函数将指定 BMP 文件读入内存,将图像信息打印输出,最后又原样存入指定文件中。并进行计算需要的数据。
界面:
**0----------** 结束
**1----------** 读图像(8/24 位)
**2-----------** 写图像(8/24 位)
3----------- 显示图像数据(1 行到 10 行,1 列到 10 列,10*10**** 的大小)(8/24 位)
4----------- 计算直方图(100-200 之间的灰度,1-10 行,1-0 列,10*10**** 的大小)(8 位)
5----------- 对图像进行增亮或减暗(如加上 50 或减去 50**** 等)(8 位)
**6-----------** 图像反色
**7-----------** 变成灰度图(8/24 位)
8----------- 显示调色板 (8 位)
9-----------24 位彩色图像转换为 8**** 位灰度图像
10-----------24 位彩色图像转换为 8**** 位彩色图像
# 代码:
代码仅供参考:
#include <Windows.h> | |
#include <stdio.h> | |
#include <bits/stdc++.h> | |
using namespace std; | |
unsigned char *pBmpBuf; | |
int bmpWidth; | |
int bmpHeight; | |
RGBQUAD *pColorTable; | |
int biBitCount; | |
unsigned int **out_r; | |
unsigned int **out_g; | |
unsigned int **out_b; | |
bool readBmp(char *bmpName) // 读图像 | |
{ | |
FILE *fp=fopen(bmpName,"rb"); | |
if(fp==0) | |
return 0; | |
fseek(fp,sizeof(BITMAPFILEHEADER),0); | |
BITMAPINFOHEADER head; | |
fread(&head,sizeof(BITMAPINFOHEADER),1,fp); | |
bmpWidth=head.biWidth; | |
bmpHeight=head.biHeight; | |
biBitCount=head.biBitCount; | |
int lineByte=(bmpWidth*biBitCount/8+3)/4*4; | |
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) // 写图像 | |
{ | |
if(!imgBuf) return 0; | |
int colorTablesize=0; | |
if(biBitCount==8) colorTablesize=1024; | |
int lineByte=(width*biBitCount/8+3)/4*4; | |
FILE *fp=fopen(bmpName,"wb"); | |
if(fp==0) return 0; | |
BITMAPFILEHEADER fileHead; | |
fileHead.bfType=0x4D42; | |
fileHead.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+colorTablesize+lineByte*height; | |
fileHead.bfReserved1=0; | |
fileHead.bfReserved2=0; | |
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; | |
} | |
bool saveBmp_2(char *bmpName,unsigned char *imgBuf,int width,int height,int biBitCount,RGBQUAD *pColorTable) // 写图像 | |
{ | |
if(!imgBuf) return 0; | |
int colorTablesize=1024; | |
int lineByte=(width*biBitCount/8+3)/4*4; | |
int fix=lineByte-bmpWidth*3; | |
unsigned char *p=imgBuf; | |
FILE *fp=fopen(bmpName,"wb"); | |
if(fp==0) return 0; | |
int lineBytetmp=((width*8)/8+3)/4*4; | |
int fixtmp=lineBytetmp-bmpWidth; | |
unsigned char *imgBuftmp=new unsigned char[lineBytetmp*bmpHeight]; | |
BITMAPFILEHEADER fileHead; | |
fileHead.bfType=0x4D42; | |
fileHead.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+256*4+lineBytetmp*bmpHeight; | |
fileHead.bfReserved1=0; | |
fileHead.bfReserved2=0; | |
fileHead.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+256*4; | |
fwrite(&fileHead,sizeof(BITMAPFILEHEADER),1,fp); | |
BITMAPINFOHEADER head; | |
head.biBitCount=8; | |
head.biClrImportant=0; | |
head.biClrUsed=256; | |
head.biCompression=BI_RGB; | |
head.biHeight=height; | |
head.biPlanes=1; | |
head.biSize=40; | |
head.biSizeImage=lineBytetmp*height; | |
head.biWidth=width; | |
head.biXPelsPerMeter=0; | |
head.biYPelsPerMeter=0; | |
fwrite(&head,sizeof(BITMAPINFOHEADER),1,fp); | |
RGBQUAD *pColorTabletmp=new RGBQUAD[256]; | |
for(int i=0;i<256;i++) | |
{ | |
pColorTabletmp[i].rgbBlue=i; | |
pColorTabletmp[i].rgbGreen=i; | |
pColorTabletmp[i].rgbRed=i; | |
pColorTabletmp[i].rgbReserved=0; | |
} | |
int pos=0; | |
for(int i=0;i<bmpHeight;i++) | |
{ | |
for(int j=0;j<bmpWidth;j++) | |
{ | |
int blue=*p++; | |
int green=*p++; | |
int red=*p++; | |
int gray=0.3*red+0.59*green+0.11*blue; | |
imgBuftmp[pos]=gray; | |
pos++; | |
} | |
p+=fix; | |
for(int i=0;i<fixtmp;i++) | |
{ | |
imgBuftmp[pos]=0; | |
pos++; | |
} | |
} | |
delete []pColorTable; | |
delete []imgBuf; | |
fwrite(pColorTabletmp,sizeof(RGBQUAD),256,fp); | |
fwrite(imgBuftmp,height*lineBytetmp,1,fp); | |
fclose(fp); | |
return 1; | |
} | |
bool saveBmp_3(char *bmpName,unsigned char *imgBuf,int width,int height,int biBitCount,RGBQUAD *pColorTable) // 写图像 | |
{ | |
if(!imgBuf) return 0; | |
int colorTablesize=1024; | |
int lineByte=(width*biBitCount/8+3)/4*4; | |
int fix=lineByte-bmpWidth*3; | |
unsigned char *p=imgBuf; | |
FILE *fp=fopen(bmpName,"wb"); | |
if(fp==0) return 0; | |
int lineBytetmp=((width*8)/8+3)/4*4; | |
int fixtmp=lineBytetmp-bmpWidth; | |
unsigned char *imgBuftmp=new unsigned char[lineBytetmp*bmpHeight]; | |
BITMAPFILEHEADER fileHead; | |
fileHead.bfType=0x4D42; | |
fileHead.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+256*4+lineBytetmp*bmpHeight; | |
fileHead.bfReserved1=0; | |
fileHead.bfReserved2=0; | |
fileHead.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+256*4; | |
fwrite(&fileHead,sizeof(BITMAPFILEHEADER),1,fp); | |
BITMAPINFOHEADER head; | |
head.biBitCount=8; | |
head.biClrImportant=0; | |
head.biClrUsed=256; | |
head.biCompression=BI_RGB; | |
head.biHeight=height; | |
head.biPlanes=1; | |
head.biSize=40; | |
head.biSizeImage=lineBytetmp*height; | |
head.biWidth=width; | |
head.biXPelsPerMeter=0; | |
head.biYPelsPerMeter=0; | |
fwrite(&head,sizeof(BITMAPINFOHEADER),1,fp); | |
RGBQUAD *pColorTabletmp=new RGBQUAD[256]; | |
int R[4]={0,85,170,255}; | |
int G[16]={0,17,34,51,68,85,102,119,136,153,170,187,204,221,238,255}; | |
int B[4]={0,85,170,255}; | |
int numcolortable=0; | |
for(int i=0;i<4;i++) | |
{ | |
for(int j=0;j<16;j++) | |
{ | |
for(int k=0;k<4;k++) | |
{ | |
pColorTabletmp[numcolortable].rgbRed=R[i]; | |
pColorTabletmp[numcolortable].rgbGreen=G[j]; | |
pColorTabletmp[numcolortable].rgbBlue=B[k]; | |
numcolortable++; | |
} | |
} | |
} | |
int pos=0; | |
for(int i=0;i<bmpHeight;i++) | |
{ | |
for(int j=0;j<bmpWidth;j++) | |
{ | |
int blue=*p++; | |
int green=*p++; | |
int red=*p++; | |
red=red/85*85; | |
green=green/17*17; | |
blue=blue/85*85; | |
int _tmp=0; | |
for(int i=0;i<256;i++) | |
{ | |
if(red==pColorTabletmp[i].rgbRed&&green==pColorTabletmp[i].rgbGreen&&blue==pColorTabletmp[i].rgbBlue) | |
{ | |
_tmp=i; | |
break; | |
} | |
} | |
imgBuftmp[pos]=_tmp; | |
pos++; | |
} | |
p+=fix; | |
for(int i=0;i<fixtmp;i++) | |
{ | |
imgBuftmp[pos]=0; | |
pos++; | |
} | |
} | |
delete []pColorTable; | |
delete []imgBuf; | |
fwrite(pColorTabletmp,sizeof(RGBQUAD),256,fp); | |
fwrite(imgBuftmp,height*lineBytetmp,1,fp); | |
fclose(fp); | |
return 1; | |
} | |
void startbmp(char *bmpName) // 打开图片 | |
{ | |
readBmp(bmpName); | |
char writePath[] = "bmp_start.bmp"; | |
saveBmp(writePath,pBmpBuf, bmpWidth, bmpHeight, biBitCount, pColorTable); | |
system("start bmp_start.bmp"); | |
system("pause"); | |
} | |
void ImageShow(char *bmpName)// 显示图像数据 | |
{ | |
readBmp(bmpName); | |
cout<<"width="<<bmpWidth<<" height="<<bmpHeight<<" biBitCount="<<biBitCount<<endl; | |
int lineByte=(bmpWidth*biBitCount/8+3)/4*4; | |
int n=0,count_pixel=0; | |
out_r=new unsigned int *[bmpHeight]; | |
for(int i=0;i<bmpHeight;i++) | |
out_r[i]=new unsigned int[bmpWidth]; | |
out_g=new unsigned int *[bmpHeight]; | |
for(int i=0;i<bmpHeight;i++) | |
out_g[i]=new unsigned int[bmpWidth]; | |
out_b=new unsigned int *[bmpHeight]; | |
for(int i=0;i<bmpHeight;i++) | |
out_b[i]=new unsigned int[bmpWidth]; | |
if(biBitCount==8) | |
{ | |
for(int i=0;i<bmpHeight;i++) | |
{ | |
for(int j=0;j<bmpWidth;j++) | |
{ | |
out_r[bmpHeight-1-i][j]=pBmpBuf[j*3+2+bmpWidth*i*3]; | |
n++; | |
} | |
} | |
cout<<"总的像素个素为:"<<n<<endl; | |
cout<<"前十行十列10*10的数据:"<<endl; | |
for(int i=0;i<10;i++) | |
{ | |
for(int j=0;j<10;j++) | |
{ | |
cout<<out_r[i][j]<<" "; | |
} | |
cout<<endl; | |
} | |
} | |
if(biBitCount==24) | |
{ | |
for(int i=0;i<bmpHeight;i++) | |
{ | |
for(int j=0;j<bmpWidth;j++) | |
{ | |
for(int k=0;k<3;k++) | |
{ | |
count_pixel++; | |
} | |
n++; | |
} | |
} | |
cout<<"总的像素个素为:"<<n<<endl; | |
} | |
if(biBitCount==24) | |
{ | |
for(int i=0;i<bmpHeight;i++) | |
{ | |
for(int j=0;j<bmpWidth;j++) | |
{ | |
out_r[bmpHeight-1-i][j]=pBmpBuf[j*3+2+bmpWidth*i*3]; | |
out_g[bmpHeight-1-i][j]=pBmpBuf[j*3+1+bmpWidth*i*3]; | |
out_b[bmpHeight-1-i][j]=pBmpBuf[j*3+bmpWidth*i*3]; | |
} | |
} | |
cout<<"前十行十列10*10的数据:"<<endl; | |
for(int i=0;i<10;i++) | |
{ | |
for(int j=0;j<10;j++) | |
{ | |
cout<<out_r[i][j]<<" "; | |
cout<<out_g[i][j]<<" "; | |
cout<<out_b[i][j]<<" "; | |
cout<<"\t"; | |
} | |
cout<<endl; | |
} | |
} | |
system("pause"); | |
} | |
void ImageShow_2(char *bmpName)// 显示图像数据 | |
{ | |
readBmp(bmpName); | |
cout<<"width="<<bmpWidth<<" height="<<bmpHeight<<" biBitCount="<<biBitCount<<endl; | |
int lineByte=(bmpWidth*biBitCount/8+3)/4*4; | |
int n=0,count_pixel=0; | |
out_r=new unsigned int *[bmpHeight]; | |
for(int i=0;i<bmpHeight;i++) | |
out_r[i]=new unsigned int[bmpWidth]; | |
out_g=new unsigned int *[bmpHeight]; | |
for(int i=0;i<bmpHeight;i++) | |
out_g[i]=new unsigned int[bmpWidth]; | |
out_b=new unsigned int *[bmpHeight]; | |
for(int i=0;i<bmpHeight;i++) | |
out_b[i]=new unsigned int[bmpWidth]; | |
if(biBitCount==8) | |
{ | |
for(int i=0;i<bmpHeight;i++) | |
{ | |
for(int j=0;j<bmpWidth;j++) | |
{ | |
out_r[bmpHeight-1-i][j]=pBmpBuf[j*3+2+bmpWidth*i*3]; | |
n++; | |
} | |
} | |
cout<<"总的像素个素为:"<<n<<endl; | |
} | |
} | |
void Histogram(char *bmpName)// 直方图 | |
{ | |
int num[256]={0}; | |
int col_r[256]={0}; | |
int bmpsize=bmpWidth*bmpHeight; | |
readBmp(bmpName); | |
out_r=new unsigned int *[bmpHeight]; | |
for(int i=0;i<bmpHeight;i++) | |
out_r[i]=new unsigned int[bmpWidth]; | |
out_g=new unsigned int *[bmpHeight]; | |
for(int i=0;i<bmpHeight;i++) | |
out_g[i]=new unsigned int[bmpWidth]; | |
out_b=new unsigned int *[bmpHeight]; | |
for(int i=0;i<bmpHeight;i++) | |
out_b[i]=new unsigned int[bmpWidth]; | |
if(biBitCount==8) | |
{ | |
for(int i=0;i<bmpHeight;i++) | |
{ | |
for(int j=0;j<bmpWidth;j++) | |
{ | |
out_r[bmpHeight-1-i][j]=pBmpBuf[j*3+2+bmpWidth*i*3]; | |
} | |
} | |
for(int k=0;k<256;k++) | |
{ | |
for(int i=0;i<bmpHeight;i++) | |
{ | |
for(int j=0;j<bmpWidth;j++) | |
{ | |
if(k==out_r[i][j]) | |
num[k]++; | |
} | |
} | |
} | |
cout<<"直方图:"<<endl; | |
for(int i=0;i<256;i++) | |
cout<<num[i]<<" "; | |
cout<<endl; | |
for(int i=0;i<256;i++) | |
{ | |
col_r[i]=((double)num[i]/bmpsize)*50; | |
} | |
for(int i=0;i<256;i++) | |
{ | |
cout<<i<<":"; | |
for(int j=0;j<col_r[i];j++) | |
cout<<"*"; | |
cout<<endl; | |
} | |
} | |
system("pause"); | |
} | |
void BrightDark(char *bmpName)// 增量或减暗 | |
{ | |
readBmp(bmpName); | |
out_r=new unsigned int *[bmpHeight]; | |
for(int i=0;i<bmpHeight;i++) | |
out_r[i]=new unsigned int[bmpWidth]; | |
out_g=new unsigned int *[bmpHeight]; | |
for(int i=0;i<bmpHeight;i++) | |
out_g[i]=new unsigned int[bmpWidth]; | |
out_b=new unsigned int *[bmpHeight]; | |
for(int i=0;i<bmpHeight;i++) | |
out_b[i]=new unsigned int[bmpWidth]; | |
int num_b_d; | |
cin>>num_b_d; | |
if(biBitCount==8) | |
{ | |
for (int i=0;i<256;i++) | |
{ | |
pColorTable[i].rgbBlue+=num_b_d; | |
pColorTable[i].rgbGreen+=num_b_d; | |
pColorTable[i].rgbRed+=num_b_d; | |
} | |
for(int i=0;i<256;i++) | |
{ | |
if(pColorTable[i].rgbBlue>255) | |
pColorTable[i].rgbBlue=255; | |
if(pColorTable[i].rgbBlue<0) | |
pColorTable[i].rgbBlue=0; | |
if(pColorTable[i].rgbGreen>255) | |
pColorTable[i].rgbGreen=255; | |
if(pColorTable[i].rgbGreen<0) | |
pColorTable[i].rgbGreen=0; | |
if(pColorTable[i].rgbRed>255) | |
pColorTable[i].rgbRed=255; | |
if(pColorTable[i].rgbRed<0) | |
pColorTable[i].rgbRed=0; | |
} | |
} | |
char writePath[] = "bmp_bri_dar.bmp"; | |
saveBmp(writePath,pBmpBuf, bmpWidth, bmpHeight, biBitCount, pColorTable); | |
delete[]pBmpBuf; | |
if (biBitCount == 8) | |
delete[]pColorTable; | |
system("start bmp_bri_dar.bmp"); | |
system("pause"); | |
} | |
void Inverse(char *bmpName) // 反色 | |
{ | |
readBmp(bmpName); | |
out_r=new unsigned int *[bmpHeight]; | |
for(int i=0;i<bmpHeight;i++) | |
out_r[i]=new unsigned int[bmpWidth]; | |
out_g=new unsigned int *[bmpHeight]; | |
for(int i=0;i<bmpHeight;i++) | |
out_g[i]=new unsigned int[bmpWidth]; | |
out_b=new unsigned int *[bmpHeight]; | |
for(int i=0;i<bmpHeight;i++) | |
out_b[i]=new unsigned int[bmpWidth]; | |
if(biBitCount==8) | |
{ | |
for (int i=0;i<256;i++) | |
{ | |
pColorTable[i].rgbBlue=255-pColorTable[i].rgbBlue; | |
pColorTable[i].rgbGreen=255-pColorTable[i].rgbGreen; | |
pColorTable[i].rgbRed=255-pColorTable[i].rgbRed; | |
} | |
} | |
if(biBitCount==24) | |
{ | |
int lineByte=(bmpWidth*biBitCount/8+3)/4*4; | |
int fix=lineByte-bmpWidth*3; | |
for(int i=0;i<bmpHeight;i++) | |
{ | |
for(int j=0;j<bmpWidth;j++) | |
{ | |
out_r[bmpHeight-1-i][j]=pBmpBuf[j*3+2+bmpWidth*i*3+i*fix]; | |
out_g[bmpHeight-1-i][j]=pBmpBuf[j*3+1+bmpWidth*i*3+i*fix]; | |
out_b[bmpHeight-1-i][j]=pBmpBuf[j*3+bmpWidth*i*3+i*fix]; | |
} | |
} | |
for(int i=0;i<bmpHeight;i++) | |
{ | |
for(int j=0;j<bmpWidth;j++) | |
{ | |
out_r[i][j]=255-out_r[i][j]; | |
out_g[i][j]=255-out_g[i][j]; | |
out_b[i][j]=255-out_b[i][j]; | |
} | |
} | |
for(int i=0;i<bmpHeight;i++) | |
{ | |
for(int j=0;j<bmpWidth;j++) | |
{ | |
pBmpBuf[j*3+2+bmpWidth*i*3+i*fix]=out_r[bmpHeight-1-i][j]; | |
pBmpBuf[j*3+1+bmpWidth*i*3+i*fix]=out_g[bmpHeight-1-i][j]; | |
pBmpBuf[j*3+bmpWidth*i*3+i*fix]=out_b[bmpHeight-1-i][j]; | |
} | |
} | |
} | |
char writePath[] = "bmp_inverse.bmp"; | |
saveBmp(writePath, pBmpBuf, bmpWidth, bmpHeight, biBitCount, pColorTable); | |
delete[]pBmpBuf; | |
if (biBitCount == 8) | |
delete[]pColorTable; | |
system("start bmp_inverse.bmp"); | |
system("pause"); | |
} | |
void ConvertGray(char *bmpName) // 变成灰度图 | |
{ | |
readBmp(bmpName); | |
out_r=new unsigned int *[bmpHeight]; | |
for(int i=0;i<bmpHeight;i++) | |
out_r[i]=new unsigned int[bmpWidth]; | |
out_g=new unsigned int *[bmpHeight]; | |
for(int i=0;i<bmpHeight;i++) | |
out_g[i]=new unsigned int[bmpWidth]; | |
out_b=new unsigned int *[bmpHeight]; | |
for(int i=0;i<bmpHeight;i++) | |
out_b[i]=new unsigned int[bmpWidth]; | |
if(biBitCount==8) | |
{ | |
for (int i=0;i<256;i++) | |
{ | |
pColorTable[i].rgbBlue=0.11*pColorTable[i].rgbBlue+0.59*pColorTable[i].rgbGreen+0.3*pColorTable[i].rgbRed; | |
pColorTable[i].rgbGreen=pColorTable[i].rgbRed=pColorTable[i].rgbBlue; | |
} | |
} | |
if(biBitCount==24) | |
{ | |
int lineByte=(bmpWidth*biBitCount/8+3)/4*4; | |
int fix=lineByte-bmpWidth*3; | |
for(int i=0;i<bmpHeight;i++) | |
{ | |
for(int j=0;j<bmpWidth;j++) | |
{ | |
out_r[bmpHeight-1-i][j]=pBmpBuf[j*3+2+bmpWidth*i*3+i*fix]; | |
out_g[bmpHeight-1-i][j]=pBmpBuf[j*3+1+bmpWidth*i*3+i*fix]; | |
out_b[bmpHeight-1-i][j]=pBmpBuf[j*3+bmpWidth*i*3+i*fix]; | |
} | |
} | |
for(int i=0;i<bmpHeight;i++) | |
{ | |
for(int j=0;j<bmpWidth;j++) | |
{ | |
out_r[i][j]=0.3*out_r[i][j]+0.59*out_g[i][j]+0.11*out_b[i][j]; | |
out_g[i][j]=out_b[i][j]=out_r[i][j]; | |
} | |
} | |
for(int i=0;i<bmpHeight;i++) | |
{ | |
for(int j=0;j<bmpWidth;j++) | |
{ | |
pBmpBuf[j*3+2+bmpWidth*i*3+i*fix]=out_r[bmpHeight-1-i][j]; | |
pBmpBuf[j*3+1+bmpWidth*i*3+i*fix]=out_g[bmpHeight-1-i][j]; | |
pBmpBuf[j*3+bmpWidth*i*3+i*fix]=out_b[bmpHeight-1-i][j]; | |
} | |
} | |
} | |
char writePath[] = "bmp_gray.bmp"; | |
saveBmp(writePath, pBmpBuf, bmpWidth, bmpHeight, biBitCount, pColorTable); | |
delete[]pBmpBuf; | |
if (biBitCount == 8) | |
delete[]pColorTable; | |
system("start bmp_gray.bmp"); | |
system("pause"); | |
} | |
void palette(char *bmpName) // 显示调色板 | |
{ | |
readBmp(bmpName); | |
out_r=new unsigned int *[bmpHeight]; | |
for(int i=0;i<bmpHeight;i++) | |
out_r[i]=new unsigned int[bmpWidth]; | |
out_g=new unsigned int *[bmpHeight]; | |
for(int i=0;i<bmpHeight;i++) | |
out_g[i]=new unsigned int[bmpWidth]; | |
out_b=new unsigned int *[bmpHeight]; | |
for(int i=0;i<bmpHeight;i++) | |
out_b[i]=new unsigned int[bmpWidth]; | |
if(biBitCount==8) | |
{ | |
for (int i=0;i<256;i++) | |
{ | |
cout<<(double)pColorTable[i].rgbRed<<"\t"<<(double)pColorTable[i].rgbGreen<<"\t"<<(double)pColorTable[i].rgbBlue; | |
cout<<endl; | |
} | |
} | |
system("pause"); | |
} | |
void TFtoEgray(char *bmpName) | |
{ | |
readBmp(bmpName); | |
char writePath[] = "24to8gray.bmp"; | |
saveBmp_2(writePath,pBmpBuf, bmpWidth, bmpHeight, biBitCount, pColorTable); | |
system("start 24to8gray.bmp"); | |
ImageShow_2("24to8gray.bmp"); | |
system("pause"); | |
} | |
void TFtoEcolor(char *bmpName) | |
{ | |
readBmp(bmpName); | |
char writePath[] = "24to8color.bmp"; | |
saveBmp_3(writePath,pBmpBuf, bmpWidth, bmpHeight, biBitCount, pColorTable); | |
system("start 24to8color.bmp"); | |
ImageShow_2("24to8color.bmp"); | |
system("pause"); | |
} | |
int InputBmp(int num) | |
{ | |
int numb; | |
cout<<"\t\t\t请输入图片的序号:"<<endl; | |
cout<<"\t\t\t1、24位彩色图像(eyes.bmp)"<<endl; | |
cout<<"\t\t\t2、24位灰度图像(bird.bmp)"<<endl; | |
cout<<"\t\t\t3、8位彩色图像(sense.bmp)"<<endl; | |
cout<<"\t\t\t4、8位灰度图像(flower.bmp)"<<endl; | |
cin>>numb; | |
return numb; | |
} | |
int main() | |
{ | |
char readPath[107]; | |
int num; | |
num=InputBmp(num); | |
switch (num) | |
{ | |
case 1:strcpy(readPath,"eyes.bmp"); break; | |
case 2:strcpy(readPath,"bird.bmp"); break; | |
case 3:strcpy(readPath,"sense.bmp"); break; | |
case 4:strcpy(readPath,"flower.bmp");break; | |
default: break; | |
} | |
int number; | |
do | |
{ | |
system("cls"); | |
cout<<"\t\t\t图像处理"<<endl; | |
cout<<"\t\t\t0、结束"<<endl; | |
cout<<"\t\t\t1、读图像(8/24位)"<<endl; | |
cout<<"\t\t\t2、写图像(8/24位)"<<endl; | |
cout<<"\t\t\t3、显示图像数据(8/24位)"<<endl; | |
cout<<"\t\t\t4、计算直方图(8位)"<<endl; | |
cout<<"\t\t\t5、对图像进行增量或减暗(8位)"<<endl; | |
cout<<"\t\t\t6、图像反色(8/24位)"<<endl; | |
cout<<"\t\t\t7、变成灰度图(8/24位)"<<endl; | |
cout<<"\t\t\t8、显示调色板(8位)"<<endl; | |
cout<<"\t\t\t9、24位彩色图像转换为8位灰度图像(24位)"<<endl; | |
cout<<"\t\t\t10、24位彩色图像转换为8位彩色图像(24位)"<<endl; | |
cin>>number; | |
if(number<0||number>10) | |
cin>>number; | |
switch (number) | |
{ | |
case 0:exit(0); break; | |
case 1:startbmp(readPath); break; | |
case 2:exit(0); break; | |
case 3:ImageShow(readPath); break; | |
case 4:Histogram(readPath); break; | |
case 5:BrightDark(readPath); break; | |
case 6:Inverse(readPath); break; | |
case 7:ConvertGray(readPath); break; | |
case 8:palette(readPath); break; | |
case 9:TFtoEgray(readPath); break; | |
case 10:TFtoEcolor(readPath);break; | |
default: break; | |
} | |
}while(number!=11); | |
system("pause"); | |
return 0; | |
} |