vc 如何将二维数组中的灰度值保存成灰度图像? vc 如何将二维数组中的灰度值保存成灰度图像? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如是要是在opencv中就简单了,IplImage* img=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);uchar* data;data=(uchar*)img->imageData;下步就是将你数组里的值赋给data就行了! 要看你想要保存成什么格式,bmp,jpg还是别的opencv封装了各种格式的细节,我们只需操作数据区不使用opencv的话,就要根据特定的文件格式说明来做了,比如说bmp常见格式都是开放的。我们只需操作数据区然后按要求该压缩的压缩 最简单的方式,就是直接存成RAW格式,即:直接将二维数组逐行写入"xxx.raw"文件。FILE *fp;fp = fopen("img.raw", "wb");// IMG[Y][X] : img dataif(fp==NULL){ printf("fopen wrong !\n"); return;}for(i=0; i<Y; i++) // Height :Y{ fwrite(IMG[i], sizeof(char), X, fp); // Width: X}fclose(fp);只包含图像数据信息,用photoshop可以查看raw图像文件 直接上代码....//把二维数组保存为BMP图像void SaveArrayToBMP(unsigned char **pdata, int height, int width){ width = width/4; width = width*4; BYTE *pBits = new BYTE[width*height]; for (int i=0; i<height; ++i) { for (int j =0; j<width; j++) { pBits[i*width+j] = pdata[i][j]; } } CFileDialog mFileDlg(FALSE, NULL,NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("BMP Files (*.BMP)|*.BMP|All Files (*.*)|*.*||") , AfxGetMainWnd()); CString str("", 10000); mFileDlg.m_ofn.lpstrFile=str.GetBuffer(10000); str.ReleaseBuffer(); POSITION mPos=mFileDlg.GetStartPosition(); CString pathName(" ", 128); CFileStatus status; while(mPos!=NULL && mFileDlg.DoModal()==IDOK) { pathName=mFileDlg.GetNextPathName(mPos); CFile::GetStatus(pathName, status); CString str=mFileDlg.GetFileName(); str=str+".bmp"; BITMAPFILEHEADER hdr; BITMAPINFOHEADER bih; RGBQUAD rgbQuad[256]; //定义调色板 bih.biBitCount=8; bih.biClrImportant=0; bih.biClrUsed=0; bih.biCompression=BI_RGB; bih.biHeight=height; bih.biPlanes=1; bih.biSize=sizeof(BITMAPINFOHEADER); bih.biSizeImage=0; bih.biWidth=width; bih.biXPelsPerMeter=0; bih.biYPelsPerMeter=0; for(i=0; i<256; i++) { rgbQuad[i].rgbBlue = (BYTE)i; rgbQuad[i].rgbGreen = (BYTE)i; rgbQuad[i].rgbRed = (BYTE)i; rgbQuad[i].rgbReserved = 0; } //Fill in the fields of the file header hdr.bfType= (WORD)0x4D42; // is always "BM" hdr.bfSize= (DWORD)(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*256 + width*height); hdr.bfReserved1 = 0; hdr.bfReserved2 = 0; hdr.bfOffBits= (DWORD)(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER) +sizeof(RGBQUAD)*256); CFile mfile; mfile.Open( str, CFile::modeWrite|CFile::modeCreate); // Write the file header mfile.Write( &hdr, sizeof(hdr) );//写文件头 mfile.Write( &bih, sizeof(bih) );//写信息头 mfile.Write((LPSTR)rgbQuad,sizeof(RGBQUAD)*256); //写调色板 mfile.Write(pBits,width * height);//写数据 mfile.Close(); } delete []pBits; pBits = NULL;}//把一维数组保存为BMP图像void SaveArray2BMP(unsigned char *pdata, int height, int width){ int i; CFileDialog mFileDlg(FALSE, NULL,NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("BMP Files (*.BMP)|*.BMP|All Files (*.*)|*.*||") , AfxGetMainWnd()); CString str("", 10000); mFileDlg.m_ofn.lpstrFile=str.GetBuffer(10000); str.ReleaseBuffer(); POSITION mPos=mFileDlg.GetStartPosition(); CString pathName(" ", 128); CFileStatus status; while(mPos!=NULL && mFileDlg.DoModal()==IDOK) { pathName=mFileDlg.GetNextPathName(mPos); CFile::GetStatus(pathName, status); CString str=mFileDlg.GetFileName(); str=str+".bmp"; BITMAPFILEHEADER hdr; BITMAPINFOHEADER bih; RGBQUAD rgbQuad[256]; //定义调色板 bih.biBitCount=8; bih.biClrImportant=0; bih.biClrUsed=0; bih.biCompression=BI_RGB; bih.biHeight=height; bih.biPlanes=1; bih.biSize=sizeof(BITMAPINFOHEADER); bih.biSizeImage=0; bih.biWidth=width; bih.biXPelsPerMeter=0; bih.biYPelsPerMeter=0; for(i=0; i<256; i++) { rgbQuad[i].rgbBlue = (BYTE)i; rgbQuad[i].rgbGreen = (BYTE)i; rgbQuad[i].rgbRed = (BYTE)i; rgbQuad[i].rgbReserved = 0; } //Fill in the fields of the file header hdr.bfType= (WORD)0x4D42; // is always "BM" hdr.bfSize= (DWORD)(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*256 + width*height); hdr.bfReserved1 = 0; hdr.bfReserved2 = 0; hdr.bfOffBits= (DWORD)(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER) +sizeof(RGBQUAD)*256); CFile mfile; mfile.Open( str, CFile::modeWrite|CFile::modeCreate); // Write the file header mfile.Write( &hdr, sizeof(hdr) );//写文件头 mfile.Write( &bih, sizeof(bih) );//写信息头 mfile.Write((LPSTR)rgbQuad,sizeof(RGBQUAD)*256); //写调色板 mfile.Write(pdata,width * height);//写数据 mfile.Close(); } } 你要保存为什么格式的图片,bmp的话需要加载头文件信息和调色板信息。你看看bmp头文件格式自己按要求填写就行,不是很难的 DirectShow实现多路视频采集问题 windows程序设计这本书的重点在哪里? 子控件如何移动到非客户区 状态栏显示Icon的问题 救急呀!!!!!!!!!! 提示找不到.Debug文件下的.sbr文件怎么办?? 有谁用过sendinput摸拟一个鼠标击键? ADO问题 怎么处理自定义消息呢 菜鸟提问:不能理解_T? VS2010下面怎么设置连接的动态库 d3d 拖动图形时闪烁 及drawindexedprimive函数的问题
IplImage* img=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
uchar* data;
data=(uchar*)img->imageData;
下步就是将你数组里的值赋给data就行了!
opencv封装了各种格式的细节,我们只需操作数据区
不使用opencv的话,就要根据特定的文件格式说明来做了,比如说bmp
常见格式都是开放的。我们只需操作数据区然后按要求该压缩的压缩
fp = fopen("img.raw", "wb");// IMG[Y][X] : img data
if(fp==NULL)
{
printf("fopen wrong !\n");
return;
}for(i=0; i<Y; i++) // Height :Y
{
fwrite(IMG[i], sizeof(char), X, fp); // Width: X
}fclose(fp);只包含图像数据信息,用photoshop可以查看raw图像文件
直接上代码....
//把二维数组保存为BMP图像
void SaveArrayToBMP(unsigned char **pdata, int height, int width)
{ width = width/4;
width = width*4; BYTE *pBits = new BYTE[width*height]; for (int i=0; i<height; ++i)
{
for (int j =0; j<width; j++)
{
pBits[i*width+j] = pdata[i][j];
}
}
CFileDialog mFileDlg(FALSE, NULL,NULL, OFN_HIDEREADONLY
| OFN_OVERWRITEPROMPT, _T("BMP Files (*.BMP)|*.BMP|All Files (*.*)|*.*||") ,
AfxGetMainWnd());
CString str("", 10000);
mFileDlg.m_ofn.lpstrFile=str.GetBuffer(10000);
str.ReleaseBuffer();
POSITION mPos=mFileDlg.GetStartPosition();
CString pathName(" ", 128);
CFileStatus status;
while(mPos!=NULL && mFileDlg.DoModal()==IDOK)
{
pathName=mFileDlg.GetNextPathName(mPos);
CFile::GetStatus(pathName, status);
CString str=mFileDlg.GetFileName();
str=str+".bmp";
BITMAPFILEHEADER hdr;
BITMAPINFOHEADER bih;
RGBQUAD rgbQuad[256]; //定义调色板
bih.biBitCount=8;
bih.biClrImportant=0;
bih.biClrUsed=0;
bih.biCompression=BI_RGB;
bih.biHeight=height;
bih.biPlanes=1;
bih.biSize=sizeof(BITMAPINFOHEADER);
bih.biSizeImage=0;
bih.biWidth=width;
bih.biXPelsPerMeter=0;
bih.biYPelsPerMeter=0;
for(i=0; i<256; i++)
{
rgbQuad[i].rgbBlue = (BYTE)i;
rgbQuad[i].rgbGreen = (BYTE)i;
rgbQuad[i].rgbRed = (BYTE)i;
rgbQuad[i].rgbReserved = 0;
}
//Fill in the fields of the file header
hdr.bfType= (WORD)0x4D42; // is always "BM"
hdr.bfSize= (DWORD)(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)
+ sizeof(RGBQUAD)*256 + width*height);
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
hdr.bfOffBits= (DWORD)(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)
+sizeof(RGBQUAD)*256);
CFile mfile; mfile.Open( str, CFile::modeWrite|CFile::modeCreate);
// Write the file header
mfile.Write( &hdr, sizeof(hdr) );//写文件头
mfile.Write( &bih, sizeof(bih) );//写信息头
mfile.Write((LPSTR)rgbQuad,sizeof(RGBQUAD)*256); //写调色板
mfile.Write(pBits,width * height);//写数据
mfile.Close();
}
delete []pBits;
pBits = NULL;
}//把一维数组保存为BMP图像
void SaveArray2BMP(unsigned char *pdata, int height, int width)
{ int i;
CFileDialog mFileDlg(FALSE, NULL,NULL, OFN_HIDEREADONLY
| OFN_OVERWRITEPROMPT, _T("BMP Files (*.BMP)|*.BMP|All Files (*.*)|*.*||") ,
AfxGetMainWnd());
CString str("", 10000);
mFileDlg.m_ofn.lpstrFile=str.GetBuffer(10000);
str.ReleaseBuffer();
POSITION mPos=mFileDlg.GetStartPosition();
CString pathName(" ", 128);
CFileStatus status;
while(mPos!=NULL && mFileDlg.DoModal()==IDOK)
{
pathName=mFileDlg.GetNextPathName(mPos);
CFile::GetStatus(pathName, status);
CString str=mFileDlg.GetFileName();
str=str+".bmp";
BITMAPFILEHEADER hdr;
BITMAPINFOHEADER bih;
RGBQUAD rgbQuad[256]; //定义调色板
bih.biBitCount=8;
bih.biClrImportant=0;
bih.biClrUsed=0;
bih.biCompression=BI_RGB;
bih.biHeight=height;
bih.biPlanes=1;
bih.biSize=sizeof(BITMAPINFOHEADER);
bih.biSizeImage=0;
bih.biWidth=width;
bih.biXPelsPerMeter=0;
bih.biYPelsPerMeter=0;
for(i=0; i<256; i++)
{
rgbQuad[i].rgbBlue = (BYTE)i;
rgbQuad[i].rgbGreen = (BYTE)i;
rgbQuad[i].rgbRed = (BYTE)i;
rgbQuad[i].rgbReserved = 0;
}
//Fill in the fields of the file header
hdr.bfType= (WORD)0x4D42; // is always "BM"
hdr.bfSize= (DWORD)(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)
+ sizeof(RGBQUAD)*256 + width*height);
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
hdr.bfOffBits= (DWORD)(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)
+sizeof(RGBQUAD)*256);
CFile mfile; mfile.Open( str, CFile::modeWrite|CFile::modeCreate);
// Write the file header
mfile.Write( &hdr, sizeof(hdr) );//写文件头
mfile.Write( &bih, sizeof(bih) );//写信息头
mfile.Write((LPSTR)rgbQuad,sizeof(RGBQUAD)*256); //写调色板
mfile.Write(pdata,width * height);//写数据
mfile.Close();
} }
你看看bmp头文件格式自己按要求填写就行,不是很难的