开发者

Sum of each column opencv

开发者 https://www.devze.com 2023-02-12 20:06 出处:网络
In Matlab, If A is a开发者_如何学JAVA matrix, sum(A) treats the columns of A as vectors, returning a row vector of the sums of each column.

In Matlab, If A is a开发者_如何学JAVA matrix, sum(A) treats the columns of A as vectors, returning a row vector of the sums of each column.

sum(Image); How could it be done with OpenCV?


Using cvReduce has worked for me. For example, if you need to store the column-wise sum of a matrix as a row matrix you could do this:

CvMat * MyMat = cvCreateMat(height, width, CV_64FC1);
// Fill in MyMat with some data...

CvMat * ColSum = cvCreateMat(1, MyMat->width, CV_64FC1);
cvReduce(MyMat, ColSum, 0, CV_REDUCE_SUM);

More information is available in the OpenCV documentation.


EDIT after 3 years:

The proper function for this is cv::reduce.

Reduces a matrix to a vector.

The function reduce reduces the matrix to a vector by treating the matrix rows/columns as a set of 1D vectors and performing the specified operation on the vectors until a single row/column is obtained. For example, the function can be used to compute horizontal and vertical projections of a raster image. In case of REDUCE_MAX and REDUCE_MIN , the output image should have the same type as the source one. In case of REDUCE_SUM and REDUCE_AVG , the output may have a larger element bit-depth to preserve accuracy. And multi-channel arrays are also supported in these two reduction modes.

OLD: I've used ROI method: move ROI of height of the image and width 1 from left to right and calculate means.

 Mat src = imread(filename, 0);     
 vector<int> graph( src.cols );
 for (int c=0; c<src.cols-1; c++)
 {
     Mat roi = src( Rect( c,0,1,src.rows ) );
     graph[c] = int(mean(roi)[0]);
 }

 Mat mgraph(  260, src.cols+10, CV_8UC3); 
 for (int c=0; c<src.cols-1; c++)
 {
     line( mgraph, Point(c+5,0), Point(c+5,graph[c]), Scalar(255,0,0), 1, CV_AA);    
 }

 imshow("mgraph", mgraph);
 imshow("source", src);

Sum of each column opencv

Sum of each column opencv

EDIT: Just out of curiosity, I've tried resize to height 1 and the result was almost the same:

 Mat test;
 cv::resize(src,test,Size( src.cols,1 ));
 Mat mgraph1(  260, src.cols+10, CV_8UC3); 
 for(int c=0; c<test.cols; c++) 
 {
        graph[c] = test.at<uchar>(0,c);
 }
 for (int c=0; c<src.cols-1; c++)
 {
     line( mgraph1, Point(c+5,0), Point(c+5,graph[c]), Scalar(255,255,0), 1, CV_AA);     
 }
 imshow("mgraph1", mgraph1);

Sum of each column opencv


cvSum respects ROI, so if you move a 1 px wide window over the whole image, you can calculate the sum of each column.

My c++ got a little rusty so I won't provide a code example, though the last time I did this I used OpenCVSharp and it worked fine. However, I'm not sure how efficient this method is.

My math skills are getting rusty too, but shouldn't it be possible to sum all elements in columns in a matrix by multiplying it by a vector of 1s?


For an 8 bit greyscale image, the following should work (I think). It shouldn't be too hard to expand to different image types.

int imgStep = image->widthStep;
uchar* imageData = (uchar*)image->imageData;
uint result[image->width];
memset(result, 0, sizeof(uchar) * image->width);
for (int col = 0; col < image->width; col++) {
  for (int row = 0; row < image->height; row++) {
    result[col] += imageData[row * imgStep + col];
  }
}

// your desired vector is in result
0

精彩评论

暂无评论...
验证码 换一张
取 消