opencv统计图像不同区域面积

#include "cv.h"
#include "highgui.h"
#include <fstream>
#include <istream>
using namespace std;

int main()
{
	IplImage* img  = cvLoadImage("1.bmp", 1);
	/* always check */
	assert(img);
	int nWidth = img->width;
	int nHeight = img->height;
	int step = img->widthStep;
	int channels = img->nChannels;
	int i,j;
	int sum1 = 0,sum2 = 0,sum3 = 0,sum4 = 0,sum5 = 0;
	for( i=0;i<nHeight;i++)
		for( j=0;j<nWidth;j++)
		{
			CvScalar s = cvGet2D(img,i,j);
			if ((int)s.val[0] == 30 && (int)s.val[1] == 140 && (int)s.val[2] == 90)
				++sum4;
			else
				if((int)s.val[0] == 90 && (int)s.val[1] == 10 && (int)s.val[2] == 80)
					++sum5;
				else
					if((int)s.val[0] == 230 && (int)s.val[1] == 80 && (int)s.val[2] == 140)
						++sum2;
					else
						if((int)s.val[0] == 140 && (int)s.val[1] == 80 && (int)s.val[2] == 30)
							++sum3;
						else
							++sum1;

		}
		ofstream infile("E:\\infile.txt");
		int sum = sum1+sum2+sum3+sum5+sum4 ;
		float fsum = sum;
		infile << "图像总像素数为:"<< sum << endl;
		infile << sum1/fsum << endl
			 << sum2/fsum <<  endl
			 << sum3/fsum << endl
			 << sum4/fsum << endl
			 << sum5/fsum << endl;
		infile.close();
		return 0;
}

你可能感兴趣的:(opencv统计图像不同区域面积)