码迷,mamicode.com
首页 > 其他好文 > 详细

【opencv学习记录】以迭代器方式访问图像像素,统计像素信息存入文件

时间:2015-05-20 22:28:26      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

【opencv学习记录】以迭代器方式访问图像像素,统计像素信息存入文件

标签:

原文地址:http://blog.csdn.net/u013617144/article/details/45874665

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!