您的位置:首页 > 运维架构

对OpenCV mat进行水平和垂直方向的投影

2014-03-23 21:39 429 查看
#include <opencv2/core/core.hpp>
#include <iostream>

using namespace cv;
using namespace std;

void HorizonProjection(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;
for(i = 0; i < src.rows; i++){
p = src.ptr<uchar>(i);
p_dst = dst.ptr<float>(i);
p_dst[0] = 0;
for(j = 0; j < src.cols; j++){
p_dst[0] += p[j];
}
}
}

void VerticalProjection(const Mat& src, Mat& dst)
{
// accept only char type matrices
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(j = 0; j < src.cols; j++){
p_dst[j] = 0;
for(i = 0; i < src.rows; i++){
p = src.ptr<uchar>(i);
p_dst[j] += p[j];
}
}
}

int main(){
Mat C = (Mat_<uchar>(3,3) << 0, 1, 0, 2, 5, 2, 0, 3, 0);
Mat hp, vp;
HorizonProjection(C, hp);
VerticalProjection(C, vp);

cout << "origin matrix: " << endl;
cout << C << endl;
cout << "horizon projection matrix: " << endl;
cout << hp << endl;
cout << "vertical projection matrix: " << endl;
cout << vp << endl;
}


注意,目前HorizonProjection和VerticalProjection只接受单通道uchar类型的输入矩阵。

结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: