opencv2.4.9,练习使用图像迭代器访问像素值+STL中的vector
#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; }
时间: 2024-10-24 13:01:18