// OpencvTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include "opencv/ml.h"
#include <afx.h>
#include "math.h"
using namespace cv;
using namespace std;
Mat hp, vp;
void HProjection(const Mat& src, Mat& dst)
{
// accept only char type matrices
CV_Assert(src.depth() != sizeof(uchar));
dst.create(src.rows, 1, CV_32F);
int i, j;
const uchar* p;
float* p_dst;
p_dst = dst.ptr<float>(0);
for(i = 0; i < src.rows; i++)
{
p = src.ptr<uchar>(i);
p_dst[i] = 0;
for(j = 0; j < src.cols; j++)
{
p_dst[i] += p[j];
}
}
}
void VProjection(const Mat& src, Mat& dst)
{
CV_Assert(src.depth() != sizeof(uchar));
dst.create(1, src.cols, CV_32F);
int i,j;
const uchar* p;
float * p_dst = dst.ptr<float>(0);
for (i = 0; i < src.cols; i++)
{
p_dst[i] = 0;
for (j=0; j < src.rows; j++)
{
p = src.ptr<uchar>(j);
p_dst[i] += p[i];
}
cout << p_dst[i] << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Mat src1Test = (Mat_<uchar>(3,3) << 1,2,3,4,5,6,7,8,9);
HProjection(src1Test, hp);
VProjection(src1Test, vp);
cout << src1Test << endl;
cout << "hp:" << endl;
cout << hp << endl;
cout << "vp" << endl;
cout << vp << endl;
waitKey(0);
}