?? loadbmp.cpp
字號:
// LoadBmp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#include "math.h"
#include "cv.h"
#include "highgui.h"
#include "cvmat.hpp"
/*
typedef struct tagBITMAPFILEHEADER
{
unsigned short bfType;
unsigned long bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned long bfOffBits;
}BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER
{
unsigned long biSize;
long biWidth;
long biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned long biCompression;
unsigned long biSizeImage;
long biXPelsPerMeter;
long biYPelsPerMeter;
unsigned long biClrUsed;
unsigned long biClrImportant;
}BITMAPINFOHEADER;
typedef struct tagRGBQUAD
{
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;
}RGBQUAD;
*/
void writeimage(double *Data,int Height,int Width,char *filename);
void showimage(char *filename1,char *filename2);
void Exit(char *ErrorCode)
{
printf("%s",ErrorCode);
printf("Programme Quit!!\n\n");
exit(0);
}
long WidthBytes(long Width,int BitCount)
{
long WBytes;
WBytes=(Width*BitCount+31)/8;
WBytes=WBytes/4*4;
return WBytes;
}
void main(int argc,char* argv[])
{
char filename1[250]; //="C:\\aaaa.bmp";
char filename2[250]; //="C:\\bb.bmp";
printf("Please input the original image path:\n ");
scanf("%s",filename1);
printf("Please input the new image path:\n ");
scanf("%s",filename2);
long i,j;
long WBytes;
int Colors;
long Height,Width;
FILE *fp1;
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
RGBQUAD QUAD[256];
double *Data;
unsigned char SrcData;
if((fp1=fopen(filename1,"rb"))==NULL)
{
Exit("Can not open the file.\n");
}
fseek(fp1,0,SEEK_SET);
fread(&bfh,sizeof(BITMAPFILEHEADER),1,fp1);
if(bfh.bfType!='M'*256+'B')
{
Exit("This is not a bmp file.\n");
}
fseek(fp1,14,SEEK_SET);
fread(&bih,sizeof(BITMAPINFOHEADER),1,fp1);
Height=bih.biHeight;
Width=bih.biWidth;
WBytes=WidthBytes(Width,bih.biBitCount);
Colors=1<<bih.biBitCount;
Data=(double*)malloc(Height*Width*sizeof(double));
if(Colors!=256)
{
Exit("This programme only for 256 colors bmp.\n");
}
fseek(fp1,54,SEEK_SET);
fread(QUAD,sizeof(RGBQUAD),256,fp1);
for(i=Height-1;i>=0;i--)
{
for(j=0;j<Width;j++)
{
fseek(fp1,54+Colors*sizeof(RGBQUAD)+i*WBytes+j,SEEK_SET);
fread(&SrcData,1,1,fp1);
Data[(Height-1-i)*Width+j]=SrcData;
}
}
fclose(fp1);
/////////////////////////////////////////////////////////////////////////
writeimage(Data,Height,Width,filename2);
printf("\nProgramme Finish!!\n");
showimage(filename1,filename2);
free(Data);
}
void writeimage(double *Data,int Height,int Width,char *filename)
{
unsigned char *CharData;
CharData=(unsigned char*)malloc(Height*Width*sizeof(unsigned char));
int i,j;
for (i=0;i<Height;i++)
for (j=0;j<Width;j++)
CharData[i*Width+j]=(unsigned char)Data[i*Width+j];
CvMat *mat=cvCreateMat(Height, Width, CV_8UC1);
cvInitMatHeader(mat, Height, Width, CV_8UC1, CharData);
cvSaveImage(filename, mat);
free(CharData);
cvReleaseMat(&mat);
}
void showimage(char *filename1,char *filename2)
{
IplImage* src_image;
src_image= cvLoadImage(filename1);
cvNamedWindow("原始圖像",CV_WINDOW_AUTOSIZE);
cvShowImage("原始圖像",src_image);
cvWaitKey(10);
IplImage* dst_image;
dst_image= cvLoadImage(filename2);
cvNamedWindow("變換后圖像",CV_WINDOW_AUTOSIZE);
cvShowImage("變換后圖像",dst_image);
cvWaitKey(0);
cvDestroyWindow("原始圖像");
cvReleaseImage(&src_image);
cvDestroyWindow("變換后圖像");
cvReleaseImage(&dst_image);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -