im2csv

im2csv すると,カレントディレクトリに image.csv を吐き出す.getPixel() は OpenCV/ラベリング - 王様の箱庭 Wiki* から頂戴した.

#include <iostream>
#include <fstream>
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"

#pragma comment(lib, "cv.lib")
#pragma comment(lib, "cxcore.lib")
#pragma comment(lib, "highgui.lib")

using namespace std;

inline unsigned char getPixel(const IplImage *IMAGE, const int X, const int Y, const int INDEX)
{
    return (unsigned char)IMAGE->imageData[Y*IMAGE->widthStep + X*IMAGE->nChannels + INDEX];
}

void im2csv(const IplImage *IMAGE, const char *FILENAME)
{
	if(IMAGE == NULL)
	{
		cout << "error: no image" << endl;
		return;
	}

	ofstream csvFile(FILENAME);

	if(csvFile==NULL)
	{
		cout << "error: can't open write file" << endl;
		return;
	}

	for(int height = 0; height < IMAGE->height; height++)
	{
		for(int width = 0; width < IMAGE->width; width++)
		{
			csvFile <<  (unsigned int)getPixel(IMAGE, width, height, 0) << ",";
		}
		csvFile << endl;
	}

	return;
}

int main(int argc, char *argv[])
{
	if(argc < 2)
	{
		cout << "error: 文法エラー" << endl;
		// TODO: print_usage();
		return 1;
	}


	IplImage *image;
	image = cvLoadImage(argv[1], 0);
	if( image == NULL )
	{
		cout << "error: no image" << endl;
		return 1;
	}

	im2csv(image, "image.csv");

	cvReleaseImage(&image);

	return 0;
}