opencvIplImage转化BMP(源代码)

// newtest3.6.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "windows.h"

int _tmain(int argc, _TCHAR* argv[])
{
FILE *fp = NULL; 
fp=fopen("E:\\Lena.bmp","rb");

unsigned char* pTotal;
pTotal=new unsigned char[786486];

BITMAPFILEHEADER fileHead1;
fileHead1.bfType = 0x4D42;//bmp类型
fileHead1.bfSize= 786486;
fileHead1.bfReserved1 = 0;
fileHead1.bfReserved2 = 0;
fileHead1.bfOffBits=54;//+colorTablesize;
memcpy(pTotal, &fileHead1, 14);

BITMAPINFOHEADER head1; 
head1.biBitCount=24;
head1.biClrImportant=0;
head1.biClrUsed=0;
head1.biCompression=0;
head1.biHeight=512;//height;
head1.biPlanes=1;
head1.biSize=40;//40;
head1.biSizeImage=1536*512;//lineByte*height;
head1.biWidth=512;//width;
head1.biXPelsPerMeter=0;
head1.biYPelsPerMeter=0;

memcpy(pTotal+14, &head1, 40);
fseek(fp,54,0);
unsigned char* p40;
p40=new unsigned char[786432];

fread(p40,786432,1,fp);
memcpy(pTotal+54, p40, 786432);

FILE *fp2=fopen("E:\\new.bmp","wb");
fwrite(pTotal,786486,1, fp2);
fclose(fp2);






IplImage *iplImg=0;

iplImg=cvCreateImageHeader(cvSize(512,512),IPL_DEPTH_8U,3);
iplImg->origin=1;//
iplImg->imageData=(char*)malloc(iplImg->imageSize);
memcpy(iplImg->imageData,p40,iplImg->imageSize);

  //显示图片
cvNamedWindow("mainWin", 1); 
cvShowImage("mainWin", iplImg); 

cvWaitKey(0);
cvDestroyWindow("mainWin");
//cvReleaseImage(&iplImg);


unsigned char* pTotal2;
pTotal2=new unsigned char[786486];

BITMAPFILEHEADER fileHead2;
fileHead2.bfType = 0x4D42;//bmp类型
fileHead2.bfSize= 786486;
fileHead2.bfReserved1 = 0;
fileHead2.bfReserved2 = 0;
fileHead2.bfOffBits=54;//+colorTablesize;
memcpy(pTotal2, &fileHead2, 14);

BITMAPINFOHEADER head2; 
head2.biBitCount=24;
head2.biClrImportant=0;
head2.biClrUsed=0;
head2.biCompression=0;
head2.biHeight=512;//height;
head2.biPlanes=1;
head2.biSize=40;//40;
head2.biSizeImage=1536*512;//lineByte*height;
head2.biWidth=512;//width;
head2.biXPelsPerMeter=0;
head2.biYPelsPerMeter=0;

memcpy(pTotal2+14, &head2, 40);

memcpy(pTotal2+54, iplImg->imageData, 786432);

FILE *fp3=fopen("E:\\new1.bmp","wb");

fwrite(pTotal,786486,1, fp3);





delete [] pTotal;
delete [] pTotal2;
delete [] p40;
cvReleaseImageHeader(&iplImg);


fclose(fp3);





return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值