把32位等任意格式图片灰度化并把图像数据保存为文本文件

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

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include "cvaux.h"
#include <string>
#include <fstream>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 int i,j;
 IplImage* img = NULL; // 原图
 IplImage* dst=NULL;//灰度图
 unsigned char DataArry[480][640];
 img = cvLoadImage("13.BMP",1);
 dst=cvCreateImage(cvSize(img->width,img->height),IPL_DEPTH_8U,1);
 cvCvtColor(img, dst, CV_RGB2GRAY);
 ofstream mf("13.txt");
 for (i=0; i<img->height; i++)
 {
  mf<<"{";
  for (j=0; j<img->width; j++)
  {
   DataArry[i][j] = ((dst->imageData + dst->widthStep*i))[j];
   mf<<(int)DataArry[i][j];
   if (j < img->width-1)
   {
    mf<<",";
   }
   else
   {
    mf<<"},";
    mf<<"/r/t";
   }
  }
 }
 mf.close();
 //cvSaveImage()
 return 0;
}

 

你可能感兴趣的:(null,DST)