#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <iostream>
#include <fstream>
using namespace std;
using namespace cv;
int ppmain()
{
fstream fs;
Mat A = imread("lena.jpg",1);
cout<<A.channels()<<endl;
cv::cvtColor(A,A,COLOR_BGR2GRAY);
cout<<A.channels()<<endl;
cv::threshold(A,A,50,255,THRESH_BINARY);
cout<<A.channels()<<endl;
imshow("Binary",A);
//resize(A,A,Size(16,32));
Mat B = A(Rect(50,50,10,10));
imshow("B",B);
float BL,counter = 0;
int Sqaure = B.cols * B.rows;
Mat_<uchar>::iterator it = B.begin<uchar>();
Mat_<uchar>::iterator itend = B.end<uchar>();
vector<float> xs;
for (; it!=itend; ++it)
{
xs.push_back(*it);
if((*it)>0)
counter+=1;
}
BL = counter/Sqaure;
cout<<xs.size()<<endl;
cout<<counter<<endl;
for(int m = 0 ;m<xs.size();m++)
cout<<xs[m]<<" ";
//写入文件
ofstream out("out.txt",ios::out);
int len = xs.size();
for(int i=0;i<len;++i)
{
out<<xs[i]<<endl;
}
out.close();
/*for(int i = 0;i<A.rows;i++)
{
for(int j= 0;j<A.cols;j++)
{
int xiangsu = A.at<uchar>(j,i);
}
}*/
cv::waitKey();
return 0;
}【opencv学习记录】以迭代器方式访问图像像素,统计像素信息存入文件
原文:http://blog.csdn.net/u013617144/article/details/45874665