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

解决方案 »

  1.   

    如是要是在opencv中就简单了,
    IplImage* img=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
    uchar* data;
    data=(uchar*)img->imageData;
    下步就是将你数组里的值赋给data就行了!
      

  2.   

    要看你想要保存成什么格式,bmp,jpg还是别的
    opencv封装了各种格式的细节,我们只需操作数据区
    不使用opencv的话,就要根据特定的文件格式说明来做了,比如说bmp
    常见格式都是开放的。我们只需操作数据区然后按要求该压缩的压缩
      

  3.   

    最简单的方式,就是直接存成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图像文件
      

  4.   


    直接上代码....
    //把二维数组保存为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();   
        }   }
      

  5.   

    你要保存为什么格式的图片,bmp的话需要加载头文件信息和调色板信息。
    你看看bmp头文件格式自己按要求填写就行,不是很难的