1. 程式人生 > >opencv讀取影象畫素值讀取並儲存到txt檔案(一)RGB

opencv讀取影象畫素值讀取並儲存到txt檔案(一)RGB

#include “stdafx.h”
#include"cv.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include
#include
#include “iostream”
#include <opencv/cv.h>
#include <opencv/highgui.h>

using namespace std;

int main()
{
IplImage* img = 0;
int height,width,step,channels;
int i,j,k;
img = cvLoadImage(“k:\lena.jpg”,-1);
CvScalar s;
height = img->height;
width = img->width;
ofstream outFile_B;
ofstream outFile_G;
ofstream outFile_R;
outFile_B.open(“k:\out_B.txt”);
outFile_G.open(“k:\out_G.txt”);
outFile_R.open(“k:\out_R.txt”);

//get the pixel value

//CvScalar s;
for(i=0;i<height;i++) //i 代表 y 軸
{
for(j=0;j<width;j++) //j 代表 x軸
{
s=cvGet2D(img,i,j); // get the (j,i) pixel value
outFile_B<<s.val[0]<<" “;
outFile_G<<s.val[1]<<” “;
outFile_R<<s.val[2]<<” ";
}
outFile_B<<endl;
outFile_G<<endl;
outFile_R<<endl;
}
return 0;
}

備註:
參考部落格 opencv中文網
SkySeraph部落格園