vc 如何将二维数组中的灰度值保存成灰度图像?

shiya666888 2010-08-18 08:44:44
vc 如何将二维数组中的灰度值保存成灰度图像?
...全文
706 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Normandie007 2010-08-19
  • 打赏
  • 举报
回复
最简单的方式,就是直接存成RAW格式,

即:直接将二维数组逐行写入"xxx.raw"文件。

FILE *fp;
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图像文件
tt2com 2010-08-19
  • 打赏
  • 举报
回复
你要保存为什么格式的图片,bmp的话需要加载头文件信息和调色板信息。
你看看bmp头文件格式自己按要求填写就行,不是很难的
The_eagles 2010-08-19
  • 打赏
  • 举报
回复

直接上代码....


//把二维数组保存为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();
}

}


onezeros 2010-08-18
  • 打赏
  • 举报
回复
要看你想要保存成什么格式,bmp,jpg还是别的
opencv封装了各种格式的细节,我们只需操作数据区
不使用opencv的话,就要根据特定的文件格式说明来做了,比如说bmp
常见格式都是开放的。我们只需操作数据区然后按要求该压缩的压缩
寂寞的秋叶 2010-08-18
  • 打赏
  • 举报
回复
如是要是在opencv中就简单了,
IplImage* img=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
uchar* data;
data=(uchar*)img->imageData;
下步就是将你数组里的值赋给data就行了!
LiYi_Star 2010-08-18
  • 打赏
  • 举报
回复
虽然不知道,但是顶一个
VC6下数字图像处理系统-DIP_system,包含以下功能函数: ============================================================================== 第3章 ============================================================================== 相关函数: PaintDIB() - 绘制DIB对象 CreateDIBPalette() - 创建DIB对象调色板 FindDIBBits() - 返回DIB图像象素起始位置 DIBWidth() - 返回DIB宽度 DIBHeight() - 返回DIB高度 PaletteSize() - 返回DIB调色板大小 DIBNumColors() - 计算DIB调色板颜色数目 CopyHandle() - 拷贝内存块 DIBBitCount() - 该函数返回DIBBitCount SaveDIB() - 将DIB保存到指定文件 ReadDIBFile() - 重指定文件读取DIB对象 SetRgnColor() - 用指定的颜色填充指定的区域 SetCircleColor() - 用指定的颜色填充指定的圆形区域 以上函数为类CDIB的员函数,CDIB类存在于DIB.h、DIB.cpp文件。 ============================================================================== 第4章 ============================================================================== 4.1 图象的灰度变换 相关函数: ToGray() - 彩色位图转化为灰度位图 PointInvert() - 对图像进行反色变换 GetIntensity() - 对图像各颜色分量的灰度分布(数目、密度)进行统计 PointEqua() - 对图像进行灰度分布均衡化处理 GrayStretch() - 对图像进行灰度折线变换 WindowTrans() - 对图像进行灰度窗口变换 PointDZ() - 对图像进行灰度带阻变换 PointDT() - 对图像进行灰度带通变换 PointSML() - 对图像进行单映射规则直方图规定化变换 PointGML() - 对图像进行组映射规则直方图规定化变换 DynamicCompress()- 对图像进行灰度动态范围压缩处理 CutWave() - 对图像进行灰度削波处理 以上函数为类CGrayProc的员函数,CGrayProc类存在于GrayProc.h、GrayProc.cpp文件。 4.2 图象的正交变换 相关函数: FFT() - 一维快速付立叶变换 IFFT() - 一维快速付立叶逆变换 Fourier() - 二维快速傅立叶变换 IFourier() - 二维快速傅立叶逆变换 DCT() - 一维快速离散余弦变换 IDCT() - 一维快速离散余弦逆变换 FreqDCT() - 二维快速离散余弦变换 IFreqDCT() - 二维快速离散余弦逆变换 WALSH() - 一维沃尔什-哈达玛变换 IWALSH() - 一维沃尔什-哈达玛逆变换 FreqWALSH() - 二维沃尔什-哈达玛变换 IFreqWALSH()- 二维沃尔什-哈达玛逆变换 DIBFourier()- 图像的付立叶变换 DIBDCT() - 图像的离散余弦变换 DIBWalsh() - 图像的沃尔什-哈达玛变换 以上函数为类CFreqCalculate的员函数,CFreqCalculate类存在于FreqCalculate.h、FreqCalculate.cpp文件。 ============================================================================== 第5章 ============================================================================== 5.1 图像的空域滤波 相关函数: MakeEmboss() -

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧