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

来源:互联网 发布:java的三大框架 编辑:程序博客网 时间:2024/06/09 04:26
#include <opencv2/core/core.hpp>#include <iostream>using namespace cv;using namespace std;void HorizonProjection(const Mat& src, Mat& dst){// accept only char type matricesCV_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 matricesCV_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类型的输入矩阵。


结果:


1 0
原创粉丝点击