Bmp 转 Mat
(2013-07-26 19:07:30)
标签:
it |
最近由于项目的需要,自己写了一个Bmp转(opencv)Mat的程序。可以通过两种方法实现,1.Bmp->IplImage->Mat;
2.Bmp->Mat 根据需要选择适当的方法,实例代码如下,比较简单我就不多解释了,看不懂的可以留言或发邮件。
// BMP 转 Mat格式
int CopencvtestDlg::BmpToMat(void)
{
int depth,
channel;
depth =
IPL_DEPTH_8U;
channel =
3;
BITMAPFILEHEADER fileHeader;
BITMAPINFOHEADER infoHeader;
int
bufsize;
BOOL
bl;
CFile
cf;
bl =
cf.Open(_T("e://4.bmp"), CFile::modeRead, NULL, NULL);
if (bl ==
TRUE)
{
cf.Read(&fileHeader,
sizeof(BITMAPFILEHEADER));
cf.Read(&infoHeader,
sizeof(BITMAPINFOHEADER));
bufsize =
infoHeader.biHeight*infoHeader.biWidth*channel;
uchar* buf = new uchar[bufsize];
uchar* reversebuf = new uchar[bufsize];
cf.Read(buf, bufsize);
//方法一:bmp->IplImage->Mat
//方法二: bmp->Mat
//把bmp数据(上下颠倒)反转过来
for (int rows = 0; rows < infoHeader.biHeight;
rows++)
{
for (int cols = 0; cols < infoHeader.biWidth;
cols++)
{
memcpy(reversebuf+(rows*infoHeader.biWidth*channel),
buf+((infoHeader.biHeight-rows-1)*infoHeader.biWidth*channel),
infoHeader.biWidth*channel);
}
}
Mat bmpmat(infoHeader.biHeight,
infoHeader.biWidth, CV_8UC3);//定义Mat矩阵
memcpy(bmpmat.data, reversebuf,
bufsize);//复制图像数据
imshow("bmp to mat", bmpmat);//显示图像
waitKey();
delete []buf;
delete []reversebuf;
}
else
{
MessageBox(_T("文件打开失败!"), NULL, NULL);
}
return
0;
}
// BMP 转 Mat格式
int CopencvtestDlg::BmpToMat(void)
{
}