1. 程式人生 > >opencv 讀取rgb.txt 並顯示出來

opencv 讀取rgb.txt 並顯示出來



///*
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;


///*


int main()
{


FILE *fin;
int a[40][40];
int i,j;
fin = fopen("D:\\rgbbina.txt", "r");  // 開啟檔案,按讀的方式開啟
for (i = 0; i<40; i++)
for (j = 0; j<40; j++)


fscanf(fin, "%x", &a[i][j]);  // 迴圈讀
fclose(fin);  //關閉檔案


for (i = 0; i<40; i++) 
for (j = 0; j<40; j++)

printf("%x\n", a[i][j]);   //輸出來看看




//system("pause");


//return 0;




//新建一個uchar型別的單通道矩陣(grayscale image 灰度圖)
//Mat m(40, 40, CV_8UC3);


Mat m(40, 40, CV_8UC3, Scalar(30, 110, 55));


//Mat m(40, 40, CV_32FC3, Scalar(0.0, 0.0, 0.99));

//m.at<int>(20, 20) = ( 0,0,255);


for (i = 0; i<40; i++)
for (j = 0; j<40; j++)

{
m.at<Vec3b>(i, j)[0] = a[i][j];
m.at<Vec3b>(i, j)[1] = a[i][j]>>8;
m.at<Vec3b>(i, j)[2] = a[i][j]>>16;

}


/*
m.at<Vec3b>(20, 20)[0] = 1;
m.at<Vec3b>(20, 20)[1] = 1;
m.at<Vec3b>(20, 20)[2] = 1;
m.at<Vec3b>(20, 21)[0] = 1;
m.at<Vec3b>(20, 21)[1] = 1;
m.at<Vec3b>(20, 21)[2] = 1;
m.at<Vec3b>(20, 22)[0] = 1;
m.at<Vec3b>(20, 22)[1] = 1;
m.at<Vec3b>(20, 22)[2] = 1;
m.at<Vec3b>(20, 23)[0] = 1;
m.at<Vec3b>(20, 23)[1] = 1;
m.at<Vec3b>(20, 23)[2] = 1;
*/
/*
m.at<Vec3f>(20, 20)[0] = 1;
m.at<Vec3f>(20, 20)[1] = 1;
m.at<Vec3f>(20, 20)[2] = 1;

*/



//m.at<uchar>(30, 30) = 255;
/*
for (int col = 0; col < 400; col++)
{
for (int row = 195; row < 205; row++)
{
cout << (int)(*(m.data + m.step[0] * row + m.step[1] * col)) << " ==> ";
//獲取第[row,col]個畫素點的地址並用 * 符號解析
*(m.data + m.step[0] * row + m.step[1] * col) = 255;
cout << (int)(*(m.data + m.step[0] * row + m.step[1] * col)) << endl;
}
}
*/


///*
imshow("canvas", m);
cvWaitKey();
return 0;
}
//*/